mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 23:37:49 +00:00
Bump to v3.0.0.
This commit is contained in:
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@@ -0,0 +1 @@
|
||||
* text=auto
|
||||
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
.DS_Store
|
||||
*.log
|
||||
node_modules
|
||||
1
.npmignore
Normal file
1
.npmignore
Normal file
@@ -0,0 +1 @@
|
||||
.*
|
||||
22
LICENSE.txt
Normal file
22
LICENSE.txt
Normal file
@@ -0,0 +1,22 @@
|
||||
Copyright 2012-2015 The Dojo Foundation <http://dojofoundation.org/>
|
||||
Based on Underscore.js 1.7.0, copyright 2009-2015 Jeremy Ashkenas,
|
||||
DocumentCloud and Investigative Reporters & Editors <http://underscorejs.org/>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
8
README.md
Normal file
8
README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
# lodash-es v3.0.0
|
||||
|
||||
The [modern build](https://github.com/lodash/lodash/wiki/Build-Differences) of [lodash](https://lodash.com/) exported as [ES](https://people.mozilla.org/~jorendorff/es6-draft.html) modules.
|
||||
|
||||
Generated using [lodash-cli](https://www.npmjs.com/package/lodash-cli):
|
||||
```bash
|
||||
$ lodash modularize modern exports=es -o ./
|
||||
```
|
||||
81
array.js
Normal file
81
array.js
Normal file
@@ -0,0 +1,81 @@
|
||||
import chunk from './array/chunk';
|
||||
import compact from './array/compact';
|
||||
import difference from './array/difference';
|
||||
import drop from './array/drop';
|
||||
import dropRight from './array/dropRight';
|
||||
import dropRightWhile from './array/dropRightWhile';
|
||||
import dropWhile from './array/dropWhile';
|
||||
import findIndex from './array/findIndex';
|
||||
import findLastIndex from './array/findLastIndex';
|
||||
import first from './array/first';
|
||||
import flatten from './array/flatten';
|
||||
import flattenDeep from './array/flattenDeep';
|
||||
import head from './array/head';
|
||||
import indexOf from './array/indexOf';
|
||||
import initial from './array/initial';
|
||||
import intersection from './array/intersection';
|
||||
import last from './array/last';
|
||||
import lastIndexOf from './array/lastIndexOf';
|
||||
import object from './array/object';
|
||||
import pull from './array/pull';
|
||||
import pullAt from './array/pullAt';
|
||||
import remove from './array/remove';
|
||||
import rest from './array/rest';
|
||||
import slice from './array/slice';
|
||||
import sortedIndex from './array/sortedIndex';
|
||||
import sortedLastIndex from './array/sortedLastIndex';
|
||||
import tail from './array/tail';
|
||||
import take from './array/take';
|
||||
import takeRight from './array/takeRight';
|
||||
import takeRightWhile from './array/takeRightWhile';
|
||||
import takeWhile from './array/takeWhile';
|
||||
import union from './array/union';
|
||||
import uniq from './array/uniq';
|
||||
import unique from './array/unique';
|
||||
import unzip from './array/unzip';
|
||||
import without from './array/without';
|
||||
import xor from './array/xor';
|
||||
import zip from './array/zip';
|
||||
import zipObject from './array/zipObject';
|
||||
|
||||
export default {
|
||||
'chunk': chunk,
|
||||
'compact': compact,
|
||||
'difference': difference,
|
||||
'drop': drop,
|
||||
'dropRight': dropRight,
|
||||
'dropRightWhile': dropRightWhile,
|
||||
'dropWhile': dropWhile,
|
||||
'findIndex': findIndex,
|
||||
'findLastIndex': findLastIndex,
|
||||
'first': first,
|
||||
'flatten': flatten,
|
||||
'flattenDeep': flattenDeep,
|
||||
'head': head,
|
||||
'indexOf': indexOf,
|
||||
'initial': initial,
|
||||
'intersection': intersection,
|
||||
'last': last,
|
||||
'lastIndexOf': lastIndexOf,
|
||||
'object': object,
|
||||
'pull': pull,
|
||||
'pullAt': pullAt,
|
||||
'remove': remove,
|
||||
'rest': rest,
|
||||
'slice': slice,
|
||||
'sortedIndex': sortedIndex,
|
||||
'sortedLastIndex': sortedLastIndex,
|
||||
'tail': tail,
|
||||
'take': take,
|
||||
'takeRight': takeRight,
|
||||
'takeRightWhile': takeRightWhile,
|
||||
'takeWhile': takeWhile,
|
||||
'union': union,
|
||||
'uniq': uniq,
|
||||
'unique': unique,
|
||||
'unzip': unzip,
|
||||
'without': without,
|
||||
'xor': xor,
|
||||
'zip': zip,
|
||||
'zipObject': zipObject
|
||||
};
|
||||
47
array/chunk.js
Normal file
47
array/chunk.js
Normal file
@@ -0,0 +1,47 @@
|
||||
import baseSlice from '../internal/baseSlice';
|
||||
import isIterateeCall from '../internal/isIterateeCall';
|
||||
|
||||
/** Native method references. */
|
||||
var ceil = Math.ceil;
|
||||
|
||||
/* Native method references for those with the same name as other `lodash` methods. */
|
||||
var nativeMax = Math.max;
|
||||
|
||||
/**
|
||||
* Creates an array of elements split into groups the length of `size`.
|
||||
* If `collection` can't be split evenly, the final chunk will be the remaining
|
||||
* elements.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The array to process.
|
||||
* @param {numer} [size=1] The length of each chunk.
|
||||
* @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
|
||||
* @returns {Array} Returns the new array containing chunks.
|
||||
* @example
|
||||
*
|
||||
* _.chunk(['a', 'b', 'c', 'd'], 2);
|
||||
* // => [['a', 'b'], ['c', 'd']]
|
||||
*
|
||||
* _.chunk(['a', 'b', 'c', 'd'], 3);
|
||||
* // => [['a', 'b', 'c'], ['d']]
|
||||
*/
|
||||
function chunk(array, size, guard) {
|
||||
if (guard ? isIterateeCall(array, size, guard) : size == null) {
|
||||
size = 1;
|
||||
} else {
|
||||
size = nativeMax(+size || 1, 1);
|
||||
}
|
||||
var index = 0,
|
||||
length = array ? array.length : 0,
|
||||
resIndex = -1,
|
||||
result = Array(ceil(length / size));
|
||||
|
||||
while (index < length) {
|
||||
result[++resIndex] = baseSlice(array, index, (index += size));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export default chunk;
|
||||
30
array/compact.js
Normal file
30
array/compact.js
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Creates an array with all falsey values removed. The values `false`, `null`,
|
||||
* `0`, `""`, `undefined`, and `NaN` are falsey.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The array to compact.
|
||||
* @returns {Array} Returns the new array of filtered values.
|
||||
* @example
|
||||
*
|
||||
* _.compact([0, 1, false, 2, '', 3]);
|
||||
* // => [1, 2, 3]
|
||||
*/
|
||||
function compact(array) {
|
||||
var index = -1,
|
||||
length = array ? array.length : 0,
|
||||
resIndex = -1,
|
||||
result = [];
|
||||
|
||||
while (++index < length) {
|
||||
var value = array[index];
|
||||
if (value) {
|
||||
result[++resIndex] = value;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export default compact;
|
||||
39
array/difference.js
Normal file
39
array/difference.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import baseDifference from '../internal/baseDifference';
|
||||
import baseFlatten from '../internal/baseFlatten';
|
||||
import isArguments from '../lang/isArguments';
|
||||
import isArray from '../lang/isArray';
|
||||
|
||||
/**
|
||||
* Creates an array excluding all values of the provided arrays using
|
||||
* `SameValueZero` for equality comparisons.
|
||||
*
|
||||
* **Note:** `SameValueZero` comparisons are like strict equality comparisons,
|
||||
* e.g. `===`, except that `NaN` matches `NaN`. See the
|
||||
* [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
|
||||
* for more details.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The array to inspect.
|
||||
* @param {...Array} [values] The arrays of values to exclude.
|
||||
* @returns {Array} Returns the new array of filtered values.
|
||||
* @example
|
||||
*
|
||||
* _.difference([1, 2, 3], [5, 2, 10]);
|
||||
* // => [1, 3]
|
||||
*/
|
||||
function difference() {
|
||||
var index = -1,
|
||||
length = arguments.length;
|
||||
|
||||
while (++index < length) {
|
||||
var value = arguments[index];
|
||||
if (isArray(value) || isArguments(value)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return baseDifference(value, baseFlatten(arguments, false, true, ++index));
|
||||
}
|
||||
|
||||
export default difference;
|
||||
40
array/drop.js
Normal file
40
array/drop.js
Normal file
@@ -0,0 +1,40 @@
|
||||
import baseSlice from '../internal/baseSlice';
|
||||
import isIterateeCall from '../internal/isIterateeCall';
|
||||
|
||||
/**
|
||||
* Creates a slice of `array` with `n` elements dropped from the beginning.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @type Function
|
||||
* @category Array
|
||||
* @param {Array} array The array to query.
|
||||
* @param {number} [n=1] The number of elements to drop.
|
||||
* @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
|
||||
* @returns {Array} Returns the slice of `array`.
|
||||
* @example
|
||||
*
|
||||
* _.drop([1, 2, 3]);
|
||||
* // => [2, 3]
|
||||
*
|
||||
* _.drop([1, 2, 3], 2);
|
||||
* // => [3]
|
||||
*
|
||||
* _.drop([1, 2, 3], 5);
|
||||
* // => []
|
||||
*
|
||||
* _.drop([1, 2, 3], 0);
|
||||
* // => [1, 2, 3]
|
||||
*/
|
||||
function drop(array, n, guard) {
|
||||
var length = array ? array.length : 0;
|
||||
if (!length) {
|
||||
return [];
|
||||
}
|
||||
if (guard ? isIterateeCall(array, n, guard) : n == null) {
|
||||
n = 1;
|
||||
}
|
||||
return baseSlice(array, n < 0 ? 0 : n);
|
||||
}
|
||||
|
||||
export default drop;
|
||||
41
array/dropRight.js
Normal file
41
array/dropRight.js
Normal file
@@ -0,0 +1,41 @@
|
||||
import baseSlice from '../internal/baseSlice';
|
||||
import isIterateeCall from '../internal/isIterateeCall';
|
||||
|
||||
/**
|
||||
* Creates a slice of `array` with `n` elements dropped from the end.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @type Function
|
||||
* @category Array
|
||||
* @param {Array} array The array to query.
|
||||
* @param {number} [n=1] The number of elements to drop.
|
||||
* @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
|
||||
* @returns {Array} Returns the slice of `array`.
|
||||
* @example
|
||||
*
|
||||
* _.dropRight([1, 2, 3]);
|
||||
* // => [1, 2]
|
||||
*
|
||||
* _.dropRight([1, 2, 3], 2);
|
||||
* // => [1]
|
||||
*
|
||||
* _.dropRight([1, 2, 3], 5);
|
||||
* // => []
|
||||
*
|
||||
* _.dropRight([1, 2, 3], 0);
|
||||
* // => [1, 2, 3]
|
||||
*/
|
||||
function dropRight(array, n, guard) {
|
||||
var length = array ? array.length : 0;
|
||||
if (!length) {
|
||||
return [];
|
||||
}
|
||||
if (guard ? isIterateeCall(array, n, guard) : n == null) {
|
||||
n = 1;
|
||||
}
|
||||
n = length - (+n || 0);
|
||||
return baseSlice(array, 0, n < 0 ? 0 : n);
|
||||
}
|
||||
|
||||
export default dropRight;
|
||||
54
array/dropRightWhile.js
Normal file
54
array/dropRightWhile.js
Normal file
@@ -0,0 +1,54 @@
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
import baseSlice from '../internal/baseSlice';
|
||||
|
||||
/**
|
||||
* Creates a slice of `array` excluding elements dropped from the end.
|
||||
* Elements are dropped until `predicate` returns falsey. The predicate is
|
||||
* bound to `thisArg` and invoked with three arguments; (value, index, array).
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @type Function
|
||||
* @category Array
|
||||
* @param {Array} array The array to query.
|
||||
* @param {Function|Object|string} [predicate=_.identity] The function invoked
|
||||
* per element.
|
||||
* @param {*} [thisArg] The `this` binding of `predicate`.
|
||||
* @returns {Array} Returns the slice of `array`.
|
||||
* @example
|
||||
*
|
||||
* _.dropRightWhile([1, 2, 3], function(n) { return n > 1; });
|
||||
* // => [1]
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'status': 'busy', 'active': false },
|
||||
* { 'user': 'fred', 'status': 'busy', 'active': true },
|
||||
* { 'user': 'pebbles', 'status': 'away', 'active': true }
|
||||
* ];
|
||||
*
|
||||
* // using the "_.property" callback shorthand
|
||||
* _.pluck(_.dropRightWhile(users, 'active'), 'user');
|
||||
* // => ['barney']
|
||||
*
|
||||
* // using the "_.matches" callback shorthand
|
||||
* _.pluck(_.dropRightWhile(users, { 'status': 'away' }), 'user');
|
||||
* // => ['barney', 'fred']
|
||||
*/
|
||||
function dropRightWhile(array, predicate, thisArg) {
|
||||
var length = array ? array.length : 0;
|
||||
if (!length) {
|
||||
return [];
|
||||
}
|
||||
predicate = baseCallback(predicate, thisArg, 3);
|
||||
while (length-- && predicate(array[length], length, array)) {}
|
||||
return baseSlice(array, 0, length + 1);
|
||||
}
|
||||
|
||||
export default dropRightWhile;
|
||||
55
array/dropWhile.js
Normal file
55
array/dropWhile.js
Normal file
@@ -0,0 +1,55 @@
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
import baseSlice from '../internal/baseSlice';
|
||||
|
||||
/**
|
||||
* Creates a slice of `array` excluding elements dropped from the beginning.
|
||||
* Elements are dropped until `predicate` returns falsey. The predicate is
|
||||
* bound to `thisArg` and invoked with three arguments; (value, index, array).
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @type Function
|
||||
* @category Array
|
||||
* @param {Array} array The array to query.
|
||||
* @param {Function|Object|string} [predicate=_.identity] The function invoked
|
||||
* per element.
|
||||
* @param {*} [thisArg] The `this` binding of `predicate`.
|
||||
* @returns {Array} Returns the slice of `array`.
|
||||
* @example
|
||||
*
|
||||
* _.dropWhile([1, 2, 3], function(n) { return n < 3; });
|
||||
* // => [3]
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'status': 'busy', 'active': true },
|
||||
* { 'user': 'fred', 'status': 'busy', 'active': false },
|
||||
* { 'user': 'pebbles', 'status': 'away', 'active': true }
|
||||
* ];
|
||||
*
|
||||
* // using the "_.property" callback shorthand
|
||||
* _.pluck(_.dropWhile(users, 'active'), 'user');
|
||||
* // => ['fred', 'pebbles']
|
||||
*
|
||||
* // using the "_.matches" callback shorthand
|
||||
* _.pluck(_.dropWhile(users, { 'status': 'busy' }), 'user');
|
||||
* // => ['pebbles']
|
||||
*/
|
||||
function dropWhile(array, predicate, thisArg) {
|
||||
var length = array ? array.length : 0;
|
||||
if (!length) {
|
||||
return [];
|
||||
}
|
||||
var index = -1;
|
||||
predicate = baseCallback(predicate, thisArg, 3);
|
||||
while (++index < length && predicate(array[index], index, array)) {}
|
||||
return baseSlice(array, index);
|
||||
}
|
||||
|
||||
export default dropWhile;
|
||||
55
array/findIndex.js
Normal file
55
array/findIndex.js
Normal file
@@ -0,0 +1,55 @@
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
|
||||
/**
|
||||
* This method is like `_.find` except that it returns the index of the first
|
||||
* element `predicate` returns truthy for, instead of the element itself.
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The array to search.
|
||||
* @param {Function|Object|string} [predicate=_.identity] The function invoked
|
||||
* per iteration. If a property name or object is provided it is used to
|
||||
* create a "_.property" or "_.matches" style callback respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `predicate`.
|
||||
* @returns {number} Returns the index of the found element, else `-1`.
|
||||
* @example
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'age': 36, 'active': false },
|
||||
* { 'user': 'fred', 'age': 40, 'active': true },
|
||||
* { 'user': 'pebbles', 'age': 1, 'active': false }
|
||||
* ];
|
||||
*
|
||||
* _.findIndex(users, function(chr) { return chr.age < 40; });
|
||||
* // => 0
|
||||
*
|
||||
* // using the "_.matches" callback shorthand
|
||||
* _.findIndex(users, { 'age': 1 });
|
||||
* // => 2
|
||||
*
|
||||
* // using the "_.property" callback shorthand
|
||||
* _.findIndex(users, 'active');
|
||||
* // => 1
|
||||
*/
|
||||
function findIndex(array, predicate, thisArg) {
|
||||
var index = -1,
|
||||
length = array ? array.length : 0;
|
||||
|
||||
predicate = baseCallback(predicate, thisArg, 3);
|
||||
while (++index < length) {
|
||||
if (predicate(array[index], index, array)) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
export default findIndex;
|
||||
53
array/findLastIndex.js
Normal file
53
array/findLastIndex.js
Normal file
@@ -0,0 +1,53 @@
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
|
||||
/**
|
||||
* This method is like `_.findIndex` except that it iterates over elements
|
||||
* of `collection` from right to left.
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The array to search.
|
||||
* @param {Function|Object|string} [predicate=_.identity] The function invoked
|
||||
* per iteration. If a property name or object is provided it is used to
|
||||
* create a "_.property" or "_.matches" style callback respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `predicate`.
|
||||
* @returns {number} Returns the index of the found element, else `-1`.
|
||||
* @example
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'age': 36, 'active': true },
|
||||
* { 'user': 'fred', 'age': 40, 'active': false },
|
||||
* { 'user': 'pebbles', 'age': 1, 'active': false }
|
||||
* ];
|
||||
*
|
||||
* _.findLastIndex(users, function(chr) { return chr.age < 40; });
|
||||
* // => 2
|
||||
*
|
||||
* // using the "_.matches" callback shorthand
|
||||
* _.findLastIndex(users, { 'age': 40 });
|
||||
* // => 1
|
||||
*
|
||||
* // using the "_.property" callback shorthand
|
||||
* _.findLastIndex(users, 'active');
|
||||
* // => 0
|
||||
*/
|
||||
function findLastIndex(array, predicate, thisArg) {
|
||||
var length = array ? array.length : 0;
|
||||
predicate = baseCallback(predicate, thisArg, 3);
|
||||
while (length--) {
|
||||
if (predicate(array[length], length, array)) {
|
||||
return length;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
export default findLastIndex;
|
||||
22
array/first.js
Normal file
22
array/first.js
Normal file
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Gets the first element of `array`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @alias head
|
||||
* @category Array
|
||||
* @param {Array} array The array to query.
|
||||
* @returns {*} Returns the first element of `array`.
|
||||
* @example
|
||||
*
|
||||
* _.first([1, 2, 3]);
|
||||
* // => 1
|
||||
*
|
||||
* _.first([]);
|
||||
* // => undefined
|
||||
*/
|
||||
function first(array) {
|
||||
return array ? array[0] : undefined;
|
||||
}
|
||||
|
||||
export default first;
|
||||
32
array/flatten.js
Normal file
32
array/flatten.js
Normal file
@@ -0,0 +1,32 @@
|
||||
import baseFlatten from '../internal/baseFlatten';
|
||||
import isIterateeCall from '../internal/isIterateeCall';
|
||||
|
||||
/**
|
||||
* Flattens a nested array. If `isDeep` is `true` the array is recursively
|
||||
* flattened, otherwise it is only flattened a single level.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The array to flatten.
|
||||
* @param {boolean} [isDeep] Specify a deep flatten.
|
||||
* @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
|
||||
* @returns {Array} Returns the new flattened array.
|
||||
* @example
|
||||
*
|
||||
* _.flatten([1, [2], [3, [[4]]]]);
|
||||
* // => [1, 2, 3, [[4]]];
|
||||
*
|
||||
* // using `isDeep`
|
||||
* _.flatten([1, [2], [3, [[4]]]], true);
|
||||
* // => [1, 2, 3, 4];
|
||||
*/
|
||||
function flatten(array, isDeep, guard) {
|
||||
var length = array ? array.length : 0;
|
||||
if (guard && isIterateeCall(array, isDeep, guard)) {
|
||||
isDeep = false;
|
||||
}
|
||||
return length ? baseFlatten(array, isDeep) : [];
|
||||
}
|
||||
|
||||
export default flatten;
|
||||
21
array/flattenDeep.js
Normal file
21
array/flattenDeep.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import baseFlatten from '../internal/baseFlatten';
|
||||
|
||||
/**
|
||||
* Recursively flattens a nested array.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The array to recursively flatten.
|
||||
* @returns {Array} Returns the new flattened array.
|
||||
* @example
|
||||
*
|
||||
* _.flattenDeep([1, [2], [3, [[4]]]]);
|
||||
* // => [1, 2, 3, 4];
|
||||
*/
|
||||
function flattenDeep(array) {
|
||||
var length = array ? array.length : 0;
|
||||
return length ? baseFlatten(array, true) : [];
|
||||
}
|
||||
|
||||
export default flattenDeep;
|
||||
2
array/head.js
Normal file
2
array/head.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import first from './first'
|
||||
export default first;
|
||||
55
array/indexOf.js
Normal file
55
array/indexOf.js
Normal file
@@ -0,0 +1,55 @@
|
||||
import baseIndexOf from '../internal/baseIndexOf';
|
||||
import binaryIndex from '../internal/binaryIndex';
|
||||
|
||||
/* Native method references for those with the same name as other `lodash` methods. */
|
||||
var nativeMax = Math.max;
|
||||
|
||||
/**
|
||||
* Gets the index at which the first occurrence of `value` is found in `array`
|
||||
* using `SameValueZero` for equality comparisons. If `fromIndex` is negative,
|
||||
* it is used as the offset from the end of `array`. If `array` is sorted
|
||||
* providing `true` for `fromIndex` performs a faster binary search.
|
||||
*
|
||||
* **Note:** `SameValueZero` comparisons are like strict equality comparisons,
|
||||
* e.g. `===`, except that `NaN` matches `NaN`. See the
|
||||
* [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
|
||||
* for more details.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The array to search.
|
||||
* @param {*} value The value to search for.
|
||||
* @param {boolean|number} [fromIndex=0] The index to search from or `true`
|
||||
* to perform a binary search on a sorted array.
|
||||
* @returns {number} Returns the index of the matched value, else `-1`.
|
||||
* @example
|
||||
*
|
||||
* _.indexOf([1, 2, 3, 1, 2, 3], 2);
|
||||
* // => 1
|
||||
*
|
||||
* // using `fromIndex`
|
||||
* _.indexOf([1, 2, 3, 1, 2, 3], 2, 3);
|
||||
* // => 4
|
||||
*
|
||||
* // performing a binary search
|
||||
* _.indexOf([4, 4, 5, 5, 6, 6], 5, true);
|
||||
* // => 2
|
||||
*/
|
||||
function indexOf(array, value, fromIndex) {
|
||||
var length = array ? array.length : 0;
|
||||
if (!length) {
|
||||
return -1;
|
||||
}
|
||||
if (typeof fromIndex == 'number') {
|
||||
fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
|
||||
} else if (fromIndex) {
|
||||
var index = binaryIndex(array, value),
|
||||
other = array[index];
|
||||
|
||||
return (value === value ? value === other : other !== other) ? index : -1;
|
||||
}
|
||||
return baseIndexOf(array, value, fromIndex);
|
||||
}
|
||||
|
||||
export default indexOf;
|
||||
20
array/initial.js
Normal file
20
array/initial.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import dropRight from './dropRight';
|
||||
|
||||
/**
|
||||
* Gets all but the last element of `array`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The array to query.
|
||||
* @returns {Array} Returns the slice of `array`.
|
||||
* @example
|
||||
*
|
||||
* _.initial([1, 2, 3]);
|
||||
* // => [1, 2]
|
||||
*/
|
||||
function initial(array) {
|
||||
return dropRight(array, 1);
|
||||
}
|
||||
|
||||
export default initial;
|
||||
68
array/intersection.js
Normal file
68
array/intersection.js
Normal file
@@ -0,0 +1,68 @@
|
||||
import baseIndexOf from '../internal/baseIndexOf';
|
||||
import cacheIndexOf from '../internal/cacheIndexOf';
|
||||
import createCache from '../internal/createCache';
|
||||
import isArguments from '../lang/isArguments';
|
||||
import isArray from '../lang/isArray';
|
||||
|
||||
/**
|
||||
* Creates an array of unique values in all provided arrays using `SameValueZero`
|
||||
* for equality comparisons.
|
||||
*
|
||||
* **Note:** `SameValueZero` comparisons are like strict equality comparisons,
|
||||
* e.g. `===`, except that `NaN` matches `NaN`. See the
|
||||
* [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
|
||||
* for more details.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {...Array} [arrays] The arrays to inspect.
|
||||
* @returns {Array} Returns the new array of shared values.
|
||||
* @example
|
||||
*
|
||||
* _.intersection([1, 2, 3], [5, 2, 1, 4], [2, 1]);
|
||||
* // => [1, 2]
|
||||
*/
|
||||
function intersection() {
|
||||
var args = [],
|
||||
argsIndex = -1,
|
||||
argsLength = arguments.length,
|
||||
caches = [],
|
||||
indexOf = baseIndexOf,
|
||||
isCommon = true;
|
||||
|
||||
while (++argsIndex < argsLength) {
|
||||
var value = arguments[argsIndex];
|
||||
if (isArray(value) || isArguments(value)) {
|
||||
args.push(value);
|
||||
caches.push(isCommon && value.length >= 120 && createCache(argsIndex && value));
|
||||
}
|
||||
}
|
||||
argsLength = args.length;
|
||||
var array = args[0],
|
||||
index = -1,
|
||||
length = array ? array.length : 0,
|
||||
result = [],
|
||||
seen = caches[0];
|
||||
|
||||
outer:
|
||||
while (++index < length) {
|
||||
value = array[index];
|
||||
if ((seen ? cacheIndexOf(seen, value) : indexOf(result, value)) < 0) {
|
||||
argsIndex = argsLength;
|
||||
while (--argsIndex) {
|
||||
var cache = caches[argsIndex];
|
||||
if ((cache ? cacheIndexOf(cache, value) : indexOf(args[argsIndex], value)) < 0) {
|
||||
continue outer;
|
||||
}
|
||||
}
|
||||
if (seen) {
|
||||
seen.push(value);
|
||||
}
|
||||
result.push(value);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export default intersection;
|
||||
19
array/last.js
Normal file
19
array/last.js
Normal file
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Gets the last element of `array`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The array to query.
|
||||
* @returns {*} Returns the last element of `array`.
|
||||
* @example
|
||||
*
|
||||
* _.last([1, 2, 3]);
|
||||
* // => 3
|
||||
*/
|
||||
function last(array) {
|
||||
var length = array ? array.length : 0;
|
||||
return length ? array[length - 1] : undefined;
|
||||
}
|
||||
|
||||
export default last;
|
||||
57
array/lastIndexOf.js
Normal file
57
array/lastIndexOf.js
Normal file
@@ -0,0 +1,57 @@
|
||||
import binaryIndex from '../internal/binaryIndex';
|
||||
import indexOfNaN from '../internal/indexOfNaN';
|
||||
|
||||
/* Native method references for those with the same name as other `lodash` methods. */
|
||||
var nativeMax = Math.max,
|
||||
nativeMin = Math.min;
|
||||
|
||||
/**
|
||||
* This method is like `_.indexOf` except that it iterates over elements of
|
||||
* `array` from right to left.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The array to search.
|
||||
* @param {*} value The value to search for.
|
||||
* @param {boolean|number} [fromIndex=array.length-1] The index to search from
|
||||
* or `true` to perform a binary search on a sorted array.
|
||||
* @returns {number} Returns the index of the matched value, else `-1`.
|
||||
* @example
|
||||
*
|
||||
* _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
|
||||
* // => 4
|
||||
*
|
||||
* // using `fromIndex`
|
||||
* _.lastIndexOf([1, 2, 3, 1, 2, 3], 2, 3);
|
||||
* // => 1
|
||||
*
|
||||
* // performing a binary search
|
||||
* _.lastIndexOf([4, 4, 5, 5, 6, 6], 5, true);
|
||||
* // => 3
|
||||
*/
|
||||
function lastIndexOf(array, value, fromIndex) {
|
||||
var length = array ? array.length : 0;
|
||||
if (!length) {
|
||||
return -1;
|
||||
}
|
||||
var index = length;
|
||||
if (typeof fromIndex == 'number') {
|
||||
index = (fromIndex < 0 ? nativeMax(length + fromIndex, 0) : nativeMin(fromIndex || 0, length - 1)) + 1;
|
||||
} else if (fromIndex) {
|
||||
index = binaryIndex(array, value, true) - 1;
|
||||
var other = array[index];
|
||||
return (value === value ? value === other : other !== other) ? index : -1;
|
||||
}
|
||||
if (value !== value) {
|
||||
return indexOfNaN(array, index, true);
|
||||
}
|
||||
while (index--) {
|
||||
if (array[index] === value) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
export default lastIndexOf;
|
||||
2
array/object.js
Normal file
2
array/object.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import zipObject from './zipObject'
|
||||
export default zipObject;
|
||||
52
array/pull.js
Normal file
52
array/pull.js
Normal file
@@ -0,0 +1,52 @@
|
||||
import baseIndexOf from '../internal/baseIndexOf';
|
||||
|
||||
/** Used for native method references. */
|
||||
var arrayProto = Array.prototype;
|
||||
|
||||
/** Native method references. */
|
||||
var splice = arrayProto.splice;
|
||||
|
||||
/**
|
||||
* Removes all provided values from `array` using `SameValueZero` for equality
|
||||
* comparisons.
|
||||
*
|
||||
* **Notes:**
|
||||
* - Unlike `_.without`, this method mutates `array`.
|
||||
* - `SameValueZero` comparisons are like strict equality comparisons, e.g. `===`,
|
||||
* except that `NaN` matches `NaN`. See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
|
||||
* for more details.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The array to modify.
|
||||
* @param {...*} [values] The values to remove.
|
||||
* @returns {Array} Returns `array`.
|
||||
* @example
|
||||
*
|
||||
* var array = [1, 2, 3, 1, 2, 3];
|
||||
* _.pull(array, 2, 3);
|
||||
* console.log(array);
|
||||
* // => [1, 1]
|
||||
*/
|
||||
function pull() {
|
||||
var array = arguments[0];
|
||||
if (!(array && array.length)) {
|
||||
return array;
|
||||
}
|
||||
var index = 0,
|
||||
indexOf = baseIndexOf,
|
||||
length = arguments.length;
|
||||
|
||||
while (++index < length) {
|
||||
var fromIndex = 0,
|
||||
value = arguments[index];
|
||||
|
||||
while ((fromIndex = indexOf(array, value, fromIndex)) > -1) {
|
||||
splice.call(array, fromIndex, 1);
|
||||
}
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
export default pull;
|
||||
33
array/pullAt.js
Normal file
33
array/pullAt.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import baseFlatten from '../internal/baseFlatten';
|
||||
import basePullAt from '../internal/basePullAt';
|
||||
|
||||
/**
|
||||
* Removes elements from `array` corresponding to the given indexes and returns
|
||||
* an array of the removed elements. Indexes may be specified as an array of
|
||||
* indexes or as individual arguments.
|
||||
*
|
||||
* **Note:** Unlike `_.at`, this method mutates `array`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The array to modify.
|
||||
* @param {...(number|number[])} [indexes] The indexes of elements to remove,
|
||||
* specified as individual indexes or arrays of indexes.
|
||||
* @returns {Array} Returns the new array of removed elements.
|
||||
* @example
|
||||
*
|
||||
* var array = [5, 10, 15, 20];
|
||||
* var evens = _.pullAt(array, [1, 3]);
|
||||
*
|
||||
* console.log(array);
|
||||
* // => [5, 15]
|
||||
*
|
||||
* console.log(evens);
|
||||
* // => [10, 20]
|
||||
*/
|
||||
function pullAt(array) {
|
||||
return basePullAt(array || [], baseFlatten(arguments, false, false, 1));
|
||||
}
|
||||
|
||||
export default pullAt;
|
||||
60
array/remove.js
Normal file
60
array/remove.js
Normal file
@@ -0,0 +1,60 @@
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
|
||||
/** Used for native method references. */
|
||||
var arrayProto = Array.prototype;
|
||||
|
||||
/** Native method references. */
|
||||
var splice = arrayProto.splice;
|
||||
|
||||
/**
|
||||
* Removes all elements from `array` that `predicate` returns truthy for
|
||||
* and returns an array of the removed elements. The predicate is bound to
|
||||
* `thisArg` and invoked with three arguments; (value, index, array).
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* **Note:** Unlike `_.filter`, this method mutates `array`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The array to modify.
|
||||
* @param {Function|Object|string} [predicate=_.identity] The function invoked
|
||||
* per iteration. If a property name or object is provided it is used to
|
||||
* create a "_.property" or "_.matches" style callback respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `predicate`.
|
||||
* @returns {Array} Returns the new array of removed elements.
|
||||
* @example
|
||||
*
|
||||
* var array = [1, 2, 3, 4];
|
||||
* var evens = _.remove(array, function(n) { return n % 2 == 0; });
|
||||
*
|
||||
* console.log(array);
|
||||
* // => [1, 3]
|
||||
*
|
||||
* console.log(evens);
|
||||
* // => [2, 4]
|
||||
*/
|
||||
function remove(array, predicate, thisArg) {
|
||||
var index = -1,
|
||||
length = array ? array.length : 0,
|
||||
result = [];
|
||||
|
||||
predicate = baseCallback(predicate, thisArg, 3);
|
||||
while (++index < length) {
|
||||
var value = array[index];
|
||||
if (predicate(value, index, array)) {
|
||||
result.push(value);
|
||||
splice.call(array, index--, 1);
|
||||
length--;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export default remove;
|
||||
21
array/rest.js
Normal file
21
array/rest.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import drop from './drop';
|
||||
|
||||
/**
|
||||
* Gets all but the first element of `array`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @alias tail
|
||||
* @category Array
|
||||
* @param {Array} array The array to query.
|
||||
* @returns {Array} Returns the slice of `array`.
|
||||
* @example
|
||||
*
|
||||
* _.rest([1, 2, 3]);
|
||||
* // => [2, 3]
|
||||
*/
|
||||
function rest(array) {
|
||||
return drop(array, 1);
|
||||
}
|
||||
|
||||
export default rest;
|
||||
30
array/slice.js
Normal file
30
array/slice.js
Normal file
@@ -0,0 +1,30 @@
|
||||
import baseSlice from '../internal/baseSlice';
|
||||
import isIterateeCall from '../internal/isIterateeCall';
|
||||
|
||||
/**
|
||||
* Creates a slice of `array` from `start` up to, but not including, `end`.
|
||||
*
|
||||
* **Note:** This function is used instead of `Array#slice` to support node
|
||||
* lists in IE < 9 and to ensure dense arrays are returned.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The array to slice.
|
||||
* @param {number} [start=0] The start position.
|
||||
* @param {number} [end=array.length] The end position.
|
||||
* @returns {Array} Returns the slice of `array`.
|
||||
*/
|
||||
function slice(array, start, end) {
|
||||
var length = array ? array.length : 0;
|
||||
if (!length) {
|
||||
return [];
|
||||
}
|
||||
if (end && typeof end != 'number' && isIterateeCall(array, start, end)) {
|
||||
start = 0;
|
||||
end = length;
|
||||
}
|
||||
return baseSlice(array, start, end);
|
||||
}
|
||||
|
||||
export default slice;
|
||||
56
array/sortedIndex.js
Normal file
56
array/sortedIndex.js
Normal file
@@ -0,0 +1,56 @@
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
import binaryIndex from '../internal/binaryIndex';
|
||||
import binaryIndexBy from '../internal/binaryIndexBy';
|
||||
|
||||
/**
|
||||
* Uses a binary search to determine the lowest index at which `value` should
|
||||
* be inserted into `array` in order to maintain its sort order. If an iteratee
|
||||
* function is provided it is invoked for `value` and each element of `array`
|
||||
* to compute their sort ranking. The iteratee is bound to `thisArg` and
|
||||
* invoked with one argument; (value).
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The sorted array to inspect.
|
||||
* @param {*} value The value to evaluate.
|
||||
* @param {Function|Object|string} [iteratee=_.identity] The function invoked
|
||||
* per iteration. If a property name or object is provided it is used to
|
||||
* create a "_.property" or "_.matches" style callback respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `iteratee`.
|
||||
* @returns {number} Returns the index at which `value` should be inserted
|
||||
* into `array`.
|
||||
* @example
|
||||
*
|
||||
* _.sortedIndex([30, 50], 40);
|
||||
* // => 1
|
||||
*
|
||||
* _.sortedIndex([4, 4, 5, 5, 6, 6], 5);
|
||||
* // => 2
|
||||
*
|
||||
* var dict = { 'data': { 'thirty': 30, 'forty': 40, 'fifty': 50 } };
|
||||
*
|
||||
* // using an iteratee function
|
||||
* _.sortedIndex(['thirty', 'fifty'], 'forty', function(word) {
|
||||
* return this.data[word];
|
||||
* }, dict);
|
||||
* // => 1
|
||||
*
|
||||
* // using the "_.property" callback shorthand
|
||||
* _.sortedIndex([{ 'x': 30 }, { 'x': 50 }], { 'x': 40 }, 'x');
|
||||
* // => 1
|
||||
*/
|
||||
function sortedIndex(array, value, iteratee, thisArg) {
|
||||
return iteratee == null
|
||||
? binaryIndex(array, value)
|
||||
: binaryIndexBy(array, value, baseCallback(iteratee, thisArg, 1));
|
||||
}
|
||||
|
||||
export default sortedIndex;
|
||||
32
array/sortedLastIndex.js
Normal file
32
array/sortedLastIndex.js
Normal file
@@ -0,0 +1,32 @@
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
import binaryIndex from '../internal/binaryIndex';
|
||||
import binaryIndexBy from '../internal/binaryIndexBy';
|
||||
|
||||
/**
|
||||
* This method is like `_.sortedIndex` except that it returns the highest
|
||||
* index at which `value` should be inserted into `array` in order to
|
||||
* maintain its sort order.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The sorted array to inspect.
|
||||
* @param {*} value The value to evaluate.
|
||||
* @param {Function|Object|string} [iteratee=_.identity] The function invoked
|
||||
* per iteration. If a property name or object is provided it is used to
|
||||
* create a "_.property" or "_.matches" style callback respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `iteratee`.
|
||||
* @returns {number} Returns the index at which `value` should be inserted
|
||||
* into `array`.
|
||||
* @example
|
||||
*
|
||||
* _.sortedLastIndex([4, 4, 5, 5, 6, 6], 5);
|
||||
* // => 4
|
||||
*/
|
||||
function sortedLastIndex(array, value, iteratee, thisArg) {
|
||||
return iteratee == null
|
||||
? binaryIndex(array, value, true)
|
||||
: binaryIndexBy(array, value, baseCallback(iteratee, thisArg, 1), true);
|
||||
}
|
||||
|
||||
export default sortedLastIndex;
|
||||
2
array/tail.js
Normal file
2
array/tail.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import rest from './rest'
|
||||
export default rest;
|
||||
40
array/take.js
Normal file
40
array/take.js
Normal file
@@ -0,0 +1,40 @@
|
||||
import baseSlice from '../internal/baseSlice';
|
||||
import isIterateeCall from '../internal/isIterateeCall';
|
||||
|
||||
/**
|
||||
* Creates a slice of `array` with `n` elements taken from the beginning.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @type Function
|
||||
* @category Array
|
||||
* @param {Array} array The array to query.
|
||||
* @param {number} [n=1] The number of elements to take.
|
||||
* @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
|
||||
* @returns {Array} Returns the slice of `array`.
|
||||
* @example
|
||||
*
|
||||
* _.take([1, 2, 3]);
|
||||
* // => [1]
|
||||
*
|
||||
* _.take([1, 2, 3], 2);
|
||||
* // => [1, 2]
|
||||
*
|
||||
* _.take([1, 2, 3], 5);
|
||||
* // => [1, 2, 3]
|
||||
*
|
||||
* _.take([1, 2, 3], 0);
|
||||
* // => []
|
||||
*/
|
||||
function take(array, n, guard) {
|
||||
var length = array ? array.length : 0;
|
||||
if (!length) {
|
||||
return [];
|
||||
}
|
||||
if (guard ? isIterateeCall(array, n, guard) : n == null) {
|
||||
n = 1;
|
||||
}
|
||||
return baseSlice(array, 0, n < 0 ? 0 : n);
|
||||
}
|
||||
|
||||
export default take;
|
||||
41
array/takeRight.js
Normal file
41
array/takeRight.js
Normal file
@@ -0,0 +1,41 @@
|
||||
import baseSlice from '../internal/baseSlice';
|
||||
import isIterateeCall from '../internal/isIterateeCall';
|
||||
|
||||
/**
|
||||
* Creates a slice of `array` with `n` elements taken from the end.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @type Function
|
||||
* @category Array
|
||||
* @param {Array} array The array to query.
|
||||
* @param {number} [n=1] The number of elements to take.
|
||||
* @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
|
||||
* @returns {Array} Returns the slice of `array`.
|
||||
* @example
|
||||
*
|
||||
* _.takeRight([1, 2, 3]);
|
||||
* // => [3]
|
||||
*
|
||||
* _.takeRight([1, 2, 3], 2);
|
||||
* // => [2, 3]
|
||||
*
|
||||
* _.takeRight([1, 2, 3], 5);
|
||||
* // => [1, 2, 3]
|
||||
*
|
||||
* _.takeRight([1, 2, 3], 0);
|
||||
* // => []
|
||||
*/
|
||||
function takeRight(array, n, guard) {
|
||||
var length = array ? array.length : 0;
|
||||
if (!length) {
|
||||
return [];
|
||||
}
|
||||
if (guard ? isIterateeCall(array, n, guard) : n == null) {
|
||||
n = 1;
|
||||
}
|
||||
n = length - (+n || 0);
|
||||
return baseSlice(array, n < 0 ? 0 : n);
|
||||
}
|
||||
|
||||
export default takeRight;
|
||||
54
array/takeRightWhile.js
Normal file
54
array/takeRightWhile.js
Normal file
@@ -0,0 +1,54 @@
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
import baseSlice from '../internal/baseSlice';
|
||||
|
||||
/**
|
||||
* Creates a slice of `array` with elements taken from the end. Elements are
|
||||
* taken until `predicate` returns falsey. The predicate is bound to `thisArg`
|
||||
* and invoked with three arguments; (value, index, array).
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @type Function
|
||||
* @category Array
|
||||
* @param {Array} array The array to query.
|
||||
* @param {Function|Object|string} [predicate=_.identity] The function invoked
|
||||
* per element.
|
||||
* @param {*} [thisArg] The `this` binding of `predicate`.
|
||||
* @returns {Array} Returns the slice of `array`.
|
||||
* @example
|
||||
*
|
||||
* _.takeRightWhile([1, 2, 3], function(n) { return n > 1; });
|
||||
* // => [2, 3]
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'status': 'busy', 'active': false },
|
||||
* { 'user': 'fred', 'status': 'busy', 'active': true },
|
||||
* { 'user': 'pebbles', 'status': 'away', 'active': true }
|
||||
* ];
|
||||
*
|
||||
* // using the "_.property" callback shorthand
|
||||
* _.pluck(_.takeRightWhile(users, 'active'), 'user');
|
||||
* // => ['fred', 'pebbles']
|
||||
*
|
||||
* // using the "_.matches" callback shorthand
|
||||
* _.pluck(_.takeRightWhile(users, { 'status': 'away' }), 'user');
|
||||
* // => ['pebbles']
|
||||
*/
|
||||
function takeRightWhile(array, predicate, thisArg) {
|
||||
var length = array ? array.length : 0;
|
||||
if (!length) {
|
||||
return [];
|
||||
}
|
||||
predicate = baseCallback(predicate, thisArg, 3);
|
||||
while (length-- && predicate(array[length], length, array)) {}
|
||||
return baseSlice(array, length + 1);
|
||||
}
|
||||
|
||||
export default takeRightWhile;
|
||||
55
array/takeWhile.js
Normal file
55
array/takeWhile.js
Normal file
@@ -0,0 +1,55 @@
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
import baseSlice from '../internal/baseSlice';
|
||||
|
||||
/**
|
||||
* Creates a slice of `array` with elements taken from the beginning. Elements
|
||||
* are taken until `predicate` returns falsey. The predicate is bound to
|
||||
* `thisArg` and invoked with three arguments; (value, index, array).
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @type Function
|
||||
* @category Array
|
||||
* @param {Array} array The array to query.
|
||||
* @param {Function|Object|string} [predicate=_.identity] The function invoked
|
||||
* per element.
|
||||
* @param {*} [thisArg] The `this` binding of `predicate`.
|
||||
* @returns {Array} Returns the slice of `array`.
|
||||
* @example
|
||||
*
|
||||
* _.takeWhile([1, 2, 3], function(n) { return n < 3; });
|
||||
* // => [1, 2]
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'status': 'busy', 'active': true },
|
||||
* { 'user': 'fred', 'status': 'busy', 'active': false },
|
||||
* { 'user': 'pebbles', 'status': 'away', 'active': true }
|
||||
* ];
|
||||
*
|
||||
* // using the "_.property" callback shorthand
|
||||
* _.pluck(_.takeWhile(users, 'active'), 'user');
|
||||
* // => ['barney']
|
||||
*
|
||||
* // using the "_.matches" callback shorthand
|
||||
* _.pluck(_.takeWhile(users, { 'status': 'busy' }), 'user');
|
||||
* // => ['barney', 'fred']
|
||||
*/
|
||||
function takeWhile(array, predicate, thisArg) {
|
||||
var length = array ? array.length : 0;
|
||||
if (!length) {
|
||||
return [];
|
||||
}
|
||||
var index = -1;
|
||||
predicate = baseCallback(predicate, thisArg, 3);
|
||||
while (++index < length && predicate(array[index], index, array)) {}
|
||||
return baseSlice(array, 0, index);
|
||||
}
|
||||
|
||||
export default takeWhile;
|
||||
27
array/union.js
Normal file
27
array/union.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import baseFlatten from '../internal/baseFlatten';
|
||||
import baseUniq from '../internal/baseUniq';
|
||||
|
||||
/**
|
||||
* Creates an array of unique values, in order, of the provided arrays using
|
||||
* `SameValueZero` for equality comparisons.
|
||||
*
|
||||
* **Note:** `SameValueZero` comparisons are like strict equality comparisons,
|
||||
* e.g. `===`, except that `NaN` matches `NaN`. See the
|
||||
* [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
|
||||
* for more details.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {...Array} [arrays] The arrays to inspect.
|
||||
* @returns {Array} Returns the new array of combined values.
|
||||
* @example
|
||||
*
|
||||
* _.union([1, 2, 3], [5, 2, 1, 4], [2, 1]);
|
||||
* // => [1, 2, 3, 5, 4]
|
||||
*/
|
||||
function union() {
|
||||
return baseUniq(baseFlatten(arguments, false, true));
|
||||
}
|
||||
|
||||
export default union;
|
||||
71
array/uniq.js
Normal file
71
array/uniq.js
Normal file
@@ -0,0 +1,71 @@
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
import baseUniq from '../internal/baseUniq';
|
||||
import isIterateeCall from '../internal/isIterateeCall';
|
||||
import sortedUniq from '../internal/sortedUniq';
|
||||
|
||||
/**
|
||||
* Creates a duplicate-value-free version of an array using `SameValueZero`
|
||||
* for equality comparisons. Providing `true` for `isSorted` performs a faster
|
||||
* search algorithm for sorted arrays. If an iteratee function is provided it
|
||||
* is invoked for each value in the array to generate the criterion by which
|
||||
* uniqueness is computed. The `iteratee` is bound to `thisArg` and invoked
|
||||
* with three arguments; (value, index, array).
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* **Note:** `SameValueZero` comparisons are like strict equality comparisons,
|
||||
* e.g. `===`, except that `NaN` matches `NaN`. See the
|
||||
* [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
|
||||
* for more details.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @alias unique
|
||||
* @category Array
|
||||
* @param {Array} array The array to inspect.
|
||||
* @param {boolean} [isSorted] Specify the array is sorted.
|
||||
* @param {Function|Object|string} [iteratee] The function invoked per iteration.
|
||||
* If a property name or object is provided it is used to create a "_.property"
|
||||
* or "_.matches" style callback respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `iteratee`.
|
||||
* @returns {Array} Returns the new duplicate-value-free array.
|
||||
* @example
|
||||
*
|
||||
* _.uniq([1, 2, 1]);
|
||||
* // => [1, 2]
|
||||
*
|
||||
* // using `isSorted`
|
||||
* _.uniq([1, 1, 2], true);
|
||||
* // => [1, 2]
|
||||
*
|
||||
* // using an iteratee function
|
||||
* _.uniq([1, 2.5, 1.5, 2], function(n) { return this.floor(n); }, Math);
|
||||
* // => [1, 2.5]
|
||||
*
|
||||
* // using the "_.property" callback shorthand
|
||||
* _.uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
|
||||
* // => [{ 'x': 1 }, { 'x': 2 }]
|
||||
*/
|
||||
function uniq(array, isSorted, iteratee, thisArg) {
|
||||
var length = array ? array.length : 0;
|
||||
if (!length) {
|
||||
return [];
|
||||
}
|
||||
// Juggle arguments.
|
||||
if (typeof isSorted != 'boolean' && isSorted != null) {
|
||||
thisArg = iteratee;
|
||||
iteratee = isIterateeCall(array, isSorted, thisArg) ? null : isSorted;
|
||||
isSorted = false;
|
||||
}
|
||||
iteratee = iteratee == null ? iteratee : baseCallback(iteratee, thisArg, 3);
|
||||
return (isSorted)
|
||||
? sortedUniq(array, iteratee)
|
||||
: baseUniq(array, iteratee);
|
||||
}
|
||||
|
||||
export default uniq;
|
||||
2
array/unique.js
Normal file
2
array/unique.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import uniq from './uniq'
|
||||
export default uniq;
|
||||
37
array/unzip.js
Normal file
37
array/unzip.js
Normal file
@@ -0,0 +1,37 @@
|
||||
import arrayMap from '../internal/arrayMap';
|
||||
import arrayMax from '../internal/arrayMax';
|
||||
import baseProperty from '../internal/baseProperty';
|
||||
|
||||
/** Used to the length of n-tuples for `_.unzip`. */
|
||||
var getLength = baseProperty('length');
|
||||
|
||||
/**
|
||||
* This method is like `_.zip` except that it accepts an array of grouped
|
||||
* elements and creates an array regrouping the elements to their pre-`_.zip`
|
||||
* configuration.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The array of grouped elements to process.
|
||||
* @returns {Array} Returns the new array of regrouped elements.
|
||||
* @example
|
||||
*
|
||||
* var zipped = _.zip(['fred', 'barney'], [30, 40], [true, false]);
|
||||
* // => [['fred', 30, true], ['barney', 40, false]]
|
||||
*
|
||||
* _.unzip(zipped);
|
||||
* // => [['fred', 'barney'], [30, 40], [true, false]]
|
||||
*/
|
||||
function unzip(array) {
|
||||
var index = -1,
|
||||
length = (array && array.length && arrayMax(arrayMap(array, getLength))) >>> 0,
|
||||
result = Array(length);
|
||||
|
||||
while (++index < length) {
|
||||
result[index] = arrayMap(array, baseProperty(index));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export default unzip;
|
||||
28
array/without.js
Normal file
28
array/without.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import baseDifference from '../internal/baseDifference';
|
||||
import baseSlice from '../internal/baseSlice';
|
||||
|
||||
/**
|
||||
* Creates an array excluding all provided values using `SameValueZero` for
|
||||
* equality comparisons.
|
||||
*
|
||||
* **Note:** `SameValueZero` comparisons are like strict equality comparisons,
|
||||
* e.g. `===`, except that `NaN` matches `NaN`. See the
|
||||
* [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
|
||||
* for more details.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The array to filter.
|
||||
* @param {...*} [values] The values to exclude.
|
||||
* @returns {Array} Returns the new array of filtered values.
|
||||
* @example
|
||||
*
|
||||
* _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
|
||||
* // => [2, 3, 4]
|
||||
*/
|
||||
function without(array) {
|
||||
return baseDifference(array, baseSlice(arguments, 1));
|
||||
}
|
||||
|
||||
export default without;
|
||||
39
array/xor.js
Normal file
39
array/xor.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import baseDifference from '../internal/baseDifference';
|
||||
import baseUniq from '../internal/baseUniq';
|
||||
import isArguments from '../lang/isArguments';
|
||||
import isArray from '../lang/isArray';
|
||||
|
||||
/**
|
||||
* Creates an array that is the symmetric difference of the provided arrays.
|
||||
* See [Wikipedia](https://en.wikipedia.org/wiki/Symmetric_difference) for
|
||||
* more details.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {...Array} [arrays] The arrays to inspect.
|
||||
* @returns {Array} Returns the new array of values.
|
||||
* @example
|
||||
*
|
||||
* _.xor([1, 2, 3], [5, 2, 1, 4]);
|
||||
* // => [3, 5, 4]
|
||||
*
|
||||
* _.xor([1, 2, 5], [2, 3, 5], [3, 4, 5]);
|
||||
* // => [1, 4, 5]
|
||||
*/
|
||||
function xor() {
|
||||
var index = -1,
|
||||
length = arguments.length;
|
||||
|
||||
while (++index < length) {
|
||||
var array = arguments[index];
|
||||
if (isArray(array) || isArguments(array)) {
|
||||
var result = result
|
||||
? baseDifference(result, array).concat(baseDifference(array, result))
|
||||
: array;
|
||||
}
|
||||
}
|
||||
return result ? baseUniq(result) : [];
|
||||
}
|
||||
|
||||
export default xor;
|
||||
28
array/zip.js
Normal file
28
array/zip.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import unzip from './unzip';
|
||||
|
||||
/**
|
||||
* Creates an array of grouped elements, the first of which contains the first
|
||||
* elements of the given arrays, the second of which contains the second elements
|
||||
* of the given arrays, and so on.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {...Array} [arrays] The arrays to process.
|
||||
* @returns {Array} Returns the new array of grouped elements.
|
||||
* @example
|
||||
*
|
||||
* _.zip(['fred', 'barney'], [30, 40], [true, false]);
|
||||
* // => [['fred', 30, true], ['barney', 40, false]]
|
||||
*/
|
||||
function zip() {
|
||||
var length = arguments.length,
|
||||
array = Array(length);
|
||||
|
||||
while (length--) {
|
||||
array[length] = arguments[length];
|
||||
}
|
||||
return unzip(array);
|
||||
}
|
||||
|
||||
export default zip;
|
||||
39
array/zipObject.js
Normal file
39
array/zipObject.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import isArray from '../lang/isArray';
|
||||
|
||||
/**
|
||||
* Creates an object composed from arrays of property names and values. Provide
|
||||
* either a single two dimensional array, e.g. `[[key1, value1], [key2, value2]]`
|
||||
* or two arrays, one of property names and one of corresponding values.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @alias object
|
||||
* @category Array
|
||||
* @param {Array} props The property names.
|
||||
* @param {Array} [values=[]] The property values.
|
||||
* @returns {Object} Returns the new object.
|
||||
* @example
|
||||
*
|
||||
* _.zipObject(['fred', 'barney'], [30, 40]);
|
||||
* // => { 'fred': 30, 'barney': 40 }
|
||||
*/
|
||||
function zipObject(props, values) {
|
||||
var index = -1,
|
||||
length = props ? props.length : 0,
|
||||
result = {};
|
||||
|
||||
if (length && !values && !isArray(props[0])) {
|
||||
values = [];
|
||||
}
|
||||
while (++index < length) {
|
||||
var key = props[index];
|
||||
if (values) {
|
||||
result[key] = values[index];
|
||||
} else if (key) {
|
||||
result[key[0]] = key[1];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export default zipObject;
|
||||
23
chain.js
Normal file
23
chain.js
Normal file
@@ -0,0 +1,23 @@
|
||||
import chain from './chain/chain';
|
||||
import lodash from './chain/lodash';
|
||||
import reverse from './chain/reverse';
|
||||
import tap from './chain/tap';
|
||||
import thru from './chain/thru';
|
||||
import toJSON from './chain/toJSON';
|
||||
import toString from './chain/toString';
|
||||
import value from './chain/value';
|
||||
import valueOf from './chain/valueOf';
|
||||
import wrapperChain from './chain/wrapperChain';
|
||||
|
||||
export default {
|
||||
'chain': chain,
|
||||
'lodash': lodash,
|
||||
'reverse': reverse,
|
||||
'tap': tap,
|
||||
'thru': thru,
|
||||
'toJSON': toJSON,
|
||||
'toString': toString,
|
||||
'value': value,
|
||||
'valueOf': valueOf,
|
||||
'wrapperChain': wrapperChain
|
||||
};
|
||||
33
chain/chain.js
Normal file
33
chain/chain.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import lodash from './lodash';
|
||||
|
||||
/**
|
||||
* Creates a `lodash` object that wraps `value` with explicit method
|
||||
* chaining enabled.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Chain
|
||||
* @param {*} value The value to wrap.
|
||||
* @returns {Object} Returns the new `lodash` object.
|
||||
* @example
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'age': 36 },
|
||||
* { 'user': 'fred', 'age': 40 },
|
||||
* { 'user': 'pebbles', 'age': 1 }
|
||||
* ];
|
||||
*
|
||||
* var youngest = _.chain(users)
|
||||
* .sortBy('age')
|
||||
* .map(function(chr) { return chr.user + ' is ' + chr.age; })
|
||||
* .first()
|
||||
* .value();
|
||||
* // => 'pebbles is 1'
|
||||
*/
|
||||
function chain(value) {
|
||||
var result = lodash(value);
|
||||
result.__chain__ = true;
|
||||
return result;
|
||||
}
|
||||
|
||||
export default chain;
|
||||
109
chain/lodash.js
Normal file
109
chain/lodash.js
Normal file
@@ -0,0 +1,109 @@
|
||||
import LodashWrapper from '../internal/LodashWrapper';
|
||||
import arrayCopy from '../internal/arrayCopy';
|
||||
import isArray from '../lang/isArray';
|
||||
import isObjectLike from '../internal/isObjectLike';
|
||||
|
||||
/** Used for native method references. */
|
||||
var objectProto = Object.prototype;
|
||||
|
||||
/** Used to check objects for own properties. */
|
||||
var hasOwnProperty = objectProto.hasOwnProperty;
|
||||
|
||||
/**
|
||||
* Creates a `lodash` object which wraps `value` to enable intuitive chaining.
|
||||
* Methods that operate on and return arrays, collections, and functions can
|
||||
* be chained together. Methods that return a boolean or single value will
|
||||
* automatically end the chain returning the unwrapped value. Explicit chaining
|
||||
* may be enabled using `_.chain`. The execution of chained methods is lazy,
|
||||
* that is, execution is deferred until `_#value` is implicitly or explicitly
|
||||
* called.
|
||||
*
|
||||
* Lazy evaluation allows several methods to support shortcut fusion. Shortcut
|
||||
* fusion is an optimization that merges iteratees to avoid creating intermediate
|
||||
* arrays and reduce the number of iteratee executions.
|
||||
*
|
||||
* Chaining is supported in custom builds as long as the `_#value` method is
|
||||
* directly or indirectly included in the build.
|
||||
*
|
||||
* In addition to lodash methods, wrappers also have the following `Array` methods:
|
||||
* `concat`, `join`, `pop`, `push`, `reverse`, `shift`, `slice`, `sort`, `splice`,
|
||||
* and `unshift`
|
||||
*
|
||||
* The wrapper functions that support shortcut fusion are:
|
||||
* `drop`, `dropRight`, `dropRightWhile`, `dropWhile`, `filter`, `first`,
|
||||
* `initial`, `last`, `map`, `pluck`, `reject`, `rest`, `reverse`, `slice`,
|
||||
* `take`, `takeRight`, `takeRightWhile`, `takeWhile`, and `where`
|
||||
*
|
||||
* The chainable wrapper functions are:
|
||||
* `after`, `ary`, `assign`, `at`, `before`, `bind`, `bindAll`, `bindKey`,
|
||||
* `callback`, `chain`, `chunk`, `compact`, `concat`, `constant`, `countBy`,
|
||||
* `create`, `curry`, `debounce`, `defaults`, `defer`, `delay`, `difference`,
|
||||
* `drop`, `dropRight`, `dropRightWhile`, `dropWhile`, `filter`, `flatten`,
|
||||
* `flattenDeep`, `flow`, `flowRight`, `forEach`, `forEachRight`, `forIn`,
|
||||
* `forInRight`, `forOwn`, `forOwnRight`, `functions`, `groupBy`, `indexBy`,
|
||||
* `initial`, `intersection`, `invert`, `invoke`, `keys`, `keysIn`, `map`,
|
||||
* `mapValues`, `matches`, `memoize`, `merge`, `mixin`, `negate`, `noop`,
|
||||
* `omit`, `once`, `pairs`, `partial`, `partialRight`, `partition`, `pick`,
|
||||
* `pluck`, `property`, `propertyOf`, `pull`, `pullAt`, `push`, `range`,
|
||||
* `rearg`, `reject`, `remove`, `rest`, `reverse`, `shuffle`, `slice`, `sort`,
|
||||
* `sortBy`, `sortByAll`, `splice`, `take`, `takeRight`, `takeRightWhile`,
|
||||
* `takeWhile`, `tap`, `throttle`, `thru`, `times`, `toArray`, `toPlainObject`,
|
||||
* `transform`, `union`, `uniq`, `unshift`, `unzip`, `values`, `valuesIn`,
|
||||
* `where`, `without`, `wrap`, `xor`, `zip`, and `zipObject`
|
||||
*
|
||||
* The wrapper functions that are **not** chainable by default are:
|
||||
* `attempt`, `camelCase`, `capitalize`, `clone`, `cloneDeep`, `deburr`,
|
||||
* `endsWith`, `escape`, `escapeRegExp`, `every`, `find`, `findIndex`, `findKey`,
|
||||
* `findLast`, `findLastIndex`, `findLastKey`, `findWhere`, `first`, `has`,
|
||||
* `identity`, `includes`, `indexOf`, `isArguments`, `isArray`, `isBoolean`,
|
||||
* `isDate`, `isElement`, `isEmpty`, `isEqual`, `isError`, `isFinite`,
|
||||
* `isFunction`, `isMatch` , `isNative`, `isNaN`, `isNull`, `isNumber`,
|
||||
* `isObject`, `isPlainObject`, `isRegExp`, `isString`, `isUndefined`,
|
||||
* `isTypedArray`, `join`, `kebabCase`, `last`, `lastIndexOf`, `max`, `min`,
|
||||
* `noConflict`, `now`, `pad`, `padLeft`, `padRight`, `parseInt`, `pop`,
|
||||
* `random`, `reduce`, `reduceRight`, `repeat`, `result`, `runInContext`,
|
||||
* `shift`, `size`, `snakeCase`, `some`, `sortedIndex`, `sortedLastIndex`,
|
||||
* `startsWith`, `template`, `trim`, `trimLeft`, `trimRight`, `trunc`,
|
||||
* `unescape`, `uniqueId`, `value`, and `words`
|
||||
*
|
||||
* The wrapper function `sample` will return a wrapped value when `n` is provided,
|
||||
* otherwise an unwrapped value is returned.
|
||||
*
|
||||
* @name _
|
||||
* @constructor
|
||||
* @category Chain
|
||||
* @param {*} value The value to wrap in a `lodash` instance.
|
||||
* @returns {Object} Returns a `lodash` instance.
|
||||
* @example
|
||||
*
|
||||
* var wrapped = _([1, 2, 3]);
|
||||
*
|
||||
* // returns an unwrapped value
|
||||
* wrapped.reduce(function(sum, n) { return sum + n; });
|
||||
* // => 6
|
||||
*
|
||||
* // returns a wrapped value
|
||||
* var squares = wrapped.map(function(n) { return n * n; });
|
||||
*
|
||||
* _.isArray(squares);
|
||||
* // => false
|
||||
*
|
||||
* _.isArray(squares.value());
|
||||
* // => true
|
||||
*/
|
||||
function lodash(value) {
|
||||
if (isObjectLike(value) && !isArray(value)) {
|
||||
if (value instanceof LodashWrapper) {
|
||||
return value;
|
||||
}
|
||||
if (hasOwnProperty.call(value, '__wrapped__')) {
|
||||
return new LodashWrapper(value.__wrapped__, value.__chain__, arrayCopy(value.__actions__));
|
||||
}
|
||||
}
|
||||
return new LodashWrapper(value);
|
||||
}
|
||||
|
||||
// Ensure `new LodashWrapper` is an instance of `lodash`.
|
||||
LodashWrapper.prototype = lodash.prototype;
|
||||
|
||||
export default lodash;
|
||||
2
chain/reverse.js
Normal file
2
chain/reverse.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import wrapperReverse from './wrapperReverse'
|
||||
export default wrapperReverse;
|
||||
27
chain/tap.js
Normal file
27
chain/tap.js
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* This method invokes `interceptor` and returns `value`. The interceptor is
|
||||
* bound to `thisArg` and invoked with one argument; (value). The purpose of
|
||||
* this method is to "tap into" a method chain in order to perform operations
|
||||
* on intermediate results within the chain.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Chain
|
||||
* @param {*} value The value to provide to `interceptor`.
|
||||
* @param {Function} interceptor The function to invoke.
|
||||
* @param {*} [thisArg] The `this` binding of `interceptor`.
|
||||
* @returns {*} Returns `value`.
|
||||
* @example
|
||||
*
|
||||
* _([1, 2, 3])
|
||||
* .tap(function(array) { array.pop(); })
|
||||
* .reverse()
|
||||
* .value();
|
||||
* // => [2, 1]
|
||||
*/
|
||||
function tap(value, interceptor, thisArg) {
|
||||
interceptor.call(thisArg, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
export default tap;
|
||||
23
chain/thru.js
Normal file
23
chain/thru.js
Normal file
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* This method is like `_.tap` except that it returns the result of `interceptor`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Chain
|
||||
* @param {*} value The value to provide to `interceptor`.
|
||||
* @param {Function} interceptor The function to invoke.
|
||||
* @param {*} [thisArg] The `this` binding of `interceptor`.
|
||||
* @returns {*} Returns the result of `interceptor`.
|
||||
* @example
|
||||
*
|
||||
* _([1, 2, 3])
|
||||
* .last()
|
||||
* .thru(function(value) { return [value]; })
|
||||
* .value();
|
||||
* // => [3]
|
||||
*/
|
||||
function thru(value, interceptor, thisArg) {
|
||||
return interceptor.call(thisArg, value);
|
||||
}
|
||||
|
||||
export default thru;
|
||||
2
chain/toJSON.js
Normal file
2
chain/toJSON.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import wrapperValue from './wrapperValue'
|
||||
export default wrapperValue;
|
||||
2
chain/toString.js
Normal file
2
chain/toString.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import wrapperToString from './wrapperToString'
|
||||
export default wrapperToString;
|
||||
2
chain/value.js
Normal file
2
chain/value.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import wrapperValue from './wrapperValue'
|
||||
export default wrapperValue;
|
||||
2
chain/valueOf.js
Normal file
2
chain/valueOf.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import wrapperValue from './wrapperValue'
|
||||
export default wrapperValue;
|
||||
32
chain/wrapperChain.js
Normal file
32
chain/wrapperChain.js
Normal file
@@ -0,0 +1,32 @@
|
||||
import chain from './chain';
|
||||
|
||||
/**
|
||||
* Enables explicit method chaining on the wrapper object.
|
||||
*
|
||||
* @name chain
|
||||
* @memberOf _
|
||||
* @category Chain
|
||||
* @returns {*} Returns the `lodash` object.
|
||||
* @example
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'age': 36 },
|
||||
* { 'user': 'fred', 'age': 40 }
|
||||
* ];
|
||||
*
|
||||
* // without explicit chaining
|
||||
* _(users).first();
|
||||
* // => { 'user': 'barney', 'age': 36 }
|
||||
*
|
||||
* // with explicit chaining
|
||||
* _(users).chain()
|
||||
* .first()
|
||||
* .pick('user')
|
||||
* .value();
|
||||
* // => { 'user': 'barney' }
|
||||
*/
|
||||
function wrapperChain() {
|
||||
return chain(this);
|
||||
}
|
||||
|
||||
export default wrapperChain;
|
||||
35
chain/wrapperReverse.js
Normal file
35
chain/wrapperReverse.js
Normal file
@@ -0,0 +1,35 @@
|
||||
import LazyWrapper from '../internal/LazyWrapper';
|
||||
import LodashWrapper from '../internal/LodashWrapper';
|
||||
import thru from './thru';
|
||||
|
||||
/**
|
||||
* Reverses the wrapped array so the first element becomes the last, the
|
||||
* second element becomes the second to last, and so on.
|
||||
*
|
||||
* **Note:** This method mutates the wrapped array.
|
||||
*
|
||||
* @name reverse
|
||||
* @memberOf _
|
||||
* @category Chain
|
||||
* @returns {Object} Returns the new reversed `lodash` object.
|
||||
* @example
|
||||
*
|
||||
* var array = [1, 2, 3];
|
||||
*
|
||||
* _(array).reverse().value()
|
||||
* // => [3, 2, 1]
|
||||
*
|
||||
* console.log(array);
|
||||
* // => [3, 2, 1]
|
||||
*/
|
||||
function wrapperReverse() {
|
||||
var value = this.__wrapped__;
|
||||
if (value instanceof LazyWrapper) {
|
||||
return new LodashWrapper(value.reverse());
|
||||
}
|
||||
return this.thru(function(value) {
|
||||
return value.reverse();
|
||||
});
|
||||
}
|
||||
|
||||
export default wrapperReverse;
|
||||
17
chain/wrapperToString.js
Normal file
17
chain/wrapperToString.js
Normal file
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Produces the result of coercing the unwrapped value to a string.
|
||||
*
|
||||
* @name toString
|
||||
* @memberOf _
|
||||
* @category Chain
|
||||
* @returns {string} Returns the coerced string value.
|
||||
* @example
|
||||
*
|
||||
* _([1, 2, 3]).toString();
|
||||
* // => '1,2,3'
|
||||
*/
|
||||
function wrapperToString() {
|
||||
return (this.value() + '');
|
||||
}
|
||||
|
||||
export default wrapperToString;
|
||||
20
chain/wrapperValue.js
Normal file
20
chain/wrapperValue.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import baseWrapperValue from '../internal/baseWrapperValue';
|
||||
|
||||
/**
|
||||
* Executes the chained sequence to extract the unwrapped value.
|
||||
*
|
||||
* @name value
|
||||
* @memberOf _
|
||||
* @alias toJSON, valueOf
|
||||
* @category Chain
|
||||
* @returns {*} Returns the resolved unwrapped value.
|
||||
* @example
|
||||
*
|
||||
* _([1, 2, 3]).value();
|
||||
* // => [1, 2, 3]
|
||||
*/
|
||||
function wrapperValue() {
|
||||
return baseWrapperValue(this.__wrapped__, this.__actions__);
|
||||
}
|
||||
|
||||
export default wrapperValue;
|
||||
83
collection.js
Normal file
83
collection.js
Normal file
@@ -0,0 +1,83 @@
|
||||
import all from './collection/all';
|
||||
import any from './collection/any';
|
||||
import at from './collection/at';
|
||||
import collect from './collection/collect';
|
||||
import contains from './collection/contains';
|
||||
import countBy from './collection/countBy';
|
||||
import detect from './collection/detect';
|
||||
import each from './collection/each';
|
||||
import eachRight from './collection/eachRight';
|
||||
import every from './collection/every';
|
||||
import filter from './collection/filter';
|
||||
import find from './collection/find';
|
||||
import findLast from './collection/findLast';
|
||||
import findWhere from './collection/findWhere';
|
||||
import foldl from './collection/foldl';
|
||||
import foldr from './collection/foldr';
|
||||
import forEach from './collection/forEach';
|
||||
import forEachRight from './collection/forEachRight';
|
||||
import groupBy from './collection/groupBy';
|
||||
import include from './collection/include';
|
||||
import includes from './collection/includes';
|
||||
import indexBy from './collection/indexBy';
|
||||
import inject from './collection/inject';
|
||||
import invoke from './collection/invoke';
|
||||
import map from './collection/map';
|
||||
import max from './collection/max';
|
||||
import min from './collection/min';
|
||||
import partition from './collection/partition';
|
||||
import pluck from './collection/pluck';
|
||||
import reduce from './collection/reduce';
|
||||
import reduceRight from './collection/reduceRight';
|
||||
import reject from './collection/reject';
|
||||
import sample from './collection/sample';
|
||||
import select from './collection/select';
|
||||
import shuffle from './collection/shuffle';
|
||||
import size from './collection/size';
|
||||
import some from './collection/some';
|
||||
import sortBy from './collection/sortBy';
|
||||
import sortByAll from './collection/sortByAll';
|
||||
import where from './collection/where';
|
||||
|
||||
export default {
|
||||
'all': all,
|
||||
'any': any,
|
||||
'at': at,
|
||||
'collect': collect,
|
||||
'contains': contains,
|
||||
'countBy': countBy,
|
||||
'detect': detect,
|
||||
'each': each,
|
||||
'eachRight': eachRight,
|
||||
'every': every,
|
||||
'filter': filter,
|
||||
'find': find,
|
||||
'findLast': findLast,
|
||||
'findWhere': findWhere,
|
||||
'foldl': foldl,
|
||||
'foldr': foldr,
|
||||
'forEach': forEach,
|
||||
'forEachRight': forEachRight,
|
||||
'groupBy': groupBy,
|
||||
'include': include,
|
||||
'includes': includes,
|
||||
'indexBy': indexBy,
|
||||
'inject': inject,
|
||||
'invoke': invoke,
|
||||
'map': map,
|
||||
'max': max,
|
||||
'min': min,
|
||||
'partition': partition,
|
||||
'pluck': pluck,
|
||||
'reduce': reduce,
|
||||
'reduceRight': reduceRight,
|
||||
'reject': reject,
|
||||
'sample': sample,
|
||||
'select': select,
|
||||
'shuffle': shuffle,
|
||||
'size': size,
|
||||
'some': some,
|
||||
'sortBy': sortBy,
|
||||
'sortByAll': sortByAll,
|
||||
'where': where
|
||||
};
|
||||
2
collection/all.js
Normal file
2
collection/all.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import every from './every'
|
||||
export default every;
|
||||
2
collection/any.js
Normal file
2
collection/any.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import some from './some'
|
||||
export default some;
|
||||
34
collection/at.js
Normal file
34
collection/at.js
Normal file
@@ -0,0 +1,34 @@
|
||||
import baseAt from '../internal/baseAt';
|
||||
import baseFlatten from '../internal/baseFlatten';
|
||||
import isLength from '../internal/isLength';
|
||||
import toIterable from '../internal/toIterable';
|
||||
|
||||
/**
|
||||
* Creates an array of elements corresponding to the given keys, or indexes,
|
||||
* of `collection`. Keys may be specified as individual arguments or as arrays
|
||||
* of keys.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {...(number|number[]|string|string[])} [props] The property names
|
||||
* or indexes of elements to pick, specified individually or in arrays.
|
||||
* @returns {Array} Returns the new array of picked elements.
|
||||
* @example
|
||||
*
|
||||
* _.at(['a', 'b', 'c', 'd', 'e'], [0, 2, 4]);
|
||||
* // => ['a', 'c', 'e']
|
||||
*
|
||||
* _.at(['fred', 'barney', 'pebbles'], 0, 2);
|
||||
* // => ['fred', 'pebbles']
|
||||
*/
|
||||
function at(collection) {
|
||||
var length = collection ? collection.length : 0;
|
||||
if (isLength(length)) {
|
||||
collection = toIterable(collection);
|
||||
}
|
||||
return baseAt(collection, baseFlatten(arguments, false, false, 1));
|
||||
}
|
||||
|
||||
export default at;
|
||||
2
collection/collect.js
Normal file
2
collection/collect.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import map from './map'
|
||||
export default map;
|
||||
2
collection/contains.js
Normal file
2
collection/contains.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import includes from './includes'
|
||||
export default includes;
|
||||
47
collection/countBy.js
Normal file
47
collection/countBy.js
Normal file
@@ -0,0 +1,47 @@
|
||||
import createAggregator from '../internal/createAggregator';
|
||||
|
||||
/** Used for native method references. */
|
||||
var objectProto = Object.prototype;
|
||||
|
||||
/** Used to check objects for own properties. */
|
||||
var hasOwnProperty = objectProto.hasOwnProperty;
|
||||
|
||||
/**
|
||||
* Creates an object composed of keys generated from the results of running
|
||||
* each element of `collection` through `iteratee`. The corresponding value
|
||||
* of each key is the number of times the key was returned by `iteratee`.
|
||||
* The `iteratee` is bound to `thisArg` and invoked with three arguments;
|
||||
* (value, index|key, collection).
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {Function|Object|string} [iteratee=_.identity] The function invoked
|
||||
* per iteration. If a property name or object is provided it is used to
|
||||
* create a "_.property" or "_.matches" style callback respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `iteratee`.
|
||||
* @returns {Object} Returns the composed aggregate object.
|
||||
* @example
|
||||
*
|
||||
* _.countBy([4.3, 6.1, 6.4], function(n) { return Math.floor(n); });
|
||||
* // => { '4': 1, '6': 2 }
|
||||
*
|
||||
* _.countBy([4.3, 6.1, 6.4], function(n) { return this.floor(n); }, Math);
|
||||
* // => { '4': 1, '6': 2 }
|
||||
*
|
||||
* _.countBy(['one', 'two', 'three'], 'length');
|
||||
* // => { '3': 2, '5': 1 }
|
||||
*/
|
||||
var countBy = createAggregator(function(result, value, key) {
|
||||
hasOwnProperty.call(result, key) ? ++result[key] : (result[key] = 1);
|
||||
});
|
||||
|
||||
export default countBy;
|
||||
2
collection/detect.js
Normal file
2
collection/detect.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import find from './find'
|
||||
export default find;
|
||||
2
collection/each.js
Normal file
2
collection/each.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import forEach from './forEach'
|
||||
export default forEach;
|
||||
2
collection/eachRight.js
Normal file
2
collection/eachRight.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import forEachRight from './forEachRight'
|
||||
export default forEachRight;
|
||||
55
collection/every.js
Normal file
55
collection/every.js
Normal file
@@ -0,0 +1,55 @@
|
||||
import arrayEvery from '../internal/arrayEvery';
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
import baseEvery from '../internal/baseEvery';
|
||||
import isArray from '../lang/isArray';
|
||||
|
||||
/**
|
||||
* Checks if `predicate` returns truthy for **all** elements of `collection`.
|
||||
* The predicate is bound to `thisArg` and invoked with three arguments;
|
||||
* (value, index|key, collection).
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @alias all
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {Function|Object|string} [predicate=_.identity] The function invoked
|
||||
* per iteration. If a property name or object is provided it is used to
|
||||
* create a "_.property" or "_.matches" style callback respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `predicate`.
|
||||
* @returns {boolean} Returns `true` if all elements pass the predicate check,
|
||||
* else `false`.
|
||||
* @example
|
||||
*
|
||||
* _.every([true, 1, null, 'yes']);
|
||||
* // => false
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'age': 36 },
|
||||
* { 'user': 'fred', 'age': 40 }
|
||||
* ];
|
||||
*
|
||||
* // using the "_.property" callback shorthand
|
||||
* _.every(users, 'age');
|
||||
* // => true
|
||||
*
|
||||
* // using the "_.matches" callback shorthand
|
||||
* _.every(users, { 'age': 36 });
|
||||
* // => false
|
||||
*/
|
||||
function every(collection, predicate, thisArg) {
|
||||
var func = isArray(collection) ? arrayEvery : baseEvery;
|
||||
if (typeof predicate != 'function' || typeof thisArg != 'undefined') {
|
||||
predicate = baseCallback(predicate, thisArg, 3);
|
||||
}
|
||||
return func(collection, predicate);
|
||||
}
|
||||
|
||||
export default every;
|
||||
52
collection/filter.js
Normal file
52
collection/filter.js
Normal file
@@ -0,0 +1,52 @@
|
||||
import arrayFilter from '../internal/arrayFilter';
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
import baseFilter from '../internal/baseFilter';
|
||||
import isArray from '../lang/isArray';
|
||||
|
||||
/**
|
||||
* Iterates over elements of `collection`, returning an array of all elements
|
||||
* `predicate` returns truthy for. The predicate is bound to `thisArg` and
|
||||
* invoked with three arguments; (value, index|key, collection).
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @alias select
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {Function|Object|string} [predicate=_.identity] The function invoked
|
||||
* per iteration. If a property name or object is provided it is used to
|
||||
* create a "_.property" or "_.matches" style callback respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `predicate`.
|
||||
* @returns {Array} Returns the new filtered array.
|
||||
* @example
|
||||
*
|
||||
* var evens = _.filter([1, 2, 3, 4], function(n) { return n % 2 == 0; });
|
||||
* // => [2, 4]
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'age': 36, 'active': false },
|
||||
* { 'user': 'fred', 'age': 40, 'active': true }
|
||||
* ];
|
||||
*
|
||||
* // using the "_.property" callback shorthand
|
||||
* _.pluck(_.filter(users, 'active'), 'user');
|
||||
* // => ['fred']
|
||||
*
|
||||
* // using the "_.matches" callback shorthand
|
||||
* _.pluck(_.filter(users, { 'age': 36 }), 'user');
|
||||
* // => ['barney']
|
||||
*/
|
||||
function filter(collection, predicate, thisArg) {
|
||||
var func = isArray(collection) ? arrayFilter : baseFilter;
|
||||
predicate = baseCallback(predicate, thisArg, 3);
|
||||
return func(collection, predicate);
|
||||
}
|
||||
|
||||
export default filter;
|
||||
57
collection/find.js
Normal file
57
collection/find.js
Normal file
@@ -0,0 +1,57 @@
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
import baseEach from '../internal/baseEach';
|
||||
import baseFind from '../internal/baseFind';
|
||||
import findIndex from '../array/findIndex';
|
||||
import isArray from '../lang/isArray';
|
||||
|
||||
/**
|
||||
* Iterates over elements of `collection`, returning the first element
|
||||
* `predicate` returns truthy for. The predicate is bound to `thisArg` and
|
||||
* invoked with three arguments; (value, index|key, collection).
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @alias detect
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to search.
|
||||
* @param {Function|Object|string} [predicate=_.identity] The function invoked
|
||||
* per iteration. If a property name or object is provided it is used to
|
||||
* create a "_.property" or "_.matches" style callback respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `predicate`.
|
||||
* @returns {*} Returns the matched element, else `undefined`.
|
||||
* @example
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'age': 36, 'active': false },
|
||||
* { 'user': 'fred', 'age': 40, 'active': true },
|
||||
* { 'user': 'pebbles', 'age': 1, 'active': false }
|
||||
* ];
|
||||
*
|
||||
* _.result(_.find(users, function(chr) { return chr.age < 40; }), 'user');
|
||||
* // => 'barney'
|
||||
*
|
||||
* // using the "_.matches" callback shorthand
|
||||
* _.result(_.find(users, { 'age': 1 }), 'user');
|
||||
* // => 'pebbles'
|
||||
*
|
||||
* // using the "_.property" callback shorthand
|
||||
* _.result(_.find(users, 'active'), 'user');
|
||||
* // => 'fred'
|
||||
*/
|
||||
function find(collection, predicate, thisArg) {
|
||||
if (isArray(collection)) {
|
||||
var index = findIndex(collection, predicate, thisArg);
|
||||
return index > -1 ? collection[index] : undefined;
|
||||
}
|
||||
predicate = baseCallback(predicate, thisArg, 3);
|
||||
return baseFind(collection, predicate, baseEach);
|
||||
}
|
||||
|
||||
export default find;
|
||||
28
collection/findLast.js
Normal file
28
collection/findLast.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
import baseEachRight from '../internal/baseEachRight';
|
||||
import baseFind from '../internal/baseFind';
|
||||
|
||||
/**
|
||||
* This method is like `_.find` except that it iterates over elements of
|
||||
* `collection` from right to left.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to search.
|
||||
* @param {Function|Object|string} [predicate=_.identity] The function invoked
|
||||
* per iteration. If a property name or object is provided it is used to
|
||||
* create a "_.property" or "_.matches" style callback respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `predicate`.
|
||||
* @returns {*} Returns the matched element, else `undefined`.
|
||||
* @example
|
||||
*
|
||||
* _.findLast([1, 2, 3, 4], function(n) { return n % 2 == 1; });
|
||||
* // => 3
|
||||
*/
|
||||
function findLast(collection, predicate, thisArg) {
|
||||
predicate = baseCallback(predicate, thisArg, 3);
|
||||
return baseFind(collection, predicate, baseEachRight);
|
||||
}
|
||||
|
||||
export default findLast;
|
||||
32
collection/findWhere.js
Normal file
32
collection/findWhere.js
Normal file
@@ -0,0 +1,32 @@
|
||||
import find from './find';
|
||||
import matches from '../utility/matches';
|
||||
|
||||
/**
|
||||
* Performs a deep comparison between each element in `collection` and the
|
||||
* source object, returning the first element that has equivalent property
|
||||
* values.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to search.
|
||||
* @param {Object} source The object of property values to match.
|
||||
* @returns {*} Returns the matched element, else `undefined`.
|
||||
* @example
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'age': 36, 'status': 'busy' },
|
||||
* { 'user': 'fred', 'age': 40, 'status': 'busy' }
|
||||
* ];
|
||||
*
|
||||
* _.result(_.findWhere(users, { 'status': 'busy' }), 'user');
|
||||
* // => 'barney'
|
||||
*
|
||||
* _.result(_.findWhere(users, { 'age': 40 }), 'user');
|
||||
* // => 'fred'
|
||||
*/
|
||||
function findWhere(collection, source) {
|
||||
return find(collection, matches(source));
|
||||
}
|
||||
|
||||
export default findWhere;
|
||||
2
collection/foldl.js
Normal file
2
collection/foldl.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import reduce from './reduce'
|
||||
export default reduce;
|
||||
2
collection/foldr.js
Normal file
2
collection/foldr.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import reduceRight from './reduceRight'
|
||||
export default reduceRight;
|
||||
38
collection/forEach.js
Normal file
38
collection/forEach.js
Normal file
@@ -0,0 +1,38 @@
|
||||
import arrayEach from '../internal/arrayEach';
|
||||
import baseEach from '../internal/baseEach';
|
||||
import bindCallback from '../internal/bindCallback';
|
||||
import isArray from '../lang/isArray';
|
||||
|
||||
/**
|
||||
* Iterates over elements of `collection` invoking `iteratee` for each element.
|
||||
* The `iteratee` is bound to `thisArg` and invoked with three arguments;
|
||||
* (value, index|key, collection). Iterator functions may exit iteration early
|
||||
* by explicitly returning `false`.
|
||||
*
|
||||
* **Note:** As with other "Collections" methods, objects with a `length` property
|
||||
* are iterated like arrays. To avoid this behavior `_.forIn` or `_.forOwn`
|
||||
* may be used for object iteration.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @alias each
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {Function} [iteratee=_.identity] The function invoked per iteration.
|
||||
* @param {*} [thisArg] The `this` binding of `iteratee`.
|
||||
* @returns {Array|Object|string} Returns `collection`.
|
||||
* @example
|
||||
*
|
||||
* _([1, 2, 3]).forEach(function(n) { console.log(n); });
|
||||
* // => logs each value from left to right and returns the array
|
||||
*
|
||||
* _.forEach({ 'one': 1, 'two': 2, 'three': 3 }, function(n, key) { console.log(n, key); });
|
||||
* // => logs each value-key pair and returns the object (iteration order is not guaranteed)
|
||||
*/
|
||||
function forEach(collection, iteratee, thisArg) {
|
||||
return (typeof iteratee == 'function' && typeof thisArg == 'undefined' && isArray(collection))
|
||||
? arrayEach(collection, iteratee)
|
||||
: baseEach(collection, bindCallback(iteratee, thisArg, 3));
|
||||
}
|
||||
|
||||
export default forEach;
|
||||
29
collection/forEachRight.js
Normal file
29
collection/forEachRight.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import arrayEachRight from '../internal/arrayEachRight';
|
||||
import baseEachRight from '../internal/baseEachRight';
|
||||
import bindCallback from '../internal/bindCallback';
|
||||
import isArray from '../lang/isArray';
|
||||
|
||||
/**
|
||||
* This method is like `_.forEach` except that it iterates over elements of
|
||||
* `collection` from right to left.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @alias eachRight
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {Function} [iteratee=_.identity] The function invoked per iteration.
|
||||
* @param {*} [thisArg] The `this` binding of `iteratee`.
|
||||
* @returns {Array|Object|string} Returns `collection`.
|
||||
* @example
|
||||
*
|
||||
* _([1, 2, 3]).forEachRight(function(n) { console.log(n); }).join(',');
|
||||
* // => logs each value from right to left and returns the array
|
||||
*/
|
||||
function forEachRight(collection, iteratee, thisArg) {
|
||||
return (typeof iteratee == 'function' && typeof thisArg == 'undefined' && isArray(collection))
|
||||
? arrayEachRight(collection, iteratee)
|
||||
: baseEachRight(collection, bindCallback(iteratee, thisArg, 3));
|
||||
}
|
||||
|
||||
export default forEachRight;
|
||||
52
collection/groupBy.js
Normal file
52
collection/groupBy.js
Normal file
@@ -0,0 +1,52 @@
|
||||
import createAggregator from '../internal/createAggregator';
|
||||
|
||||
/** Used for native method references. */
|
||||
var objectProto = Object.prototype;
|
||||
|
||||
/** Used to check objects for own properties. */
|
||||
var hasOwnProperty = objectProto.hasOwnProperty;
|
||||
|
||||
/**
|
||||
* Creates an object composed of keys generated from the results of running
|
||||
* each element of `collection` through `iteratee`. The corresponding value
|
||||
* of each key is an array of the elements responsible for generating the key.
|
||||
* The `iteratee` is bound to `thisArg` and invoked with three arguments;
|
||||
* (value, index|key, collection).
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {Function|Object|string} [iteratee=_.identity] The function invoked
|
||||
* per iteration. If a property name or object is provided it is used to
|
||||
* create a "_.property" or "_.matches" style callback respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `iteratee`.
|
||||
* @returns {Object} Returns the composed aggregate object.
|
||||
* @example
|
||||
*
|
||||
* _.groupBy([4.2, 6.1, 6.4], function(n) { return Math.floor(n); });
|
||||
* // => { '4': [4.2], '6': [6.1, 6.4] }
|
||||
*
|
||||
* _.groupBy([4.2, 6.1, 6.4], function(n) { return this.floor(n); }, Math);
|
||||
* // => { '4': [4.2], '6': [6.1, 6.4] }
|
||||
*
|
||||
* // using the "_.property" callback shorthand
|
||||
* _.groupBy(['one', 'two', 'three'], 'length');
|
||||
* // => { '3': ['one', 'two'], '5': ['three'] }
|
||||
*/
|
||||
var groupBy = createAggregator(function(result, value, key) {
|
||||
if (hasOwnProperty.call(result, key)) {
|
||||
result[key].push(value);
|
||||
} else {
|
||||
result[key] = [value];
|
||||
}
|
||||
});
|
||||
|
||||
export default groupBy;
|
||||
2
collection/include.js
Normal file
2
collection/include.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import includes from './includes'
|
||||
export default includes;
|
||||
61
collection/includes.js
Normal file
61
collection/includes.js
Normal file
@@ -0,0 +1,61 @@
|
||||
import baseIndexOf from '../internal/baseIndexOf';
|
||||
import isArray from '../lang/isArray';
|
||||
import isLength from '../internal/isLength';
|
||||
import isString from '../lang/isString';
|
||||
import values from '../object/values';
|
||||
|
||||
/* Native method references for those with the same name as other `lodash` methods. */
|
||||
var nativeMax = Math.max;
|
||||
|
||||
/**
|
||||
* Checks if `value` is in `collection` using `SameValueZero` for equality
|
||||
* comparisons. If `fromIndex` is negative, it is used as the offset from
|
||||
* the end of `collection`.
|
||||
*
|
||||
* **Note:** `SameValueZero` comparisons are like strict equality comparisons,
|
||||
* e.g. `===`, except that `NaN` matches `NaN`. See the
|
||||
* [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
|
||||
* for more details.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @alias contains, include
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to search.
|
||||
* @param {*} target The value to search for.
|
||||
* @param {number} [fromIndex=0] The index to search from.
|
||||
* @returns {boolean} Returns `true` if a matching element is found, else `false`.
|
||||
* @example
|
||||
*
|
||||
* _.includes([1, 2, 3], 1);
|
||||
* // => true
|
||||
*
|
||||
* _.includes([1, 2, 3], 1, 2);
|
||||
* // => false
|
||||
*
|
||||
* _.includes({ 'user': 'fred', 'age': 40 }, 'fred');
|
||||
* // => true
|
||||
*
|
||||
* _.includes('pebbles', 'eb');
|
||||
* // => true
|
||||
*/
|
||||
function includes(collection, target, fromIndex) {
|
||||
var length = collection ? collection.length : 0;
|
||||
if (!isLength(length)) {
|
||||
collection = values(collection);
|
||||
length = collection.length;
|
||||
}
|
||||
if (!length) {
|
||||
return false;
|
||||
}
|
||||
if (typeof fromIndex == 'number') {
|
||||
fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
|
||||
} else {
|
||||
fromIndex = 0;
|
||||
}
|
||||
return (typeof collection == 'string' || !isArray(collection) && isString(collection))
|
||||
? (fromIndex < length && collection.indexOf(target, fromIndex) > -1)
|
||||
: (baseIndexOf(collection, target, fromIndex) > -1);
|
||||
}
|
||||
|
||||
export default includes;
|
||||
46
collection/indexBy.js
Normal file
46
collection/indexBy.js
Normal file
@@ -0,0 +1,46 @@
|
||||
import createAggregator from '../internal/createAggregator';
|
||||
|
||||
/**
|
||||
* Creates an object composed of keys generated from the results of running
|
||||
* each element of `collection` through `iteratee`. The corresponding value
|
||||
* of each key is the last element responsible for generating the key. The
|
||||
* iteratee function is bound to `thisArg` and invoked with three arguments;
|
||||
* (value, index|key, collection).
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {Function|Object|string} [iteratee=_.identity] The function invoked
|
||||
* per iteration. If a property name or object is provided it is used to
|
||||
* create a "_.property" or "_.matches" style callback respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `iteratee`.
|
||||
* @returns {Object} Returns the composed aggregate object.
|
||||
* @example
|
||||
*
|
||||
* var keyData = [
|
||||
* { 'dir': 'left', 'code': 97 },
|
||||
* { 'dir': 'right', 'code': 100 }
|
||||
* ];
|
||||
*
|
||||
* _.indexBy(keyData, 'dir');
|
||||
* // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }
|
||||
*
|
||||
* _.indexBy(keyData, function(object) { return String.fromCharCode(object.code); });
|
||||
* // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
|
||||
*
|
||||
* _.indexBy(keyData, function(object) { return this.fromCharCode(object.code); }, String);
|
||||
* // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
|
||||
*/
|
||||
var indexBy = createAggregator(function(result, value, key) {
|
||||
result[key] = value;
|
||||
});
|
||||
|
||||
export default indexBy;
|
||||
2
collection/inject.js
Normal file
2
collection/inject.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import reduce from './reduce'
|
||||
export default reduce;
|
||||
30
collection/invoke.js
Normal file
30
collection/invoke.js
Normal file
@@ -0,0 +1,30 @@
|
||||
import baseInvoke from '../internal/baseInvoke';
|
||||
import baseSlice from '../internal/baseSlice';
|
||||
|
||||
/**
|
||||
* Invokes the method named by `methodName` on each element in `collection`,
|
||||
* returning an array of the results of each invoked method. Any additional
|
||||
* arguments are provided to each invoked method. If `methodName` is a function
|
||||
* it is invoked for, and `this` bound to, each element in `collection`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {Function|string} methodName The name of the method to invoke or
|
||||
* the function invoked per iteration.
|
||||
* @param {...*} [args] The arguments to invoke the method with.
|
||||
* @returns {Array} Returns the array of results.
|
||||
* @example
|
||||
*
|
||||
* _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
|
||||
* // => [[1, 5, 7], [1, 2, 3]]
|
||||
*
|
||||
* _.invoke([123, 456], String.prototype.split, '');
|
||||
* // => [['1', '2', '3'], ['4', '5', '6']]
|
||||
*/
|
||||
function invoke(collection, methodName) {
|
||||
return baseInvoke(collection, methodName, baseSlice(arguments, 2));
|
||||
}
|
||||
|
||||
export default invoke;
|
||||
51
collection/map.js
Normal file
51
collection/map.js
Normal file
@@ -0,0 +1,51 @@
|
||||
import arrayMap from '../internal/arrayMap';
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
import baseMap from '../internal/baseMap';
|
||||
import isArray from '../lang/isArray';
|
||||
|
||||
/**
|
||||
* Creates an array of values by running each element in `collection` through
|
||||
* `iteratee`. The `iteratee` is bound to `thisArg` and invoked with three
|
||||
* arguments; (value, index|key, collection).
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @alias collect
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {Function|Object|string} [iteratee=_.identity] The function invoked
|
||||
* per iteration. If a property name or object is provided it is used to
|
||||
* create a "_.property" or "_.matches" style callback respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `iteratee`.
|
||||
* @returns {Array} Returns the new mapped array.
|
||||
* @example
|
||||
*
|
||||
* _.map([1, 2, 3], function(n) { return n * 3; });
|
||||
* // => [3, 6, 9]
|
||||
*
|
||||
* _.map({ 'one': 1, 'two': 2, 'three': 3 }, function(n) { return n * 3; });
|
||||
* // => [3, 6, 9] (iteration order is not guaranteed)
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney' },
|
||||
* { 'user': 'fred' }
|
||||
* ];
|
||||
*
|
||||
* // using the "_.property" callback shorthand
|
||||
* _.map(users, 'user');
|
||||
* // => ['barney', 'fred']
|
||||
*/
|
||||
function map(collection, iteratee, thisArg) {
|
||||
var func = isArray(collection) ? arrayMap : baseMap;
|
||||
iteratee = baseCallback(iteratee, thisArg, 3);
|
||||
return func(collection, iteratee);
|
||||
}
|
||||
|
||||
export default map;
|
||||
49
collection/max.js
Normal file
49
collection/max.js
Normal file
@@ -0,0 +1,49 @@
|
||||
import arrayMax from '../internal/arrayMax';
|
||||
import createExtremum from '../internal/createExtremum';
|
||||
|
||||
/**
|
||||
* Gets the maximum value of `collection`. If `collection` is empty or falsey
|
||||
* `-Infinity` is returned. If an iteratee function is provided it is invoked
|
||||
* for each value in `collection` to generate the criterion by which the value
|
||||
* is ranked. The `iteratee` is bound to `thisArg` and invoked with three
|
||||
* arguments; (value, index, collection).
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {Function|Object|string} [iteratee] The function invoked per iteration.
|
||||
* If a property name or object is provided it is used to create a "_.property"
|
||||
* or "_.matches" style callback respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `iteratee`.
|
||||
* @returns {*} Returns the maximum value.
|
||||
* @example
|
||||
*
|
||||
* _.max([4, 2, 8, 6]);
|
||||
* // => 8
|
||||
*
|
||||
* _.max([]);
|
||||
* // => -Infinity
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'age': 36 },
|
||||
* { 'user': 'fred', 'age': 40 }
|
||||
* ];
|
||||
*
|
||||
* _.max(users, function(chr) { return chr.age; });
|
||||
* // => { 'user': 'fred', 'age': 40 };
|
||||
*
|
||||
* // using the "_.property" callback shorthand
|
||||
* _.max(users, 'age');
|
||||
* // => { 'user': 'fred', 'age': 40 };
|
||||
*/
|
||||
var max = createExtremum(arrayMax);
|
||||
|
||||
export default max;
|
||||
49
collection/min.js
Normal file
49
collection/min.js
Normal file
@@ -0,0 +1,49 @@
|
||||
import arrayMin from '../internal/arrayMin';
|
||||
import createExtremum from '../internal/createExtremum';
|
||||
|
||||
/**
|
||||
* Gets the minimum value of `collection`. If `collection` is empty or falsey
|
||||
* `Infinity` is returned. If an iteratee function is provided it is invoked
|
||||
* for each value in `collection` to generate the criterion by which the value
|
||||
* is ranked. The `iteratee` is bound to `thisArg` and invoked with three
|
||||
* arguments; (value, index, collection).
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {Function|Object|string} [iteratee] The function invoked per iteration.
|
||||
* If a property name or object is provided it is used to create a "_.property"
|
||||
* or "_.matches" style callback respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `iteratee`.
|
||||
* @returns {*} Returns the minimum value.
|
||||
* @example
|
||||
*
|
||||
* _.min([4, 2, 8, 6]);
|
||||
* // => 2
|
||||
*
|
||||
* _.min([]);
|
||||
* // => Infinity
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'age': 36 },
|
||||
* { 'user': 'fred', 'age': 40 }
|
||||
* ];
|
||||
*
|
||||
* _.min(users, function(chr) { return chr.age; });
|
||||
* // => { 'user': 'barney', 'age': 36 };
|
||||
*
|
||||
* // using the "_.property" callback shorthand
|
||||
* _.min(users, 'age');
|
||||
* // => { 'user': 'barney', 'age': 36 };
|
||||
*/
|
||||
var min = createExtremum(arrayMin, true);
|
||||
|
||||
export default min;
|
||||
51
collection/partition.js
Normal file
51
collection/partition.js
Normal file
@@ -0,0 +1,51 @@
|
||||
import createAggregator from '../internal/createAggregator';
|
||||
|
||||
/**
|
||||
* Creates an array of elements split into two groups, the first of which
|
||||
* contains elements `predicate` returns truthy for, while the second of which
|
||||
* contains elements `predicate` returns falsey for. The predicate is bound
|
||||
* to `thisArg` and invoked with three arguments; (value, index|key, collection).
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {Function|Object|string} [predicate=_.identity] The function invoked
|
||||
* per iteration. If a property name or object is provided it is used to
|
||||
* create a "_.property" or "_.matches" style callback respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `predicate`.
|
||||
* @returns {Array} Returns the array of grouped elements.
|
||||
* @example
|
||||
*
|
||||
* _.partition([1, 2, 3], function(n) { return n % 2; });
|
||||
* // => [[1, 3], [2]]
|
||||
*
|
||||
* _.partition([1.2, 2.3, 3.4], function(n) { return this.floor(n) % 2; }, Math);
|
||||
* // => [[1, 3], [2]]
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'age': 36, 'active': false },
|
||||
* { 'user': 'fred', 'age': 40, 'active': true },
|
||||
* { 'user': 'pebbles', 'age': 1, 'active': false }
|
||||
* ];
|
||||
*
|
||||
* // using the "_.matches" callback shorthand
|
||||
* _.map(_.partition(users, { 'age': 1 }), function(array) { return _.pluck(array, 'user'); });
|
||||
* // => [['pebbles'], ['barney', 'fred']]
|
||||
*
|
||||
* // using the "_.property" callback shorthand
|
||||
* _.map(_.partition(users, 'active'), function(array) { return _.pluck(array, 'user'); });
|
||||
* // => [['fred'], ['barney', 'pebbles']]
|
||||
*/
|
||||
var partition = createAggregator(function(result, value, key) {
|
||||
result[key ? 0 : 1].push(value);
|
||||
}, function() { return [[], []]; });
|
||||
|
||||
export default partition;
|
||||
31
collection/pluck.js
Normal file
31
collection/pluck.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import map from './map';
|
||||
import property from '../utility/property';
|
||||
|
||||
/**
|
||||
* Gets the value of `key` from all elements in `collection`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {string} key The key of the property to pluck.
|
||||
* @returns {Array} Returns the property values.
|
||||
* @example
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'age': 36 },
|
||||
* { 'user': 'fred', 'age': 40 }
|
||||
* ];
|
||||
*
|
||||
* _.pluck(users, 'user');
|
||||
* // => ['barney', 'fred']
|
||||
*
|
||||
* var userIndex = _.indexBy(users, 'user');
|
||||
* _.pluck(userIndex, 'age');
|
||||
* // => [36, 40] (iteration order is not guaranteed)
|
||||
*/
|
||||
function pluck(collection, key) {
|
||||
return map(collection, property(key));
|
||||
}
|
||||
|
||||
export default pluck;
|
||||
40
collection/reduce.js
Normal file
40
collection/reduce.js
Normal file
@@ -0,0 +1,40 @@
|
||||
import arrayReduce from '../internal/arrayReduce';
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
import baseEach from '../internal/baseEach';
|
||||
import baseReduce from '../internal/baseReduce';
|
||||
import isArray from '../lang/isArray';
|
||||
|
||||
/**
|
||||
* Reduces `collection` to a value which is the accumulated result of running
|
||||
* each element in `collection` through `iteratee`, where each successive
|
||||
* invocation is supplied the return value of the previous. If `accumulator`
|
||||
* is not provided the first element of `collection` is used as the initial
|
||||
* value. The `iteratee` is bound to `thisArg`and invoked with four arguments;
|
||||
* (accumulator, value, index|key, collection).
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @alias foldl, inject
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {Function} [iteratee=_.identity] The function invoked per iteration.
|
||||
* @param {*} [accumulator] The initial value.
|
||||
* @param {*} [thisArg] The `this` binding of `iteratee`.
|
||||
* @returns {*} Returns the accumulated value.
|
||||
* @example
|
||||
*
|
||||
* var sum = _.reduce([1, 2, 3], function(sum, n) { return sum + n; });
|
||||
* // => 6
|
||||
*
|
||||
* var mapped = _.reduce({ 'a': 1, 'b': 2, 'c': 3 }, function(result, n, key) {
|
||||
* result[key] = n * 3;
|
||||
* return result;
|
||||
* }, {});
|
||||
* // => { 'a': 3, 'b': 6, 'c': 9 } (iteration order is not guaranteed)
|
||||
*/
|
||||
function reduce(collection, iteratee, accumulator, thisArg) {
|
||||
var func = isArray(collection) ? arrayReduce : baseReduce;
|
||||
return func(collection, baseCallback(iteratee, thisArg, 4), accumulator, arguments.length < 3, baseEach);
|
||||
}
|
||||
|
||||
export default reduce;
|
||||
31
collection/reduceRight.js
Normal file
31
collection/reduceRight.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import arrayReduceRight from '../internal/arrayReduceRight';
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
import baseEachRight from '../internal/baseEachRight';
|
||||
import baseReduce from '../internal/baseReduce';
|
||||
import isArray from '../lang/isArray';
|
||||
|
||||
/**
|
||||
* This method is like `_.reduce` except that it iterates over elements of
|
||||
* `collection` from right to left.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @alias foldr
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {Function} [iteratee=_.identity] The function invoked per iteration.
|
||||
* @param {*} [accumulator] The initial value.
|
||||
* @param {*} [thisArg] The `this` binding of `iteratee`.
|
||||
* @returns {*} Returns the accumulated value.
|
||||
* @example
|
||||
*
|
||||
* var array = [[0, 1], [2, 3], [4, 5]];
|
||||
* _.reduceRight(array, function(flattened, other) { return flattened.concat(other); }, []);
|
||||
* // => [4, 5, 2, 3, 0, 1]
|
||||
*/
|
||||
function reduceRight(collection, iteratee, accumulator, thisArg) {
|
||||
var func = isArray(collection) ? arrayReduceRight : baseReduce;
|
||||
return func(collection, baseCallback(iteratee, thisArg, 4), accumulator, arguments.length < 3, baseEachRight);
|
||||
}
|
||||
|
||||
export default reduceRight;
|
||||
52
collection/reject.js
Normal file
52
collection/reject.js
Normal file
@@ -0,0 +1,52 @@
|
||||
import arrayFilter from '../internal/arrayFilter';
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
import baseFilter from '../internal/baseFilter';
|
||||
import isArray from '../lang/isArray';
|
||||
|
||||
/**
|
||||
* The opposite of `_.filter`; this method returns the elements of `collection`
|
||||
* that `predicate` does **not** return truthy for.
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {Function|Object|string} [predicate=_.identity] The function invoked
|
||||
* per iteration. If a property name or object is provided it is used to
|
||||
* create a "_.property" or "_.matches" style callback respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `predicate`.
|
||||
* @returns {Array} Returns the new filtered array.
|
||||
* @example
|
||||
*
|
||||
* var odds = _.reject([1, 2, 3, 4], function(n) { return n % 2 == 0; });
|
||||
* // => [1, 3]
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'age': 36, 'active': false },
|
||||
* { 'user': 'fred', 'age': 40, 'active': true }
|
||||
* ];
|
||||
*
|
||||
* // using the "_.property" callback shorthand
|
||||
* _.pluck(_.reject(users, 'active'), 'user');
|
||||
* // => ['barney']
|
||||
*
|
||||
* // using the "_.matches" callback shorthand
|
||||
* _.pluck(_.reject(users, { 'age': 36 }), 'user');
|
||||
* // => ['fred']
|
||||
*/
|
||||
function reject(collection, predicate, thisArg) {
|
||||
var func = isArray(collection) ? arrayFilter : baseFilter;
|
||||
predicate = baseCallback(predicate, thisArg, 3);
|
||||
return func(collection, function(value, index, collection) {
|
||||
return !predicate(value, index, collection);
|
||||
});
|
||||
}
|
||||
|
||||
export default reject;
|
||||
38
collection/sample.js
Normal file
38
collection/sample.js
Normal file
@@ -0,0 +1,38 @@
|
||||
import baseRandom from '../internal/baseRandom';
|
||||
import isIterateeCall from '../internal/isIterateeCall';
|
||||
import shuffle from './shuffle';
|
||||
import toIterable from '../internal/toIterable';
|
||||
|
||||
/* Native method references for those with the same name as other `lodash` methods. */
|
||||
var nativeMin = Math.min;
|
||||
|
||||
/**
|
||||
* Gets a random element or `n` random elements from a collection.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to sample.
|
||||
* @param {number} [n] The number of elements to sample.
|
||||
* @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
|
||||
* @returns {*} Returns the random sample(s).
|
||||
* @example
|
||||
*
|
||||
* _.sample([1, 2, 3, 4]);
|
||||
* // => 2
|
||||
*
|
||||
* _.sample([1, 2, 3, 4], 2);
|
||||
* // => [3, 1]
|
||||
*/
|
||||
function sample(collection, n, guard) {
|
||||
if (guard ? isIterateeCall(collection, n, guard) : n == null) {
|
||||
collection = toIterable(collection);
|
||||
var length = collection.length;
|
||||
return length > 0 ? collection[baseRandom(0, length - 1)] : undefined;
|
||||
}
|
||||
var result = shuffle(collection);
|
||||
result.length = nativeMin(n < 0 ? 0 : (+n || 0), result.length);
|
||||
return result;
|
||||
}
|
||||
|
||||
export default sample;
|
||||
2
collection/select.js
Normal file
2
collection/select.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import filter from './filter'
|
||||
export default filter;
|
||||
36
collection/shuffle.js
Normal file
36
collection/shuffle.js
Normal file
@@ -0,0 +1,36 @@
|
||||
import baseRandom from '../internal/baseRandom';
|
||||
import toIterable from '../internal/toIterable';
|
||||
|
||||
/**
|
||||
* Creates an array of shuffled values, using a version of the Fisher-Yates
|
||||
* shuffle. See [Wikipedia](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle)
|
||||
* for more details.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to shuffle.
|
||||
* @returns {Array} Returns the new shuffled array.
|
||||
* @example
|
||||
*
|
||||
* _.shuffle([1, 2, 3, 4]);
|
||||
* // => [4, 1, 3, 2]
|
||||
*/
|
||||
function shuffle(collection) {
|
||||
collection = toIterable(collection);
|
||||
|
||||
var index = -1,
|
||||
length = collection.length,
|
||||
result = Array(length);
|
||||
|
||||
while (++index < length) {
|
||||
var rand = baseRandom(0, index);
|
||||
if (index != rand) {
|
||||
result[index] = result[rand];
|
||||
}
|
||||
result[rand] = collection[index];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export default shuffle;
|
||||
29
collection/size.js
Normal file
29
collection/size.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import isLength from '../internal/isLength';
|
||||
import keys from '../object/keys';
|
||||
|
||||
/**
|
||||
* Gets the size of `collection` by returning `collection.length` for
|
||||
* array-like values or the number of own enumerable properties for objects.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to inspect.
|
||||
* @returns {number} Returns the size of `collection`.
|
||||
* @example
|
||||
*
|
||||
* _.size([1, 2]);
|
||||
* // => 2
|
||||
*
|
||||
* _.size({ 'one': 1, 'two': 2, 'three': 3 });
|
||||
* // => 3
|
||||
*
|
||||
* _.size('pebbles');
|
||||
* // => 7
|
||||
*/
|
||||
function size(collection) {
|
||||
var length = collection ? collection.length : 0;
|
||||
return isLength(length) ? length : keys(collection).length;
|
||||
}
|
||||
|
||||
export default size;
|
||||
56
collection/some.js
Normal file
56
collection/some.js
Normal file
@@ -0,0 +1,56 @@
|
||||
import arraySome from '../internal/arraySome';
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
import baseSome from '../internal/baseSome';
|
||||
import isArray from '../lang/isArray';
|
||||
|
||||
/**
|
||||
* Checks if `predicate` returns truthy for **any** element of `collection`.
|
||||
* The function returns as soon as it finds a passing value and does not iterate
|
||||
* over the entire collection. The predicate is bound to `thisArg` and invoked
|
||||
* with three arguments; (value, index|key, collection).
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @alias any
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {Function|Object|string} [predicate=_.identity] The function invoked
|
||||
* per iteration. If a property name or object is provided it is used to
|
||||
* create a "_.property" or "_.matches" style callback respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `predicate`.
|
||||
* @returns {boolean} Returns `true` if any element passes the predicate check,
|
||||
* else `false`.
|
||||
* @example
|
||||
*
|
||||
* _.some([null, 0, 'yes', false], Boolean);
|
||||
* // => true
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'age': 36, 'active': false },
|
||||
* { 'user': 'fred', 'age': 40, 'active': true }
|
||||
* ];
|
||||
*
|
||||
* // using the "_.property" callback shorthand
|
||||
* _.some(users, 'active');
|
||||
* // => true
|
||||
*
|
||||
* // using the "_.matches" callback shorthand
|
||||
* _.some(users, { 'age': 1 });
|
||||
* // => false
|
||||
*/
|
||||
function some(collection, predicate, thisArg) {
|
||||
var func = isArray(collection) ? arraySome : baseSome;
|
||||
if (typeof predicate != 'function' || typeof thisArg != 'undefined') {
|
||||
predicate = baseCallback(predicate, thisArg, 3);
|
||||
}
|
||||
return func(collection, predicate);
|
||||
}
|
||||
|
||||
export default some;
|
||||
64
collection/sortBy.js
Normal file
64
collection/sortBy.js
Normal file
@@ -0,0 +1,64 @@
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
import baseEach from '../internal/baseEach';
|
||||
import baseSortBy from '../internal/baseSortBy';
|
||||
import compareAscending from '../internal/compareAscending';
|
||||
import isIterateeCall from '../internal/isIterateeCall';
|
||||
import isLength from '../internal/isLength';
|
||||
|
||||
/**
|
||||
* Creates an array of elements, sorted in ascending order by the results of
|
||||
* running each element in a collection through `iteratee`. This method performs
|
||||
* a stable sort, that is, it preserves the original sort order of equal elements.
|
||||
* The `iteratee` is bound to `thisArg` and invoked with three arguments;
|
||||
* (value, index|key, collection).
|
||||
*
|
||||
* If a property name is provided for `predicate` the created "_.property"
|
||||
* style callback returns the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `predicate` the created "_.matches" style
|
||||
* callback returns `true` for elements that have the properties of the given
|
||||
* object, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {Array|Function|Object|string} [iteratee=_.identity] The function
|
||||
* invoked per iteration. If a property name or an object is provided it is
|
||||
* used to create a "_.property" or "_.matches" style callback respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `iteratee`.
|
||||
* @returns {Array} Returns the new sorted array.
|
||||
* @example
|
||||
*
|
||||
* _.sortBy([1, 2, 3], function(n) { return Math.sin(n); });
|
||||
* // => [3, 1, 2]
|
||||
*
|
||||
* _.sortBy([1, 2, 3], function(n) { return this.sin(n); }, Math);
|
||||
* // => [3, 1, 2]
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'fred' },
|
||||
* { 'user': 'pebbles' },
|
||||
* { 'user': 'barney' }
|
||||
* ];
|
||||
*
|
||||
* // using the "_.property" callback shorthand
|
||||
* _.pluck(_.sortBy(users, 'user'), 'user');
|
||||
* // => ['barney', 'fred', 'pebbles']
|
||||
*/
|
||||
function sortBy(collection, iteratee, thisArg) {
|
||||
var index = -1,
|
||||
length = collection ? collection.length : 0,
|
||||
result = isLength(length) ? Array(length) : [];
|
||||
|
||||
if (thisArg && isIterateeCall(collection, iteratee, thisArg)) {
|
||||
iteratee = null;
|
||||
}
|
||||
iteratee = baseCallback(iteratee, thisArg, 3);
|
||||
baseEach(collection, function(value, key, collection) {
|
||||
result[++index] = { 'criteria': iteratee(value, key, collection), 'index': index, 'value': value };
|
||||
});
|
||||
return baseSortBy(result, compareAscending);
|
||||
}
|
||||
|
||||
export default sortBy;
|
||||
53
collection/sortByAll.js
Normal file
53
collection/sortByAll.js
Normal file
@@ -0,0 +1,53 @@
|
||||
import baseEach from '../internal/baseEach';
|
||||
import baseFlatten from '../internal/baseFlatten';
|
||||
import baseSortBy from '../internal/baseSortBy';
|
||||
import compareMultipleAscending from '../internal/compareMultipleAscending';
|
||||
import isIterateeCall from '../internal/isIterateeCall';
|
||||
import isLength from '../internal/isLength';
|
||||
|
||||
/**
|
||||
* This method is like `_.sortBy` except that it sorts by property names
|
||||
* instead of an iteratee function.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {...(string|string[])} props The property names to sort by,
|
||||
* specified as individual property names or arrays of property names.
|
||||
* @returns {Array} Returns the new sorted array.
|
||||
* @example
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'age': 36 },
|
||||
* { 'user': 'fred', 'age': 40 },
|
||||
* { 'user': 'barney', 'age': 26 },
|
||||
* { 'user': 'fred', 'age': 30 }
|
||||
* ];
|
||||
*
|
||||
* _.map(_.sortByAll(users, ['user', 'age']), _.values);
|
||||
* // => [['barney', 26], ['barney', 36], ['fred', 30], ['fred', 40]]
|
||||
*/
|
||||
function sortByAll(collection) {
|
||||
var args = arguments;
|
||||
if (args.length > 3 && isIterateeCall(args[1], args[2], args[3])) {
|
||||
args = [collection, args[1]];
|
||||
}
|
||||
var index = -1,
|
||||
length = collection ? collection.length : 0,
|
||||
props = baseFlatten(args, false, false, 1),
|
||||
result = isLength(length) ? Array(length) : [];
|
||||
|
||||
baseEach(collection, function(value, key, collection) {
|
||||
var length = props.length,
|
||||
criteria = Array(length);
|
||||
|
||||
while (length--) {
|
||||
criteria[length] = value == null ? undefined : value[props[length]];
|
||||
}
|
||||
result[++index] = { 'criteria': criteria, 'index': index, 'value': value };
|
||||
});
|
||||
return baseSortBy(result, compareMultipleAscending);
|
||||
}
|
||||
|
||||
export default sortByAll;
|
||||
35
collection/where.js
Normal file
35
collection/where.js
Normal file
@@ -0,0 +1,35 @@
|
||||
import filter from './filter';
|
||||
import matches from '../utility/matches';
|
||||
|
||||
/**
|
||||
* Performs a deep comparison between each element in `collection` and the
|
||||
* source object, returning an array of all elements that have equivalent
|
||||
* property values.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collection
|
||||
* @param {Array|Object|string} collection The collection to search.
|
||||
* @param {Object} source The object of property values to match.
|
||||
* @returns {Array} Returns the new filtered array.
|
||||
* @example
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'age': 36, 'status': 'busy', 'pets': ['hoppy'] },
|
||||
* { 'user': 'fred', 'age': 40, 'status': 'busy', 'pets': ['baby puss', 'dino'] }
|
||||
* ];
|
||||
*
|
||||
* _.pluck(_.where(users, { 'age': 36 }), 'user');
|
||||
* // => ['barney']
|
||||
*
|
||||
* _.pluck(_.where(users, { 'pets': ['dino'] }), 'user');
|
||||
* // => ['fred']
|
||||
*
|
||||
* _.pluck(_.where(users, { 'status': 'busy' }), 'user');
|
||||
* // => ['barney', 'fred']
|
||||
*/
|
||||
function where(collection, source) {
|
||||
return filter(collection, matches(source));
|
||||
}
|
||||
|
||||
export default where;
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user