mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 15:27:50 +00:00
Add unit tests to ensure non-underscore builds may include _.chain and _.findWhere methods.
Former-commit-id: 91a4ea5114a45bc5038a37d7ff4ea4b6212182ca
This commit is contained in:
92
build.js
92
build.js
@@ -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
|
||||
|
||||
@@ -90,10 +90,13 @@
|
||||
};
|
||||
|
||||
/** List of all Lo-Dash methods */
|
||||
var allMethods = _.functions(_).filter(function(methodName) {
|
||||
var lodashMethods = _.functions(_).filter(function(methodName) {
|
||||
return !/^_/.test(methodName);
|
||||
});
|
||||
|
||||
/** List of all methods */
|
||||
var allMethods = lodashMethods.concat('chain', 'findWhere');
|
||||
|
||||
/** List of "Arrays" category methods */
|
||||
var arraysMethods = [
|
||||
'compact',
|
||||
@@ -125,6 +128,7 @@
|
||||
|
||||
/** List of "Chaining" category methods */
|
||||
var chainingMethods = [
|
||||
'chain',
|
||||
'tap',
|
||||
'value'
|
||||
];
|
||||
@@ -315,10 +319,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));
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
@@ -804,14 +806,15 @@
|
||||
(function() {
|
||||
var commands = [
|
||||
'backbone',
|
||||
'underscore'
|
||||
'underscore',
|
||||
'modern plus=chain'
|
||||
];
|
||||
|
||||
commands.forEach(function(command) {
|
||||
asyncTest('`lodash ' + command +'`', function() {
|
||||
var start = _.after(2, _.once(QUnit.start));
|
||||
|
||||
build(['-s', command], function(data) {
|
||||
build(['-s'].concat(command.split(' ')), function(data) {
|
||||
var basename = path.basename(data.outputPath, '.js'),
|
||||
context = createContext();
|
||||
|
||||
@@ -843,6 +846,37 @@
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
QUnit.module('modifiers with findWhere');
|
||||
|
||||
(function() {
|
||||
var commands = [
|
||||
'underscore include=findWhere',
|
||||
'modern include=findWhere',
|
||||
'plus=findWhere'
|
||||
];
|
||||
|
||||
commands.forEach(function(command) {
|
||||
asyncTest('`lodash ' + command + '`', function() {
|
||||
var start = _.after(2, _.once(QUnit.start));
|
||||
|
||||
build(['-s'].concat(command.split(' ')), function(data) {
|
||||
var basename = path.basename(data.outputPath, '.js'),
|
||||
context = createContext();
|
||||
|
||||
vm.runInContext(data.source, context);
|
||||
var lodash = context._;
|
||||
|
||||
var collection = [{ 'a': 1 }, { 'a': 1 }];
|
||||
deepEqual(lodash.findWhere(collection, { 'a': 1 }), collection[0], '_.findWhere: ' + basename);
|
||||
|
||||
start();
|
||||
});
|
||||
});
|
||||
});
|
||||
}());
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
QUnit.module('underscore modifier');
|
||||
|
||||
(function() {
|
||||
@@ -985,23 +1019,6 @@
|
||||
});
|
||||
});
|
||||
|
||||
asyncTest('`lodash underscore include=findWhere`', function() {
|
||||
var start = _.after(2, _.once(QUnit.start));
|
||||
|
||||
build(['-s', 'underscore', 'include=findWhere'], function(data) {
|
||||
var basename = path.basename(data.outputPath, '.js'),
|
||||
context = createContext();
|
||||
|
||||
vm.runInContext(data.source, context);
|
||||
var lodash = context._;
|
||||
|
||||
var collection = [{ 'a': 1 }, { 'a': 1 }];
|
||||
deepEqual(lodash.findWhere(collection, { 'a': 1 }), collection[0], '_.findWhere: ' + basename);
|
||||
|
||||
start();
|
||||
});
|
||||
});
|
||||
|
||||
asyncTest('`lodash underscore include=partial`', function() {
|
||||
var start = _.after(2, _.once(QUnit.start));
|
||||
|
||||
@@ -1412,7 +1429,7 @@
|
||||
methodNames = (methodNames || []).concat(command.match(/category=(\S*)/)[1].split(/, */).map(capitalize));
|
||||
}
|
||||
if (!methodNames) {
|
||||
methodNames = allMethods.slice();
|
||||
methodNames = lodashMethods.slice();
|
||||
}
|
||||
if (/plus/.test(command)) {
|
||||
methodNames = methodNames.concat(command.match(/plus=(\S*)/)[1].split(/, */));
|
||||
@@ -1429,16 +1446,13 @@
|
||||
var result = getMethodsByCategory(category);
|
||||
|
||||
// limit category methods to those available for specific builds
|
||||
if (isBackbone) {
|
||||
result = result.filter(function(methodName) {
|
||||
return _.contains(backboneDependencies, methodName);
|
||||
});
|
||||
}
|
||||
else if (isUnderscore) {
|
||||
result = result.filter(function(methodName) {
|
||||
return _.contains(underscoreMethods, methodName);
|
||||
});
|
||||
}
|
||||
result = result.filter(function(methodName) {
|
||||
return _.contains(
|
||||
isBackbone ? backboneDependencies :
|
||||
isUnderscore ? underscoreMethods :
|
||||
lodashMethods, methodName
|
||||
);
|
||||
});
|
||||
if (result.length) {
|
||||
methodNames = _.without(methodNames, category);
|
||||
push.apply(methodNames, result);
|
||||
|
||||
@@ -3205,7 +3205,7 @@
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
QUnit.module('"Arrays" methods');
|
||||
QUnit.module('"Arrays" category methods');
|
||||
|
||||
(function() {
|
||||
var args = arguments;
|
||||
|
||||
Reference in New Issue
Block a user