Add unit tests to ensure non-underscore builds may include _.chain and _.findWhere methods.

Former-commit-id: 91a4ea5114a45bc5038a37d7ff4ea4b6212182ca
This commit is contained in:
John-David Dalton
2013-04-07 22:55:52 -07:00
parent f1d3df1ec0
commit 1faa5a80e4
3 changed files with 98 additions and 80 deletions

View File

@@ -182,7 +182,7 @@
// method used by the `backbone` and `underscore` builds
'chain': ['value'],
'findWhere': ['where']
'findWhere': ['find']
};
/** Used to inline `iteratorTemplate` */
@@ -199,8 +199,11 @@
'useKeys'
];
/** List of all Lo-Dash methods */
var allMethods = _.without(_.keys(dependencyMap), 'chain', 'findWhere');
/** List of all methods */
var allMethods = _.without(_.keys(dependencyMap));
/** List of Lo-Dash methods */
var lodashMethods = _.without(allMethods, 'chain', 'findWhere');
/** List of Backbone's Lo-Dash dependencies */
var backboneDependencies = [
@@ -277,10 +280,8 @@
'unzip'
];
/** List of methods used by Underscore */
var underscoreMethods = _.without
.apply(_, [allMethods].concat(lodashOnlyMethods))
.concat('chain', 'findWhere');
/** List of Underscore methods */
var underscoreMethods = _.without.apply(_, [allMethods].concat(lodashOnlyMethods));
/** List of ways to export the `lodash` function */
var exportsAll = [
@@ -1740,6 +1741,7 @@
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');
@@ -1815,7 +1817,7 @@
}, [])));
}
if (!result) {
result = allMethods.slice();
result = lodashMethods.slice();
}
if (plusMethods.length) {
result = _.union(result, getDependencies(plusMethods));
@@ -2443,44 +2445,46 @@
});
}
}
// add `_.findWhere`
// add Underscore's `_.findWhere`
if (_.contains(buildMethods, 'findWhere')) {
source = source.replace(matchFunction(source, 'find'), function(match) {
var indent = getIndent(match);
return match && (match + [
'',
'/**',
' * Examines each element in a `collection`, returning the first that',
' * has the given `properties`. When checking `properties`, this method',
' * performs a deep comparison between values to determine if they are',
' * equivalent to each other.',
' *',
' * @static',
' * @memberOf _',
' * @category Collections',
' * @param {Array|Object|String} collection The collection to iterate over.',
' * @param {Object} properties The object of property values to filter by.',
' * @returns {Mixed} Returns the found element, else `undefined`.',
' * @example',
' *',
' * var food = [',
" * { 'name': 'apple', 'organic': false, 'type': 'fruit' },",
" * { 'name': 'banana', 'organic': true, 'type': 'fruit' },",
" * { 'name': 'beet', 'organic': false, 'type': 'vegetable' }",
' * ];',
' *',
" * _.findWhere(food, { 'type': 'vegetable' });",
" * // => { 'name': 'beet', 'organic': false, 'type': 'vegetable' }",
' */',
'function findWhere(object, properties) {',
' return where(object, properties, true);',
'}',
''
].join('\n' + indent));
});
if (isUnderscore) {
source = source.replace(matchFunction(source, 'find'), function(match) {
var indent = getIndent(match);
return match && (match + [
'',
'/**',
' * Examines each element in a `collection`, returning the first that',
' * has the given `properties`. When checking `properties`, this method',
' * performs a deep comparison between values to determine if they are',
' * equivalent to each other.',
' *',
' * @static',
' * @memberOf _',
' * @category Collections',
' * @param {Array|Object|String} collection The collection to iterate over.',
' * @param {Object} properties The object of property values to filter by.',
' * @returns {Mixed} Returns the found element, else `undefined`.',
' * @example',
' *',
' * var food = [',
" * { 'name': 'apple', 'organic': false, 'type': 'fruit' },",
" * { 'name': 'banana', 'organic': true, 'type': 'fruit' },",
" * { 'name': 'beet', 'organic': false, 'type': 'vegetable' }",
' * ];',
' *',
" * _.findWhere(food, { 'type': 'vegetable' });",
" * // => { 'name': 'beet', 'organic': false, 'type': 'vegetable' }",
' */',
'function findWhere(object, properties) {',
' return where(object, properties, true);',
'}',
''
].join('\n' + indent));
});
}
source = source.replace(getMethodAssignments(source), function(match) {
return match.replace(/^( *)lodash.find *=.+/m, '$&\n$1lodash.findWhere = findWhere;');
var methodName = isUnderscore ? 'findWhere' : 'find';
return match.replace(/^( *)lodash.find *=.+/m, '$&\n$1lodash.findWhere = ' + methodName + ';');
});
}
// add Underscore's chaining methods