mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 10:17:48 +00:00
Add baseAt to reduce _.at and _.pullAt.
This commit is contained in:
55
lodash.js
55
lodash.js
@@ -248,6 +248,28 @@
|
|||||||
return typeof objectValue == 'undefined' ? sourceValue : objectValue;
|
return typeof objectValue == 'undefined' ? sourceValue : objectValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The base implementation of `_.at` without support for strings or individual
|
||||||
|
* key arguments.
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @memberOf _
|
||||||
|
* @category Collections
|
||||||
|
* @param {Array|Object} collection The collection to iterate over.
|
||||||
|
* @param {number[]|string[]} [keys] The keys of elements to pick.
|
||||||
|
* @returns {Array} Returns the new array of picked elements.
|
||||||
|
*/
|
||||||
|
function baseAt(collection, props) {
|
||||||
|
var index = -1,
|
||||||
|
length = props.length,
|
||||||
|
result = Array(length);
|
||||||
|
|
||||||
|
while(++index < length) {
|
||||||
|
result[index] = collection[props[index]];
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
@@ -2936,7 +2958,7 @@
|
|||||||
* returns an array of removed elements. Indexes may be specified as an array
|
* returns an array of removed elements. Indexes may be specified as an array
|
||||||
* of indexes or as individual arguments.
|
* of indexes or as individual arguments.
|
||||||
*
|
*
|
||||||
* Note: Like `_.pull`, this method mutates `array`.
|
* Note: Unlike `_.at`, this method mutates `array`.
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
@@ -2957,21 +2979,16 @@
|
|||||||
* // => [10, 20]
|
* // => [10, 20]
|
||||||
*/
|
*/
|
||||||
function pullAt(array) {
|
function pullAt(array) {
|
||||||
var previous,
|
var indexes = baseFlatten(arguments, true, false, 1),
|
||||||
index = -1,
|
length = indexes.length,
|
||||||
removals = baseFlatten(arguments, true, false, 1),
|
result = baseAt(array, indexes);
|
||||||
length = removals.length,
|
|
||||||
result = Array(length);
|
|
||||||
|
|
||||||
while (++index < length) {
|
indexes.sort(baseCompareAscending);
|
||||||
result[index] = array[removals[index]];
|
|
||||||
}
|
|
||||||
removals.sort(baseCompareAscending);
|
|
||||||
while (length--) {
|
while (length--) {
|
||||||
var removal = removals[length];
|
var index = indexes[length];
|
||||||
if (removal != previous) {
|
if (index != previous) {
|
||||||
splice.call(array, removal, 1);
|
var previous = removal;
|
||||||
previous = removal;
|
splice.call(array, index, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
@@ -3657,18 +3674,10 @@
|
|||||||
* // => ['fred', 'pebbles']
|
* // => ['fred', 'pebbles']
|
||||||
*/
|
*/
|
||||||
function at(collection) {
|
function at(collection) {
|
||||||
var index = -1,
|
|
||||||
props = baseFlatten(arguments, true, false, 1),
|
|
||||||
length = props.length;
|
|
||||||
|
|
||||||
if (support.unindexedChars && isString(collection)) {
|
if (support.unindexedChars && isString(collection)) {
|
||||||
collection = collection.split('');
|
collection = collection.split('');
|
||||||
}
|
}
|
||||||
var result = Array(length);
|
return baseAt(collection, baseFlatten(arguments, true, false, 1));
|
||||||
while(++index < length) {
|
|
||||||
result[index] = collection[props[index]];
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user