Allow the underscore build to opt-in to more lodash build methods.

Former-commit-id: 3f685fe1ced25ba37ea9d09e2ed8fa59acb5b8b7
This commit is contained in:
John-David Dalton
2013-05-07 09:22:31 -07:00
parent e0cf4e644b
commit cc14c34dc2

197
build.js
View File

@@ -1764,41 +1764,64 @@
} }
} }
if (isUnderscore) { if (isUnderscore) {
dependencyMap.contains = _.without(dependencyMap.contains, 'isString');
dependencyMap.createCallback = _.without(dependencyMap.createCallback, 'isEqual');
dependencyMap.findWhere = ['where'];
dependencyMap.flatten = _.without(dependencyMap.flatten, 'createCallback');
dependencyMap.isEmpty = ['isArray', 'isString'];
dependencyMap.isEqual = _.without(dependencyMap.isEqual, 'forIn', 'isArguments');
dependencyMap.max = _.without(dependencyMap.max, 'isArray', 'isString');
dependencyMap.min = _.without(dependencyMap.min, 'isArray', 'isString');
dependencyMap.pick = _.without(dependencyMap.pick, 'forIn', 'isObject');
dependencyMap.reduceRight = _.without(dependencyMap.reduceRight, 'isString');
dependencyMap.template = _.without(dependencyMap.template, 'keys', 'values');
dependencyMap.toArray.push('isArray', 'map');
dependencyMap.value = _.without(dependencyMap.value, 'isArray');
dependencyMap.where.push('find', 'isEmpty');
if (!useLodashMethod('clone') && !useLodashMethod('cloneDeep')) { if (!useLodashMethod('clone') && !useLodashMethod('cloneDeep')) {
dependencyMap.clone = _.without(dependencyMap.clone, 'forEach', 'forOwn'); dependencyMap.clone = _.without(dependencyMap.clone, 'forEach', 'forOwn');
} }
if (!useLodashMethod('contains')) {
dependencyMap.contains = _.without(dependencyMap.contains, 'isString');
}
if (!useLodashMethod('flatten')) {
dependencyMap.flatten = _.without(dependencyMap.flatten, 'createCallback');
}
if (!useLodashMethod('isEmpty')) {
dependencyMap.isEmpty = ['isArray', 'isString'];
}
if (!useLodashMethod('isEqual')) {
dependencyMap.isEqual = _.without(dependencyMap.isEqual, 'forIn', 'isArguments');
}
if (!useLodashMethod('pick')){
dependencyMap.pick = _.without(dependencyMap.pick, 'forIn', 'isObject');
}
if (!useLodashMethod('where')) {
dependencyMap.createCallback = _.without(dependencyMap.createCallback, 'isEqual');
dependencyMap.where.push('find', 'isEmpty');
}
if (!useLodashMethod('template')) {
dependencyMap.template = _.without(dependencyMap.template, 'keys', 'values');
}
if (!useLodashMethod('toArray')) {
dependencyMap.toArray.push('isArray', 'map');
}
_.each(['max', 'min'], function(methodName) {
if (!useLodashMethod(methodName)) {
dependencyMap[methodName] = _.without(dependencyMap[methodName], 'isArray', 'isString');
}
});
dependencyMap.findWhere = ['where'];
dependencyMap.reduceRight = _.without(dependencyMap.reduceRight, 'isString');
dependencyMap.value = _.without(dependencyMap.value, 'isArray');
} }
if (isModern || isUnderscore) { if (isModern || isUnderscore) {
dependencyMap.at = _.without(dependencyMap.at, 'isString'); _.each(['at', 'forEach', 'toArray'], function(methodName) {
dependencyMap.forEach = _.without(dependencyMap.forEach, 'isString'); if (!(isUnderscore && useLodashMethod(methodName))) {
dependencyMap.toArray = _.without(dependencyMap.toArray, 'isString'); dependencyMap[methodName] = _.without(dependencyMap[methodName], 'isString');
}
});
if (!isMobile) { if (!isMobile) {
dependencyMap.every = _.without(dependencyMap.every, 'isArray'); _.each(['every', 'find', 'filter', 'forEach', 'forIn', 'forOwn', 'map', 'reduce'], function(methodName) {
dependencyMap.find = _.without(dependencyMap.find, 'isArray'); if (!(isUnderscore && useLodashMethod(methodName))) {
dependencyMap.filter = _.without(dependencyMap.filter, 'isArray'); dependencyMap[methodName] = _.without(dependencyMap[methodName], 'isArguments', 'isArray');
dependencyMap.forEach = _.without(dependencyMap.forEach, 'isArguments', 'isArray'); }
dependencyMap.forIn = _.without(dependencyMap.forIn, 'isArguments'); });
dependencyMap.forOwn = _.without(dependencyMap.forOwn, 'isArguments');
dependencyMap.map = _.without(dependencyMap.map, 'isArray'); _.each(['max', 'min'], function(methodName) {
dependencyMap.max.push('forEach'); if (!(isUnderscore && useLodashMethod(methodName))) {
dependencyMap.min.push('forEach'); dependencyMap[methodName].push('forEach');
dependencyMap.reduce = _.without(dependencyMap.reduce, 'isArray'); }
});
} }
} }
// add method names explicitly // add method names explicitly
@@ -2036,7 +2059,17 @@
} }
} }
if (isUnderscore) { if (isUnderscore) {
// replace `lodash`
source = replaceFunction(source, 'lodash', [
'function lodash(value) {',
' return (value instanceof lodash)',
' ? value',
' : new lodashWrapper(value);',
'}'
].join('\n'));
// replace `_.assign` // replace `_.assign`
if (!useLodashMethod('assign')) {
source = replaceFunction(source, 'assign', [ source = replaceFunction(source, 'assign', [
'function assign(object) {', 'function assign(object) {',
' if (!object) {', ' if (!object) {',
@@ -2053,7 +2086,7 @@
' return object;', ' return object;',
'}' '}'
].join('\n')); ].join('\n'));
}
// replace `_.clone` // replace `_.clone`
if (!useLodashMethod('clone') && !useLodashMethod('cloneDeep')) { if (!useLodashMethod('clone') && !useLodashMethod('cloneDeep')) {
source = replaceFunction(source, 'clone', [ source = replaceFunction(source, 'clone', [
@@ -2064,8 +2097,8 @@
'}' '}'
].join('\n')); ].join('\n'));
} }
// replace `_.contains` // replace `_.contains`
if (!useLodashMethod('contains')) {
source = replaceFunction(source, 'contains', [ source = replaceFunction(source, 'contains', [
'function contains(collection, target) {', 'function contains(collection, target) {',
' var length = collection ? collection.length : 0,', ' var length = collection ? collection.length : 0,',
@@ -2080,8 +2113,9 @@
' return result;', ' return result;',
'}' '}'
].join('\n')); ].join('\n'));
}
// replace `_.debounce` // replace `_.debounce`
if (!useLodashMethod('debounce')) {
source = replaceFunction(source, 'debounce', [ source = replaceFunction(source, 'debounce', [
'function debounce(func, wait, immediate) {', 'function debounce(func, wait, immediate) {',
' var args,', ' var args,',
@@ -2110,8 +2144,9 @@
' };', ' };',
'}' '}'
].join('\n')); ].join('\n'));
}
// replace `_.defaults` // replace `_.defaults`
if (!useLodashMethod('defaults')) {
source = replaceFunction(source, 'defaults', [ source = replaceFunction(source, 'defaults', [
'function defaults(object) {', 'function defaults(object) {',
' if (!object) {', ' if (!object) {',
@@ -2130,8 +2165,9 @@
' return object;', ' return object;',
'}' '}'
].join('\n')); ].join('\n'));
}
// replace `_.difference` // replace `_.difference`
if (!useLodashMethod('difference')) {
source = replaceFunction(source, 'difference', [ source = replaceFunction(source, 'difference', [
'function difference(array) {', 'function difference(array) {',
' var index = -1,', ' var index = -1,',
@@ -2148,8 +2184,9 @@
' return result;', ' return result;',
'}' '}'
].join('\n')); ].join('\n'));
}
// replace `_.flatten` // replace `_.flatten`
if (!useLodashMethod('flatten')) {
source = replaceFunction(source, 'flatten', [ source = replaceFunction(source, 'flatten', [
'function flatten(array, isShallow) {', 'function flatten(array, isShallow) {',
' var index = -1,', ' var index = -1,',
@@ -2167,8 +2204,9 @@
' return result;', ' return result;',
'}' '}'
].join('\n')); ].join('\n'));
}
// replace `_.intersection` // replace `_.intersection`
if (!useLodashMethod('intersection')) {
source = replaceFunction(source, 'intersection', [ source = replaceFunction(source, 'intersection', [
'function intersection(array) {', 'function intersection(array) {',
' var args = arguments,', ' var args = arguments,',
@@ -2193,8 +2231,9 @@
' return result;', ' return result;',
'}' '}'
].join('\n')); ].join('\n'));
}
// replace `_.isEmpty` // replace `_.isEmpty`
if (!useLodashMethod('isEmpty')) {
source = replaceFunction(source, 'isEmpty', [ source = replaceFunction(source, 'isEmpty', [
'function isEmpty(value) {', 'function isEmpty(value) {',
' if (!value) {', ' if (!value) {',
@@ -2211,8 +2250,9 @@
' return true;', ' return true;',
'}' '}'
].join('\n')); ].join('\n'));
}
// replace `_.isEqual` // replace `_.isEqual`
if (!useLodashMethod('isEqual')) {
source = replaceFunction(source, 'isEqual', [ source = replaceFunction(source, 'isEqual', [
'function isEqual(a, b, stackA, stackB) {', 'function isEqual(a, b, stackA, stackB) {',
' if (a === b) {', ' if (a === b) {',
@@ -2312,17 +2352,9 @@
' return result;', ' return result;',
'}' '}'
].join('\n')); ].join('\n'));
}
// replace `lodash`
source = replaceFunction(source, 'lodash', [
'function lodash(value) {',
' return (value instanceof lodash)',
' ? value',
' : new lodashWrapper(value);',
'}'
].join('\n'));
// replace `_.omit` // replace `_.omit`
if (!useLodashMethod('omit')) {
source = replaceFunction(source, 'omit', [ source = replaceFunction(source, 'omit', [
'function omit(object) {', 'function omit(object) {',
' var props = concat.apply(arrayRef, nativeSlice.call(arguments, 1)),', ' var props = concat.apply(arrayRef, nativeSlice.call(arguments, 1)),',
@@ -2336,8 +2368,9 @@
' return result;', ' return result;',
'}' '}'
].join('\n')); ].join('\n'));
}
// replace `_.pick` // replace `_.pick`
if (!useLodashMethod('pick')) {
source = replaceFunction(source, 'pick', [ source = replaceFunction(source, 'pick', [
'function pick(object) {', 'function pick(object) {',
' var index = -1,', ' var index = -1,',
@@ -2354,16 +2387,21 @@
' return result;', ' return result;',
'}' '}'
].join('\n')); ].join('\n'));
}
// replace `_.result` // replace `_.result`
if (!useLodashMethod('result')) {
source = replaceFunction(source, 'result', [ source = replaceFunction(source, 'result', [
'function result(object, property) {', 'function result(object, property) {',
' var value = object ? object[property] : null;', ' var value = object ? object[property] : null;',
' return isFunction(value) ? object[property]() : value;', ' return isFunction(value) ? object[property]() : value;',
'}' '}'
].join('\n')); ].join('\n'));
}
// replace `_.template` // replace `_.template`
if (!useLodashMethod('template')) {
// remove `_.templateSettings.imports assignment
source = source.replace(/,[^']*'imports':[^}]+}/, '');
source = replaceFunction(source, 'template', [ source = replaceFunction(source, 'template', [
'function template(text, data, options) {', 'function template(text, data, options) {',
" text || (text = '');", " text || (text = '');",
@@ -2418,8 +2456,9 @@
' return result;', ' return result;',
'}' '}'
].join('\n')); ].join('\n'));
}
// replace `_.throttle` // replace `_.throttle`
if (!useLodashMethod('throttle')) {
source = replaceFunction(source, 'throttle', [ source = replaceFunction(source, 'throttle', [
'function throttle(func, wait) {', 'function throttle(func, wait) {',
' var args,', ' var args,',
@@ -2453,8 +2492,9 @@
' };', ' };',
'}' '}'
].join('\n')); ].join('\n'));
}
// replace `_.times` // replace `_.times`
if (!useLodashMethod('times')) {
source = replaceFunction(source, 'times', [ source = replaceFunction(source, 'times', [
'function times(n, callback, thisArg) {', 'function times(n, callback, thisArg) {',
' var index = -1,', ' var index = -1,',
@@ -2466,8 +2506,9 @@
' return result;', ' return result;',
'}' '}'
].join('\n')); ].join('\n'));
}
// replace `_.toArray` // replace `_.toArray`
if (!useLodashMethod('toArray')) {
source = replaceFunction(source, 'toArray', [ source = replaceFunction(source, 'toArray', [
'function toArray(collection) {', 'function toArray(collection) {',
' if (isArray(collection)) {', ' if (isArray(collection)) {',
@@ -2479,8 +2520,9 @@
' return values(collection);', ' return values(collection);',
'}' '}'
].join('\n')); ].join('\n'));
}
// replace `_.uniq` // replace `_.uniq`
if (!useLodashMethod('uniq')) {
source = replaceFunction(source, 'uniq', [ source = replaceFunction(source, 'uniq', [
'function uniq(array, isSorted, callback, thisArg) {', 'function uniq(array, isSorted, callback, thisArg) {',
' var index = -1,', ' var index = -1,',
@@ -2514,16 +2556,18 @@
' return result;', ' return result;',
'}' '}'
].join('\n')); ].join('\n'));
}
// replace `_.uniqueId` // replace `_.uniqueId`
if (!useLodashMethod('uniqueId')) {
source = replaceFunction(source, 'uniqueId', [ source = replaceFunction(source, 'uniqueId', [
'function uniqueId(prefix) {', 'function uniqueId(prefix) {',
" var id = ++idCounter + '';", " var id = ++idCounter + '';",
' return prefix ? prefix + id : id;', ' return prefix ? prefix + id : id;',
'}' '}'
].join('\n')); ].join('\n'));
}
// replace `_.where` // replace `_.where`
if (!useLodashMethod('where')) {
source = replaceFunction(source, 'where', [ source = replaceFunction(source, 'where', [
'function where(collection, properties, first) {', 'function where(collection, properties, first) {',
' return (first && isEmpty(properties))', ' return (first && isEmpty(properties))',
@@ -2531,13 +2575,26 @@
' : (first ? find : filter)(collection, properties);', ' : (first ? find : filter)(collection, properties);',
'}' '}'
].join('\n')); ].join('\n'));
}
// replace `_.zip`
if (!useLodashMethod('unzip')) {
source = replaceFunction(source, 'zip', [
'function zip(array) {',
' var index = -1,',
" length = array ? max(pluck(arguments, 'length')) : 0,",
' result = Array(length < 0 ? 0 : length);',
'',
' while (++index < length) {',
' result[index] = pluck(arguments, index);',
' }',
' return result;',
'}'
].join('\n'));
}
// unexpose `lodash.support` // unexpose `lodash.support`
source = source.replace(/lodash\.support *= */, ''); source = source.replace(/lodash\.support *= */, '');
// remove `_.templateSettings.imports assignment
source = source.replace(/,[^']*'imports':[^}]+}/, '');
// remove large array optimizations // remove large array optimizations
source = removeFunction(source, 'cachedContains'); source = removeFunction(source, 'cachedContains');
@@ -2756,24 +2813,12 @@
var snippet = getMethodAssignments(source), var snippet = getMethodAssignments(source),
modified = snippet; modified = snippet;
if (!useLodashMethod('assign')) { _.each(['assign', 'createCallback', 'forIn', 'forOwn', 'isPlainObject', 'zipObject'], function(methodName) {
modified = modified.replace(/^(?: *\/\/.*\s*)* *lodash\.assign *=.+\n/m, ''); if (!useLodashMethod(methodName)) {
} modified = modified.replace(RegExp('^(?: *//.*\\s*)* *lodash\\.' + methodName + ' *=.+\\n', 'm'), '');
if (!useLodashMethod('createCallback')) {
modified = modified.replace(/^(?: *\/\/.*\s*)* *lodash\.createCallback *=.+\n/m, '');
}
if (!useLodashMethod('forIn')) {
modified = modified.replace(/^(?: *\/\/.*\s*)* *lodash\.forIn *=.+\n/m, '');
}
if (!useLodashMethod('forOwn')) {
modified = modified.replace(/^(?: *\/\/.*\s*)* *lodash\.forOwn *=.+\n/m, '');
}
if (!useLodashMethod('isPlainObject')) {
modified = modified.replace(/^(?: *\/\/.*\s*)* *lodash\.isPlainObject *=.+\n/m, '');
}
if (!useLodashMethod('zipObject')) {
modified = modified.replace(/^(?: *\/\/.*\s*)* *lodash\.zipObject *=.+\n/m, '');
} }
});
source = source.replace(snippet, function() { source = source.replace(snippet, function() {
return modified; return modified;
}); });