Update build to work with _.runInContext.

Former-commit-id: da4a9da0e2c74bbcbd142c077794486d0ac45835
This commit is contained in:
John-David Dalton
2013-02-21 07:45:22 -08:00
parent 4251b36b25
commit f6e724c4c5
5 changed files with 125 additions and 63 deletions

View File

@@ -142,6 +142,7 @@
'reject': ['filter', 'identity', 'isEqual', 'keys'],
'rest': [],
'result': ['isFunction'],
'runInContext': [],
'shuffle': ['forEach'],
'size': ['keys'],
'some': ['identity', 'isArray', 'isEqual', 'keys'],
@@ -250,7 +251,8 @@
'forOwn',
'isPlainObject',
'merge',
'partialRight'
'partialRight',
'runInContext'
]));
/** List of ways to export the `lodash` function */
@@ -350,14 +352,15 @@
source = source.replace(/(?:\s*\/\/.*)*\n( *)forOwn\(lodash, *function\(func, *methodName\)[\s\S]+?\n\1}.+/g, '');
// move `mixin(lodash)` to after the method assignments
source = source.replace(/(?:\s*\/\/.*)*\s*mixin\(lodash\).+/, '');
source = source.replace(/(?:\s*\/\/.*)*\n( *)mixin\(lodash\).+/, '');
source = source.replace(getMethodAssignments(source), function(match) {
var indent = /^ *(?=lodash)/m.exec(match)[0];
return match + [
'',
'',
' // add functions to `lodash.prototype`',
' mixin(lodash);'
].join('\n');
'// add functions to `lodash.prototype`',
'mixin(lodash);'
].join('\n' + indent);
});
// add `__chain__` checks to `_.mixin`
@@ -378,38 +381,39 @@
// replace wrapper `Array` method assignments
source = source.replace(/^(?: *\/\/.*\n)*( *)each\(\['[\s\S]+?\n\1}$/m, function() {
return [
' // add `Array` mutator functions to the wrapper',
" each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {",
' var func = arrayRef[methodName];',
' lodash.prototype[methodName] = function() {',
' var value = this.__wrapped__;',
' func.apply(value, arguments);',
var indent = arguments[1];
return indent + [
'// add `Array` mutator functions to the wrapper',
"each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {",
' var func = arrayRef[methodName];',
' lodash.prototype[methodName] = function() {',
' var value = this.__wrapped__;',
' func.apply(value, arguments);',
'',
' // avoid array-like object bugs with `Array#shift` and `Array#splice`',
' // in Firefox < 10 and IE < 9',
' if (hasObjectSpliceBug && value.length === 0) {',
' delete value[0];',
' }',
' return this;',
' };',
' });',
' // avoid array-like object bugs with `Array#shift` and `Array#splice`',
' // in Firefox < 10 and IE < 9',
' if (hasObjectSpliceBug && value.length === 0) {',
' delete value[0];',
' }',
' return this;',
' };',
'});',
'',
' // add `Array` accessor functions to the wrapper',
" each(['concat', 'join', 'slice'], function(methodName) {",
' var func = arrayRef[methodName];',
' lodash.prototype[methodName] = function() {',
' var value = this.__wrapped__,',
' result = func.apply(value, arguments);',
'// add `Array` accessor functions to the wrapper',
"each(['concat', 'join', 'slice'], function(methodName) {",
' var func = arrayRef[methodName];',
' lodash.prototype[methodName] = function() {',
' var value = this.__wrapped__,',
' result = func.apply(value, arguments);',
'',
' if (this.__chain__) {',
' result = new lodash(result);',
' result.__chain__ = true;',
' }',
' return result;',
' };',
' });'
].join('\n');
' if (this.__chain__) {',
' result = new lodash(result);',
' result.__chain__ = true;',
' }',
' return result;',
' };',
'});'
].join('\n' + indent);
});
return source;
@@ -930,7 +934,21 @@
// remove function
var snippet = matchFunction(source, funcName);
if (snippet) {
source = source.replace(snippet, '');
if (funcName == 'runInContext') {
source = source.replace(snippet, function() {
return snippet.replace(/^[\s\S]+?\n( *).+?context *=.+(\n[\s\S]+?\n)\1return lodash[\s\S]+$/, function() {
return arguments[2].replace(/^ {4}/gm, ' ');
});
});
source = source
.replace(/context/g, 'window')
.replace(/(?:\n +\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/)?\n *var Array *=[\s\S]+?;\n/m, '')
.replace(/(?: *\/\/.*\n)* *var lodash *= *runInContext.+\n/, '');
}
else {
source = source.replace(snippet, '');
}
}
// grab the method assignments snippet
snippet = getMethodAssignments(source);
@@ -1647,6 +1665,8 @@
});
}
if (isUnderscore) {
source = removeFunction(source, 'runInContext');
// replace `_.assign`
source = replaceFunction(source, 'assign', [
' function assign(object) {',

View File

@@ -64,12 +64,14 @@
'all',
'amd',
'any',
'Array',
'assign',
'at',
'attachEvent',
'bind',
'bindAll',
'bindKey',
'Boolean',
'clearTimeout',
'clone',
'cloneDeep',
@@ -79,6 +81,7 @@
'contains',
'countBy',
'criteria',
'Date',
'debounce',
'defaults',
'defer',
@@ -102,6 +105,7 @@
'forEach',
'forIn',
'forOwn',
'Function',
'functions',
'global',
'groupBy',
@@ -141,6 +145,7 @@
'last',
'lastIndexOf',
'map',
'Math',
'max',
'memoize',
'merge',
@@ -148,10 +153,11 @@
'min',
'mixin',
'noConflict',
'Object',
'object',
'omit',
'once',
'opera',
'Number',
'pairs',
'partial',
'partialRight',
@@ -161,9 +167,11 @@
'range',
'reduce',
'reduceRight',
'RegExp',
'reject',
'rest',
'result',
'runInContext',
'select',
'setImmediate',
'setTimeout',
@@ -173,6 +181,7 @@
'sortBy',
'sortedIndex',
'source',
'String',
'tail',
'take',
'tap',

View File

@@ -126,9 +126,9 @@
Boolean = context.Boolean,
Date = context.Date,
Function = context.Function,
Object = context.Object,
Number = context.Number,
Math = context.Math,
Number = context.Number,
Object = context.Object,
RegExp = context.RegExp,
String = context.String;

View File

@@ -14,7 +14,7 @@
var QUnit = (
global.addEventListener || (global.addEventListener = Function.prototype),
global.QUnit = require('../vendor/qunit/qunit/qunit.js'),
require('../vendor/qunit-clib/qunit-clib.js'),
require('../vendor/qunit-clib/qunit-clib.js').runInContext(global),
global.addEventListener === Function.prototype && delete global.addEventListener,
global.QUnit
);
@@ -208,6 +208,7 @@
'noConflict',
'random',
'result',
'runInContext',
'template',
'times',
'unescape',
@@ -276,7 +277,8 @@
'forOwn',
'isPlainObject',
'merge',
'partialRight'
'partialRight',
'runInContext'
]));
/*--------------------------------------------------------------------------*/
@@ -462,6 +464,8 @@
else if (utilityMethods.indexOf(methodName) > -1) {
if (methodName == 'result') {
func(object, 'b');
} else if (methodName == 'runInContext') {
func();
} else if (methodName == 'template') {
func(template, object);
func(template, null, { 'imports': object })(object);
@@ -1298,6 +1302,9 @@
if (!exposeAssign) {
methodNames = _.without(methodNames, 'assign');
}
if (/utilities/.test(command) && /backbone|underscore/.test(command)) {
methodNames = _.without(methodNames, 'runInContext');
}
var lodash = context._ || {};
methodNames.forEach(function(methodName) {
testMethod(lodash, methodName, basename);

View File

@@ -18,10 +18,10 @@
result = (result.length > min && last != 'test.js') ? last : '../lodash.js';
try {
result = require('fs').realpathSync(result);
} catch(e) { }
return result;
return require('fs').realpathSync(result);
} catch(e) {
return result;
}
}());
/** The basename of the Lo-Dash file to test */
@@ -34,15 +34,17 @@
window.platform;
/** The unit testing framework */
var QUnit =
window.QUnit || (
window.addEventListener || (window.addEventListener = Function.prototype),
window.setTimeout || (window.setTimeout = Function.prototype),
var QUnit = (function() {
var noop = Function.prototype;
return window.QUnit || (
window.addEventListener || (window.addEventListener = noop),
window.setTimeout || (window.setTimeout = noop),
window.QUnit = load('../vendor/qunit/qunit/qunit.js') || window.QUnit,
load('../vendor/qunit-clib/qunit-clib.js'),
window.addEventListener === Function.prototype && delete window.addEventListener,
(load('../vendor/qunit-clib/qunit-clib.js') || { 'runInContext': noop }).runInContext(window),
addEventListener === noop && delete window.addEventListener,
window.QUnit
);
}());
/** The `lodash` function to test */
var _ = window._ || (
@@ -50,6 +52,8 @@
_._ || _
);
_ = _.runInContext(window);
/** Used to pass falsey values to methods */
var falsey = [
,
@@ -65,15 +69,16 @@
var freeze = Object.freeze;
/** Used to set property descriptors */
var setDescriptor = (function(fn) {
var setDescriptor = (function() {
try {
var o = {};
return fn(o, o, o) && fn;
var o = {},
fn = (fn = Object.defineProperty)(o, o, o) && fn;
} catch(e) { }
}(Object.defineProperty));
return fn;
}());
/** Shortcut used to convert array-like objects to arrays */
var slice = [].slice;
var slice = Array.prototype.slice;
/** Used to check problem JScript properties (a.k.a. the [[DontEnum]] bug) */
var shadowed = {
@@ -2351,7 +2356,7 @@
});
test('should work with "interpolate" delimiters containing global values', function() {
var compiled = _.template('<%= typeof QUnit.init %>');
var compiled = _.template('<%= typeof Math.abs %>');
try {
var actual = compiled();
@@ -2395,18 +2400,39 @@
});
test('should clear timeout when `func` is called', function() {
var counter = 0,
oldDate = Date,
throttled = _.throttle(function() { counter++; }, 32);
var callCount = 0,
dateCount = 0;
var context = {
'Array': Array,
'Boolean': Boolean,
'Function': Function,
'Object': Object,
'Math': Math,
'Number': Number,
'RegExp': RegExp,
'String': String,
'clearTimeout': clearTimeout,
'isFinite': isFinite,
'isNaN': isNaN,
'setTimeout': setTimeout
};
var lodash = _.runInContext(_.extend(context, {
'Date': function() {
return ++dateCount < 3 ? new Date : Object(Infinity);
}
}));
var throttled = lodash.throttle(function() {
callCount++;
}, 32);
throttled();
throttled();
window.Date = function() { return Object(Infinity); };
throttled();
window.Date = oldDate;
equal(counter, 2);
equal(callCount, 2);
});
asyncTest('supports recursive calls', function() {