Add _.findIndex and _.findKey. [closes #199]

Former-commit-id: 5ac98b559e074082d4019cd30c27bface063f9c9
This commit is contained in:
John-David Dalton
2013-03-17 19:28:46 -05:00
parent e941de50e8
commit 2dc539747b
5 changed files with 152 additions and 42 deletions

View File

@@ -97,6 +97,7 @@
'compact',
'difference',
'drop',
'findIndex',
'first',
'flatten',
'head',
@@ -187,6 +188,7 @@
'cloneDeep',
'defaults',
'extend',
'findKey',
'forIn',
'forOwn',
'functions',
@@ -293,6 +295,8 @@
'bindKey',
'cloneDeep',
'createCallback',
'findIndex',
'findKey',
'forIn',
'forOwn',
'isPlainObject',
@@ -946,6 +950,8 @@
'at',
'bindKey',
'createCallback',
'findIndex',
'findKey',
'forIn',
'forOwn',
'isPlainObject',
@@ -1362,6 +1368,9 @@
isUnderscore = /backbone|underscore/.test(command),
exposeAssign = !isUnderscore,
exposeCreateCallback = !isUnderscore,
exposeForIn = !isUnderscore,
exposeForOwn = !isUnderscore,
exposeIsPlainObject = !isUnderscore,
exposeZipObject = !isUnderscore;
try {
@@ -1418,12 +1427,24 @@
// remove nonexistent and duplicate method names
methodNames = _.uniq(_.intersection(allMethods, expandMethodNames(methodNames)));
if (isUnderscore) {
methodNames = _.without.apply(_, [methodNames].concat(['findIndex', 'findKey']));
}
if (!exposeAssign) {
methodNames = _.without(methodNames, 'assign');
}
if (!exposeCreateCallback) {
methodNames = _.without(methodNames, 'createCallback');
}
if (!exposeForIn) {
methodNames = _.without(methodNames, 'forIn');
}
if (!exposeForOwn) {
methodNames = _.without(methodNames, 'forOwn');
}
if (!exposeIsPlainObject) {
methodNames = _.without(methodNames, 'isPlainobject');
}
if (!exposeZipObject) {
methodNames = _.without(methodNames, 'zipObject');
}

View File

@@ -671,8 +671,6 @@
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.find');
(function() {
var objects = [
{ 'a': 0, 'b': 0 },
@@ -680,20 +678,31 @@
{ 'a': 2, 'b': 2 }
];
test('should return found `value`', function() {
equal(_.find(objects, function(object) { return object.a == 1; }), objects[1]);
});
_.each({
'find': [objects[1], undefined, objects[2], objects[1]],
'findIndex': [1, -1, 2, 1],
'findKey': ['1', undefined, '2', '1']
},
function(expected, methodName) {
QUnit.module('lodash.' + methodName);
test('should return `undefined` if `value` is not found', function() {
equal(_.find(objects, function(object) { return object.a == 3; }), undefined);
});
var func = _[methodName];
test('should work with an object for `callback`', function() {
equal(_.find(objects, { 'b': 2 }), objects[2]);
});
test('should return the correct value', function() {
strictEqual(func(objects, function(object) { return object.a == 1; }), expected[0]);
});
test('should work with a string for `callback`', function() {
equal(_.find(objects, 'b'), objects[1]);
test('should return `' + expected[1] + '` if value is not found', function() {
strictEqual(func(objects, function(object) { return object.a == 3; }), expected[1]);
});
test('should work with an object for `callback`', function() {
strictEqual(func(objects, { 'b': 2 }), expected[2]);
});
test('should work with a string for `callback`', function() {
strictEqual(func(objects, 'b'), expected[3]);
});
});
}());