Compare commits

...

8 Commits

Author SHA1 Message Date
jdalton
f84f83a4a5 Bump to v3.9.0. 2015-05-19 13:46:23 -07:00
jdalton
53c14e5b9b Bump to v3.8.0. 2015-05-01 08:25:59 -07:00
jdalton
863bb301bb Bump to v3.7.0. 2015-04-16 08:01:14 -07:00
jdalton
d58549ce0b Bump to v3.6.0. 2015-03-25 08:35:37 -07:00
jdalton
06f6ffa303 Bump to v3.5.0. 2015-03-08 19:07:13 -07:00
jdalton
4881dda9d1 Bump to v3.4.0. 2015-03-06 09:14:50 -08:00
jdalton
cc16e113c2 Bump to v3.3.1. 2015-02-24 01:00:48 -08:00
jdalton
5dc85cc9a8 Bump to v3.3.0. 2015-02-20 00:57:26 -08:00
311 changed files with 6072 additions and 3978 deletions

View File

@@ -1,5 +1,5 @@
Copyright 2012-2015 The Dojo Foundation <http://dojofoundation.org/> Copyright 2012-2015 The Dojo Foundation <http://dojofoundation.org/>
Based on Underscore.js 1.7.0, copyright 2009-2015 Jeremy Ashkenas, Based on Underscore.js, copyright 2009-2015 Jeremy Ashkenas,
DocumentCloud and Investigative Reporters & Editors <http://underscorejs.org/> DocumentCloud and Investigative Reporters & Editors <http://underscorejs.org/>
Permission is hereby granted, free of charge, to any person obtaining Permission is hereby granted, free of charge, to any person obtaining

View File

@@ -1,4 +1,4 @@
# lodash v3.2.0 # lodash v3.9.0
The [modern build](https://github.com/lodash/lodash/wiki/Build-Differences) of [lodash](https://lodash.com/) exported as [Node.js](http://nodejs.org/)/[io.js](https://iojs.org/) modules. The [modern build](https://github.com/lodash/lodash/wiki/Build-Differences) of [lodash](https://lodash.com/) exported as [Node.js](http://nodejs.org/)/[io.js](https://iojs.org/) modules.
@@ -24,11 +24,11 @@ In Node.js/io.js:
var _ = require('lodash'); var _ = require('lodash');
// or a method category // or a method category
var array = require('lodash/array'); var array = require('lodash/array');
// or a method // or a method (great for smaller builds with browserify/webpack)
var chunk = require('lodash/array/chunk'); var chunk = require('lodash/array/chunk');
``` ```
See the [package source](https://github.com/lodash/lodash/tree/3.2.0-npm) for more details. See the [package source](https://github.com/lodash/lodash/tree/3.9.0-npm) for more details.
**Note:**<br> **Note:**<br>
Dont assign values to the [special variable](http://nodejs.org/api/repl.html#repl_repl_features) `_` when in the REPL.<br> Dont assign values to the [special variable](http://nodejs.org/api/repl.html#repl_repl_features) `_` when in the REPL.<br>
@@ -39,8 +39,8 @@ Install [n_](https://www.npmjs.com/package/n_) for a REPL that includes lodash b
lodash is also available in a variety of other builds & module formats. lodash is also available in a variety of other builds & module formats.
* npm packages for [modern](https://www.npmjs.com/package/lodash), [compatibility](https://www.npmjs.com/package/lodash-compat), & [per method](https://www.npmjs.com/browse/keyword/lodash-modularized) builds * npm packages for [modern](https://www.npmjs.com/package/lodash), [compatibility](https://www.npmjs.com/package/lodash-compat), & [per method](https://www.npmjs.com/browse/keyword/lodash-modularized) builds
* AMD modules for [modern](https://github.com/lodash/lodash/tree/3.2.0-amd) & [compatibility](https://github.com/lodash/lodash-compat/tree/3.2.0-amd) builds * AMD modules for [modern](https://github.com/lodash/lodash/tree/3.9.0-amd) & [compatibility](https://github.com/lodash/lodash-compat/tree/3.9.0-amd) builds
* ES modules for the [modern](https://github.com/lodash/lodash/tree/3.2.0-es) build * ES modules for the [modern](https://github.com/lodash/lodash/tree/3.9.0-es) build
## Further Reading ## Further Reading
@@ -51,12 +51,13 @@ lodash is also available in a variety of other builds & module formats.
* [Roadmap](https://github.com/lodash/lodash/wiki/Roadmap) * [Roadmap](https://github.com/lodash/lodash/wiki/Roadmap)
* [More Resources](https://github.com/lodash/lodash/wiki/Resources) * [More Resources](https://github.com/lodash/lodash/wiki/Resources)
## Features *not* in Underscore ## Features
* ~100% [code coverage](https://coveralls.io/r/lodash) * ~100% [code coverage](https://coveralls.io/r/lodash)
* Follows [semantic versioning](http://semver.org/) for releases * Follows [semantic versioning](http://semver.org/) for releases
* [Lazily evaluated](http://filimanjaro.com/blog/2014/introducing-lazy-evaluation/) chaining * [Lazily evaluated](http://filimanjaro.com/blog/2014/introducing-lazy-evaluation/) chaining
* [_(…)](https://lodash.com/docs#_) supports intuitive chaining * [_(…)](https://lodash.com/docs#_) supports intuitive chaining
* [_.add](https://lodash.com/docs#add) for mathematical composition
* [_.ary](https://lodash.com/docs#ary) & [_.rearg](https://lodash.com/docs#rearg) to change function argument limits & order * [_.ary](https://lodash.com/docs#ary) & [_.rearg](https://lodash.com/docs#rearg) to change function argument limits & order
* [_.at](https://lodash.com/docs#at) for cherry-picking collection values * [_.at](https://lodash.com/docs#at) for cherry-picking collection values
* [_.attempt](https://lodash.com/docs#attempt) to execute functions which may error without a try-catch * [_.attempt](https://lodash.com/docs#attempt) to execute functions which may error without a try-catch
@@ -65,54 +66,55 @@ lodash is also available in a variety of other builds & module formats.
* [_.chunk](https://lodash.com/docs#chunk) for splitting an array into chunks of a given size * [_.chunk](https://lodash.com/docs#chunk) for splitting an array into chunks of a given size
* [_.clone](https://lodash.com/docs#clone) supports shallow cloning of `Date` & `RegExp` objects * [_.clone](https://lodash.com/docs#clone) supports shallow cloning of `Date` & `RegExp` objects
* [_.cloneDeep](https://lodash.com/docs#cloneDeep) for deep cloning arrays & objects * [_.cloneDeep](https://lodash.com/docs#cloneDeep) for deep cloning arrays & objects
* [_.create](https://lodash.com/docs#create) for easier object inheritance
* [_.curry](https://lodash.com/docs#curry) & [_.curryRight](https://lodash.com/docs#curryRight) for creating [curried](http://hughfdjackson.com/javascript/why-curry-helps/) functions * [_.curry](https://lodash.com/docs#curry) & [_.curryRight](https://lodash.com/docs#curryRight) for creating [curried](http://hughfdjackson.com/javascript/why-curry-helps/) functions
* [_.debounce](https://lodash.com/docs#debounce) & [_.throttle](https://lodash.com/docs#throttle) are cancelable & accept options for more control * [_.debounce](https://lodash.com/docs#debounce) & [_.throttle](https://lodash.com/docs#throttle) are cancelable & accept options for more control
* [_.fill](https://lodash.com/docs#fill) to fill arrays with values * [_.fill](https://lodash.com/docs#fill) to fill arrays with values
* [_.findIndex](https://lodash.com/docs#findIndex) & [_.findKey](https://lodash.com/docs#findKey) for finding indexes & keys * [_.findKey](https://lodash.com/docs#findKey) for finding keys
* [_.flow](https://lodash.com/docs#flow) to complement [_.flowRight](https://lodash.com/docs#flowRight) (a.k.a `_.compose`) * [_.flow](https://lodash.com/docs#flow) to complement [_.flowRight](https://lodash.com/docs#flowRight) (a.k.a `_.compose`)
* [_.forEach](https://lodash.com/docs#forEach) supports exiting early * [_.forEach](https://lodash.com/docs#forEach) supports exiting early
* [_.forIn](https://lodash.com/docs#forIn) for iterating all enumerable properties * [_.forIn](https://lodash.com/docs#forIn) for iterating all enumerable properties
* [_.forOwn](https://lodash.com/docs#forOwn) for iterating own properties * [_.forOwn](https://lodash.com/docs#forOwn) for iterating own properties
* [_.includes](https://lodash.com/docs#includes) accepts a `fromIndex` * [_.get](https://lodash.com/docs#get) & [_.set](https://lodash.com/docs#set) for deep property getting & setting
* [_.isError](https://lodash.com/docs#isError) to check for error objects * [_.gt](https://lodash.com/docs#gt), [_.gte](https://lodash.com/docs#gte), [_.lt](https://lodash.com/docs#lt), & [_.lte](https://lodash.com/docs#lte) relational methods
* [_.inRange](https://lodash.com/docs#inRange) for checking whether a number is within a given range
* [_.isNative](https://lodash.com/docs#isNative) to check for native functions * [_.isNative](https://lodash.com/docs#isNative) to check for native functions
* [_.isPlainObject](https://lodash.com/docs#isPlainObject) & [_.toPlainObject](https://lodash.com/docs#toPlainObject) to check for & convert to `Object` objects * [_.isPlainObject](https://lodash.com/docs#isPlainObject) & [_.toPlainObject](https://lodash.com/docs#toPlainObject) to check for & convert to `Object` objects
* [_.isTypedArray](https://lodash.com/docs#isTypedArray) to check for typed arrays * [_.isTypedArray](https://lodash.com/docs#isTypedArray) to check for typed arrays
* [_.keysIn](https://lodash.com/docs#keysIn) & [_.valuesIn](https://lodash.com/docs#valuesIn) for getting keys & values of all enumerable properties * [_.mapKeys](https://lodash.com/docs#mapKeys) for mapping keys to an object
* [_.mapValues](https://lodash.com/docs#mapValues) for [mapping](https://lodash.com/docs#map) values to an object
* [_.matches](https://lodash.com/docs#matches) supports deep object comparisons * [_.matches](https://lodash.com/docs#matches) supports deep object comparisons
* [_.matchesProperty](https://lodash.com/docs#matchesProperty) to complement [_.matches](https://lodash.com/docs#matches) & [_.property](https://lodash.com/docs#property) * [_.matchesProperty](https://lodash.com/docs#matchesProperty) to complement [_.matches](https://lodash.com/docs#matches) & [_.property](https://lodash.com/docs#property)
* [_.method](https://lodash.com/docs#method) & [_.methodOf](https://lodash.com/docs#methodOf) to create functions that invoke methods
* [_.merge](https://lodash.com/docs#merge) for a deep [_.extend](https://lodash.com/docs#extend) * [_.merge](https://lodash.com/docs#merge) for a deep [_.extend](https://lodash.com/docs#extend)
* [_.parseInt](https://lodash.com/docs#parseInt) for consistent cross-environment behavior * [_.parseInt](https://lodash.com/docs#parseInt) for consistent cross-environment behavior
* [_.propertyOf](https://lodash.com/docs#propertyOf) to complement [_.property](https://lodash.com/docs#property)
* [_.pull](https://lodash.com/docs#pull), [_.pullAt](https://lodash.com/docs#pullAt), & [_.remove](https://lodash.com/docs#remove) for mutating arrays * [_.pull](https://lodash.com/docs#pull), [_.pullAt](https://lodash.com/docs#pullAt), & [_.remove](https://lodash.com/docs#remove) for mutating arrays
* [_.random](https://lodash.com/docs#random) supports returning floating-point numbers * [_.random](https://lodash.com/docs#random) supports returning floating-point numbers
* [_.restParam](https://lodash.com/docs#restParam) & [_.spread](https://lodash.com/docs#spread) for applying rest parameters & spreading arguments to functions
* [_.runInContext](https://lodash.com/docs#runInContext) for collisionless mixins & easier mocking * [_.runInContext](https://lodash.com/docs#runInContext) for collisionless mixins & easier mocking
* [_.slice](https://lodash.com/docs#slice) for creating subsets of array-like values * [_.slice](https://lodash.com/docs#slice) for creating subsets of array-like values
* [_.sortByAll](https://lodash.com/docs#sortBy) for sorting by multiple properties * [_.sortByAll](https://lodash.com/docs#sortByAll) & [_.sortByOrder](https://lodash.com/docs#sortByOrder) for sorting by multiple properties & orders
* [_.spread](https://lodash.com/docs#spread) for creating a function to spread an array of arguments to another * [_.sum](https://lodash.com/docs#sum) to get the sum of values
* [_.support](https://lodash.com/docs#support) for flagging environment features * [_.support](https://lodash.com/docs#support) for flagging environment features
* [_.template](https://lodash.com/docs#template) supports [*“imports”*](https://lodash.com/docs#templateSettings-imports) options & [ES template delimiters](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-template-literal-lexical-components) * [_.template](https://lodash.com/docs#template) supports [*“imports”*](https://lodash.com/docs#templateSettings-imports) options & [ES template delimiters](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-template-literal-lexical-components)
* [_.transform](https://lodash.com/docs#transform) as a powerful alternative to [_.reduce](https://lodash.com/docs#reduce) for transforming objects * [_.transform](https://lodash.com/docs#transform) as a powerful alternative to [_.reduce](https://lodash.com/docs#reduce) for transforming objects
* [_.unzipWith](https://lodash.com/docs#unzipWith) & [_.zipWith](https://lodash.com/docs#zipWith) to specify how grouped values should be combined
* [_.xor](https://lodash.com/docs#xor) to complement [_.difference](https://lodash.com/docs#difference), [_.intersection](https://lodash.com/docs#intersection), & [_.union](https://lodash.com/docs#union) * [_.xor](https://lodash.com/docs#xor) to complement [_.difference](https://lodash.com/docs#difference), [_.intersection](https://lodash.com/docs#intersection), & [_.union](https://lodash.com/docs#union)
* [_.valuesIn](https://lodash.com/docs#valuesIn) for getting values of all enumerable properties
* [_.bind](https://lodash.com/docs#bind), [_.curry](https://lodash.com/docs#curry), [_.partial](https://lodash.com/docs#partial), & * [_.bind](https://lodash.com/docs#bind), [_.curry](https://lodash.com/docs#curry), [_.partial](https://lodash.com/docs#partial), &
[more](https://lodash.com/docs "_.bindKey, _.curryRight, _.partialRight") support customizable argument placeholders [more](https://lodash.com/docs "_.bindKey, _.curryRight, _.partialRight") support customizable argument placeholders
* [_.capitalize](https://lodash.com/docs#capitalize), [_.trim](https://lodash.com/docs#trim), & * [_.capitalize](https://lodash.com/docs#capitalize), [_.trim](https://lodash.com/docs#trim), &
[more](https://lodash.com/docs "_.camelCase, _.deburr, _.endsWith, _.escapeRegExp, _.kebabCase, _.pad, _.padLeft, _.padRight, _.repeat, _.snakeCase, _.startCase, _.startsWith, _.trimLeft, _.trimRight, _.trunc, _.words") string methods [more](https://lodash.com/docs "_.camelCase, _.deburr, _.endsWith, _.escapeRegExp, _.kebabCase, _.pad, _.padLeft, _.padRight, _.repeat, _.snakeCase, _.startCase, _.startsWith, _.trimLeft, _.trimRight, _.trunc, _.words") string methods
* [_.clone](https://lodash.com/docs#clone), [_.isEqual](https://lodash.com/docs#isEqual), & * [_.clone](https://lodash.com/docs#clone), [_.isEqual](https://lodash.com/docs#isEqual), &
[more](https://lodash.com/docs "_.assign, _.cloneDeep, _.merge") accept callbacks [more](https://lodash.com/docs "_.assign, _.cloneDeep, _.merge") accept customizer callbacks
* [_.dropWhile](https://lodash.com/docs#dropWhile), [_.takeWhile](https://lodash.com/docs#takeWhile), & * [_.dropWhile](https://lodash.com/docs#dropWhile), [_.takeWhile](https://lodash.com/docs#takeWhile), &
[more](https://lodash.com/docs "_.drop, _.dropRightWhile, _.take, _.takeRightWhile") to complement [_.first](https://lodash.com/docs#first), [_.initial](https://lodash.com/docs#initial), [_.last](https://lodash.com/docs#last), & [_.rest](https://lodash.com/docs#rest) [more](https://lodash.com/docs "_.drop, _.dropRight, _.dropRightWhile, _.take, _.takeRight, _.takeRightWhile") to complement [_.first](https://lodash.com/docs#first), [_.initial](https://lodash.com/docs#initial), [_.last](https://lodash.com/docs#last), & [_.rest](https://lodash.com/docs#rest)
* [_.findLast](https://lodash.com/docs#findLast), [_.findLastIndex](https://lodash.com/docs#findLastIndex), & * [_.findLast](https://lodash.com/docs#findLast), [_.findLastKey](https://lodash.com/docs#findLastKey), &
[more](https://lodash.com/docs "_.findLastKey, _.flowRight, _.forEachRight, _.forInRight, _.forOwnRight, _.partialRight") right-associative methods [more](https://lodash.com/docs "_.curryRight, _.dropRight, _.dropRightWhile, _.flowRight, _.forEachRight, _.forInRight, _.forOwnRight, _.padRight, partialRight, _.takeRight, _.trimRight, _.takeRightWhile") right-associative methods
* [_.includes](https://lodash.com/docs#includes), [_.toArray](https://lodash.com/docs#toArray), & * [_.includes](https://lodash.com/docs#includes), [_.toArray](https://lodash.com/docs#toArray), &
[more](https://lodash.com/docs "_.at, _.countBy, _.every, _.filter, _.find, _.findLast, _.forEach, _.forEachRight, _.groupBy, _.indexBy, _.invoke, _.map, _.max, _.min, _.partition, _.pluck, _.reduce, _.reduceRight, _.reject, _.shuffle, _.size, _.some, _.sortBy") accept strings [more](https://lodash.com/docs "_.at, _.countBy, _.every, _.filter, _.find, _.findLast, _.findWhere, _.forEach, _.forEachRight, _.groupBy, _.indexBy, _.invoke, _.map, _.max, _.min, _.partition, _.pluck, _.reduce, _.reduceRight, _.reject, _.shuffle, _.size, _.some, _.sortBy, _.sortByAll, _.sortByOrder, _.sum, _.where") accept strings
* [_#commit](https://lodash.com/docs#prototype-commit) & [_#plant](https://lodash.com/docs#prototype-plant) for working with chain sequences * [_#commit](https://lodash.com/docs#prototype-commit) & [_#plant](https://lodash.com/docs#prototype-plant) for working with chain sequences
* [_#thru](https://lodash.com/docs#thru) to pass values thru a chain sequence * [_#thru](https://lodash.com/docs#thru) to pass values thru a chain sequence
## Support ## Support
Tested in Chrome 39-40, Firefox 34-35, IE 6-11, Opera 26-27, Safari 5-8, io.js 1.2.0, Node.js 0.8.28, 0.10.36, & 0.12.0, PhantomJS 1.9.8, RingoJS 0.11, & Rhino 1.7RC5. Tested in Chrome 41-42, Firefox 37-38, IE 6-11, MS Edge, Opera 28-29, Safari 5-8, ChakraNode 0.12.2, io.js 2.0.2, Node.js 0.8.28, 0.10.38, & 0.12.3, PhantomJS 1.9.8, RingoJS 0.11, & Rhino 1.7.6
Automated [browser](https://saucelabs.com/u/lodash) & [CI](https://travis-ci.org/lodash/lodash/) test runs are available. Special thanks to [Sauce Labs](https://saucelabs.com/) for providing automated browser testing. Automated [browser](https://saucelabs.com/u/lodash) & [CI](https://travis-ci.org/lodash/lodash/) test runs are available. Special thanks to [Sauce Labs](https://saucelabs.com/) for providing automated browser testing.

View File

@@ -35,8 +35,10 @@ module.exports = {
'uniq': require('./array/uniq'), 'uniq': require('./array/uniq'),
'unique': require('./array/unique'), 'unique': require('./array/unique'),
'unzip': require('./array/unzip'), 'unzip': require('./array/unzip'),
'unzipWith': require('./array/unzipWith'),
'without': require('./array/without'), 'without': require('./array/without'),
'xor': require('./array/xor'), 'xor': require('./array/xor'),
'zip': require('./array/zip'), 'zip': require('./array/zip'),
'zipObject': require('./array/zipObject') 'zipObject': require('./array/zipObject'),
'zipWith': require('./array/zipWith')
}; };

View File

@@ -1,16 +1,12 @@
var baseDifference = require('../internal/baseDifference'), var baseDifference = require('../internal/baseDifference'),
baseFlatten = require('../internal/baseFlatten'), baseFlatten = require('../internal/baseFlatten'),
isArguments = require('../lang/isArguments'), isArrayLike = require('../internal/isArrayLike'),
isArray = require('../lang/isArray'); restParam = require('../function/restParam');
/** /**
* Creates an array excluding all values of the provided arrays using * Creates an array excluding all values of the provided arrays using
* `SameValueZero` for equality comparisons. * [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-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 * @static
* @memberOf _ * @memberOf _
@@ -20,20 +16,13 @@ var baseDifference = require('../internal/baseDifference'),
* @returns {Array} Returns the new array of filtered values. * @returns {Array} Returns the new array of filtered values.
* @example * @example
* *
* _.difference([1, 2, 3], [5, 2, 10]); * _.difference([1, 2, 3], [4, 2]);
* // => [1, 3] * // => [1, 3]
*/ */
function difference() { var difference = restParam(function(array, values) {
var index = -1, return isArrayLike(array)
length = arguments.length; ? baseDifference(array, baseFlatten(values, false, true))
: [];
while (++index < length) { });
var value = arguments[index];
if (isArray(value) || isArguments(value)) {
break;
}
}
return baseDifference(value, baseFlatten(arguments, false, true, ++index));
}
module.exports = difference; module.exports = difference;

View File

@@ -1,10 +1,10 @@
var baseCallback = require('../internal/baseCallback'), var baseCallback = require('../internal/baseCallback'),
baseSlice = require('../internal/baseSlice'); baseWhile = require('../internal/baseWhile');
/** /**
* Creates a slice of `array` excluding elements dropped from the end. * Creates a slice of `array` excluding elements dropped from the end.
* Elements are dropped until `predicate` returns falsey. The predicate is * Elements are dropped until `predicate` returns falsey. The predicate is
* bound to `thisArg` and invoked with three arguments; (value, index, array). * bound to `thisArg` and invoked with three arguments: (value, index, array).
* *
* If a property name is provided for `predicate` the created `_.property` * If a property name is provided for `predicate` the created `_.property`
* style callback returns the property value of the given element. * style callback returns the property value of the given element.
@@ -27,7 +27,9 @@ var baseCallback = require('../internal/baseCallback'),
* @returns {Array} Returns the slice of `array`. * @returns {Array} Returns the slice of `array`.
* @example * @example
* *
* _.dropRightWhile([1, 2, 3], function(n) { return n > 1; }); * _.dropRightWhile([1, 2, 3], function(n) {
* return n > 1;
* });
* // => [1] * // => [1]
* *
* var users = [ * var users = [
@@ -37,7 +39,7 @@ var baseCallback = require('../internal/baseCallback'),
* ]; * ];
* *
* // using the `_.matches` callback shorthand * // using the `_.matches` callback shorthand
* _.pluck(_.dropRightWhile(users, { 'user': pebbles, 'active': false }), 'user'); * _.pluck(_.dropRightWhile(users, { 'user': 'pebbles', 'active': false }), 'user');
* // => ['barney', 'fred'] * // => ['barney', 'fred']
* *
* // using the `_.matchesProperty` callback shorthand * // using the `_.matchesProperty` callback shorthand
@@ -49,13 +51,9 @@ var baseCallback = require('../internal/baseCallback'),
* // => ['barney', 'fred', 'pebbles'] * // => ['barney', 'fred', 'pebbles']
*/ */
function dropRightWhile(array, predicate, thisArg) { function dropRightWhile(array, predicate, thisArg) {
var length = array ? array.length : 0; return (array && array.length)
if (!length) { ? baseWhile(array, baseCallback(predicate, thisArg, 3), true, true)
return []; : [];
}
predicate = baseCallback(predicate, thisArg, 3);
while (length-- && predicate(array[length], length, array)) {}
return baseSlice(array, 0, length + 1);
} }
module.exports = dropRightWhile; module.exports = dropRightWhile;

View File

@@ -1,10 +1,10 @@
var baseCallback = require('../internal/baseCallback'), var baseCallback = require('../internal/baseCallback'),
baseSlice = require('../internal/baseSlice'); baseWhile = require('../internal/baseWhile');
/** /**
* Creates a slice of `array` excluding elements dropped from the beginning. * Creates a slice of `array` excluding elements dropped from the beginning.
* Elements are dropped until `predicate` returns falsey. The predicate is * Elements are dropped until `predicate` returns falsey. The predicate is
* bound to `thisArg` and invoked with three arguments; (value, index, array). * bound to `thisArg` and invoked with three arguments: (value, index, array).
* *
* If a property name is provided for `predicate` the created `_.property` * If a property name is provided for `predicate` the created `_.property`
* style callback returns the property value of the given element. * style callback returns the property value of the given element.
@@ -27,7 +27,9 @@ var baseCallback = require('../internal/baseCallback'),
* @returns {Array} Returns the slice of `array`. * @returns {Array} Returns the slice of `array`.
* @example * @example
* *
* _.dropWhile([1, 2, 3], function(n) { return n < 3; }); * _.dropWhile([1, 2, 3], function(n) {
* return n < 3;
* });
* // => [3] * // => [3]
* *
* var users = [ * var users = [
@@ -49,14 +51,9 @@ var baseCallback = require('../internal/baseCallback'),
* // => ['barney', 'fred', 'pebbles'] * // => ['barney', 'fred', 'pebbles']
*/ */
function dropWhile(array, predicate, thisArg) { function dropWhile(array, predicate, thisArg) {
var length = array ? array.length : 0; return (array && array.length)
if (!length) { ? baseWhile(array, baseCallback(predicate, thisArg, 3), true)
return []; : [];
}
var index = -1;
predicate = baseCallback(predicate, thisArg, 3);
while (++index < length && predicate(array[index], index, array)) {}
return baseSlice(array, index);
} }
module.exports = dropWhile; module.exports = dropWhile;

View File

@@ -15,6 +15,19 @@ var baseFill = require('../internal/baseFill'),
* @param {number} [start=0] The start position. * @param {number} [start=0] The start position.
* @param {number} [end=array.length] The end position. * @param {number} [end=array.length] The end position.
* @returns {Array} Returns `array`. * @returns {Array} Returns `array`.
* @example
*
* var array = [1, 2, 3];
*
* _.fill(array, 'a');
* console.log(array);
* // => ['a', 'a', 'a']
*
* _.fill(Array(3), 2);
* // => [2, 2, 2]
*
* _.fill([4, 6, 8], '*', 1, 2);
* // => [4, '*', 8]
*/ */
function fill(array, value, start, end) { function fill(array, value, start, end) {
var length = array ? array.length : 0; var length = array ? array.length : 0;

View File

@@ -1,8 +1,8 @@
var baseCallback = require('../internal/baseCallback'); var createFindIndex = require('../internal/createFindIndex');
/** /**
* This method is like `_.find` except that it returns the index of the first * This method is like `_.find` except that it returns the index of the first
* element `predicate` returns truthy for, instead of the element itself. * element `predicate` returns truthy for instead of the element itself.
* *
* If a property name is provided for `predicate` the created `_.property` * If a property name is provided for `predicate` the created `_.property`
* style callback returns the property value of the given element. * style callback returns the property value of the given element.
@@ -31,7 +31,9 @@ var baseCallback = require('../internal/baseCallback');
* { 'user': 'pebbles', 'active': true } * { 'user': 'pebbles', 'active': true }
* ]; * ];
* *
* _.findIndex(users, function(chr) { return chr.user == 'barney'; }); * _.findIndex(users, function(chr) {
* return chr.user == 'barney';
* });
* // => 0 * // => 0
* *
* // using the `_.matches` callback shorthand * // using the `_.matches` callback shorthand
@@ -46,17 +48,6 @@ var baseCallback = require('../internal/baseCallback');
* _.findIndex(users, 'active'); * _.findIndex(users, 'active');
* // => 2 * // => 2
*/ */
function findIndex(array, predicate, thisArg) { var findIndex = createFindIndex();
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;
}
module.exports = findIndex; module.exports = findIndex;

View File

@@ -1,4 +1,4 @@
var baseCallback = require('../internal/baseCallback'); var createFindIndex = require('../internal/createFindIndex');
/** /**
* This method is like `_.findIndex` except that it iterates over elements * This method is like `_.findIndex` except that it iterates over elements
@@ -31,30 +31,23 @@ var baseCallback = require('../internal/baseCallback');
* { 'user': 'pebbles', 'active': false } * { 'user': 'pebbles', 'active': false }
* ]; * ];
* *
* _.findLastIndex(users, function(chr) { return chr.user == 'pebbles'; }); * _.findLastIndex(users, function(chr) {
* return chr.user == 'pebbles';
* });
* // => 2 * // => 2
* *
* // using the `_.matches` callback shorthand * // using the `_.matches` callback shorthand
* _.findLastIndex(users, { user': 'barney', 'active': true }); * _.findLastIndex(users, { 'user': 'barney', 'active': true });
* // => 0 * // => 0
* *
* // using the `_.matchesProperty` callback shorthand * // using the `_.matchesProperty` callback shorthand
* _.findLastIndex(users, 'active', false); * _.findLastIndex(users, 'active', false);
* // => 1 * // => 2
* *
* // using the `_.property` callback shorthand * // using the `_.property` callback shorthand
* _.findLastIndex(users, 'active'); * _.findLastIndex(users, 'active');
* // => 0 * // => 0
*/ */
function findLastIndex(array, predicate, thisArg) { var findLastIndex = createFindIndex(true);
var length = array ? array.length : 0;
predicate = baseCallback(predicate, thisArg, 3);
while (length--) {
if (predicate(array[length], length, array)) {
return length;
}
}
return -1;
}
module.exports = findLastIndex; module.exports = findLastIndex;

View File

@@ -14,12 +14,12 @@ var baseFlatten = require('../internal/baseFlatten'),
* @returns {Array} Returns the new flattened array. * @returns {Array} Returns the new flattened array.
* @example * @example
* *
* _.flatten([1, [2], [3, [[4]]]]); * _.flatten([1, [2, 3, [4]]]);
* // => [1, 2, 3, [[4]]]; * // => [1, 2, 3, [4]]
* *
* // using `isDeep` * // using `isDeep`
* _.flatten([1, [2], [3, [[4]]]], true); * _.flatten([1, [2, 3, [4]]], true);
* // => [1, 2, 3, 4]; * // => [1, 2, 3, 4]
*/ */
function flatten(array, isDeep, guard) { function flatten(array, isDeep, guard) {
var length = array ? array.length : 0; var length = array ? array.length : 0;

View File

@@ -10,8 +10,8 @@ var baseFlatten = require('../internal/baseFlatten');
* @returns {Array} Returns the new flattened array. * @returns {Array} Returns the new flattened array.
* @example * @example
* *
* _.flattenDeep([1, [2], [3, [[4]]]]); * _.flattenDeep([1, [2, 3, [4]]]);
* // => [1, 2, 3, 4]; * // => [1, 2, 3, 4]
*/ */
function flattenDeep(array) { function flattenDeep(array) {
var length = array ? array.length : 0; var length = array ? array.length : 0;

View File

@@ -6,14 +6,10 @@ var nativeMax = Math.max;
/** /**
* Gets the index at which the first occurrence of `value` is found in `array` * Gets the index at which the first occurrence of `value` is found in `array`
* using `SameValueZero` for equality comparisons. If `fromIndex` is negative, * using [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
* it is used as the offset from the end of `array`. If `array` is sorted * for equality comparisons. If `fromIndex` is negative, it is used as the offset
* providing `true` for `fromIndex` performs a faster binary search. * 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 * @static
* @memberOf _ * @memberOf _
@@ -25,15 +21,15 @@ var nativeMax = Math.max;
* @returns {number} Returns the index of the matched value, else `-1`. * @returns {number} Returns the index of the matched value, else `-1`.
* @example * @example
* *
* _.indexOf([1, 2, 3, 1, 2, 3], 2); * _.indexOf([1, 2, 1, 2], 2);
* // => 1 * // => 1
* *
* // using `fromIndex` * // using `fromIndex`
* _.indexOf([1, 2, 3, 1, 2, 3], 2, 3); * _.indexOf([1, 2, 1, 2], 2, 2);
* // => 4 * // => 3
* *
* // performing a binary search * // performing a binary search
* _.indexOf([4, 4, 5, 5, 6, 6], 5, true); * _.indexOf([1, 1, 2, 2], 2, true);
* // => 2 * // => 2
*/ */
function indexOf(array, value, fromIndex) { function indexOf(array, value, fromIndex) {
@@ -42,14 +38,17 @@ function indexOf(array, value, fromIndex) {
return -1; return -1;
} }
if (typeof fromIndex == 'number') { if (typeof fromIndex == 'number') {
fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0); fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : fromIndex;
} else if (fromIndex) { } else if (fromIndex) {
var index = binaryIndex(array, value), var index = binaryIndex(array, value),
other = array[index]; other = array[index];
return (value === value ? value === other : other !== other) ? index : -1; if (value === value ? (value === other) : (other !== other)) {
return index;
}
return -1;
} }
return baseIndexOf(array, value, fromIndex); return baseIndexOf(array, value, fromIndex || 0);
} }
module.exports = indexOf; module.exports = indexOf;

View File

@@ -1,58 +1,48 @@
var baseIndexOf = require('../internal/baseIndexOf'), var baseIndexOf = require('../internal/baseIndexOf'),
cacheIndexOf = require('../internal/cacheIndexOf'), cacheIndexOf = require('../internal/cacheIndexOf'),
createCache = require('../internal/createCache'), createCache = require('../internal/createCache'),
isArguments = require('../lang/isArguments'), isArrayLike = require('../internal/isArrayLike'),
isArray = require('../lang/isArray'); restParam = require('../function/restParam');
/** /**
* Creates an array of unique values in all provided arrays using `SameValueZero` * Creates an array of unique values in all provided arrays using
* [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
* for equality comparisons. * 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 * @static
* @memberOf _ * @memberOf _
* @category Array * @category Array
* @param {...Array} [arrays] The arrays to inspect. * @param {...Array} [arrays] The arrays to inspect.
* @returns {Array} Returns the new array of shared values. * @returns {Array} Returns the new array of shared values.
* @example * @example
* * _.intersection([1, 2], [4, 2], [2, 1]);
* _.intersection([1, 2, 3], [5, 2, 1, 4], [2, 1]); * // => [2]
* // => [1, 2]
*/ */
function intersection() { var intersection = restParam(function(arrays) {
var args = [], var othLength = arrays.length,
argsIndex = -1, othIndex = othLength,
argsLength = arguments.length, caches = Array(length),
caches = [],
indexOf = baseIndexOf, indexOf = baseIndexOf,
isCommon = true; isCommon = true,
result = [];
while (++argsIndex < argsLength) { while (othIndex--) {
var value = arguments[argsIndex]; var value = arrays[othIndex] = isArrayLike(value = arrays[othIndex]) ? value : [];
if (isArray(value) || isArguments(value)) { caches[othIndex] = (isCommon && value.length >= 120) ? createCache(othIndex && value) : null;
args.push(value);
caches.push(isCommon && value.length >= 120 && createCache(argsIndex && value));
}
} }
argsLength = args.length; var array = arrays[0],
var array = args[0],
index = -1, index = -1,
length = array ? array.length : 0, length = array ? array.length : 0,
result = [],
seen = caches[0]; seen = caches[0];
outer: outer:
while (++index < length) { while (++index < length) {
value = array[index]; value = array[index];
if ((seen ? cacheIndexOf(seen, value) : indexOf(result, value)) < 0) { if ((seen ? cacheIndexOf(seen, value) : indexOf(result, value, 0)) < 0) {
argsIndex = argsLength; var othIndex = othLength;
while (--argsIndex) { while (--othIndex) {
var cache = caches[argsIndex]; var cache = caches[othIndex];
if ((cache ? cacheIndexOf(cache, value) : indexOf(args[argsIndex], value)) < 0) { if ((cache ? cacheIndexOf(cache, value) : indexOf(arrays[othIndex], value, 0)) < 0) {
continue outer; continue outer;
} }
} }
@@ -63,6 +53,6 @@ function intersection() {
} }
} }
return result; return result;
} });
module.exports = intersection; module.exports = intersection;

View File

@@ -19,15 +19,15 @@ var nativeMax = Math.max,
* @returns {number} Returns the index of the matched value, else `-1`. * @returns {number} Returns the index of the matched value, else `-1`.
* @example * @example
* *
* _.lastIndexOf([1, 2, 3, 1, 2, 3], 2); * _.lastIndexOf([1, 2, 1, 2], 2);
* // => 4 * // => 3
* *
* // using `fromIndex` * // using `fromIndex`
* _.lastIndexOf([1, 2, 3, 1, 2, 3], 2, 3); * _.lastIndexOf([1, 2, 1, 2], 2, 2);
* // => 1 * // => 1
* *
* // performing a binary search * // performing a binary search
* _.lastIndexOf([4, 4, 5, 5, 6, 6], 5, true); * _.lastIndexOf([1, 1, 2, 2], 2, true);
* // => 3 * // => 3
*/ */
function lastIndexOf(array, value, fromIndex) { function lastIndexOf(array, value, fromIndex) {
@@ -41,7 +41,10 @@ function lastIndexOf(array, value, fromIndex) {
} else if (fromIndex) { } else if (fromIndex) {
index = binaryIndex(array, value, true) - 1; index = binaryIndex(array, value, true) - 1;
var other = array[index]; var other = array[index];
return (value === value ? value === other : other !== other) ? index : -1; if (value === value ? (value === other) : (other !== other)) {
return index;
}
return -1;
} }
if (value !== value) { if (value !== value) {
return indexOfNaN(array, index, true); return indexOfNaN(array, index, true);

View File

@@ -7,14 +7,11 @@ var arrayProto = Array.prototype;
var splice = arrayProto.splice; var splice = arrayProto.splice;
/** /**
* Removes all provided values from `array` using `SameValueZero` for equality * Removes all provided values from `array` using
* comparisons. * [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
* for equality comparisons.
* *
* **Notes:** * **Note:** Unlike `_.without`, this method mutates `array`.
* - 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 * @static
* @memberOf _ * @memberOf _
@@ -25,22 +22,25 @@ var splice = arrayProto.splice;
* @example * @example
* *
* var array = [1, 2, 3, 1, 2, 3]; * var array = [1, 2, 3, 1, 2, 3];
*
* _.pull(array, 2, 3); * _.pull(array, 2, 3);
* console.log(array); * console.log(array);
* // => [1, 1] * // => [1, 1]
*/ */
function pull() { function pull() {
var array = arguments[0]; var args = arguments,
array = args[0];
if (!(array && array.length)) { if (!(array && array.length)) {
return array; return array;
} }
var index = 0, var index = 0,
indexOf = baseIndexOf, indexOf = baseIndexOf,
length = arguments.length; length = args.length;
while (++index < length) { while (++index < length) {
var fromIndex = 0, var fromIndex = 0,
value = arguments[index]; value = args[index];
while ((fromIndex = indexOf(array, value, fromIndex)) > -1) { while ((fromIndex = indexOf(array, value, fromIndex)) > -1) {
splice.call(array, fromIndex, 1); splice.call(array, fromIndex, 1);

View File

@@ -1,5 +1,8 @@
var baseFlatten = require('../internal/baseFlatten'), var baseAt = require('../internal/baseAt'),
basePullAt = require('../internal/basePullAt'); baseCompareAscending = require('../internal/baseCompareAscending'),
baseFlatten = require('../internal/baseFlatten'),
basePullAt = require('../internal/basePullAt'),
restParam = require('../function/restParam');
/** /**
* Removes elements from `array` corresponding to the given indexes and returns * Removes elements from `array` corresponding to the given indexes and returns
@@ -18,7 +21,7 @@ var baseFlatten = require('../internal/baseFlatten'),
* @example * @example
* *
* var array = [5, 10, 15, 20]; * var array = [5, 10, 15, 20];
* var evens = _.pullAt(array, [1, 3]); * var evens = _.pullAt(array, 1, 3);
* *
* console.log(array); * console.log(array);
* // => [5, 15] * // => [5, 15]
@@ -26,8 +29,12 @@ var baseFlatten = require('../internal/baseFlatten'),
* console.log(evens); * console.log(evens);
* // => [10, 20] * // => [10, 20]
*/ */
function pullAt(array) { var pullAt = restParam(function(array, indexes) {
return basePullAt(array || [], baseFlatten(arguments, false, false, 1)); indexes = baseFlatten(indexes);
}
var result = baseAt(array, indexes);
basePullAt(array, indexes.sort(baseCompareAscending));
return result;
});
module.exports = pullAt; module.exports = pullAt;

View File

@@ -1,15 +1,10 @@
var baseCallback = require('../internal/baseCallback'); var baseCallback = require('../internal/baseCallback'),
basePullAt = require('../internal/basePullAt');
/** 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 * Removes all elements from `array` that `predicate` returns truthy for
* and returns an array of the removed elements. The predicate is bound to * and returns an array of the removed elements. The predicate is bound to
* `thisArg` and invoked with three arguments; (value, index, array). * `thisArg` and invoked with three arguments: (value, index, array).
* *
* If a property name is provided for `predicate` the created `_.property` * If a property name is provided for `predicate` the created `_.property`
* style callback returns the property value of the given element. * style callback returns the property value of the given element.
@@ -35,7 +30,9 @@ var splice = arrayProto.splice;
* @example * @example
* *
* var array = [1, 2, 3, 4]; * var array = [1, 2, 3, 4];
* var evens = _.remove(array, function(n) { return n % 2 == 0; }); * var evens = _.remove(array, function(n) {
* return n % 2 == 0;
* });
* *
* console.log(array); * console.log(array);
* // => [1, 3] * // => [1, 3]
@@ -44,19 +41,23 @@ var splice = arrayProto.splice;
* // => [2, 4] * // => [2, 4]
*/ */
function remove(array, predicate, thisArg) { function remove(array, predicate, thisArg) {
var result = [];
if (!(array && array.length)) {
return result;
}
var index = -1, var index = -1,
length = array ? array.length : 0, indexes = [],
result = []; length = array.length;
predicate = baseCallback(predicate, thisArg, 3); predicate = baseCallback(predicate, thisArg, 3);
while (++index < length) { while (++index < length) {
var value = array[index]; var value = array[index];
if (predicate(value, index, array)) { if (predicate(value, index, array)) {
result.push(value); result.push(value);
splice.call(array, index--, 1); indexes.push(index);
length--;
} }
} }
basePullAt(array, indexes);
return result; return result;
} }

View File

@@ -4,7 +4,7 @@ var baseSlice = require('../internal/baseSlice'),
/** /**
* Creates a slice of `array` from `start` up to, but not including, `end`. * 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 * **Note:** This method is used instead of `Array#slice` to support node
* lists in IE < 9 and to ensure dense arrays are returned. * lists in IE < 9 and to ensure dense arrays are returned.
* *
* @static * @static

View File

@@ -1,6 +1,4 @@
var baseCallback = require('../internal/baseCallback'), var createSortedIndex = require('../internal/createSortedIndex');
binaryIndex = require('../internal/binaryIndex'),
binaryIndexBy = require('../internal/binaryIndexBy');
/** /**
* Uses a binary search to determine the lowest index at which `value` should * Uses a binary search to determine the lowest index at which `value` should
@@ -9,14 +7,14 @@ var baseCallback = require('../internal/baseCallback'),
* to compute their sort ranking. The iteratee is bound to `thisArg` and * to compute their sort ranking. The iteratee is bound to `thisArg` and
* invoked with one argument; (value). * invoked with one argument; (value).
* *
* If a property name is provided for `predicate` the created `_.property` * If a property name is provided for `iteratee` the created `_.property`
* style callback returns the property value of the given element. * style callback returns the property value of the given element.
* *
* If a value is also provided for `thisArg` the created `_.matchesProperty` * If a value is also provided for `thisArg` the created `_.matchesProperty`
* style callback returns `true` for elements that have a matching property * style callback returns `true` for elements that have a matching property
* value, else `false`. * value, else `false`.
* *
* If an object is provided for `predicate` the created `_.matches` style * If an object is provided for `iteratee` the created `_.matches` style
* callback returns `true` for elements that have the properties of the given * callback returns `true` for elements that have the properties of the given
* object, else `false`. * object, else `false`.
* *
@@ -35,7 +33,7 @@ var baseCallback = require('../internal/baseCallback'),
* _.sortedIndex([30, 50], 40); * _.sortedIndex([30, 50], 40);
* // => 1 * // => 1
* *
* _.sortedIndex([4, 4, 5, 5, 6, 6], 5); * _.sortedIndex([4, 4, 5, 5], 5);
* // => 2 * // => 2
* *
* var dict = { 'data': { 'thirty': 30, 'forty': 40, 'fifty': 50 } }; * var dict = { 'data': { 'thirty': 30, 'forty': 40, 'fifty': 50 } };
@@ -50,10 +48,6 @@ var baseCallback = require('../internal/baseCallback'),
* _.sortedIndex([{ 'x': 30 }, { 'x': 50 }], { 'x': 40 }, 'x'); * _.sortedIndex([{ 'x': 30 }, { 'x': 50 }], { 'x': 40 }, 'x');
* // => 1 * // => 1
*/ */
function sortedIndex(array, value, iteratee, thisArg) { var sortedIndex = createSortedIndex();
return iteratee == null
? binaryIndex(array, value)
: binaryIndexBy(array, value, baseCallback(iteratee, thisArg, 1));
}
module.exports = sortedIndex; module.exports = sortedIndex;

View File

@@ -1,6 +1,4 @@
var baseCallback = require('../internal/baseCallback'), var createSortedIndex = require('../internal/createSortedIndex');
binaryIndex = require('../internal/binaryIndex'),
binaryIndexBy = require('../internal/binaryIndexBy');
/** /**
* This method is like `_.sortedIndex` except that it returns the highest * This method is like `_.sortedIndex` except that it returns the highest
@@ -19,13 +17,9 @@ var baseCallback = require('../internal/baseCallback'),
* into `array`. * into `array`.
* @example * @example
* *
* _.sortedLastIndex([4, 4, 5, 5, 6, 6], 5); * _.sortedLastIndex([4, 4, 5, 5], 5);
* // => 4 * // => 4
*/ */
function sortedLastIndex(array, value, iteratee, thisArg) { var sortedLastIndex = createSortedIndex(true);
return iteratee == null
? binaryIndex(array, value, true)
: binaryIndexBy(array, value, baseCallback(iteratee, thisArg, 1), true);
}
module.exports = sortedLastIndex; module.exports = sortedLastIndex;

View File

@@ -1,10 +1,10 @@
var baseCallback = require('../internal/baseCallback'), var baseCallback = require('../internal/baseCallback'),
baseSlice = require('../internal/baseSlice'); baseWhile = require('../internal/baseWhile');
/** /**
* Creates a slice of `array` with elements taken from the end. Elements are * Creates a slice of `array` with elements taken from the end. Elements are
* taken until `predicate` returns falsey. The predicate is bound to `thisArg` * taken until `predicate` returns falsey. The predicate is bound to `thisArg`
* and invoked with three arguments; (value, index, array). * and invoked with three arguments: (value, index, array).
* *
* If a property name is provided for `predicate` the created `_.property` * If a property name is provided for `predicate` the created `_.property`
* style callback returns the property value of the given element. * style callback returns the property value of the given element.
@@ -27,7 +27,9 @@ var baseCallback = require('../internal/baseCallback'),
* @returns {Array} Returns the slice of `array`. * @returns {Array} Returns the slice of `array`.
* @example * @example
* *
* _.takeRightWhile([1, 2, 3], function(n) { return n > 1; }); * _.takeRightWhile([1, 2, 3], function(n) {
* return n > 1;
* });
* // => [2, 3] * // => [2, 3]
* *
* var users = [ * var users = [
@@ -49,13 +51,9 @@ var baseCallback = require('../internal/baseCallback'),
* // => [] * // => []
*/ */
function takeRightWhile(array, predicate, thisArg) { function takeRightWhile(array, predicate, thisArg) {
var length = array ? array.length : 0; return (array && array.length)
if (!length) { ? baseWhile(array, baseCallback(predicate, thisArg, 3), false, true)
return []; : [];
}
predicate = baseCallback(predicate, thisArg, 3);
while (length-- && predicate(array[length], length, array)) {}
return baseSlice(array, length + 1);
} }
module.exports = takeRightWhile; module.exports = takeRightWhile;

View File

@@ -1,10 +1,10 @@
var baseCallback = require('../internal/baseCallback'), var baseCallback = require('../internal/baseCallback'),
baseSlice = require('../internal/baseSlice'); baseWhile = require('../internal/baseWhile');
/** /**
* Creates a slice of `array` with elements taken from the beginning. Elements * Creates a slice of `array` with elements taken from the beginning. Elements
* are taken until `predicate` returns falsey. The predicate is bound to * are taken until `predicate` returns falsey. The predicate is bound to
* `thisArg` and invoked with three arguments; (value, index, array). * `thisArg` and invoked with three arguments: (value, index, array).
* *
* If a property name is provided for `predicate` the created `_.property` * If a property name is provided for `predicate` the created `_.property`
* style callback returns the property value of the given element. * style callback returns the property value of the given element.
@@ -27,7 +27,9 @@ var baseCallback = require('../internal/baseCallback'),
* @returns {Array} Returns the slice of `array`. * @returns {Array} Returns the slice of `array`.
* @example * @example
* *
* _.takeWhile([1, 2, 3], function(n) { return n < 3; }); * _.takeWhile([1, 2, 3], function(n) {
* return n < 3;
* });
* // => [1, 2] * // => [1, 2]
* *
* var users = [ * var users = [
@@ -49,14 +51,9 @@ var baseCallback = require('../internal/baseCallback'),
* // => [] * // => []
*/ */
function takeWhile(array, predicate, thisArg) { function takeWhile(array, predicate, thisArg) {
var length = array ? array.length : 0; return (array && array.length)
if (!length) { ? baseWhile(array, baseCallback(predicate, thisArg, 3))
return []; : [];
}
var index = -1;
predicate = baseCallback(predicate, thisArg, 3);
while (++index < length && predicate(array[index], index, array)) {}
return baseSlice(array, 0, index);
} }
module.exports = takeWhile; module.exports = takeWhile;

View File

@@ -1,14 +1,11 @@
var baseFlatten = require('../internal/baseFlatten'), var baseFlatten = require('../internal/baseFlatten'),
baseUniq = require('../internal/baseUniq'); baseUniq = require('../internal/baseUniq'),
restParam = require('../function/restParam');
/** /**
* Creates an array of unique values, in order, of the provided arrays using * Creates an array of unique values, in order, of the provided arrays using
* `SameValueZero` for equality comparisons. * [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-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 * @static
* @memberOf _ * @memberOf _
@@ -17,11 +14,11 @@ var baseFlatten = require('../internal/baseFlatten'),
* @returns {Array} Returns the new array of combined values. * @returns {Array} Returns the new array of combined values.
* @example * @example
* *
* _.union([1, 2, 3], [5, 2, 1, 4], [2, 1]); * _.union([1, 2], [4, 2], [2, 1]);
* // => [1, 2, 3, 5, 4] * // => [1, 2, 4]
*/ */
function union() { var union = restParam(function(arrays) {
return baseUniq(baseFlatten(arguments, false, true)); return baseUniq(baseFlatten(arrays, false, true));
} });
module.exports = union; module.exports = union;

View File

@@ -4,29 +4,26 @@ var baseCallback = require('../internal/baseCallback'),
sortedUniq = require('../internal/sortedUniq'); sortedUniq = require('../internal/sortedUniq');
/** /**
* Creates a duplicate-value-free version of an array using `SameValueZero` * Creates a duplicate-free version of an array, using
* for equality comparisons. Providing `true` for `isSorted` performs a faster * [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
* search algorithm for sorted arrays. If an iteratee function is provided it * for equality comparisons, in which only the first occurence of each element
* is invoked for each value in the array to generate the criterion by which * is kept. Providing `true` for `isSorted` performs a faster search algorithm
* uniqueness is computed. The `iteratee` is bound to `thisArg` and invoked * for sorted arrays. If an iteratee function is provided it is invoked for
* with three arguments; (value, index, array). * each element 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` * If a property name is provided for `iteratee` the created `_.property`
* style callback returns the property value of the given element. * style callback returns the property value of the given element.
* *
* If a value is also provided for `thisArg` the created `_.matchesProperty` * If a value is also provided for `thisArg` the created `_.matchesProperty`
* style callback returns `true` for elements that have a matching property * style callback returns `true` for elements that have a matching property
* value, else `false`. * value, else `false`.
* *
* If an object is provided for `predicate` the created `_.matches` style * If an object is provided for `iteratee` the created `_.matches` style
* callback returns `true` for elements that have the properties of the given * callback returns `true` for elements that have the properties of the given
* object, else `false`. * 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 * @static
* @memberOf _ * @memberOf _
* @alias unique * @alias unique
@@ -38,15 +35,17 @@ var baseCallback = require('../internal/baseCallback'),
* @returns {Array} Returns the new duplicate-value-free array. * @returns {Array} Returns the new duplicate-value-free array.
* @example * @example
* *
* _.uniq([1, 2, 1]); * _.uniq([2, 1, 2]);
* // => [1, 2] * // => [2, 1]
* *
* // using `isSorted` * // using `isSorted`
* _.uniq([1, 1, 2], true); * _.uniq([1, 1, 2], true);
* // => [1, 2] * // => [1, 2]
* *
* // using an iteratee function * // using an iteratee function
* _.uniq([1, 2.5, 1.5, 2], function(n) { return this.floor(n); }, Math); * _.uniq([1, 2.5, 1.5, 2], function(n) {
* return this.floor(n);
* }, Math);
* // => [1, 2.5] * // => [1, 2.5]
* *
* // using the `_.property` callback shorthand * // using the `_.property` callback shorthand
@@ -58,8 +57,7 @@ function uniq(array, isSorted, iteratee, thisArg) {
if (!length) { if (!length) {
return []; return [];
} }
// Juggle arguments. if (isSorted != null && typeof isSorted != 'boolean') {
if (typeof isSorted != 'boolean' && isSorted != null) {
thisArg = iteratee; thisArg = iteratee;
iteratee = isIterateeCall(array, isSorted, thisArg) ? null : isSorted; iteratee = isIterateeCall(array, isSorted, thisArg) ? null : isSorted;
isSorted = false; isSorted = false;

View File

@@ -1,13 +1,14 @@
var arrayMap = require('../internal/arrayMap'), var arrayFilter = require('../internal/arrayFilter'),
arrayMax = require('../internal/arrayMax'), arrayMap = require('../internal/arrayMap'),
baseProperty = require('../internal/baseProperty'); baseProperty = require('../internal/baseProperty'),
isArrayLike = require('../internal/isArrayLike');
/** Used to the length of n-tuples for `_.unzip`. */ /* Native method references for those with the same name as other `lodash` methods. */
var getLength = baseProperty('length'); var nativeMax = Math.max;
/** /**
* This method is like `_.zip` except that it accepts an array of grouped * 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` * elements and creates an array regrouping the elements to their pre-zip
* configuration. * configuration.
* *
* @static * @static
@@ -24,10 +25,19 @@ var getLength = baseProperty('length');
* // => [['fred', 'barney'], [30, 40], [true, false]] * // => [['fred', 'barney'], [30, 40], [true, false]]
*/ */
function unzip(array) { function unzip(array) {
if (!(array && array.length)) {
return [];
}
var index = -1, var index = -1,
length = (array && array.length && arrayMax(arrayMap(array, getLength))) >>> 0, length = 0;
result = Array(length);
array = arrayFilter(array, function(group) {
if (isArrayLike(group)) {
length = nativeMax(group.length, length);
return true;
}
});
var result = Array(length);
while (++index < length) { while (++index < length) {
result[index] = arrayMap(array, baseProperty(index)); result[index] = arrayMap(array, baseProperty(index));
} }

41
array/unzipWith.js Normal file
View File

@@ -0,0 +1,41 @@
var arrayMap = require('../internal/arrayMap'),
arrayReduce = require('../internal/arrayReduce'),
bindCallback = require('../internal/bindCallback'),
unzip = require('./unzip');
/**
* This method is like `_.unzip` except that it accepts an iteratee to specify
* how regrouped values should be combined. The `iteratee` is bound to `thisArg`
* and invoked with four arguments: (accumulator, value, index, group).
*
* @static
* @memberOf _
* @category Array
* @param {Array} array The array of grouped elements to process.
* @param {Function} [iteratee] The function to combine regrouped values.
* @param {*} [thisArg] The `this` binding of `iteratee`.
* @returns {Array} Returns the new array of regrouped elements.
* @example
*
* var zipped = _.zip([1, 2], [10, 20], [100, 200]);
* // => [[1, 10, 100], [2, 20, 200]]
*
* _.unzipWith(zipped, _.add);
* // => [3, 30, 300]
*/
function unzipWith(array, iteratee, thisArg) {
var length = array ? array.length : 0;
if (!length) {
return [];
}
var result = unzip(array);
if (iteratee == null) {
return result;
}
iteratee = bindCallback(iteratee, thisArg, 4);
return arrayMap(result, function(group) {
return arrayReduce(group, iteratee, undefined, true);
});
}
module.exports = unzipWith;

View File

@@ -1,14 +1,11 @@
var baseDifference = require('../internal/baseDifference'), var baseDifference = require('../internal/baseDifference'),
baseSlice = require('../internal/baseSlice'); isArrayLike = require('../internal/isArrayLike'),
restParam = require('../function/restParam');
/** /**
* Creates an array excluding all provided values using `SameValueZero` for * Creates an array excluding all provided values using
* equality comparisons. * [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-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 * @static
* @memberOf _ * @memberOf _
@@ -18,11 +15,13 @@ var baseDifference = require('../internal/baseDifference'),
* @returns {Array} Returns the new array of filtered values. * @returns {Array} Returns the new array of filtered values.
* @example * @example
* *
* _.without([1, 2, 1, 0, 3, 1, 4], 0, 1); * _.without([1, 2, 1, 3], 1, 2);
* // => [2, 3, 4] * // => [3]
*/ */
function without(array) { var without = restParam(function(array, values) {
return baseDifference(array, baseSlice(arguments, 1)); return isArrayLike(array)
} ? baseDifference(array, values)
: [];
});
module.exports = without; module.exports = without;

View File

@@ -1,12 +1,10 @@
var baseDifference = require('../internal/baseDifference'), var baseDifference = require('../internal/baseDifference'),
baseUniq = require('../internal/baseUniq'), baseUniq = require('../internal/baseUniq'),
isArguments = require('../lang/isArguments'), isArrayLike = require('../internal/isArrayLike');
isArray = require('../lang/isArray');
/** /**
* Creates an array that is the symmetric difference of the provided arrays. * Creates an array that is the [symmetric difference](https://en.wikipedia.org/wiki/Symmetric_difference)
* See [Wikipedia](https://en.wikipedia.org/wiki/Symmetric_difference) for * of the provided arrays.
* more details.
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -15,11 +13,8 @@ var baseDifference = require('../internal/baseDifference'),
* @returns {Array} Returns the new array of values. * @returns {Array} Returns the new array of values.
* @example * @example
* *
* _.xor([1, 2, 3], [5, 2, 1, 4]); * _.xor([1, 2], [4, 2]);
* // => [3, 5, 4] * // => [1, 4]
*
* _.xor([1, 2, 5], [2, 3, 5], [3, 4, 5]);
* // => [1, 4, 5]
*/ */
function xor() { function xor() {
var index = -1, var index = -1,
@@ -27,7 +22,7 @@ function xor() {
while (++index < length) { while (++index < length) {
var array = arguments[index]; var array = arguments[index];
if (isArray(array) || isArguments(array)) { if (isArrayLike(array)) {
var result = result var result = result
? baseDifference(result, array).concat(baseDifference(array, result)) ? baseDifference(result, array).concat(baseDifference(array, result))
: array; : array;

View File

@@ -1,4 +1,5 @@
var unzip = require('./unzip'); var restParam = require('../function/restParam'),
unzip = require('./unzip');
/** /**
* Creates an array of grouped elements, the first of which contains the first * Creates an array of grouped elements, the first of which contains the first
@@ -15,14 +16,6 @@ var unzip = require('./unzip');
* _.zip(['fred', 'barney'], [30, 40], [true, false]); * _.zip(['fred', 'barney'], [30, 40], [true, false]);
* // => [['fred', 30, true], ['barney', 40, false]] * // => [['fred', 30, true], ['barney', 40, false]]
*/ */
function zip() { var zip = restParam(unzip);
var length = arguments.length,
array = Array(length);
while (length--) {
array[length] = arguments[length];
}
return unzip(array);
}
module.exports = zip; module.exports = zip;

View File

@@ -1,9 +1,10 @@
var isArray = require('../lang/isArray'); var isArray = require('../lang/isArray');
/** /**
* Creates an object composed from arrays of property names and values. Provide * The inverse of `_.pairs`; this method returns an object composed from arrays
* either a single two dimensional array, e.g. `[[key1, value1], [key2, value2]]` * of property names and values. Provide either a single two dimensional array,
* or two arrays, one of property names and one of corresponding values. * e.g. `[[key1, value1], [key2, value2]]` or two arrays, one of property names
* and one of corresponding values.
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -14,6 +15,9 @@ var isArray = require('../lang/isArray');
* @returns {Object} Returns the new object. * @returns {Object} Returns the new object.
* @example * @example
* *
* _.zipObject([['fred', 30], ['barney', 40]]);
* // => { 'fred': 30, 'barney': 40 }
*
* _.zipObject(['fred', 'barney'], [30, 40]); * _.zipObject(['fred', 'barney'], [30, 40]);
* // => { 'fred': 30, 'barney': 40 } * // => { 'fred': 30, 'barney': 40 }
*/ */

36
array/zipWith.js Normal file
View File

@@ -0,0 +1,36 @@
var restParam = require('../function/restParam'),
unzipWith = require('./unzipWith');
/**
* This method is like `_.zip` except that it accepts an iteratee to specify
* how grouped values should be combined. The `iteratee` is bound to `thisArg`
* and invoked with four arguments: (accumulator, value, index, group).
*
* @static
* @memberOf _
* @category Array
* @param {...Array} [arrays] The arrays to process.
* @param {Function} [iteratee] The function to combine grouped values.
* @param {*} [thisArg] The `this` binding of `iteratee`.
* @returns {Array} Returns the new array of grouped elements.
* @example
*
* _.zipWith([1, 2], [10, 20], [100, 200], _.add);
* // => [111, 222]
*/
var zipWith = restParam(function(arrays) {
var length = arrays.length,
iteratee = length > 2 ? arrays[length - 2] : undefined,
thisArg = length > 1 ? arrays[length - 1] : undefined;
if (length > 2 && typeof iteratee == 'function') {
length -= 2;
} else {
iteratee = (length > 1 && typeof thisArg == 'function') ? (--length, thisArg) : undefined;
thisArg = undefined;
}
arrays.length = length;
return unzipWith(arrays, iteratee, thisArg);
});
module.exports = zipWith;

View File

@@ -19,7 +19,9 @@ var lodash = require('./lodash');
* *
* var youngest = _.chain(users) * var youngest = _.chain(users)
* .sortBy('age') * .sortBy('age')
* .map(function(chr) { return chr.user + ' is ' + chr.age; }) * .map(function(chr) {
* return chr.user + ' is ' + chr.age;
* })
* .first() * .first()
* .value(); * .value();
* // => 'pebbles is 1' * // => 'pebbles is 1'

View File

@@ -1,5 +1,6 @@
var LazyWrapper = require('../internal/LazyWrapper'), var LazyWrapper = require('../internal/LazyWrapper'),
LodashWrapper = require('../internal/LodashWrapper'), LodashWrapper = require('../internal/LodashWrapper'),
baseLodash = require('../internal/baseLodash'),
isArray = require('../lang/isArray'), isArray = require('../lang/isArray'),
isObjectLike = require('../internal/isObjectLike'), isObjectLike = require('../internal/isObjectLike'),
wrapperClone = require('../internal/wrapperClone'); wrapperClone = require('../internal/wrapperClone');
@@ -26,9 +27,14 @@ var hasOwnProperty = objectProto.hasOwnProperty;
* Chaining is supported in custom builds as long as the `_#value` method is * Chaining is supported in custom builds as long as the `_#value` method is
* directly or indirectly included in the build. * directly or indirectly included in the build.
* *
* In addition to lodash methods, wrappers also have the following `Array` methods: * In addition to lodash methods, wrappers have `Array` and `String` methods.
* `concat`, `join`, `pop`, `push`, `reverse`, `shift`, `slice`, `sort`, `splice`, *
* and `unshift` * The wrapper `Array` methods are:
* `concat`, `join`, `pop`, `push`, `reverse`, `shift`, `slice`, `sort`,
* `splice`, and `unshift`
*
* The wrapper `String` methods are:
* `replace` and `split`
* *
* The wrapper methods that support shortcut fusion are: * The wrapper methods that support shortcut fusion are:
* `compact`, `drop`, `dropRight`, `dropRightWhile`, `dropWhile`, `filter`, * `compact`, `drop`, `dropRight`, `dropRightWhile`, `dropWhile`, `filter`,
@@ -44,30 +50,31 @@ var hasOwnProperty = objectProto.hasOwnProperty;
* `filter`, `flatten`, `flattenDeep`, `flow`, `flowRight`, `forEach`, * `filter`, `flatten`, `flattenDeep`, `flow`, `flowRight`, `forEach`,
* `forEachRight`, `forIn`, `forInRight`, `forOwn`, `forOwnRight`, `functions`, * `forEachRight`, `forIn`, `forInRight`, `forOwn`, `forOwnRight`, `functions`,
* `groupBy`, `indexBy`, `initial`, `intersection`, `invert`, `invoke`, `keys`, * `groupBy`, `indexBy`, `initial`, `intersection`, `invert`, `invoke`, `keys`,
* `keysIn`, `map`, `mapValues`, `matches`, `matchesProperty`, `memoize`, `merge`, * `keysIn`, `map`, `mapKeys`, `mapValues`, `matches`, `matchesProperty`,
* `mixin`, `negate`, `noop`, `omit`, `once`, `pairs`, `partial`, `partialRight`, * `memoize`, `merge`, `method`, `methodOf`, `mixin`, `negate`, `omit`, `once`,
* `partition`, `pick`, `plant`, `pluck`, `property`, `propertyOf`, `pull`, * `pairs`, `partial`, `partialRight`, `partition`, `pick`, `plant`, `pluck`,
* `pullAt`, `push`, `range`, `rearg`, `reject`, `remove`, `rest`, `reverse`, * `property`, `propertyOf`, `pull`, `pullAt`, `push`, `range`, `rearg`,
* `shuffle`, `slice`, `sort`, `sortBy`, `sortByAll`, `splice`, `spread`, * `reject`, `remove`, `rest`, `restParam`, `reverse`, `set`, `shuffle`,
* `slice`, `sort`, `sortBy`, `sortByAll`, `sortByOrder`, `splice`, `spread`,
* `take`, `takeRight`, `takeRightWhile`, `takeWhile`, `tap`, `throttle`, * `take`, `takeRight`, `takeRightWhile`, `takeWhile`, `tap`, `throttle`,
* `thru`, `times`, `toArray`, `toPlainObject`, `transform`, `union`, `uniq`, * `thru`, `times`, `toArray`, `toPlainObject`, `transform`, `union`, `uniq`,
* `unshift`, `unzip`, `values`, `valuesIn`, `where`, `without`, `wrap`, `xor`, * `unshift`, `unzip`, `unzipWith`, `values`, `valuesIn`, `where`, `without`,
* `zip`, and `zipObject` * `wrap`, `xor`, `zip`, `zipObject`, `zipWith`
* *
* The wrapper methods that are **not** chainable by default are: * The wrapper methods that are **not** chainable by default are:
* `attempt`, `camelCase`, `capitalize`, `clone`, `cloneDeep`, `deburr`, * `add`, `attempt`, `camelCase`, `capitalize`, `clone`, `cloneDeep`, `deburr`,
* `endsWith`, `escape`, `escapeRegExp`, `every`, `find`, `findIndex`, `findKey`, * `endsWith`, `escape`, `escapeRegExp`, `every`, `find`, `findIndex`, `findKey`,
* `findLast`, `findLastIndex`, `findLastKey`, `findWhere`, `first`, `has`, * `findLast`, `findLastIndex`, `findLastKey`, `findWhere`, `first`, `get`,
* `identity`, `includes`, `indexOf`, `isArguments`, `isArray`, `isBoolean`, * `gt`, `gte`, `has`, `identity`, `includes`, `indexOf`, `inRange`, `isArguments`,
* `isDate`, `isElement`, `isEmpty`, `isEqual`, `isError`, `isFinite`, * `isArray`, `isBoolean`, `isDate`, `isElement`, `isEmpty`, `isEqual`, `isError`,
* `isFunction`, `isMatch`, `isNative`, `isNaN`, `isNull`, `isNumber`, * `isFinite` `isFunction`, `isMatch`, `isNative`, `isNaN`, `isNull`, `isNumber`,
* `isObject`, `isPlainObject`, `isRegExp`, `isString`, `isUndefined`, * `isObject`, `isPlainObject`, `isRegExp`, `isString`, `isUndefined`,
* `isTypedArray`, `join`, `kebabCase`, `last`, `lastIndexOf`, `max`, `min`, * `isTypedArray`, `join`, `kebabCase`, `last`, `lastIndexOf`, `lt`, `lte`,
* `noConflict`, `now`, `pad`, `padLeft`, `padRight`, `parseInt`, `pop`, * `max`, `min`, `noConflict`, `noop`, `now`, `pad`, `padLeft`, `padRight`,
* `random`, `reduce`, `reduceRight`, `repeat`, `result`, `runInContext`, * `parseInt`, `pop`, `random`, `reduce`, `reduceRight`, `repeat`, `result`,
* `shift`, `size`, `snakeCase`, `some`, `sortedIndex`, `sortedLastIndex`, * `runInContext`, `shift`, `size`, `snakeCase`, `some`, `sortedIndex`,
* `startCase`, `startsWith`, `template`, `trim`, `trimLeft`, `trimRight`, * `sortedLastIndex`, `startCase`, `startsWith`, `sum`, `template`, `trim`,
* `trunc`, `unescape`, `uniqueId`, `value`, and `words` * `trimLeft`, `trimRight`, `trunc`, `unescape`, `uniqueId`, `value`, and `words`
* *
* The wrapper method `sample` will return a wrapped value when `n` is provided, * The wrapper method `sample` will return a wrapped value when `n` is provided,
* otherwise an unwrapped value is returned. * otherwise an unwrapped value is returned.
@@ -82,11 +89,15 @@ var hasOwnProperty = objectProto.hasOwnProperty;
* var wrapped = _([1, 2, 3]); * var wrapped = _([1, 2, 3]);
* *
* // returns an unwrapped value * // returns an unwrapped value
* wrapped.reduce(function(sum, n) { return sum + n; }); * wrapped.reduce(function(total, n) {
* return total + n;
* });
* // => 6 * // => 6
* *
* // returns a wrapped value * // returns a wrapped value
* var squares = wrapped.map(function(n) { return n * n; }); * var squares = wrapped.map(function(n) {
* return n * n;
* });
* *
* _.isArray(squares); * _.isArray(squares);
* // => false * // => false
@@ -106,4 +117,7 @@ function lodash(value) {
return new LodashWrapper(value); return new LodashWrapper(value);
} }
// Ensure wrappers are instances of `baseLodash`.
lodash.prototype = baseLodash.prototype;
module.exports = lodash; module.exports = lodash;

View File

@@ -14,7 +14,9 @@
* @example * @example
* *
* _([1, 2, 3]) * _([1, 2, 3])
* .tap(function(array) { array.pop(); }) * .tap(function(array) {
* array.pop();
* })
* .reverse() * .reverse()
* .value(); * .value();
* // => [2, 1] * // => [2, 1]

View File

@@ -10,11 +10,14 @@
* @returns {*} Returns the result of `interceptor`. * @returns {*} Returns the result of `interceptor`.
* @example * @example
* *
* _([1, 2, 3]) * _(' abc ')
* .last() * .chain()
* .thru(function(value) { return [value]; }) * .trim()
* .thru(function(value) {
* return [value];
* })
* .value(); * .value();
* // => [3] * // => ['abc']
*/ */
function thru(value, interceptor, thisArg) { function thru(value, interceptor, thisArg) {
return interceptor.call(thisArg, value); return interceptor.call(thisArg, value);

View File

@@ -1,4 +1,4 @@
var LodashWrapper = require('../internal/LodashWrapper'), var baseLodash = require('../internal/baseLodash'),
wrapperClone = require('../internal/wrapperClone'); wrapperClone = require('../internal/wrapperClone');
/** /**
@@ -28,7 +28,7 @@ function wrapperPlant(value) {
var result, var result,
parent = this; parent = this;
while (parent instanceof LodashWrapper) { while (parent instanceof baseLodash) {
var clone = wrapperClone(parent); var clone = wrapperClone(parent);
if (result) { if (result) {
previous.__wrapped__ = clone; previous.__wrapped__ = clone;

View File

@@ -24,8 +24,8 @@ module.exports = {
'inject': require('./collection/inject'), 'inject': require('./collection/inject'),
'invoke': require('./collection/invoke'), 'invoke': require('./collection/invoke'),
'map': require('./collection/map'), 'map': require('./collection/map'),
'max': require('./collection/max'), 'max': require('./math/max'),
'min': require('./collection/min'), 'min': require('./math/min'),
'partition': require('./collection/partition'), 'partition': require('./collection/partition'),
'pluck': require('./collection/pluck'), 'pluck': require('./collection/pluck'),
'reduce': require('./collection/reduce'), 'reduce': require('./collection/reduce'),
@@ -38,5 +38,7 @@ module.exports = {
'some': require('./collection/some'), 'some': require('./collection/some'),
'sortBy': require('./collection/sortBy'), 'sortBy': require('./collection/sortBy'),
'sortByAll': require('./collection/sortByAll'), 'sortByAll': require('./collection/sortByAll'),
'sortByOrder': require('./collection/sortByOrder'),
'sum': require('./math/sum'),
'where': require('./collection/where') 'where': require('./collection/where')
}; };

View File

@@ -1,7 +1,6 @@
var baseAt = require('../internal/baseAt'), var baseAt = require('../internal/baseAt'),
baseFlatten = require('../internal/baseFlatten'), baseFlatten = require('../internal/baseFlatten'),
isLength = require('../internal/isLength'), restParam = require('../function/restParam');
toIterable = require('../internal/toIterable');
/** /**
* Creates an array of elements corresponding to the given keys, or indexes, * Creates an array of elements corresponding to the given keys, or indexes,
@@ -17,18 +16,14 @@ var baseAt = require('../internal/baseAt'),
* @returns {Array} Returns the new array of picked elements. * @returns {Array} Returns the new array of picked elements.
* @example * @example
* *
* _.at(['a', 'b', 'c', 'd', 'e'], [0, 2, 4]); * _.at(['a', 'b', 'c'], [0, 2]);
* // => ['a', 'c', 'e'] * // => ['a', 'c']
* *
* _.at(['fred', 'barney', 'pebbles'], 0, 2); * _.at(['barney', 'fred', 'pebbles'], 0, 2);
* // => ['fred', 'pebbles'] * // => ['barney', 'pebbles']
*/ */
function at(collection) { var at = restParam(function(collection, props) {
var length = collection ? collection.length : 0; return baseAt(collection, baseFlatten(props));
if (isLength(length)) { });
collection = toIterable(collection);
}
return baseAt(collection, baseFlatten(arguments, false, false, 1));
}
module.exports = at; module.exports = at;

View File

@@ -10,17 +10,17 @@ var hasOwnProperty = objectProto.hasOwnProperty;
* Creates an object composed of keys generated from the results of running * Creates an object composed of keys generated from the results of running
* each element of `collection` through `iteratee`. The corresponding value * each element of `collection` through `iteratee`. The corresponding value
* of each key is the number of times the key was returned by `iteratee`. * 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; * The `iteratee` is bound to `thisArg` and invoked with three arguments:
* (value, index|key, collection). * (value, index|key, collection).
* *
* If a property name is provided for `predicate` the created `_.property` * If a property name is provided for `iteratee` the created `_.property`
* style callback returns the property value of the given element. * style callback returns the property value of the given element.
* *
* If a value is also provided for `thisArg` the created `_.matchesProperty` * If a value is also provided for `thisArg` the created `_.matchesProperty`
* style callback returns `true` for elements that have a matching property * style callback returns `true` for elements that have a matching property
* value, else `false`. * value, else `false`.
* *
* If an object is provided for `predicate` the created `_.matches` style * If an object is provided for `iteratee` the created `_.matches` style
* callback returns `true` for elements that have the properties of the given * callback returns `true` for elements that have the properties of the given
* object, else `false`. * object, else `false`.
* *
@@ -34,10 +34,14 @@ var hasOwnProperty = objectProto.hasOwnProperty;
* @returns {Object} Returns the composed aggregate object. * @returns {Object} Returns the composed aggregate object.
* @example * @example
* *
* _.countBy([4.3, 6.1, 6.4], function(n) { return Math.floor(n); }); * _.countBy([4.3, 6.1, 6.4], function(n) {
* return Math.floor(n);
* });
* // => { '4': 1, '6': 2 } * // => { '4': 1, '6': 2 }
* *
* _.countBy([4.3, 6.1, 6.4], function(n) { return this.floor(n); }, Math); * _.countBy([4.3, 6.1, 6.4], function(n) {
* return this.floor(n);
* }, Math);
* // => { '4': 1, '6': 2 } * // => { '4': 1, '6': 2 }
* *
* _.countBy(['one', 'two', 'three'], 'length'); * _.countBy(['one', 'two', 'three'], 'length');

View File

@@ -1,11 +1,12 @@
var arrayEvery = require('../internal/arrayEvery'), var arrayEvery = require('../internal/arrayEvery'),
baseCallback = require('../internal/baseCallback'), baseCallback = require('../internal/baseCallback'),
baseEvery = require('../internal/baseEvery'), baseEvery = require('../internal/baseEvery'),
isArray = require('../lang/isArray'); isArray = require('../lang/isArray'),
isIterateeCall = require('../internal/isIterateeCall');
/** /**
* Checks if `predicate` returns truthy for **all** elements of `collection`. * Checks if `predicate` returns truthy for **all** elements of `collection`.
* The predicate is bound to `thisArg` and invoked with three arguments; * The predicate is bound to `thisArg` and invoked with three arguments:
* (value, index|key, collection). * (value, index|key, collection).
* *
* If a property name is provided for `predicate` the created `_.property` * If a property name is provided for `predicate` the created `_.property`
@@ -53,7 +54,10 @@ var arrayEvery = require('../internal/arrayEvery'),
*/ */
function every(collection, predicate, thisArg) { function every(collection, predicate, thisArg) {
var func = isArray(collection) ? arrayEvery : baseEvery; var func = isArray(collection) ? arrayEvery : baseEvery;
if (typeof predicate != 'function' || typeof thisArg != 'undefined') { if (thisArg && isIterateeCall(collection, predicate, thisArg)) {
predicate = null;
}
if (typeof predicate != 'function' || thisArg !== undefined) {
predicate = baseCallback(predicate, thisArg, 3); predicate = baseCallback(predicate, thisArg, 3);
} }
return func(collection, predicate); return func(collection, predicate);

View File

@@ -6,7 +6,7 @@ var arrayFilter = require('../internal/arrayFilter'),
/** /**
* Iterates over elements of `collection`, returning an array of all elements * Iterates over elements of `collection`, returning an array of all elements
* `predicate` returns truthy for. The predicate is bound to `thisArg` and * `predicate` returns truthy for. The predicate is bound to `thisArg` and
* invoked with three arguments; (value, index|key, collection). * invoked with three arguments: (value, index|key, collection).
* *
* If a property name is provided for `predicate` the created `_.property` * If a property name is provided for `predicate` the created `_.property`
* style callback returns the property value of the given element. * style callback returns the property value of the given element.
@@ -30,8 +30,10 @@ var arrayFilter = require('../internal/arrayFilter'),
* @returns {Array} Returns the new filtered array. * @returns {Array} Returns the new filtered array.
* @example * @example
* *
* var evens = _.filter([1, 2, 3, 4], function(n) { return n % 2 == 0; }); * _.filter([4, 5, 6], function(n) {
* // => [2, 4] * return n % 2 == 0;
* });
* // => [4, 6]
* *
* var users = [ * var users = [
* { 'user': 'barney', 'age': 36, 'active': true }, * { 'user': 'barney', 'age': 36, 'active': true },

View File

@@ -1,13 +1,10 @@
var baseCallback = require('../internal/baseCallback'), var baseEach = require('../internal/baseEach'),
baseEach = require('../internal/baseEach'), createFind = require('../internal/createFind');
baseFind = require('../internal/baseFind'),
findIndex = require('../array/findIndex'),
isArray = require('../lang/isArray');
/** /**
* Iterates over elements of `collection`, returning the first element * Iterates over elements of `collection`, returning the first element
* `predicate` returns truthy for. The predicate is bound to `thisArg` and * `predicate` returns truthy for. The predicate is bound to `thisArg` and
* invoked with three arguments; (value, index|key, collection). * invoked with three arguments: (value, index|key, collection).
* *
* If a property name is provided for `predicate` the created `_.property` * If a property name is provided for `predicate` the created `_.property`
* style callback returns the property value of the given element. * style callback returns the property value of the given element.
@@ -37,7 +34,9 @@ var baseCallback = require('../internal/baseCallback'),
* { 'user': 'pebbles', 'age': 1, 'active': true } * { 'user': 'pebbles', 'age': 1, 'active': true }
* ]; * ];
* *
* _.result(_.find(users, function(chr) { return chr.age < 40; }), 'user'); * _.result(_.find(users, function(chr) {
* return chr.age < 40;
* }), 'user');
* // => 'barney' * // => 'barney'
* *
* // using the `_.matches` callback shorthand * // using the `_.matches` callback shorthand
@@ -52,13 +51,6 @@ var baseCallback = require('../internal/baseCallback'),
* _.result(_.find(users, 'active'), 'user'); * _.result(_.find(users, 'active'), 'user');
* // => 'barney' * // => 'barney'
*/ */
function find(collection, predicate, thisArg) { var find = createFind(baseEach);
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);
}
module.exports = find; module.exports = find;

View File

@@ -1,6 +1,5 @@
var baseCallback = require('../internal/baseCallback'), var baseEachRight = require('../internal/baseEachRight'),
baseEachRight = require('../internal/baseEachRight'), createFind = require('../internal/createFind');
baseFind = require('../internal/baseFind');
/** /**
* This method is like `_.find` except that it iterates over elements of * This method is like `_.find` except that it iterates over elements of
@@ -16,12 +15,11 @@ var baseCallback = require('../internal/baseCallback'),
* @returns {*} Returns the matched element, else `undefined`. * @returns {*} Returns the matched element, else `undefined`.
* @example * @example
* *
* _.findLast([1, 2, 3, 4], function(n) { return n % 2 == 1; }); * _.findLast([1, 2, 3, 4], function(n) {
* return n % 2 == 1;
* });
* // => 3 * // => 3
*/ */
function findLast(collection, predicate, thisArg) { var findLast = createFind(baseEachRight, true);
predicate = baseCallback(predicate, thisArg, 3);
return baseFind(collection, predicate, baseEachRight);
}
module.exports = findLast; module.exports = findLast;

View File

@@ -1,15 +1,14 @@
var arrayEach = require('../internal/arrayEach'), var arrayEach = require('../internal/arrayEach'),
baseEach = require('../internal/baseEach'), baseEach = require('../internal/baseEach'),
bindCallback = require('../internal/bindCallback'), createForEach = require('../internal/createForEach');
isArray = require('../lang/isArray');
/** /**
* Iterates over elements of `collection` invoking `iteratee` for each element. * Iterates over elements of `collection` invoking `iteratee` for each element.
* The `iteratee` is bound to `thisArg` and invoked with three arguments; * The `iteratee` is bound to `thisArg` and invoked with three arguments:
* (value, index|key, collection). Iterator functions may exit iteration early * (value, index|key, collection). Iteratee functions may exit iteration early
* by explicitly returning `false`. * by explicitly returning `false`.
* *
* **Note:** As with other "Collections" methods, objects with a `length` property * **Note:** As with other "Collections" methods, objects with a "length" property
* are iterated like arrays. To avoid this behavior `_.forIn` or `_.forOwn` * are iterated like arrays. To avoid this behavior `_.forIn` or `_.forOwn`
* may be used for object iteration. * may be used for object iteration.
* *
@@ -23,16 +22,16 @@ var arrayEach = require('../internal/arrayEach'),
* @returns {Array|Object|string} Returns `collection`. * @returns {Array|Object|string} Returns `collection`.
* @example * @example
* *
* _([1, 2, 3]).forEach(function(n) { console.log(n); }).value(); * _([1, 2]).forEach(function(n) {
* console.log(n);
* }).value();
* // => logs each value from left to right and returns the array * // => 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); }); * _.forEach({ 'a': 1, 'b': 2 }, function(n, key) {
* console.log(n, key);
* });
* // => logs each value-key pair and returns the object (iteration order is not guaranteed) * // => logs each value-key pair and returns the object (iteration order is not guaranteed)
*/ */
function forEach(collection, iteratee, thisArg) { var forEach = createForEach(arrayEach, baseEach);
return (typeof iteratee == 'function' && typeof thisArg == 'undefined' && isArray(collection))
? arrayEach(collection, iteratee)
: baseEach(collection, bindCallback(iteratee, thisArg, 3));
}
module.exports = forEach; module.exports = forEach;

View File

@@ -1,7 +1,6 @@
var arrayEachRight = require('../internal/arrayEachRight'), var arrayEachRight = require('../internal/arrayEachRight'),
baseEachRight = require('../internal/baseEachRight'), baseEachRight = require('../internal/baseEachRight'),
bindCallback = require('../internal/bindCallback'), createForEach = require('../internal/createForEach');
isArray = require('../lang/isArray');
/** /**
* This method is like `_.forEach` except that it iterates over elements of * This method is like `_.forEach` except that it iterates over elements of
@@ -17,13 +16,11 @@ var arrayEachRight = require('../internal/arrayEachRight'),
* @returns {Array|Object|string} Returns `collection`. * @returns {Array|Object|string} Returns `collection`.
* @example * @example
* *
* _([1, 2, 3]).forEachRight(function(n) { console.log(n); }).join(','); * _([1, 2]).forEachRight(function(n) {
* console.log(n);
* }).value();
* // => logs each value from right to left and returns the array * // => logs each value from right to left and returns the array
*/ */
function forEachRight(collection, iteratee, thisArg) { var forEachRight = createForEach(arrayEachRight, baseEachRight);
return (typeof iteratee == 'function' && typeof thisArg == 'undefined' && isArray(collection))
? arrayEachRight(collection, iteratee)
: baseEachRight(collection, bindCallback(iteratee, thisArg, 3));
}
module.exports = forEachRight; module.exports = forEachRight;

View File

@@ -10,17 +10,17 @@ var hasOwnProperty = objectProto.hasOwnProperty;
* Creates an object composed of keys generated from the results of running * Creates an object composed of keys generated from the results of running
* each element of `collection` through `iteratee`. The corresponding value * each element of `collection` through `iteratee`. The corresponding value
* of each key is an array of the elements responsible for generating the key. * 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; * The `iteratee` is bound to `thisArg` and invoked with three arguments:
* (value, index|key, collection). * (value, index|key, collection).
* *
* If a property name is provided for `predicate` the created `_.property` * If a property name is provided for `iteratee` the created `_.property`
* style callback returns the property value of the given element. * style callback returns the property value of the given element.
* *
* If a value is also provided for `thisArg` the created `_.matchesProperty` * If a value is also provided for `thisArg` the created `_.matchesProperty`
* style callback returns `true` for elements that have a matching property * style callback returns `true` for elements that have a matching property
* value, else `false`. * value, else `false`.
* *
* If an object is provided for `predicate` the created `_.matches` style * If an object is provided for `iteratee` the created `_.matches` style
* callback returns `true` for elements that have the properties of the given * callback returns `true` for elements that have the properties of the given
* object, else `false`. * object, else `false`.
* *
@@ -34,10 +34,14 @@ var hasOwnProperty = objectProto.hasOwnProperty;
* @returns {Object} Returns the composed aggregate object. * @returns {Object} Returns the composed aggregate object.
* @example * @example
* *
* _.groupBy([4.2, 6.1, 6.4], function(n) { return Math.floor(n); }); * _.groupBy([4.2, 6.1, 6.4], function(n) {
* return Math.floor(n);
* });
* // => { '4': [4.2], '6': [6.1, 6.4] } * // => { '4': [4.2], '6': [6.1, 6.4] }
* *
* _.groupBy([4.2, 6.1, 6.4], function(n) { return this.floor(n); }, Math); * _.groupBy([4.2, 6.1, 6.4], function(n) {
* return this.floor(n);
* }, Math);
* // => { '4': [4.2], '6': [6.1, 6.4] } * // => { '4': [4.2], '6': [6.1, 6.4] }
* *
* // using the `_.property` callback shorthand * // using the `_.property` callback shorthand

View File

@@ -1,5 +1,7 @@
var baseIndexOf = require('../internal/baseIndexOf'), var baseIndexOf = require('../internal/baseIndexOf'),
getLength = require('../internal/getLength'),
isArray = require('../lang/isArray'), isArray = require('../lang/isArray'),
isIterateeCall = require('../internal/isIterateeCall'),
isLength = require('../internal/isLength'), isLength = require('../internal/isLength'),
isString = require('../lang/isString'), isString = require('../lang/isString'),
values = require('../object/values'); values = require('../object/values');
@@ -8,14 +10,10 @@ var baseIndexOf = require('../internal/baseIndexOf'),
var nativeMax = Math.max; var nativeMax = Math.max;
/** /**
* Checks if `value` is in `collection` using `SameValueZero` for equality * Checks if `value` is in `collection` using
* comparisons. If `fromIndex` is negative, it is used as the offset from * [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
* the end of `collection`. * 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 * @static
* @memberOf _ * @memberOf _
@@ -24,6 +22,7 @@ var nativeMax = Math.max;
* @param {Array|Object|string} collection The collection to search. * @param {Array|Object|string} collection The collection to search.
* @param {*} target The value to search for. * @param {*} target The value to search for.
* @param {number} [fromIndex=0] The index to search from. * @param {number} [fromIndex=0] The index to search from.
* @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`.
* @returns {boolean} Returns `true` if a matching element is found, else `false`. * @returns {boolean} Returns `true` if a matching element is found, else `false`.
* @example * @example
* *
@@ -39,8 +38,8 @@ var nativeMax = Math.max;
* _.includes('pebbles', 'eb'); * _.includes('pebbles', 'eb');
* // => true * // => true
*/ */
function includes(collection, target, fromIndex) { function includes(collection, target, fromIndex, guard) {
var length = collection ? collection.length : 0; var length = collection ? getLength(collection) : 0;
if (!isLength(length)) { if (!isLength(length)) {
collection = values(collection); collection = values(collection);
length = collection.length; length = collection.length;
@@ -48,10 +47,10 @@ function includes(collection, target, fromIndex) {
if (!length) { if (!length) {
return false; return false;
} }
if (typeof fromIndex == 'number') { if (typeof fromIndex != 'number' || (guard && isIterateeCall(target, fromIndex, guard))) {
fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
} else {
fromIndex = 0; fromIndex = 0;
} else {
fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
} }
return (typeof collection == 'string' || !isArray(collection) && isString(collection)) return (typeof collection == 'string' || !isArray(collection) && isString(collection))
? (fromIndex < length && collection.indexOf(target, fromIndex) > -1) ? (fromIndex < length && collection.indexOf(target, fromIndex) > -1)

View File

@@ -4,17 +4,17 @@ var createAggregator = require('../internal/createAggregator');
* Creates an object composed of keys generated from the results of running * Creates an object composed of keys generated from the results of running
* each element of `collection` through `iteratee`. The corresponding value * each element of `collection` through `iteratee`. The corresponding value
* of each key is the last element responsible for generating the key. The * of each key is the last element responsible for generating the key. The
* iteratee function is bound to `thisArg` and invoked with three arguments; * iteratee function is bound to `thisArg` and invoked with three arguments:
* (value, index|key, collection). * (value, index|key, collection).
* *
* If a property name is provided for `predicate` the created `_.property` * If a property name is provided for `iteratee` the created `_.property`
* style callback returns the property value of the given element. * style callback returns the property value of the given element.
* *
* If a value is also provided for `thisArg` the created `_.matchesProperty` * If a value is also provided for `thisArg` the created `_.matchesProperty`
* style callback returns `true` for elements that have a matching property * style callback returns `true` for elements that have a matching property
* value, else `false`. * value, else `false`.
* *
* If an object is provided for `predicate` the created `_.matches` style * If an object is provided for `iteratee` the created `_.matches` style
* callback returns `true` for elements that have the properties of the given * callback returns `true` for elements that have the properties of the given
* object, else `false`. * object, else `false`.
* *
@@ -36,10 +36,14 @@ var createAggregator = require('../internal/createAggregator');
* _.indexBy(keyData, 'dir'); * _.indexBy(keyData, 'dir');
* // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } } * // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }
* *
* _.indexBy(keyData, function(object) { return String.fromCharCode(object.code); }); * _.indexBy(keyData, function(object) {
* return String.fromCharCode(object.code);
* });
* // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } } * // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
* *
* _.indexBy(keyData, function(object) { return this.fromCharCode(object.code); }, String); * _.indexBy(keyData, function(object) {
* return this.fromCharCode(object.code);
* }, String);
* // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } } * // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
*/ */
var indexBy = createAggregator(function(result, value, key) { var indexBy = createAggregator(function(result, value, key) {

View File

@@ -1,17 +1,20 @@
var baseInvoke = require('../internal/baseInvoke'), var baseEach = require('../internal/baseEach'),
baseSlice = require('../internal/baseSlice'); invokePath = require('../internal/invokePath'),
isArrayLike = require('../internal/isArrayLike'),
isKey = require('../internal/isKey'),
restParam = require('../function/restParam');
/** /**
* Invokes the method named by `methodName` on each element in `collection`, * Invokes the method at `path` of each element in `collection`, returning
* returning an array of the results of each invoked method. Any additional * an array of the results of each invoked method. Any additional arguments
* arguments are provided to each invoked method. If `methodName` is a function * are provided to each invoked method. If `methodName` is a function it is
* it is invoked for, and `this` bound to, each element in `collection`. * invoked for, and `this` bound to, each element in `collection`.
* *
* @static * @static
* @memberOf _ * @memberOf _
* @category Collection * @category Collection
* @param {Array|Object|string} collection The collection to iterate over. * @param {Array|Object|string} collection The collection to iterate over.
* @param {Function|string} methodName The name of the method to invoke or * @param {Array|Function|string} path The path of the method to invoke or
* the function invoked per iteration. * the function invoked per iteration.
* @param {...*} [args] The arguments to invoke the method with. * @param {...*} [args] The arguments to invoke the method with.
* @returns {Array} Returns the array of results. * @returns {Array} Returns the array of results.
@@ -23,8 +26,17 @@ var baseInvoke = require('../internal/baseInvoke'),
* _.invoke([123, 456], String.prototype.split, ''); * _.invoke([123, 456], String.prototype.split, '');
* // => [['1', '2', '3'], ['4', '5', '6']] * // => [['1', '2', '3'], ['4', '5', '6']]
*/ */
function invoke(collection, methodName) { var invoke = restParam(function(collection, path, args) {
return baseInvoke(collection, methodName, baseSlice(arguments, 2)); var index = -1,
} isFunc = typeof path == 'function',
isProp = isKey(path),
result = isArrayLike(collection) ? Array(collection.length) : [];
baseEach(collection, function(value) {
var func = isFunc ? path : ((isProp && value != null) ? value[path] : null);
result[++index] = func ? func.apply(value, args) : invokePath(value, path, args);
});
return result;
});
module.exports = invoke; module.exports = invoke;

View File

@@ -6,27 +6,28 @@ var arrayMap = require('../internal/arrayMap'),
/** /**
* Creates an array of values by running each element in `collection` through * Creates an array of values by running each element in `collection` through
* `iteratee`. The `iteratee` is bound to `thisArg` and invoked with three * `iteratee`. The `iteratee` is bound to `thisArg` and invoked with three
* arguments; (value, index|key, collection). * arguments: (value, index|key, collection).
* *
* If a property name is provided for `predicate` the created `_.property` * If a property name is provided for `iteratee` the created `_.property`
* style callback returns the property value of the given element. * style callback returns the property value of the given element.
* *
* If a value is also provided for `thisArg` the created `_.matchesProperty` * If a value is also provided for `thisArg` the created `_.matchesProperty`
* style callback returns `true` for elements that have a matching property * style callback returns `true` for elements that have a matching property
* value, else `false`. * value, else `false`.
* *
* If an object is provided for `predicate` the created `_.matches` style * If an object is provided for `iteratee` the created `_.matches` style
* callback returns `true` for elements that have the properties of the given * callback returns `true` for elements that have the properties of the given
* object, else `false`. * object, else `false`.
* *
* Many lodash methods are guarded to work as interatees for methods like * Many lodash methods are guarded to work as iteratees for methods like
* `_.every`, `_.filter`, `_.map`, `_.mapValues`, `_.reject`, and `_.some`. * `_.every`, `_.filter`, `_.map`, `_.mapValues`, `_.reject`, and `_.some`.
* *
* The guarded methods are: * The guarded methods are:
* `ary`, `callback`, `chunk`, `clone`, `create`, `curry`, `curryRight`, `drop`, * `ary`, `callback`, `chunk`, `clone`, `create`, `curry`, `curryRight`,
* `dropRight`, `fill`, `flatten`, `invert`, `max`, `min`, `parseInt`, `slice`, * `drop`, `dropRight`, `every`, `fill`, `flatten`, `invert`, `max`, `min`,
* `sortBy`, `take`, `takeRight`, `template`, `trim`, `trimLeft`, `trimRight`, * `parseInt`, `slice`, `sortBy`, `take`, `takeRight`, `template`, `trim`,
* `trunc`, `random`, `range`, `sample`, `uniq`, and `words` * `trimLeft`, `trimRight`, `trunc`, `random`, `range`, `sample`, `some`,
* `sum`, `uniq`, and `words`
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -35,16 +36,19 @@ var arrayMap = require('../internal/arrayMap'),
* @param {Array|Object|string} collection The collection to iterate over. * @param {Array|Object|string} collection The collection to iterate over.
* @param {Function|Object|string} [iteratee=_.identity] The function invoked * @param {Function|Object|string} [iteratee=_.identity] The function invoked
* per iteration. * per iteration.
* create a `_.property` or `_.matches` style callback respectively.
* @param {*} [thisArg] The `this` binding of `iteratee`. * @param {*} [thisArg] The `this` binding of `iteratee`.
* @returns {Array} Returns the new mapped array. * @returns {Array} Returns the new mapped array.
* @example * @example
* *
* _.map([1, 2, 3], function(n) { return n * 3; }); * function timesThree(n) {
* // => [3, 6, 9] * return n * 3;
* }
* *
* _.map({ 'one': 1, 'two': 2, 'three': 3 }, function(n) { return n * 3; }); * _.map([1, 2], timesThree);
* // => [3, 6, 9] (iteration order is not guaranteed) * // => [3, 6]
*
* _.map({ 'a': 1, 'b': 2 }, timesThree);
* // => [3, 6] (iteration order is not guaranteed)
* *
* var users = [ * var users = [
* { 'user': 'barney' }, * { 'user': 'barney' },

View File

@@ -1,51 +1 @@
var arrayMax = require('../internal/arrayMax'), module.exports = require('../math/max');
createExtremum = require('../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 a value is also provided for `thisArg` the created `_.matchesProperty`
* style callback returns `true` for elements that have a matching property
* value, else `false`.
*
* 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.
* @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);
module.exports = max;

View File

@@ -1,51 +1 @@
var arrayMin = require('../internal/arrayMin'), module.exports = require('../math/min');
createExtremum = require('../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 a value is also provided for `thisArg` the created `_.matchesProperty`
* style callback returns `true` for elements that have a matching property
* value, else `false`.
*
* 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.
* @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);
module.exports = min;

View File

@@ -4,7 +4,7 @@ var createAggregator = require('../internal/createAggregator');
* Creates an array of elements split into two groups, the first of which * 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 truthy for, while the second of which
* contains elements `predicate` returns falsey for. The predicate is bound * contains elements `predicate` returns falsey for. The predicate is bound
* to `thisArg` and invoked with three arguments; (value, index|key, collection). * to `thisArg` and invoked with three arguments: (value, index|key, collection).
* *
* If a property name is provided for `predicate` the created `_.property` * If a property name is provided for `predicate` the created `_.property`
* style callback returns the property value of the given element. * style callback returns the property value of the given element.
@@ -27,11 +27,15 @@ var createAggregator = require('../internal/createAggregator');
* @returns {Array} Returns the array of grouped elements. * @returns {Array} Returns the array of grouped elements.
* @example * @example
* *
* _.partition([1, 2, 3], function(n) { return n % 2; }); * _.partition([1, 2, 3], function(n) {
* return n % 2;
* });
* // => [[1, 3], [2]] * // => [[1, 3], [2]]
* *
* _.partition([1.2, 2.3, 3.4], function(n) { return this.floor(n) % 2; }, Math); * _.partition([1.2, 2.3, 3.4], function(n) {
* // => [[1, 3], [2]] * return this.floor(n) % 2;
* }, Math);
* // => [[1.2, 3.4], [2.3]]
* *
* var users = [ * var users = [
* { 'user': 'barney', 'age': 36, 'active': false }, * { 'user': 'barney', 'age': 36, 'active': false },
@@ -39,7 +43,9 @@ var createAggregator = require('../internal/createAggregator');
* { 'user': 'pebbles', 'age': 1, 'active': false } * { 'user': 'pebbles', 'age': 1, 'active': false }
* ]; * ];
* *
* var mapper = function(array) { return _.pluck(array, 'user'); }; * var mapper = function(array) {
* return _.pluck(array, 'user');
* };
* *
* // using the `_.matches` callback shorthand * // using the `_.matches` callback shorthand
* _.map(_.partition(users, { 'age': 1, 'active': false }), mapper); * _.map(_.partition(users, { 'age': 1, 'active': false }), mapper);

View File

@@ -1,14 +1,14 @@
var baseProperty = require('../internal/baseProperty'), var map = require('./map'),
map = require('./map'); property = require('../utility/property');
/** /**
* Gets the value of `key` from all elements in `collection`. * Gets the property value of `path` from all elements in `collection`.
* *
* @static * @static
* @memberOf _ * @memberOf _
* @category Collection * @category Collection
* @param {Array|Object|string} collection The collection to iterate over. * @param {Array|Object|string} collection The collection to iterate over.
* @param {string} key The key of the property to pluck. * @param {Array|string} path The path of the property to pluck.
* @returns {Array} Returns the property values. * @returns {Array} Returns the property values.
* @example * @example
* *
@@ -24,8 +24,8 @@ var baseProperty = require('../internal/baseProperty'),
* _.pluck(userIndex, 'age'); * _.pluck(userIndex, 'age');
* // => [36, 40] (iteration order is not guaranteed) * // => [36, 40] (iteration order is not guaranteed)
*/ */
function pluck(collection, key) { function pluck(collection, path) {
return map(collection, baseProperty(key)); return map(collection, property(path));
} }
module.exports = pluck; module.exports = pluck;

View File

@@ -1,22 +1,20 @@
var arrayReduce = require('../internal/arrayReduce'), var arrayReduce = require('../internal/arrayReduce'),
baseCallback = require('../internal/baseCallback'),
baseEach = require('../internal/baseEach'), baseEach = require('../internal/baseEach'),
baseReduce = require('../internal/baseReduce'), createReduce = require('../internal/createReduce');
isArray = require('../lang/isArray');
/** /**
* Reduces `collection` to a value which is the accumulated result of running * Reduces `collection` to a value which is the accumulated result of running
* each element in `collection` through `iteratee`, where each successive * each element in `collection` through `iteratee`, where each successive
* invocation is supplied the return value of the previous. If `accumulator` * invocation is supplied the return value of the previous. If `accumulator`
* is not provided the first element of `collection` is used as the initial * 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; * value. The `iteratee` is bound to `thisArg` and invoked with four arguments:
* (accumulator, value, index|key, collection). * (accumulator, value, index|key, collection).
* *
* Many lodash methods are guarded to work as interatees for methods like * Many lodash methods are guarded to work as iteratees for methods like
* `_.reduce`, `_.reduceRight`, and `_.transform`. * `_.reduce`, `_.reduceRight`, and `_.transform`.
* *
* The guarded methods are: * The guarded methods are:
* `assign`, `defaults`, `merge`, and `sortAllBy` * `assign`, `defaults`, `includes`, `merge`, `sortByAll`, and `sortByOrder`
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -29,18 +27,17 @@ var arrayReduce = require('../internal/arrayReduce'),
* @returns {*} Returns the accumulated value. * @returns {*} Returns the accumulated value.
* @example * @example
* *
* var sum = _.reduce([1, 2, 3], function(sum, n) { return sum + n; }); * _.reduce([1, 2], function(total, n) {
* // => 6 * return total + n;
* });
* // => 3
* *
* var mapped = _.reduce({ 'a': 1, 'b': 2, 'c': 3 }, function(result, n, key) { * _.reduce({ 'a': 1, 'b': 2 }, function(result, n, key) {
* result[key] = n * 3; * result[key] = n * 3;
* return result; * return result;
* }, {}); * }, {});
* // => { 'a': 3, 'b': 6, 'c': 9 } (iteration order is not guaranteed) * // => { 'a': 3, 'b': 6 } (iteration order is not guaranteed)
*/ */
function reduce(collection, iteratee, accumulator, thisArg) { var reduce = createReduce(arrayReduce, baseEach);
var func = isArray(collection) ? arrayReduce : baseReduce;
return func(collection, baseCallback(iteratee, thisArg, 4), accumulator, arguments.length < 3, baseEach);
}
module.exports = reduce; module.exports = reduce;

View File

@@ -1,8 +1,6 @@
var arrayReduceRight = require('../internal/arrayReduceRight'), var arrayReduceRight = require('../internal/arrayReduceRight'),
baseCallback = require('../internal/baseCallback'),
baseEachRight = require('../internal/baseEachRight'), baseEachRight = require('../internal/baseEachRight'),
baseReduce = require('../internal/baseReduce'), createReduce = require('../internal/createReduce');
isArray = require('../lang/isArray');
/** /**
* This method is like `_.reduce` except that it iterates over elements of * This method is like `_.reduce` except that it iterates over elements of
@@ -20,12 +18,12 @@ var arrayReduceRight = require('../internal/arrayReduceRight'),
* @example * @example
* *
* var array = [[0, 1], [2, 3], [4, 5]]; * var array = [[0, 1], [2, 3], [4, 5]];
* _.reduceRight(array, function(flattened, other) { return flattened.concat(other); }, []); *
* _.reduceRight(array, function(flattened, other) {
* return flattened.concat(other);
* }, []);
* // => [4, 5, 2, 3, 0, 1] * // => [4, 5, 2, 3, 0, 1]
*/ */
function reduceRight(collection, iteratee, accumulator, thisArg) { var reduceRight = createReduce(arrayReduceRight, baseEachRight);
var func = isArray(collection) ? arrayReduceRight : baseReduce;
return func(collection, baseCallback(iteratee, thisArg, 4), accumulator, arguments.length < 3, baseEachRight);
}
module.exports = reduceRight; module.exports = reduceRight;

View File

@@ -7,17 +7,6 @@ var arrayFilter = require('../internal/arrayFilter'),
* The opposite of `_.filter`; this method returns the elements of `collection` * The opposite of `_.filter`; this method returns the elements of `collection`
* that `predicate` does **not** return truthy for. * 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 a value is also provided for `thisArg` the created `_.matchesProperty`
* style callback returns `true` for elements that have a matching property
* value, else `false`.
*
* 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 * @static
* @memberOf _ * @memberOf _
* @category Collection * @category Collection
@@ -28,7 +17,9 @@ var arrayFilter = require('../internal/arrayFilter'),
* @returns {Array} Returns the new filtered array. * @returns {Array} Returns the new filtered array.
* @example * @example
* *
* var odds = _.reject([1, 2, 3, 4], function(n) { return n % 2 == 0; }); * _.reject([1, 2, 3, 4], function(n) {
* return n % 2 == 0;
* });
* // => [1, 3] * // => [1, 3]
* *
* var users = [ * var users = [

View File

@@ -2,9 +2,8 @@ var baseRandom = require('../internal/baseRandom'),
toIterable = require('../internal/toIterable'); toIterable = require('../internal/toIterable');
/** /**
* Creates an array of shuffled values, using a version of the Fisher-Yates * Creates an array of shuffled values, using a version of the
* shuffle. See [Wikipedia](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle) * [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle).
* for more details.
* *
* @static * @static
* @memberOf _ * @memberOf _

View File

@@ -1,9 +1,10 @@
var isLength = require('../internal/isLength'), var getLength = require('../internal/getLength'),
isLength = require('../internal/isLength'),
keys = require('../object/keys'); keys = require('../object/keys');
/** /**
* Gets the size of `collection` by returning `collection.length` for * Gets the size of `collection` by returning its length for array-like
* array-like values or the number of own enumerable properties for objects. * values or the number of own enumerable properties for objects.
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -12,17 +13,17 @@ var isLength = require('../internal/isLength'),
* @returns {number} Returns the size of `collection`. * @returns {number} Returns the size of `collection`.
* @example * @example
* *
* _.size([1, 2]); * _.size([1, 2, 3]);
* // => 2
*
* _.size({ 'one': 1, 'two': 2, 'three': 3 });
* // => 3 * // => 3
* *
* _.size({ 'a': 1, 'b': 2 });
* // => 2
*
* _.size('pebbles'); * _.size('pebbles');
* // => 7 * // => 7
*/ */
function size(collection) { function size(collection) {
var length = collection ? collection.length : 0; var length = collection ? getLength(collection) : 0;
return isLength(length) ? length : keys(collection).length; return isLength(length) ? length : keys(collection).length;
} }

View File

@@ -1,13 +1,14 @@
var arraySome = require('../internal/arraySome'), var arraySome = require('../internal/arraySome'),
baseCallback = require('../internal/baseCallback'), baseCallback = require('../internal/baseCallback'),
baseSome = require('../internal/baseSome'), baseSome = require('../internal/baseSome'),
isArray = require('../lang/isArray'); isArray = require('../lang/isArray'),
isIterateeCall = require('../internal/isIterateeCall');
/** /**
* Checks if `predicate` returns truthy for **any** element of `collection`. * 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 * 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 * over the entire collection. The predicate is bound to `thisArg` and invoked
* with three arguments; (value, index|key, collection). * with three arguments: (value, index|key, collection).
* *
* If a property name is provided for `predicate` the created `_.property` * If a property name is provided for `predicate` the created `_.property`
* style callback returns the property value of the given element. * style callback returns the property value of the given element.
@@ -41,7 +42,7 @@ var arraySome = require('../internal/arraySome'),
* ]; * ];
* *
* // using the `_.matches` callback shorthand * // using the `_.matches` callback shorthand
* _.some(users, { user': 'barney', 'active': false }); * _.some(users, { 'user': 'barney', 'active': false });
* // => false * // => false
* *
* // using the `_.matchesProperty` callback shorthand * // using the `_.matchesProperty` callback shorthand
@@ -54,7 +55,10 @@ var arraySome = require('../internal/arraySome'),
*/ */
function some(collection, predicate, thisArg) { function some(collection, predicate, thisArg) {
var func = isArray(collection) ? arraySome : baseSome; var func = isArray(collection) ? arraySome : baseSome;
if (typeof predicate != 'function' || typeof thisArg != 'undefined') { if (thisArg && isIterateeCall(collection, predicate, thisArg)) {
predicate = null;
}
if (typeof predicate != 'function' || thisArg !== undefined) {
predicate = baseCallback(predicate, thisArg, 3); predicate = baseCallback(predicate, thisArg, 3);
} }
return func(collection, predicate); return func(collection, predicate);

View File

@@ -1,25 +1,24 @@
var baseCallback = require('../internal/baseCallback'), var baseCallback = require('../internal/baseCallback'),
baseEach = require('../internal/baseEach'), baseMap = require('../internal/baseMap'),
baseSortBy = require('../internal/baseSortBy'), baseSortBy = require('../internal/baseSortBy'),
compareAscending = require('../internal/compareAscending'), compareAscending = require('../internal/compareAscending'),
isIterateeCall = require('../internal/isIterateeCall'), isIterateeCall = require('../internal/isIterateeCall');
isLength = require('../internal/isLength');
/** /**
* Creates an array of elements, sorted in ascending order by the results of * Creates an array of elements, sorted in ascending order by the results of
* running each element in a collection through `iteratee`. This method performs * 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. * 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; * The `iteratee` is bound to `thisArg` and invoked with three arguments:
* (value, index|key, collection). * (value, index|key, collection).
* *
* If a property name is provided for `predicate` the created `_.property` * If a property name is provided for `iteratee` the created `_.property`
* style callback returns the property value of the given element. * style callback returns the property value of the given element.
* *
* If a value is also provided for `thisArg` the created `_.matchesProperty` * If a value is also provided for `thisArg` the created `_.matchesProperty`
* style callback returns `true` for elements that have a matching property * style callback returns `true` for elements that have a matching property
* value, else `false`. * value, else `false`.
* *
* If an object is provided for `predicate` the created `_.matches` style * If an object is provided for `iteratee` the created `_.matches` style
* callback returns `true` for elements that have the properties of the given * callback returns `true` for elements that have the properties of the given
* object, else `false`. * object, else `false`.
* *
@@ -27,17 +26,20 @@ var baseCallback = require('../internal/baseCallback'),
* @memberOf _ * @memberOf _
* @category Collection * @category Collection
* @param {Array|Object|string} collection The collection to iterate over. * @param {Array|Object|string} collection The collection to iterate over.
* @param {Array|Function|Object|string} [iteratee=_.identity] The function * @param {Function|Object|string} [iteratee=_.identity] The function invoked
* invoked per iteration. If a property name or an object is provided it is * per iteration.
* used to create a `_.property` or `_.matches` style callback respectively.
* @param {*} [thisArg] The `this` binding of `iteratee`. * @param {*} [thisArg] The `this` binding of `iteratee`.
* @returns {Array} Returns the new sorted array. * @returns {Array} Returns the new sorted array.
* @example * @example
* *
* _.sortBy([1, 2, 3], function(n) { return Math.sin(n); }); * _.sortBy([1, 2, 3], function(n) {
* return Math.sin(n);
* });
* // => [3, 1, 2] * // => [3, 1, 2]
* *
* _.sortBy([1, 2, 3], function(n) { return this.sin(n); }, Math); * _.sortBy([1, 2, 3], function(n) {
* return this.sin(n);
* }, Math);
* // => [3, 1, 2] * // => [3, 1, 2]
* *
* var users = [ * var users = [
@@ -51,16 +53,17 @@ var baseCallback = require('../internal/baseCallback'),
* // => ['barney', 'fred', 'pebbles'] * // => ['barney', 'fred', 'pebbles']
*/ */
function sortBy(collection, iteratee, thisArg) { function sortBy(collection, iteratee, thisArg) {
var index = -1, if (collection == null) {
length = collection ? collection.length : 0, return [];
result = isLength(length) ? Array(length) : []; }
if (thisArg && isIterateeCall(collection, iteratee, thisArg)) { if (thisArg && isIterateeCall(collection, iteratee, thisArg)) {
iteratee = null; iteratee = null;
} }
var index = -1;
iteratee = baseCallback(iteratee, thisArg, 3); iteratee = baseCallback(iteratee, thisArg, 3);
baseEach(collection, function(value, key, collection) {
result[++index] = { 'criteria': iteratee(value, key, collection), 'index': index, 'value': value }; var result = baseMap(collection, function(value, key, collection) {
return { 'criteria': iteratee(value, key, collection), 'index': ++index, 'value': value };
}); });
return baseSortBy(result, compareAscending); return baseSortBy(result, compareAscending);
} }

View File

@@ -1,53 +1,52 @@
var baseEach = require('../internal/baseEach'), var baseFlatten = require('../internal/baseFlatten'),
baseFlatten = require('../internal/baseFlatten'), baseSortByOrder = require('../internal/baseSortByOrder'),
baseSortBy = require('../internal/baseSortBy'),
compareMultipleAscending = require('../internal/compareMultipleAscending'),
isIterateeCall = require('../internal/isIterateeCall'), isIterateeCall = require('../internal/isIterateeCall'),
isLength = require('../internal/isLength'); restParam = require('../function/restParam');
/** /**
* This method is like `_.sortBy` except that it sorts by property names * This method is like `_.sortBy` except that it can sort by multiple iteratees
* instead of an iteratee function. * or property names.
*
* If a property name is provided for an iteratee the created `_.property`
* style callback returns the property value of the given element.
*
* If an object is provided for an iteratee the created `_.matches` style
* callback returns `true` for elements that have the properties of the given
* object, else `false`.
* *
* @static * @static
* @memberOf _ * @memberOf _
* @category Collection * @category Collection
* @param {Array|Object|string} collection The collection to iterate over. * @param {Array|Object|string} collection The collection to iterate over.
* @param {...(string|string[])} props The property names to sort by, * @param {...(Function|Function[]|Object|Object[]|string|string[])} iteratees
* specified as individual property names or arrays of property names. * The iteratees to sort by, specified as individual values or arrays of values.
* @returns {Array} Returns the new sorted array. * @returns {Array} Returns the new sorted array.
* @example * @example
* *
* var users = [ * var users = [
* { 'user': 'fred', 'age': 48 },
* { 'user': 'barney', 'age': 36 }, * { 'user': 'barney', 'age': 36 },
* { 'user': 'fred', 'age': 40 }, * { 'user': 'fred', 'age': 42 },
* { 'user': 'barney', 'age': 26 }, * { 'user': 'barney', 'age': 34 }
* { 'user': 'fred', 'age': 30 }
* ]; * ];
* *
* _.map(_.sortByAll(users, ['user', 'age']), _.values); * _.map(_.sortByAll(users, ['user', 'age']), _.values);
* // => [['barney', 26], ['barney', 36], ['fred', 30], ['fred', 40]] * // => [['barney', 34], ['barney', 36], ['fred', 42], ['fred', 48]]
*
* _.map(_.sortByAll(users, 'user', function(chr) {
* return Math.floor(chr.age / 10);
* }), _.values);
* // => [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]]
*/ */
function sortByAll(collection) { var sortByAll = restParam(function(collection, iteratees) {
var args = arguments; if (collection == null) {
if (args.length > 3 && isIterateeCall(args[1], args[2], args[3])) { return [];
args = [collection, args[1]];
} }
var index = -1, var guard = iteratees[2];
length = collection ? collection.length : 0, if (guard && isIterateeCall(iteratees[0], iteratees[1], guard)) {
props = baseFlatten(args, false, false, 1), iteratees.length = 1;
result = isLength(length) ? Array(length) : []; }
return baseSortByOrder(collection, baseFlatten(iteratees), []);
baseEach(collection, function(value) { });
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);
}
module.exports = sortByAll; module.exports = sortByAll;

55
collection/sortByOrder.js Normal file
View File

@@ -0,0 +1,55 @@
var baseSortByOrder = require('../internal/baseSortByOrder'),
isArray = require('../lang/isArray'),
isIterateeCall = require('../internal/isIterateeCall');
/**
* This method is like `_.sortByAll` except that it allows specifying the
* sort orders of the iteratees to sort by. A truthy value in `orders` will
* sort the corresponding property name in ascending order while a falsey
* value will sort it in descending order.
*
* If a property name is provided for an iteratee the created `_.property`
* style callback returns the property value of the given element.
*
* If an object is provided for an iteratee 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[]} iteratees The iteratees to sort by.
* @param {boolean[]} orders The sort orders of `iteratees`.
* @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`.
* @returns {Array} Returns the new sorted array.
* @example
*
* var users = [
* { 'user': 'fred', 'age': 48 },
* { 'user': 'barney', 'age': 34 },
* { 'user': 'fred', 'age': 42 },
* { 'user': 'barney', 'age': 36 }
* ];
*
* // sort by `user` in ascending order and by `age` in descending order
* _.map(_.sortByOrder(users, ['user', 'age'], [true, false]), _.values);
* // => [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]]
*/
function sortByOrder(collection, iteratees, orders, guard) {
if (collection == null) {
return [];
}
if (guard && isIterateeCall(iteratees, orders, guard)) {
orders = null;
}
if (!isArray(iteratees)) {
iteratees = iteratees == null ? [] : [iteratees];
}
if (!isArray(orders)) {
orders = orders == null ? [] : [orders];
}
return baseSortByOrder(collection, iteratees, orders);
}
module.exports = sortByOrder;

1
collection/sum.js Normal file
View File

@@ -0,0 +1 @@
module.exports = require('../math/sum');

View File

@@ -1,7 +1,7 @@
var isNative = require('../lang/isNative'); var getNative = require('../internal/getNative');
/* Native method references for those with the same name as other `lodash` methods. */ /* Native method references for those with the same name as other `lodash` methods. */
var nativeNow = isNative(nativeNow = Date.now) && nativeNow; var nativeNow = getNative(Date, 'now');
/** /**
* Gets the number of milliseconds that have elapsed since the Unix epoch * Gets the number of milliseconds that have elapsed since the Unix epoch
@@ -12,7 +12,9 @@ var nativeNow = isNative(nativeNow = Date.now) && nativeNow;
* @category Date * @category Date
* @example * @example
* *
* _.defer(function(stamp) { console.log(_.now() - stamp); }, _.now()); * _.defer(function(stamp) {
* console.log(_.now() - stamp);
* }, _.now());
* // => logs the number of milliseconds it took for the deferred function to be invoked * // => logs the number of milliseconds it took for the deferred function to be invoked
*/ */
var now = nativeNow || function() { var now = nativeNow || function() {

View File

@@ -20,6 +20,7 @@ module.exports = {
'partial': require('./function/partial'), 'partial': require('./function/partial'),
'partialRight': require('./function/partialRight'), 'partialRight': require('./function/partialRight'),
'rearg': require('./function/rearg'), 'rearg': require('./function/rearg'),
'restParam': require('./function/restParam'),
'spread': require('./function/spread'), 'spread': require('./function/spread'),
'throttle': require('./function/throttle'), 'throttle': require('./function/throttle'),
'wrap': require('./function/wrap') 'wrap': require('./function/wrap')

View File

@@ -2,7 +2,7 @@ var createWrapper = require('../internal/createWrapper'),
isIterateeCall = require('../internal/isIterateeCall'); isIterateeCall = require('../internal/isIterateeCall');
/** Used to compose bitmasks for wrapper metadata. */ /** Used to compose bitmasks for wrapper metadata. */
var ARY_FLAG = 256; var ARY_FLAG = 128;
/* Native method references for those with the same name as other `lodash` methods. */ /* Native method references for those with the same name as other `lodash` methods. */
var nativeMax = Math.max; var nativeMax = Math.max;

View File

@@ -31,7 +31,8 @@ function before(n, func) {
return function() { return function() {
if (--n > 0) { if (--n > 0) {
result = func.apply(this, arguments); result = func.apply(this, arguments);
} else { }
if (n <= 1) {
func = null; func = null;
} }
return result; return result;

View File

@@ -1,6 +1,6 @@
var baseSlice = require('../internal/baseSlice'), var createWrapper = require('../internal/createWrapper'),
createWrapper = require('../internal/createWrapper'), replaceHolders = require('../internal/replaceHolders'),
replaceHolders = require('../internal/replaceHolders'); restParam = require('./restParam');
/** Used to compose bitmasks for wrapper metadata. */ /** Used to compose bitmasks for wrapper metadata. */
var BIND_FLAG = 1, var BIND_FLAG = 1,
@@ -14,7 +14,7 @@ var BIND_FLAG = 1,
* The `_.bind.placeholder` value, which defaults to `_` in monolithic builds, * The `_.bind.placeholder` value, which defaults to `_` in monolithic builds,
* may be used as a placeholder for partially applied arguments. * may be used as a placeholder for partially applied arguments.
* *
* **Note:** Unlike native `Function#bind` this method does not set the `length` * **Note:** Unlike native `Function#bind` this method does not set the "length"
* property of bound functions. * property of bound functions.
* *
* @static * @static
@@ -22,7 +22,7 @@ var BIND_FLAG = 1,
* @category Function * @category Function
* @param {Function} func The function to bind. * @param {Function} func The function to bind.
* @param {*} thisArg The `this` binding of `func`. * @param {*} thisArg The `this` binding of `func`.
* @param {...*} [args] The arguments to be partially applied. * @param {...*} [partials] The arguments to be partially applied.
* @returns {Function} Returns the new bound function. * @returns {Function} Returns the new bound function.
* @example * @example
* *
@@ -41,16 +41,14 @@ var BIND_FLAG = 1,
* bound('hi'); * bound('hi');
* // => 'hi fred!' * // => 'hi fred!'
*/ */
function bind(func, thisArg) { var bind = restParam(function(func, thisArg, partials) {
var bitmask = BIND_FLAG; var bitmask = BIND_FLAG;
if (arguments.length > 2) { if (partials.length) {
var partials = baseSlice(arguments, 2), var holders = replaceHolders(partials, bind.placeholder);
holders = replaceHolders(partials, bind.placeholder);
bitmask |= PARTIAL_FLAG; bitmask |= PARTIAL_FLAG;
} }
return createWrapper(func, bitmask, thisArg, partials, holders); return createWrapper(func, bitmask, thisArg, partials, holders);
} });
// Assign default placeholders. // Assign default placeholders.
bind.placeholder = {}; bind.placeholder = {};

View File

@@ -1,6 +1,10 @@
var baseBindAll = require('../internal/baseBindAll'), var baseFlatten = require('../internal/baseFlatten'),
baseFlatten = require('../internal/baseFlatten'), createWrapper = require('../internal/createWrapper'),
functions = require('../object/functions'); functions = require('../object/functions'),
restParam = require('./restParam');
/** Used to compose bitmasks for wrapper metadata. */
var BIND_FLAG = 1;
/** /**
* Binds methods of an object to the object itself, overwriting the existing * Binds methods of an object to the object itself, overwriting the existing
@@ -8,7 +12,7 @@ var baseBindAll = require('../internal/baseBindAll'),
* of method names. If no method names are provided all enumerable function * of method names. If no method names are provided all enumerable function
* properties, own and inherited, of `object` are bound. * properties, own and inherited, of `object` are bound.
* *
* **Note:** This method does not set the `length` property of bound functions. * **Note:** This method does not set the "length" property of bound functions.
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -21,19 +25,26 @@ var baseBindAll = require('../internal/baseBindAll'),
* *
* var view = { * var view = {
* 'label': 'docs', * 'label': 'docs',
* 'onClick': function() { console.log('clicked ' + this.label); } * 'onClick': function() {
* console.log('clicked ' + this.label);
* }
* }; * };
* *
* _.bindAll(view); * _.bindAll(view);
* jQuery('#docs').on('click', view.onClick); * jQuery('#docs').on('click', view.onClick);
* // => logs 'clicked docs' when the element is clicked * // => logs 'clicked docs' when the element is clicked
*/ */
function bindAll(object) { var bindAll = restParam(function(object, methodNames) {
return baseBindAll(object, methodNames = methodNames.length ? baseFlatten(methodNames) : functions(object);
arguments.length > 1
? baseFlatten(arguments, false, false, 1) var index = -1,
: functions(object) length = methodNames.length;
);
} while (++index < length) {
var key = methodNames[index];
object[key] = createWrapper(object[key], BIND_FLAG, object);
}
return object;
});
module.exports = bindAll; module.exports = bindAll;

View File

@@ -1,6 +1,6 @@
var baseSlice = require('../internal/baseSlice'), var createWrapper = require('../internal/createWrapper'),
createWrapper = require('../internal/createWrapper'), replaceHolders = require('../internal/replaceHolders'),
replaceHolders = require('../internal/replaceHolders'); restParam = require('./restParam');
/** Used to compose bitmasks for wrapper metadata. */ /** Used to compose bitmasks for wrapper metadata. */
var BIND_FLAG = 1, var BIND_FLAG = 1,
@@ -13,7 +13,7 @@ var BIND_FLAG = 1,
* *
* This method differs from `_.bind` by allowing bound functions to reference * This method differs from `_.bind` by allowing bound functions to reference
* methods that may be redefined or don't yet exist. * methods that may be redefined or don't yet exist.
* See [Peter Michaux's article](http://michaux.ca/articles/lazy-function-definition-pattern) * See [Peter Michaux's article](http://peter.michaux.ca/articles/lazy-function-definition-pattern)
* for more details. * for more details.
* *
* The `_.bindKey.placeholder` value, which defaults to `_` in monolithic * The `_.bindKey.placeholder` value, which defaults to `_` in monolithic
@@ -24,7 +24,7 @@ var BIND_FLAG = 1,
* @category Function * @category Function
* @param {Object} object The object the method belongs to. * @param {Object} object The object the method belongs to.
* @param {string} key The key of the method. * @param {string} key The key of the method.
* @param {...*} [args] The arguments to be partially applied. * @param {...*} [partials] The arguments to be partially applied.
* @returns {Function} Returns the new bound function. * @returns {Function} Returns the new bound function.
* @example * @example
* *
@@ -51,16 +51,14 @@ var BIND_FLAG = 1,
* bound('hi'); * bound('hi');
* // => 'hiya fred!' * // => 'hiya fred!'
*/ */
function bindKey(object, key) { var bindKey = restParam(function(object, key, partials) {
var bitmask = BIND_FLAG | BIND_KEY_FLAG; var bitmask = BIND_FLAG | BIND_KEY_FLAG;
if (arguments.length > 2) { if (partials.length) {
var partials = baseSlice(arguments, 2), var holders = replaceHolders(partials, bindKey.placeholder);
holders = replaceHolders(partials, bindKey.placeholder);
bitmask |= PARTIAL_FLAG; bitmask |= PARTIAL_FLAG;
} }
return createWrapper(key, bitmask, object, partials, holders); return createWrapper(key, bitmask, object, partials, holders);
} });
// Assign default placeholders. // Assign default placeholders.
bindKey.placeholder = {}; bindKey.placeholder = {};

View File

@@ -1,5 +1,4 @@
var createWrapper = require('../internal/createWrapper'), var createCurry = require('../internal/createCurry');
isIterateeCall = require('../internal/isIterateeCall');
/** Used to compose bitmasks for wrapper metadata. */ /** Used to compose bitmasks for wrapper metadata. */
var CURRY_FLAG = 8; var CURRY_FLAG = 8;
@@ -14,7 +13,7 @@ var CURRY_FLAG = 8;
* The `_.curry.placeholder` value, which defaults to `_` in monolithic builds, * The `_.curry.placeholder` value, which defaults to `_` in monolithic builds,
* may be used as a placeholder for provided arguments. * may be used as a placeholder for provided arguments.
* *
* **Note:** This method does not set the `length` property of curried functions. * **Note:** This method does not set the "length" property of curried functions.
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -44,14 +43,7 @@ var CURRY_FLAG = 8;
* curried(1)(_, 3)(2); * curried(1)(_, 3)(2);
* // => [1, 2, 3] * // => [1, 2, 3]
*/ */
function curry(func, arity, guard) { var curry = createCurry(CURRY_FLAG);
if (guard && isIterateeCall(func, arity, guard)) {
arity = null;
}
var result = createWrapper(func, CURRY_FLAG, null, null, null, null, null, arity);
result.placeholder = curry.placeholder;
return result;
}
// Assign default placeholders. // Assign default placeholders.
curry.placeholder = {}; curry.placeholder = {};

View File

@@ -1,5 +1,4 @@
var createWrapper = require('../internal/createWrapper'), var createCurry = require('../internal/createCurry');
isIterateeCall = require('../internal/isIterateeCall');
/** Used to compose bitmasks for wrapper metadata. */ /** Used to compose bitmasks for wrapper metadata. */
var CURRY_RIGHT_FLAG = 16; var CURRY_RIGHT_FLAG = 16;
@@ -11,7 +10,7 @@ var CURRY_RIGHT_FLAG = 16;
* The `_.curryRight.placeholder` value, which defaults to `_` in monolithic * The `_.curryRight.placeholder` value, which defaults to `_` in monolithic
* builds, may be used as a placeholder for provided arguments. * builds, may be used as a placeholder for provided arguments.
* *
* **Note:** This method does not set the `length` property of curried functions. * **Note:** This method does not set the "length" property of curried functions.
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -41,14 +40,7 @@ var CURRY_RIGHT_FLAG = 16;
* curried(3)(1, _)(2); * curried(3)(1, _)(2);
* // => [1, 2, 3] * // => [1, 2, 3]
*/ */
function curryRight(func, arity, guard) { var curryRight = createCurry(CURRY_RIGHT_FLAG);
if (guard && isIterateeCall(func, arity, guard)) {
arity = null;
}
var result = createWrapper(func, CURRY_RIGHT_FLAG, null, null, null, null, null, arity);
result.placeholder = curryRight.placeholder;
return result;
}
// Assign default placeholders. // Assign default placeholders.
curryRight.placeholder = {}; curryRight.placeholder = {};

View File

@@ -8,12 +8,13 @@ var FUNC_ERROR_TEXT = 'Expected a function';
var nativeMax = Math.max; var nativeMax = Math.max;
/** /**
* Creates a function that delays invoking `func` until after `wait` milliseconds * Creates a debounced function that delays invoking `func` until after `wait`
* have elapsed since the last time it was invoked. The created function comes * milliseconds have elapsed since the last time the debounced function was
* with a `cancel` method to cancel delayed invocations. Provide an options * invoked. The debounced function comes with a `cancel` method to cancel
* object to indicate that `func` should be invoked on the leading and/or * delayed invocations. Provide an options object to indicate that `func`
* trailing edge of the `wait` timeout. Subsequent calls to the debounced * should be invoked on the leading and/or trailing edge of the `wait` timeout.
* function return the result of the last `func` invocation. * Subsequent calls to the debounced function return the result of the last
* `func` invocation.
* *
* **Note:** If `leading` and `trailing` options are `true`, `func` is invoked * **Note:** If `leading` and `trailing` options are `true`, `func` is invoked
* on the trailing edge of the timeout only if the the debounced function is * on the trailing edge of the timeout only if the the debounced function is
@@ -26,7 +27,7 @@ var nativeMax = Math.max;
* @memberOf _ * @memberOf _
* @category Function * @category Function
* @param {Function} func The function to debounce. * @param {Function} func The function to debounce.
* @param {number} wait The number of milliseconds to delay. * @param {number} [wait=0] The number of milliseconds to delay.
* @param {Object} [options] The options object. * @param {Object} [options] The options object.
* @param {boolean} [options.leading=false] Specify invoking on the leading * @param {boolean} [options.leading=false] Specify invoking on the leading
* edge of the timeout. * edge of the timeout.
@@ -84,7 +85,7 @@ function debounce(func, wait, options) {
if (typeof func != 'function') { if (typeof func != 'function') {
throw new TypeError(FUNC_ERROR_TEXT); throw new TypeError(FUNC_ERROR_TEXT);
} }
wait = wait < 0 ? 0 : wait; wait = wait < 0 ? 0 : (+wait || 0);
if (options === true) { if (options === true) {
var leading = true; var leading = true;
trailing = false; trailing = false;

View File

@@ -1,4 +1,5 @@
var baseDelay = require('../internal/baseDelay'); var baseDelay = require('../internal/baseDelay'),
restParam = require('./restParam');
/** /**
* Defers invoking the `func` until the current call stack has cleared. Any * Defers invoking the `func` until the current call stack has cleared. Any
@@ -12,11 +13,13 @@ var baseDelay = require('../internal/baseDelay');
* @returns {number} Returns the timer id. * @returns {number} Returns the timer id.
* @example * @example
* *
* _.defer(function(text) { console.log(text); }, 'deferred'); * _.defer(function(text) {
* console.log(text);
* }, 'deferred');
* // logs 'deferred' after one or more milliseconds * // logs 'deferred' after one or more milliseconds
*/ */
function defer(func) { var defer = restParam(function(func, args) {
return baseDelay(func, 1, arguments, 1); return baseDelay(func, 1, args);
} });
module.exports = defer; module.exports = defer;

View File

@@ -1,4 +1,5 @@
var baseDelay = require('../internal/baseDelay'); var baseDelay = require('../internal/baseDelay'),
restParam = require('./restParam');
/** /**
* Invokes `func` after `wait` milliseconds. Any additional arguments are * Invokes `func` after `wait` milliseconds. Any additional arguments are
@@ -13,11 +14,13 @@ var baseDelay = require('../internal/baseDelay');
* @returns {number} Returns the timer id. * @returns {number} Returns the timer id.
* @example * @example
* *
* _.delay(function(text) { console.log(text); }, 1000, 'later'); * _.delay(function(text) {
* console.log(text);
* }, 1000, 'later');
* // => logs 'later' after one second * // => logs 'later' after one second
*/ */
function delay(func, wait) { var delay = restParam(function(func, wait, args) {
return baseDelay(func, wait, arguments, 2); return baseDelay(func, wait, args);
} });
module.exports = delay; module.exports = delay;

View File

@@ -1,8 +1,4 @@
var arrayEvery = require('../internal/arrayEvery'), var createFlow = require('../internal/createFlow');
isFunction = require('../lang/isFunction');
/** Used as the `TypeError` message for "Functions" methods. */
var FUNC_ERROR_TEXT = 'Expected a function';
/** /**
* Creates a function that returns the result of invoking the provided * Creates a function that returns the result of invoking the provided
@@ -16,37 +12,14 @@ var FUNC_ERROR_TEXT = 'Expected a function';
* @returns {Function} Returns the new function. * @returns {Function} Returns the new function.
* @example * @example
* *
* function add(x, y) {
* return x + y;
* }
*
* function square(n) { * function square(n) {
* return n * n; * return n * n;
* } * }
* *
* var addSquare = _.flow(add, square); * var addSquare = _.flow(_.add, square);
* addSquare(1, 2); * addSquare(1, 2);
* // => 9 * // => 9
*/ */
function flow() { var flow = createFlow();
var funcs = arguments,
length = funcs.length;
if (!length) {
return function() { return arguments[0]; };
}
if (!arrayEvery(funcs, isFunction)) {
throw new TypeError(FUNC_ERROR_TEXT);
}
return function() {
var index = 0,
result = funcs[index].apply(this, arguments);
while (++index < length) {
result = funcs[index].call(this, result);
}
return result;
};
}
module.exports = flow; module.exports = flow;

View File

@@ -1,8 +1,4 @@
var arrayEvery = require('../internal/arrayEvery'), var createFlow = require('../internal/createFlow');
isFunction = require('../lang/isFunction');
/** Used as the `TypeError` message for "Functions" methods. */
var FUNC_ERROR_TEXT = 'Expected a function';
/** /**
* This method is like `_.flow` except that it creates a function that * This method is like `_.flow` except that it creates a function that
@@ -16,37 +12,14 @@ var FUNC_ERROR_TEXT = 'Expected a function';
* @returns {Function} Returns the new function. * @returns {Function} Returns the new function.
* @example * @example
* *
* function add(x, y) {
* return x + y;
* }
*
* function square(n) { * function square(n) {
* return n * n; * return n * n;
* } * }
* *
* var addSquare = _.flowRight(square, add); * var addSquare = _.flowRight(square, _.add);
* addSquare(1, 2); * addSquare(1, 2);
* // => 9 * // => 9
*/ */
function flowRight() { var flowRight = createFlow(true);
var funcs = arguments,
fromIndex = funcs.length - 1;
if (fromIndex < 0) {
return function() { return arguments[0]; };
}
if (!arrayEvery(funcs, isFunction)) {
throw new TypeError(FUNC_ERROR_TEXT);
}
return function() {
var index = fromIndex,
result = funcs[index].apply(this, arguments);
while (index--) {
result = funcs[index].call(this, result);
}
return result;
};
}
module.exports = flowRight; module.exports = flowRight;

View File

@@ -13,10 +13,8 @@ var FUNC_ERROR_TEXT = 'Expected a function';
* *
* **Note:** The cache is exposed as the `cache` property on the memoized * **Note:** The cache is exposed as the `cache` property on the memoized
* function. Its creation may be customized by replacing the `_.memoize.Cache` * function. Its creation may be customized by replacing the `_.memoize.Cache`
* constructor with one whose instances implement the ES `Map` method interface * constructor with one whose instances implement the [`Map`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-properties-of-the-map-prototype-object)
* of `get`, `has`, and `set`. See the * method interface of `get`, `has`, and `set`.
* [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-properties-of-the-map-prototype-object)
* for more details.
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -61,14 +59,15 @@ function memoize(func, resolver) {
throw new TypeError(FUNC_ERROR_TEXT); throw new TypeError(FUNC_ERROR_TEXT);
} }
var memoized = function() { var memoized = function() {
var cache = memoized.cache, var args = arguments,
key = resolver ? resolver.apply(this, arguments) : arguments[0]; key = resolver ? resolver.apply(this, args) : args[0],
cache = memoized.cache;
if (cache.has(key)) { if (cache.has(key)) {
return cache.get(key); return cache.get(key);
} }
var result = func.apply(this, arguments); var result = func.apply(this, args);
cache.set(key, result); memoized.cache = cache.set(key, result);
return result; return result;
}; };
memoized.cache = new memoize.Cache; memoized.cache = new memoize.Cache;

View File

@@ -3,7 +3,7 @@ var before = require('./before');
/** /**
* Creates a function that is restricted to invoking `func` once. Repeat calls * Creates a function that is restricted to invoking `func` once. Repeat calls
* to the function return the value of the first call. The `func` is invoked * to the function return the value of the first call. The `func` is invoked
* with the `this` binding of the created function. * with the `this` binding and arguments of the created function.
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -18,7 +18,7 @@ var before = require('./before');
* // `initialize` invokes `createApplication` once * // `initialize` invokes `createApplication` once
*/ */
function once(func) { function once(func) {
return before(func, 2); return before(2, func);
} }
module.exports = once; module.exports = once;

View File

@@ -1,6 +1,4 @@
var baseSlice = require('../internal/baseSlice'), var createPartial = require('../internal/createPartial');
createWrapper = require('../internal/createWrapper'),
replaceHolders = require('../internal/replaceHolders');
/** Used to compose bitmasks for wrapper metadata. */ /** Used to compose bitmasks for wrapper metadata. */
var PARTIAL_FLAG = 32; var PARTIAL_FLAG = 32;
@@ -13,14 +11,14 @@ var PARTIAL_FLAG = 32;
* The `_.partial.placeholder` value, which defaults to `_` in monolithic * The `_.partial.placeholder` value, which defaults to `_` in monolithic
* builds, may be used as a placeholder for partially applied arguments. * builds, may be used as a placeholder for partially applied arguments.
* *
* **Note:** This method does not set the `length` property of partially * **Note:** This method does not set the "length" property of partially
* applied functions. * applied functions.
* *
* @static * @static
* @memberOf _ * @memberOf _
* @category Function * @category Function
* @param {Function} func The function to partially apply arguments to. * @param {Function} func The function to partially apply arguments to.
* @param {...*} [args] The arguments to be partially applied. * @param {...*} [partials] The arguments to be partially applied.
* @returns {Function} Returns the new partially applied function. * @returns {Function} Returns the new partially applied function.
* @example * @example
* *
@@ -37,12 +35,7 @@ var PARTIAL_FLAG = 32;
* greetFred('hi'); * greetFred('hi');
* // => 'hi fred' * // => 'hi fred'
*/ */
function partial(func) { var partial = createPartial(PARTIAL_FLAG);
var partials = baseSlice(arguments, 1),
holders = replaceHolders(partials, partial.placeholder);
return createWrapper(func, PARTIAL_FLAG, null, partials, holders);
}
// Assign default placeholders. // Assign default placeholders.
partial.placeholder = {}; partial.placeholder = {};

View File

@@ -1,6 +1,4 @@
var baseSlice = require('../internal/baseSlice'), var createPartial = require('../internal/createPartial');
createWrapper = require('../internal/createWrapper'),
replaceHolders = require('../internal/replaceHolders');
/** Used to compose bitmasks for wrapper metadata. */ /** Used to compose bitmasks for wrapper metadata. */
var PARTIAL_RIGHT_FLAG = 64; var PARTIAL_RIGHT_FLAG = 64;
@@ -12,14 +10,14 @@ var PARTIAL_RIGHT_FLAG = 64;
* The `_.partialRight.placeholder` value, which defaults to `_` in monolithic * The `_.partialRight.placeholder` value, which defaults to `_` in monolithic
* builds, may be used as a placeholder for partially applied arguments. * builds, may be used as a placeholder for partially applied arguments.
* *
* **Note:** This method does not set the `length` property of partially * **Note:** This method does not set the "length" property of partially
* applied functions. * applied functions.
* *
* @static * @static
* @memberOf _ * @memberOf _
* @category Function * @category Function
* @param {Function} func The function to partially apply arguments to. * @param {Function} func The function to partially apply arguments to.
* @param {...*} [args] The arguments to be partially applied. * @param {...*} [partials] The arguments to be partially applied.
* @returns {Function} Returns the new partially applied function. * @returns {Function} Returns the new partially applied function.
* @example * @example
* *
@@ -36,12 +34,7 @@ var PARTIAL_RIGHT_FLAG = 64;
* sayHelloTo('fred'); * sayHelloTo('fred');
* // => 'hello fred' * // => 'hello fred'
*/ */
function partialRight(func) { var partialRight = createPartial(PARTIAL_RIGHT_FLAG);
var partials = baseSlice(arguments, 1),
holders = replaceHolders(partials, partialRight.placeholder);
return createWrapper(func, PARTIAL_RIGHT_FLAG, null, partials, holders);
}
// Assign default placeholders. // Assign default placeholders.
partialRight.placeholder = {}; partialRight.placeholder = {};

View File

@@ -1,8 +1,9 @@
var baseFlatten = require('../internal/baseFlatten'), var baseFlatten = require('../internal/baseFlatten'),
createWrapper = require('../internal/createWrapper'); createWrapper = require('../internal/createWrapper'),
restParam = require('./restParam');
/** Used to compose bitmasks for wrapper metadata. */ /** Used to compose bitmasks for wrapper metadata. */
var REARG_FLAG = 128; var REARG_FLAG = 256;
/** /**
* Creates a function that invokes `func` with arguments arranged according * Creates a function that invokes `func` with arguments arranged according
@@ -27,12 +28,13 @@ var REARG_FLAG = 128;
* // => ['a', 'b', 'c'] * // => ['a', 'b', 'c']
* *
* var map = _.rearg(_.map, [1, 0]); * var map = _.rearg(_.map, [1, 0]);
* map(function(n) { return n * 3; }, [1, 2, 3]); * map(function(n) {
* return n * 3;
* }, [1, 2, 3]);
* // => [3, 6, 9] * // => [3, 6, 9]
*/ */
function rearg(func) { var rearg = restParam(function(func, indexes) {
var indexes = baseFlatten(arguments, false, false, 1); return createWrapper(func, REARG_FLAG, null, null, null, baseFlatten(indexes));
return createWrapper(func, REARG_FLAG, null, null, null, indexes); });
}
module.exports = rearg; module.exports = rearg;

58
function/restParam.js Normal file
View File

@@ -0,0 +1,58 @@
/** Used as the `TypeError` message for "Functions" methods. */
var FUNC_ERROR_TEXT = 'Expected a function';
/* Native method references for those with the same name as other `lodash` methods. */
var nativeMax = Math.max;
/**
* Creates a function that invokes `func` with the `this` binding of the
* created function and arguments from `start` and beyond provided as an array.
*
* **Note:** This method is based on the [rest parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters).
*
* @static
* @memberOf _
* @category Function
* @param {Function} func The function to apply a rest parameter to.
* @param {number} [start=func.length-1] The start position of the rest parameter.
* @returns {Function} Returns the new function.
* @example
*
* var say = _.restParam(function(what, names) {
* return what + ' ' + _.initial(names).join(', ') +
* (_.size(names) > 1 ? ', & ' : '') + _.last(names);
* });
*
* say('hello', 'fred', 'barney', 'pebbles');
* // => 'hello fred, barney, & pebbles'
*/
function restParam(func, start) {
if (typeof func != 'function') {
throw new TypeError(FUNC_ERROR_TEXT);
}
start = nativeMax(start === undefined ? (func.length - 1) : (+start || 0), 0);
return function() {
var args = arguments,
index = -1,
length = nativeMax(args.length - start, 0),
rest = Array(length);
while (++index < length) {
rest[index] = args[start + index];
}
switch (start) {
case 0: return func.call(this, rest);
case 1: return func.call(this, args[0], rest);
case 2: return func.call(this, args[0], args[1], rest);
}
var otherArgs = Array(start + 1);
index = -1;
while (++index < start) {
otherArgs[index] = args[index];
}
otherArgs[start] = rest;
return func.apply(this, otherArgs);
};
}
module.exports = restParam;

View File

@@ -2,23 +2,24 @@
var FUNC_ERROR_TEXT = 'Expected a function'; var FUNC_ERROR_TEXT = 'Expected a function';
/** /**
* Creates a function that invokes `func` with the `this` binding of the * Creates a function that invokes `func` with the `this` binding of the created
* created function and the array of arguments provided to the created * function and an array of arguments much like [`Function#apply`](https://es5.github.io/#x15.3.4.3).
* function much like [Function#apply](http://es5.github.io/#x15.3.4.3). *
* **Note:** This method is based on the [spread operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator).
* *
* @static * @static
* @memberOf _ * @memberOf _
* @category Function * @category Function
* @param {Function} func The function to spread arguments over. * @param {Function} func The function to spread arguments over.
* @returns {*} Returns the new function. * @returns {Function} Returns the new function.
* @example * @example
* *
* var spread = _.spread(function(who, what) { * var say = _.spread(function(who, what) {
* return who + ' says ' + what; * return who + ' says ' + what;
* }); * });
* *
* spread(['Fred', 'hello']); * say(['fred', 'hello']);
* // => 'Fred says hello' * // => 'fred says hello'
* *
* // with a Promise * // with a Promise
* var numbers = Promise.all([ * var numbers = Promise.all([

View File

@@ -12,12 +12,12 @@ var debounceOptions = {
}; };
/** /**
* Creates a function that only invokes `func` at most once per every `wait` * Creates a throttled function that only invokes `func` at most once per
* milliseconds. The created function comes with a `cancel` method to cancel * every `wait` milliseconds. The throttled function comes with a `cancel`
* delayed invocations. Provide an options object to indicate that `func` * method to cancel delayed invocations. Provide an options object to indicate
* should be invoked on the leading and/or trailing edge of the `wait` timeout. * that `func` should be invoked on the leading and/or trailing edge of the
* Subsequent calls to the throttled function return the result of the last * `wait` timeout. Subsequent calls to the throttled function return the
* `func` call. * result of the last `func` call.
* *
* **Note:** If `leading` and `trailing` options are `true`, `func` is invoked * **Note:** If `leading` and `trailing` options are `true`, `func` is invoked
* on the trailing edge of the timeout only if the the throttled function is * on the trailing edge of the timeout only if the the throttled function is
@@ -30,7 +30,7 @@ var debounceOptions = {
* @memberOf _ * @memberOf _
* @category Function * @category Function
* @param {Function} func The function to throttle. * @param {Function} func The function to throttle.
* @param {number} wait The number of milliseconds to throttle invocations to. * @param {number} [wait=0] The number of milliseconds to throttle invocations to.
* @param {Object} [options] The options object. * @param {Object} [options] The options object.
* @param {boolean} [options.leading=true] Specify invoking on the leading * @param {boolean} [options.leading=true] Specify invoking on the leading
* edge of the timeout. * edge of the timeout.
@@ -43,8 +43,9 @@ var debounceOptions = {
* jQuery(window).on('scroll', _.throttle(updatePosition, 100)); * jQuery(window).on('scroll', _.throttle(updatePosition, 100));
* *
* // invoke `renewToken` when the click event is fired, but not more than once every 5 minutes * // invoke `renewToken` when the click event is fired, but not more than once every 5 minutes
* var throttled = _.throttle(renewToken, 300000, { 'trailing': false }) * jQuery('.interactive').on('click', _.throttle(renewToken, 300000, {
* jQuery('.interactive').on('click', throttled); * 'trailing': false
* }));
* *
* // cancel a trailing throttled call * // cancel a trailing throttled call
* jQuery(window).on('popstate', throttled.cancel); * jQuery(window).on('popstate', throttled.cancel);

4727
index.js

File diff suppressed because it is too large Load Diff

View File

@@ -1,3 +1,6 @@
var baseCreate = require('./baseCreate'),
baseLodash = require('./baseLodash');
/** Used as references for `-Infinity` and `Infinity`. */ /** Used as references for `-Infinity` and `Infinity`. */
var POSITIVE_INFINITY = Number.POSITIVE_INFINITY; var POSITIVE_INFINITY = Number.POSITIVE_INFINITY;
@@ -18,4 +21,7 @@ function LazyWrapper(value) {
this.__views__ = null; this.__views__ = null;
} }
LazyWrapper.prototype = baseCreate(baseLodash.prototype);
LazyWrapper.prototype.constructor = LazyWrapper;
module.exports = LazyWrapper; module.exports = LazyWrapper;

View File

@@ -1,3 +1,6 @@
var baseCreate = require('./baseCreate'),
baseLodash = require('./baseLodash');
/** /**
* The base constructor for creating `lodash` wrapper objects. * The base constructor for creating `lodash` wrapper objects.
* *
@@ -12,4 +15,7 @@ function LodashWrapper(value, chainAll, actions) {
this.__chain__ = !!chainAll; this.__chain__ = !!chainAll;
} }
LodashWrapper.prototype = baseCreate(baseLodash.prototype);
LodashWrapper.prototype.constructor = LodashWrapper;
module.exports = LodashWrapper; module.exports = LodashWrapper;

View File

@@ -1,11 +1,11 @@
var cachePush = require('./cachePush'), var cachePush = require('./cachePush'),
isNative = require('../lang/isNative'); getNative = require('./getNative');
/** Native method references. */ /** Native method references. */
var Set = isNative(Set = global.Set) && Set; var Set = getNative(root, 'Set');
/* Native method references for those with the same name as other `lodash` methods. */ /* Native method references for those with the same name as other `lodash` methods. */
var nativeCreate = isNative(nativeCreate = Object.create) && nativeCreate; var nativeCreate = getNative(Object, 'create');
/** /**
* *

View File

@@ -1,6 +1,6 @@
/** /**
* A specialized version of `_.forEach` for arrays without support for callback * A specialized version of `_.forEach` for arrays without support for callback
* shorthands or `this` binding. * shorthands and `this` binding.
* *
* @private * @private
* @param {Array} array The array to iterate over. * @param {Array} array The array to iterate over.

View File

@@ -1,6 +1,6 @@
/** /**
* A specialized version of `_.forEachRight` for arrays without support for * A specialized version of `_.forEachRight` for arrays without support for
* callback shorthands or `this` binding. * callback shorthands and `this` binding.
* *
* @private * @private
* @param {Array} array The array to iterate over. * @param {Array} array The array to iterate over.

View File

@@ -1,6 +1,6 @@
/** /**
* A specialized version of `_.every` for arrays without support for callback * A specialized version of `_.every` for arrays without support for callback
* shorthands or `this` binding. * shorthands and `this` binding.
* *
* @private * @private
* @param {Array} array The array to iterate over. * @param {Array} array The array to iterate over.

30
internal/arrayExtremum.js Normal file
View File

@@ -0,0 +1,30 @@
/**
* A specialized version of `baseExtremum` for arrays whichs invokes `iteratee`
* with one argument: (value).
*
* @private
* @param {Array} array The array to iterate over.
* @param {Function} iteratee The function invoked per iteration.
* @param {Function} comparator The function used to compare values.
* @param {*} exValue The initial extremum value.
* @returns {*} Returns the extremum value.
*/
function arrayExtremum(array, iteratee, comparator, exValue) {
var index = -1,
length = array.length,
computed = exValue,
result = computed;
while (++index < length) {
var value = array[index],
current = +iteratee(value);
if (comparator(current, computed)) {
computed = current;
result = value;
}
}
return result;
}
module.exports = arrayExtremum;

View File

@@ -1,6 +1,6 @@
/** /**
* A specialized version of `_.filter` for arrays without support for callback * A specialized version of `_.filter` for arrays without support for callback
* shorthands or `this` binding. * shorthands and `this` binding.
* *
* @private * @private
* @param {Array} array The array to iterate over. * @param {Array} array The array to iterate over.

View File

@@ -1,6 +1,6 @@
/** /**
* A specialized version of `_.map` for arrays without support for callback * A specialized version of `_.map` for arrays without support for callback
* shorthands or `this` binding. * shorthands and `this` binding.
* *
* @private * @private
* @param {Array} array The array to iterate over. * @param {Array} array The array to iterate over.

View File

@@ -1,25 +0,0 @@
/** Used as references for `-Infinity` and `Infinity`. */
var NEGATIVE_INFINITY = Number.NEGATIVE_INFINITY;
/**
* A specialized version of `_.max` for arrays without support for iteratees.
*
* @private
* @param {Array} array The array to iterate over.
* @returns {*} Returns the maximum value.
*/
function arrayMax(array) {
var index = -1,
length = array.length,
result = NEGATIVE_INFINITY;
while (++index < length) {
var value = array[index];
if (value > result) {
result = value;
}
}
return result;
}
module.exports = arrayMax;

View File

@@ -1,25 +0,0 @@
/** Used as references for `-Infinity` and `Infinity`. */
var POSITIVE_INFINITY = Number.POSITIVE_INFINITY;
/**
* A specialized version of `_.min` for arrays without support for iteratees.
*
* @private
* @param {Array} array The array to iterate over.
* @returns {*} Returns the minimum value.
*/
function arrayMin(array) {
var index = -1,
length = array.length,
result = POSITIVE_INFINITY;
while (++index < length) {
var value = array[index];
if (value < result) {
result = value;
}
}
return result;
}
module.exports = arrayMin;

View File

@@ -1,6 +1,6 @@
/** /**
* A specialized version of `_.reduce` for arrays without support for callback * A specialized version of `_.reduce` for arrays without support for callback
* shorthands or `this` binding. * shorthands and `this` binding.
* *
* @private * @private
* @param {Array} array The array to iterate over. * @param {Array} array The array to iterate over.

View File

@@ -1,6 +1,6 @@
/** /**
* A specialized version of `_.reduceRight` for arrays without support for * A specialized version of `_.reduceRight` for arrays without support for
* callback shorthands or `this` binding. * callback shorthands and `this` binding.
* *
* @private * @private
* @param {Array} array The array to iterate over. * @param {Array} array The array to iterate over.

Some files were not shown because too many files have changed in this diff Show More