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