Add capitalize and getCategoryDependencies method to build.js

Former-commit-id: d939fbb482926a1673c5841f0c08b280ddca18e5
This commit is contained in:
John-David Dalton
2013-01-12 16:46:52 -08:00
parent 1eff48a429
commit 016391e442

View File

@@ -511,6 +511,17 @@
return source.join('\n'); return source.join('\n');
} }
/**
* Capitalizes a given string.
*
* @private
* @param {String} string The string to capitalize.
* @returns {String} Returns the capitalized string.
*/
function capitalize(string) {
return string[0].toUpperCase() + string.toLowerCase().slice(1);
}
/** /**
* Removes unnecessary comments, whitespace, and pseudo private properties. * Removes unnecessary comments, whitespace, and pseudo private properties.
* *
@@ -592,21 +603,46 @@
} }
/** /**
* Gets the Lo-Dash method assignments snippet from `source`. * Gets the category of the given method name.
* *
* @private * @private
* @param {String} source The source to inspect. * @param {String} source The source to inspect.
* @returns {String} Returns the method assignments snippet. * @param {String} methodName The method name.
* @returns {String} Returns the method name's category.
*/ */
function getMethodAssignments(source) { function getCategory(source, methodName) {
return (source.match(/\/\*-+\*\/\n(?:\s*\/\/.*)*\s*lodash\.\w+ *=[\s\S]+?lodash\.VERSION *=.+/) || [''])[0]; var result = /@category *(\w+)/.exec(matchFunction(source, methodName));
return result ? result[1] : '';
}
/**
* Gets an array of category dependencies for a given category.
*
* @private
* @param {String} source The source to inspect.
* @param {String} category The category.
* @returns {Array} Returns an array of cetegory dependants.
*/
function getCategoryDependencies(source, category) {
var methods = _.uniq(getMethodsByCategory(source, category).reduce(function(result, methodName) {
push.apply(result, getDependencies(methodName));
return result;
}, []));
var categories = _.uniq(methods.map(function(methodName) {
return getCategory(source, methodName);
}));
return categories.filter(function(other) {
return other != category;
});
} }
/** /**
* Gets an array of depenants for a method by a given name. * Gets an array of depenants for a method by a given name.
* *
* @private * @private
* @param {String} methodName The name of the method to query. * @param {String} methodName The method name.
* @returns {Array} Returns an array of method dependants. * @returns {Array} Returns an array of method dependants.
*/ */
function getDependants(methodName) { function getDependants(methodName) {
@@ -695,6 +731,17 @@
return (source.match(/^( *)var iteratorTemplate *= *[\s\S]+?\n\1.+?;\n/m) || [''])[0]; return (source.match(/^( *)var iteratorTemplate *= *[\s\S]+?\n\1.+?;\n/m) || [''])[0];
} }
/**
* Gets the Lo-Dash method assignments snippet from `source`.
*
* @private
* @param {String} source The source to inspect.
* @returns {String} Returns the method assignments snippet.
*/
function getMethodAssignments(source) {
return (source.match(/\/\*-+\*\/\n(?:\s*\/\/.*)*\s*lodash\.\w+ *=[\s\S]+?lodash\.VERSION *=.+/) || [''])[0];
}
/** /**
* Gets the names of methods in `source` belonging to the given `category`. * Gets the names of methods in `source` belonging to the given `category`.
* *
@@ -705,7 +752,7 @@
*/ */
function getMethodsByCategory(source, category) { function getMethodsByCategory(source, category) {
return allMethods.filter(function(methodName) { return allMethods.filter(function(methodName) {
return category && RegExp('@category ' + category + '\\b').test(matchFunction(source, methodName)); return getCategory(source, methodName) == category;
}); });
} }
@@ -1228,6 +1275,11 @@
// constructed using the "use strict" directive // constructed using the "use strict" directive
var isStrict = options.indexOf('strict') > -1; var isStrict = options.indexOf('strict') > -1;
// used to specify methods of specific categories
var categories = options.reduce(function(result, value) {
return /category/.test(value) ? optionToArray(value) : result;
}, []);
// used to specify the ways to export the `lodash` function // used to specify the ways to export the `lodash` function
var exportsOptions = options.reduce(function(result, value) { var exportsOptions = options.reduce(function(result, value) {
return /exports/.test(value) ? optionToArray(value).sort() : result; return /exports/.test(value) ? optionToArray(value).sort() : result;
@@ -1340,21 +1392,13 @@
if (isUnderscore && !result) { if (isUnderscore && !result) {
result = getDependencies(underscoreMethods); result = getDependencies(underscoreMethods);
} }
// add method names by category // add method names by category
options.some(function(value) { if (categories.length) {
if (!/category/.test(value)) { result = _.union(result || [], getDependencies(categories.reduce(function(accumulator, category) {
return false; // resolve method names belonging to each category (case-insensitive)
} return accumulator.concat(getMethodsByCategory(source, capitalize(category)));
// resolve method names belonging to each category (case-insensitive) }, [])));
var methodNames = optionToArray(value).reduce(function(accumulator, category) { }
var capitalized = category[0].toUpperCase() + category.toLowerCase().slice(1);
return accumulator.concat(getMethodsByCategory(source, capitalized));
}, []);
return (result = _.union(result || [], getDependencies(methodNames)));
});
if (!result) { if (!result) {
result = allMethods.slice(); result = allMethods.slice();
} }
@@ -1983,7 +2027,6 @@
} else { } else {
source = source.replace(/(?: *\/\/.*\n)* *(?:else )?if *\(freeExports\) *{\s*}(?:\s*else *{([\s\S]+?) *})?\n/, '$1\n'); source = source.replace(/(?: *\/\/.*\n)* *(?:else )?if *\(freeExports\) *{\s*}(?:\s*else *{([\s\S]+?) *})?\n/, '$1\n');
} }
if ((source.match(/\bfreeExports\b/g) || []).length < 2) { if ((source.match(/\bfreeExports\b/g) || []).length < 2) {
source = removeVar(source, 'freeExports'); source = removeVar(source, 'freeExports');
} }