mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 02:17:50 +00:00
Ensure find methods work with strings.
This commit is contained in:
41
lodash.js
41
lodash.js
@@ -3797,14 +3797,27 @@
|
|||||||
* // => { 'name': 'fred', 'age': 40, 'blocked': true }
|
* // => { 'name': 'fred', 'age': 40, 'blocked': true }
|
||||||
*/
|
*/
|
||||||
function find(collection, predicate, thisArg) {
|
function find(collection, predicate, thisArg) {
|
||||||
var length = collection ? collection.length : 0;
|
predicate = lodash.createCallback(predicate, thisArg, 3);
|
||||||
|
if (isArray(collection)) {
|
||||||
|
var index = -1,
|
||||||
|
length = collection.length;
|
||||||
|
|
||||||
if (typeof length == 'number' && length > -1 && length <= maxSafeInteger) {
|
while (++index < length) {
|
||||||
var index = findIndex(collection, predicate, thisArg);
|
var value = collection[index];
|
||||||
return index > -1 ? collection[index] : undefined;
|
if (predicate(value, index, collection)) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var result;
|
||||||
|
baseEach(collection, function(value, index, collection) {
|
||||||
|
if (predicate(value, index, collection)) {
|
||||||
|
result = value;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
var key = findKey(collection, predicate, thisArg);
|
|
||||||
return typeof key == 'string' ? collection[key] : undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -3828,14 +3841,16 @@
|
|||||||
* // => 3
|
* // => 3
|
||||||
*/
|
*/
|
||||||
function findLast(collection, predicate, thisArg) {
|
function findLast(collection, predicate, thisArg) {
|
||||||
var length = collection ? collection.length : 0;
|
var result;
|
||||||
|
|
||||||
if (typeof length == 'number' && length > -1 && length <= maxSafeInteger) {
|
predicate = lodash.createCallback(predicate, thisArg, 3);
|
||||||
var index = findLastIndex(collection, predicate, thisArg);
|
baseEachRight(collection, function(value, index, collection) {
|
||||||
return index > -1 ? collection[index] : undefined;
|
if (predicate(value, index, collection)) {
|
||||||
}
|
result = value;
|
||||||
var key = findLastKey(collection, predicate, thisArg);
|
return false;
|
||||||
return typeof key == 'string' ? collection[key] : undefined;
|
}
|
||||||
|
});
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
84
test/test.js
84
test/test.js
@@ -2393,25 +2393,26 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
(function() {
|
_.forEach(['find', 'findLast', 'findIndex', 'findLastIndex', 'findKey', 'findLastKey'], function(methodName) {
|
||||||
var objects = [
|
QUnit.module('lodash.' + methodName);
|
||||||
{ 'a': 0, 'b': 0 },
|
|
||||||
{ 'a': 1, 'b': 1 },
|
|
||||||
{ 'a': 2, 'b': 2 }
|
|
||||||
];
|
|
||||||
|
|
||||||
_.forEach({
|
var func = _[methodName];
|
||||||
'find': [objects[1], undefined, objects[2], objects[1]],
|
|
||||||
'findLast': [objects[2], undefined, objects[2], objects[2]],
|
|
||||||
'findIndex': [1, -1, 2, 1],
|
|
||||||
'findLastIndex': [2, -1, 2, 2],
|
|
||||||
'findKey': ['1', undefined, '2', '1'],
|
|
||||||
'findLastKey': ['2', undefined, '2', '2']
|
|
||||||
},
|
|
||||||
function(expected, methodName) {
|
|
||||||
QUnit.module('lodash.' + methodName);
|
|
||||||
|
|
||||||
var func = _[methodName];
|
(function() {
|
||||||
|
var objects = [
|
||||||
|
{ 'a': 0, 'b': 0 },
|
||||||
|
{ 'a': 1, 'b': 1 },
|
||||||
|
{ 'a': 2, 'b': 2 }
|
||||||
|
];
|
||||||
|
|
||||||
|
var expected = ({
|
||||||
|
'find': [objects[1], undefined, objects[2], objects[1]],
|
||||||
|
'findLast': [objects[2], undefined, objects[2], objects[2]],
|
||||||
|
'findIndex': [1, -1, 2, 1],
|
||||||
|
'findLastIndex': [2, -1, 2, 2],
|
||||||
|
'findKey': ['1', undefined, '2', '1'],
|
||||||
|
'findLastKey': ['2', undefined, '2', '2']
|
||||||
|
})[methodName];
|
||||||
|
|
||||||
test('should return the correct value', 1, function() {
|
test('should return the correct value', 1, function() {
|
||||||
strictEqual(func(objects, function(object) { return object.a; }), expected[0]);
|
strictEqual(func(objects, function(object) { return object.a; }), expected[0]);
|
||||||
@@ -2425,14 +2426,6 @@
|
|||||||
strictEqual(func(objects, function(object) { return object.a == 3; }), expected[1]);
|
strictEqual(func(objects, function(object) { return object.a == 3; }), expected[1]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should work with an object for `collection`', 1, function() {
|
|
||||||
var actual = _.find({ 'a': 1, 'b': 2, 'c': 3 }, function(num) {
|
|
||||||
return num > 2;
|
|
||||||
});
|
|
||||||
|
|
||||||
equal(actual, 3);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should work with an object for `callback`', 1, function() {
|
test('should work with an object for `callback`', 1, function() {
|
||||||
strictEqual(func(objects, { 'b': 2 }), expected[2]);
|
strictEqual(func(objects, { 'b': 2 }), expected[2]);
|
||||||
});
|
});
|
||||||
@@ -2454,15 +2447,52 @@
|
|||||||
|
|
||||||
deepEqual(actual, expecting);
|
deepEqual(actual, expecting);
|
||||||
});
|
});
|
||||||
|
}());
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
var expected = ({
|
||||||
|
'find': 1,
|
||||||
|
'findLast': 2,
|
||||||
|
'findKey': 'a',
|
||||||
|
'findLastKey': 'b'
|
||||||
|
})[methodName];
|
||||||
|
|
||||||
|
if (expected != null) {
|
||||||
|
test('should work with an object for `collection`', 1, function() {
|
||||||
|
var actual = func({ 'a': 1, 'b': 2, 'c': 3 }, function(num) {
|
||||||
|
return num < 3;
|
||||||
|
});
|
||||||
|
|
||||||
|
equal(actual, expected);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}());
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
var expected = ({
|
||||||
|
'find': 'a',
|
||||||
|
'findLast': 'b',
|
||||||
|
'findIndex': 0,
|
||||||
|
'findLastIndex': 1
|
||||||
|
})[methodName];
|
||||||
|
|
||||||
|
if (expected != null) {
|
||||||
|
test('should work with a string for `collection`', 1, function() {
|
||||||
|
var actual = func('abc', function(chr, index) {
|
||||||
|
return index < 2;
|
||||||
|
});
|
||||||
|
|
||||||
|
equal(actual, expected);
|
||||||
|
});
|
||||||
|
}
|
||||||
if (methodName == 'find') {
|
if (methodName == 'find') {
|
||||||
test('should be aliased', 2, function() {
|
test('should be aliased', 2, function() {
|
||||||
strictEqual(_.detect, func);
|
strictEqual(_.detect, func);
|
||||||
strictEqual(_.findWhere, func);
|
strictEqual(_.findWhere, func);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
}());
|
||||||
}());
|
});
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user