8000 Limit the `category` build option to adding only those methods availa… · lodash/lodash@4e2ef90 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4e2ef90

Browse files
committed
Limit the category build option to adding only those methods available for specific builds (i.e. underscore and backbone builds).
Former-commit-id: 63a5509cd953b20376723335d42fb5a136eb3a5c
1 parent f5dd05b commit 4e2ef90

File tree

2 files changed

+72
-52
lines changed

2 files changed

+72
-52
lines changed

build.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,10 @@
893893
* @returns {Array} Returns the new converted array.
894894
*/
895895
function optionToArray(value) {
896-
return _.compact(value.match(/\w+=(.*)$/)[1].split(/, */));
896+
return _.compact(_.isArray(value)
897+
? value
898+
: value.match(/\w+=(.*)$/)[1].split(/, */)
899+
);
897900
}
898901

899902
/**
@@ -908,11 +911,6 @@
908911
function optionToMethodsArray(source, value) {
909912
var methodNames = optionToArray(value);
910913

911-
// convert categories to method names
912-
methodNames.forEach(function(category) {
913-
push.apply(methodNames, getMethodsByCategory(source, category));
914-
});
915-
916914
// convert aliases to real method names
917915
methodNames = methodNames.map(getRealName);
918916

@@ -1606,11 +1604,6 @@
16061604
// flag to specify a legacy build
16071605
var isLegacy = !(isModern || isUnderscore) && options.indexOf('legacy') > -1;
16081606

1609-
// used to specify methods of specific categories
1610-
var categories = options.reduce(function(result, value) {
1611-
return /category/.test(value) ? optionToArray(value) : result;
1612-
}, []);
1613-
16141607
// used to specify the ways to export the `lodash` function
16151608
var exportsOptions = options.reduce(function(result, value) {
16161609
return /exports/.test(value) ? optionToArray(value).sort() : result;
@@ -1644,7 +1637,7 @@
16441637
: result;
16451638
}, '');
16461639

1647-
// used when precompiling template files
1640+
// used as the template settings for precompiled templates
16481641
var templateSettings = options.reduce(function(result, value) {
16491642
var match = value.match(/settings=(.+)$/);
16501643
return match
@@ -1701,6 +1694,17 @@
17011694
: accumulator;
17021695
}, []);
17031696

1697+
var categories = options.reduce(function(accumulator, value) {
1698+
if (/category|exclude|include|minus|plus/.test(value)) {
1699+
var array = optionToArray(value);
1700+
accumulator = _.union(accumulator, /category/.test(value)
1701+
? array
1702+
: array.filter(function(category) { return /^[A-Z]/.test(category); })
1703+
);
1704+
}
1705+
return accumulator;
1706+
}, []);
1707+
17041708
// set flags to include Lo-Dash's methods if explicitly requested
17051709
if (isUnderscore) {
17061710
var methods = _.without.apply(_, [_.union(includeMethods, plusMethods)].concat(minusMethods));
@@ -1775,8 +1779,21 @@
17751779
// add method names by category
17761780
if (categories.length) {
17771781
result = _.union(result || [], getDependencies(categories.reduce(function(accumulator, category) {
1778-
// resolve method names belonging to each category (case-insensitive)
1779-
return accumulator.concat(getMethodsByCategory(source, capitalize(category)));
1782+
// get method names belonging to each category (case-insensitive)
1783+
var methodNames = getMethodsByCategory(source, capitalize(category));
1784+
1785+
// limit category methods to those available for specific builds
1786+
if (isBackbone) {
1787+
methodNames = methodNames.filter(function(methodName) {
1788+
return _.contains(backboneDependencies, methodName);
1789+
});
1790+
}
1791+
else if (isUnderscore) {
1792+
methodNames = methodNames.filter(function(methodName) {
1793+
return _.contains(underscoreMethods, methodName);
1794+
});
1795+
}
1796+
return accumulator.concat(methodNames);
17801797
}, [])));
17811798
}
17821799
if (!result) {

test/test-build.js

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
global.QUnit
3434
);
3535

36+
/** Shortcut used to push arrays of values to an array */
37+
var push = Array.prototype.push;
38+
3639
/** The time limit for the tests to run (milliseconds) */
3740
var timeLimit = process.argv.reduce(function(result, value, index) {
3841
if (/--time-limit/.test(value)) {
@@ -308,6 +311,17 @@
308311

309312
/*--------------------------------------------------------------------------*/
310313

314+
/**
315+
* Capitalizes a given string.
316+
*
317+
* @private
318+
* @param {String} string The string to capitalize.
319+
* @returns {String} Returns the capitalized string.
320+
*/
321+
function capitalize(string) {
322+
return string[0].toUpperCase() + string.toLowerCase().slice(1);
323+
}
324+
311325
/**
312326
* Creates a context object to use with `vm.runInContext`.
313327
*
@@ -331,7 +345,7 @@
331345
function expandMethodNames(methodNames) {
332346
return methodNames.reduce(function(result, methodName) {
333347
var realName = getRealName(methodName);
334-
result.push.apply(result, [realName].concat(getAliases(realName)));
348+
push.apply(result, [realName].concat(getAliases(realName)));
335349
return result;
336350
}, []);
337351
}
@@ -1365,12 +1379,9 @@
13651379
var methodNames,
13661380
basename = path.basename(data.outputPath, '.js'),
13671381
context = createContext(),
1368-
isUnderscore = /backbone|underscore/.test(command),
1382+
isBackbone = /backbone/.test(command),
1383+
isUnderscore = isBackbone || /underscore/.test(command),
13691384
exposeAssign = !isUnderscore,
1370-
exposeCreateCallback = !isUnderscore,
1371-
exposeForIn = !isUnderscore,
1372-
exposeForOwn = !isUnderscore,
1373-
exposeIsPlainObject = !isUnderscore,
13741385
exposeZipObject = !isUnderscore;
13751386

13761387
try {
@@ -1389,62 +1400,54 @@
13891400
if (isUnderscore) {
13901401
if (methodNames) {
13911402
exposeAssign = methodNames.indexOf('assign') > -1;
1392-
exposeCreateCallback = methodNames.indexOf('createCallback') > -1;
13931403
exposeZipObject = methodNames.indexOf('zipObject') > -1;
13941404
} else {
13951405
methodNames = underscoreMethods.slice();
13961406
}
13971407
}
1398-
// add method names explicitly by category
13991408
if (/category/.test(command)) {
1400-
// resolve method names belonging to each category (case-insensitive)
1401-
methodNames = command.match(/category=(\S*)/)[1].split(/, */).reduce(function(result, category) {
1402-
var capitalized = category[0].toUpperCase() + category.toLowerCase().slice(1);
1403-
return result.concat(getMethodsByCategory(capitalized));
1404-
}, methodNames || []);
1409+
methodNames = (methodNames || []).concat(command.match(/category=(\S*)/)[1].split(/, */).map(capitalize));
14051410
}
1406-
// init `methodNames` if it hasn't been inited
14071411
if (!methodNames) {
14081412
methodNames = allMethods.slice();
14091413
}
14101414
if (/plus/.test(command)) {
14111415
methodNames = methodNames.concat(command.match(/plus=(\S*)/)[1].split(/, */));
14121416
}
14131417
if (/minus/.test(command)) {
1414-
methodNames = _.without.apply(_, [methodNames]
1415-
.concat(expandMethodNames(command.match(/minus=(\S*)/)[1].split(/, */))));
1418+
methodNames = _.without.apply(_, [methodNames].concat(expandMethodNames(command.match(/minus=(\S*)/)[1].split(/, */))));
14161419
}
14171420
if (/exclude/.test(command)) {
1418-
methodNames = _.without.apply(_, [methodNames]
1419-
.concat(expandMethodNames(command.match(/exclude=(\S*)/)[1].split(/, */))));
1421+
methodNames = _.without.apply(_, [methodNames].concat(expandMethodNames(command.match(/exclude=(\S*)/)[1].split(/, */))));
14201422
}
14211423

1422-
// expand aliases and categories to real method names
1423-
methodNames = expandMethodNames(methodNames).reduce(function(result, methodName) {
1424-
return result.concat(methodName, getMethodsByCategory(methodName));
1425-
}, []);
1424+
// expand categories to real method names
1425+
methodNames.slice().forEach(function(category) {
1426+
var result = getMethodsByCategory(category);
1427+
1428+
// limit category methods to those available for specific builds
1429+
if (isBackbone) {
1430+
result = result.filter(function(methodName) {
1431+
return _.contains(backboneDependencies, methodName);
1432+
});
1433+
}
1434+
else if (isUnderscore) {
1435+
result = result.filter(function(methodName) {
1436+
return _.contains(underscoreMethods, methodName);
1437+
});
1438+
}
1439+
if (result.length) {
1440+
methodNames = _.without(methodNames, category);
1441+
push.apply(methodNames, result);
1442+
}
1443+
});
14261444

1427-
// remove nonexistent and duplicate method names
1445+
// expand aliases and remove nonexistent and duplicate method names
14281446
methodNames = _.uniq(_.intersection(allMethods, expandMethodNames(methodNames)));
14291447

1430-
if (isUnderscore) {
1431-
methodNames = _.without.apply(_, [methodNames].concat(['findIndex', 'findKey']));
1432-
}
14331448
if (!exposeAssign) {
14341449
methodNames = _.without(methodNames, 'assign');
14351450
}
1436-
if (!exposeCreateCallback) {
1437-
methodNames = _.without(methodNames, 'createCallback');
1438-
}
1439-
if (!exposeForIn) {
1440-
methodNames = _.without(methodNames, 'forIn');
1441-
}
1442-
if (!exposeForOwn) {
1443-
methodNames = _.without(methodNames, 'forOwn');
1444-
}
1445-
if (!exposeIsPlainObject) {
1446-
methodNames = _.without(methodNames, 'isPlainobject');
1447-
}
14481451
if (!exposeZipObject) {
14491452
methodNames = _.without(methodNames, 'zipObject');
14501453
}

0 commit comments

Comments
 (0)
0