mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
Reduce _.find, _.findLast, and _.defaults.
This commit is contained in:
84
lodash.js
84
lodash.js
@@ -230,6 +230,18 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used by `_.defaults` to customize its `_.assign` use.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {*} objectValue The destination object property value.
|
||||||
|
* @param {*} sourceValue The source object property value.
|
||||||
|
* @returns {*} Returns the value to assign to the destination object.
|
||||||
|
*/
|
||||||
|
function assignDefaults(objectValue, sourceValue) {
|
||||||
|
return typeof objectValue == 'undefined' ? sourceValue : objectValue;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base implementation of `compareAscending` used to compare values and
|
* The base implementation of `compareAscending` used to compare values and
|
||||||
* sort them in ascending order without guaranteeing a stable sort.
|
* sort them in ascending order without guaranteeing a stable sort.
|
||||||
@@ -3837,27 +3849,12 @@
|
|||||||
* // => { 'name': 'fred', 'age': 40, 'blocked': true }
|
* // => { 'name': 'fred', 'age': 40, 'blocked': true }
|
||||||
*/
|
*/
|
||||||
function find(collection, predicate, thisArg) {
|
function find(collection, predicate, thisArg) {
|
||||||
predicate = lodash.createCallback(predicate, thisArg, 3);
|
|
||||||
if (isArray(collection)) {
|
if (isArray(collection)) {
|
||||||
var index = -1,
|
var index = findIndex(collection, predicate, thisArg);
|
||||||
length = collection.length;
|
return index > -1 ? collection[index] : undefined;
|
||||||
|
|
||||||
while (++index < length) {
|
|
||||||
var value = collection[index];
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -3881,16 +3878,12 @@
|
|||||||
* // => 3
|
* // => 3
|
||||||
*/
|
*/
|
||||||
function findLast(collection, predicate, thisArg) {
|
function findLast(collection, predicate, thisArg) {
|
||||||
var result;
|
if (isArray(collection)) {
|
||||||
|
var index = findLastIndex(collection, predicate, thisArg);
|
||||||
predicate = lodash.createCallback(predicate, thisArg, 3);
|
return index > -1 ? collection[index] : undefined;
|
||||||
baseEachRight(collection, function(value, index, collection) {
|
}
|
||||||
if (predicate(value, index, collection)) {
|
var key = findLastKey(collection, predicate, thisArg);
|
||||||
result = value;
|
return typeof key == 'string' ? collection[key] : undefined;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -4517,7 +4510,7 @@
|
|||||||
collection = collection.split('');
|
collection = collection.split('');
|
||||||
}
|
}
|
||||||
if (n == null || guard) {
|
if (n == null || guard) {
|
||||||
var length = collection && collection.length | 0;
|
var length = (collection && collection.length) | 0;
|
||||||
return length > 0 ? collection[baseRandom(0, length - 1)] : undefined;
|
return length > 0 ? collection[baseRandom(0, length - 1)] : undefined;
|
||||||
}
|
}
|
||||||
var result = shuffle(collection);
|
var result = shuffle(collection);
|
||||||
@@ -5692,33 +5685,10 @@
|
|||||||
* _.defaults({ 'name': 'barney' }, { 'name': 'fred', 'employer': 'slate' });
|
* _.defaults({ 'name': 'barney' }, { 'name': 'fred', 'employer': 'slate' });
|
||||||
* // => { 'name': 'barney', 'employer': 'slate' }
|
* // => { 'name': 'barney', 'employer': 'slate' }
|
||||||
*/
|
*/
|
||||||
function defaults(object, source, guard) {
|
function defaults() {
|
||||||
if (!object) {
|
var args = slice(arguments);
|
||||||
return object;
|
args.push(assignDefaults);
|
||||||
}
|
return assign.apply(null, args);
|
||||||
var args = arguments,
|
|
||||||
argsIndex = 0,
|
|
||||||
argsLength = args.length,
|
|
||||||
type = typeof guard;
|
|
||||||
|
|
||||||
// enables use as a callback for functions like `_.reduce`
|
|
||||||
if ((type == 'number' || type == 'string') && args[3] && args[3][guard] === source) {
|
|
||||||
argsLength = 2;
|
|
||||||
}
|
|
||||||
while (++argsIndex < argsLength) {
|
|
||||||
source = args[argsIndex];
|
|
||||||
var index = -1,
|
|
||||||
props = keys(source),
|
|
||||||
length = props.length;
|
|
||||||
|
|
||||||
while (++index < length) {
|
|
||||||
var key = props[index];
|
|
||||||
if (typeof object[key] == 'undefined') {
|
|
||||||
object[key] = source[key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return object;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user