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

View File

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

View File

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

View File

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

View File

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