mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 10:07:48 +00:00
Bump to v3.0.0.
This commit is contained in:
31
utility/attempt.js
Normal file
31
utility/attempt.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import isError from '../lang/isError';
|
||||
|
||||
/**
|
||||
* Attempts to invoke `func`, returning either the result or the caught
|
||||
* error object.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Utility
|
||||
* @param {*} func The function to attempt.
|
||||
* @returns {*} Returns the `func` result or error object.
|
||||
* @example
|
||||
*
|
||||
* // avoid throwing errors for invalid selectors
|
||||
* var elements = _.attempt(function() {
|
||||
* return document.querySelectorAll(selector);
|
||||
* });
|
||||
*
|
||||
* if (_.isError(elements)) {
|
||||
* elements = [];
|
||||
* }
|
||||
*/
|
||||
function attempt(func) {
|
||||
try {
|
||||
return func();
|
||||
} catch(e) {
|
||||
return isError(e) ? e : Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
export default attempt;
|
||||
46
utility/callback.js
Normal file
46
utility/callback.js
Normal file
@@ -0,0 +1,46 @@
|
||||
import baseCallback from '../internal/baseCallback';
|
||||
import isIterateeCall from '../internal/isIterateeCall';
|
||||
|
||||
/**
|
||||
* Creates a function bound to an optional `thisArg`. If `func` is a property
|
||||
* name the created callback returns the property value for a given element.
|
||||
* If `func` is an object the created callback returns `true` for elements
|
||||
* that contain the equivalent object properties, otherwise it returns `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @alias iteratee
|
||||
* @category Utility
|
||||
* @param {*} [func=_.identity] The value to convert to a callback.
|
||||
* @param {*} [thisArg] The `this` binding of `func`.
|
||||
* @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
|
||||
* @returns {Function} Returns the callback.
|
||||
* @example
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'age': 36 },
|
||||
* { 'user': 'fred', 'age': 40 }
|
||||
* ];
|
||||
*
|
||||
* // wrap to create custom callback shorthands
|
||||
* _.callback = _.wrap(_.callback, function(callback, func, thisArg) {
|
||||
* var match = /^(.+?)__([gl]t)(.+)$/.exec(func);
|
||||
* if (!match) {
|
||||
* return callback(func, thisArg);
|
||||
* }
|
||||
* return function(object) {
|
||||
* return match[2] == 'gt' ? object[match[1]] > match[3] : object[match[1]] < match[3];
|
||||
* };
|
||||
* });
|
||||
*
|
||||
* _.filter(users, 'age__gt36');
|
||||
* // => [{ 'user': 'fred', 'age': 40 }]
|
||||
*/
|
||||
function callback(func, thisArg, guard) {
|
||||
if (guard && isIterateeCall(func, thisArg, guard)) {
|
||||
thisArg = null;
|
||||
}
|
||||
return baseCallback(func, thisArg);
|
||||
}
|
||||
|
||||
export default callback;
|
||||
22
utility/constant.js
Normal file
22
utility/constant.js
Normal file
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Creates a function that returns `value`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Utility
|
||||
* @param {*} value The value to return from the new function.
|
||||
* @returns {Function} Returns the new function.
|
||||
* @example
|
||||
*
|
||||
* var object = { 'user': 'fred' };
|
||||
* var getter = _.constant(object);
|
||||
* getter() === object;
|
||||
* // => true
|
||||
*/
|
||||
function constant(value) {
|
||||
return function() {
|
||||
return value;
|
||||
};
|
||||
}
|
||||
|
||||
export default constant;
|
||||
19
utility/identity.js
Normal file
19
utility/identity.js
Normal file
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* This method returns the first argument provided to it.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Utility
|
||||
* @param {*} value Any value.
|
||||
* @returns {*} Returns `value`.
|
||||
* @example
|
||||
*
|
||||
* var object = { 'user': 'fred' };
|
||||
* _.identity(object) === object;
|
||||
* // => true
|
||||
*/
|
||||
function identity(value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
export default identity;
|
||||
2
utility/iteratee.js
Normal file
2
utility/iteratee.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import callback from './callback'
|
||||
export default callback;
|
||||
32
utility/matches.js
Normal file
32
utility/matches.js
Normal file
@@ -0,0 +1,32 @@
|
||||
import baseMatches from '../internal/baseMatches';
|
||||
|
||||
/**
|
||||
* Creates a function which performs a deep comparison between a given object
|
||||
* and `source`, returning `true` if the given object has equivalent property
|
||||
* values, else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Utility
|
||||
* @param {Object} source The object of property values to match.
|
||||
* @returns {Function} Returns the new function.
|
||||
* @example
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'fred', 'age': 40 },
|
||||
* { 'user': 'barney', 'age': 36 }
|
||||
* ];
|
||||
*
|
||||
* var matchesAge = _.matches({ 'age': 36 });
|
||||
*
|
||||
* _.filter(users, matchesAge);
|
||||
* // => [{ 'user': 'barney', 'age': 36 }]
|
||||
*
|
||||
* _.find(users, matchesAge);
|
||||
* // => { 'user': 'barney', 'age': 36 }
|
||||
*/
|
||||
function matches(source) {
|
||||
return baseMatches(source, true);
|
||||
}
|
||||
|
||||
export default matches;
|
||||
84
utility/mixin.js
Normal file
84
utility/mixin.js
Normal file
@@ -0,0 +1,84 @@
|
||||
import arrayCopy from '../internal/arrayCopy';
|
||||
import baseFunctions from '../internal/baseFunctions';
|
||||
import isFunction from '../lang/isFunction';
|
||||
import isObject from '../lang/isObject';
|
||||
import keys from '../object/keys';
|
||||
|
||||
/** Used for native method references. */
|
||||
var arrayProto = Array.prototype;
|
||||
|
||||
/** Native method references. */
|
||||
var push = arrayProto.push;
|
||||
|
||||
/**
|
||||
* Adds all own enumerable function properties of a source object to the
|
||||
* destination object. If `object` is a function then methods are added to
|
||||
* its prototype as well.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Utility
|
||||
* @param {Function|Object} [object=this] object The destination object.
|
||||
* @param {Object} source The object of functions to add.
|
||||
* @param {Object} [options] The options object.
|
||||
* @param {boolean} [options.chain=true] Specify whether the functions added
|
||||
* are chainable.
|
||||
* @returns {Function|Object} Returns `object`.
|
||||
* @example
|
||||
*
|
||||
* function vowels(string) {
|
||||
* return _.filter(string, function(v) {
|
||||
* return /[aeiou]/i.test(v);
|
||||
* });
|
||||
* }
|
||||
*
|
||||
* _.mixin({ 'vowels': vowels });
|
||||
* _.vowels('fred');
|
||||
* // => ['e']
|
||||
*
|
||||
* _('fred').vowels().value();
|
||||
* // => ['e']
|
||||
*
|
||||
* _.mixin({ 'vowels': vowels }, { 'chain': false });
|
||||
* _('fred').vowels();
|
||||
* // => ['e']
|
||||
*/
|
||||
function mixin(object, source, options) {
|
||||
var methodNames = baseFunctions(source, keys(source));
|
||||
|
||||
var chain = true,
|
||||
index = -1,
|
||||
isFunc = isFunction(object),
|
||||
length = methodNames.length;
|
||||
|
||||
if (options === false) {
|
||||
chain = false;
|
||||
} else if (isObject(options) && 'chain' in options) {
|
||||
chain = options.chain;
|
||||
}
|
||||
while (++index < length) {
|
||||
var methodName = methodNames[index],
|
||||
func = source[methodName];
|
||||
|
||||
object[methodName] = func;
|
||||
if (isFunc) {
|
||||
object.prototype[methodName] = (function(func) {
|
||||
return function() {
|
||||
var chainAll = this.__chain__;
|
||||
if (chain || chainAll) {
|
||||
var result = object(this.__wrapped__);
|
||||
(result.__actions__ = arrayCopy(this.__actions__)).push({ 'func': func, 'args': arguments, 'thisArg': object });
|
||||
result.__chain__ = chainAll;
|
||||
return result;
|
||||
}
|
||||
var args = [this.value()];
|
||||
push.apply(args, arguments);
|
||||
return func.apply(object, args);
|
||||
};
|
||||
}(func));
|
||||
}
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
export default mixin;
|
||||
17
utility/noop.js
Normal file
17
utility/noop.js
Normal file
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* A no-operation function.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Utility
|
||||
* @example
|
||||
*
|
||||
* var object = { 'user': 'fred' };
|
||||
* _.noop(object) === undefined;
|
||||
* // => true
|
||||
*/
|
||||
function noop() {
|
||||
// No operation performed.
|
||||
}
|
||||
|
||||
export default noop;
|
||||
30
utility/property.js
Normal file
30
utility/property.js
Normal file
@@ -0,0 +1,30 @@
|
||||
import baseProperty from '../internal/baseProperty';
|
||||
|
||||
/**
|
||||
* Creates a function which returns the property value of `key` on a given object.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Utility
|
||||
* @param {string} key The key of the property to get.
|
||||
* @returns {Function} Returns the new function.
|
||||
* @example
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'fred' },
|
||||
* { 'user': 'barney' }
|
||||
* ];
|
||||
*
|
||||
* var getName = _.property('user');
|
||||
*
|
||||
* _.map(users, getName);
|
||||
* // => ['fred', barney']
|
||||
*
|
||||
* _.pluck(_.sortBy(users, getName), 'user');
|
||||
* // => ['barney', 'fred']
|
||||
*/
|
||||
function property(key) {
|
||||
return baseProperty(key + '');
|
||||
}
|
||||
|
||||
export default property;
|
||||
26
utility/propertyOf.js
Normal file
26
utility/propertyOf.js
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* The inverse of `_.property`; this method creates a function which returns
|
||||
* the property value of a given key on `object`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Utility
|
||||
* @param {Object} object The object to inspect.
|
||||
* @returns {Function} Returns the new function.
|
||||
* @example
|
||||
*
|
||||
* var object = { 'user': 'fred', 'age': 40, 'active': true };
|
||||
* _.map(['active', 'user'], _.propertyOf(object));
|
||||
* // => [true, 'fred']
|
||||
*
|
||||
* var object = { 'a': 3, 'b': 1, 'c': 2 };
|
||||
* _.sortBy(['a', 'b', 'c'], _.propertyOf(object));
|
||||
* // => ['b', 'c', 'a']
|
||||
*/
|
||||
function propertyOf(object) {
|
||||
return function(key) {
|
||||
return object == null ? undefined : object[key];
|
||||
};
|
||||
}
|
||||
|
||||
export default propertyOf;
|
||||
67
utility/range.js
Normal file
67
utility/range.js
Normal file
@@ -0,0 +1,67 @@
|
||||
import isIterateeCall from '../internal/isIterateeCall';
|
||||
|
||||
/** Native method references. */
|
||||
var ceil = Math.ceil;
|
||||
|
||||
/* Native method references for those with the same name as other `lodash` methods. */
|
||||
var nativeMax = Math.max;
|
||||
|
||||
/**
|
||||
* Creates an array of numbers (positive and/or negative) progressing from
|
||||
* `start` up to, but not including, `end`. If `start` is less than `end` a
|
||||
* zero-length range is created unless a negative `step` is specified.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Utility
|
||||
* @param {number} [start=0] The start of the range.
|
||||
* @param {number} end The end of the range.
|
||||
* @param {number} [step=1] The value to increment or decrement by.
|
||||
* @returns {Array} Returns the new array of numbers.
|
||||
* @example
|
||||
*
|
||||
* _.range(4);
|
||||
* // => [0, 1, 2, 3]
|
||||
*
|
||||
* _.range(1, 5);
|
||||
* // => [1, 2, 3, 4]
|
||||
*
|
||||
* _.range(0, 20, 5);
|
||||
* // => [0, 5, 10, 15]
|
||||
*
|
||||
* _.range(0, -4, -1);
|
||||
* // => [0, -1, -2, -3]
|
||||
*
|
||||
* _.range(1, 4, 0);
|
||||
* // => [1, 1, 1]
|
||||
*
|
||||
* _.range(0);
|
||||
* // => []
|
||||
*/
|
||||
function range(start, end, step) {
|
||||
if (step && isIterateeCall(start, end, step)) {
|
||||
end = step = null;
|
||||
}
|
||||
start = +start || 0;
|
||||
step = step == null ? 1 : (+step || 0);
|
||||
|
||||
if (end == null) {
|
||||
end = start;
|
||||
start = 0;
|
||||
} else {
|
||||
end = +end || 0;
|
||||
}
|
||||
// Use `Array(length)` so engines like Chakra and V8 avoid slower modes.
|
||||
// See https://youtu.be/XAqIpGU8ZZk#t=17m25s for more details.
|
||||
var index = -1,
|
||||
length = nativeMax(ceil((end - start) / (step || 1)), 0),
|
||||
result = Array(length);
|
||||
|
||||
while (++index < length) {
|
||||
result[index] = start;
|
||||
start += step;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export default range;
|
||||
56
utility/times.js
Normal file
56
utility/times.js
Normal file
@@ -0,0 +1,56 @@
|
||||
import bindCallback from '../internal/bindCallback';
|
||||
import root from '../internal/root';
|
||||
|
||||
/* Native method references for those with the same name as other `lodash` methods. */
|
||||
var nativeIsFinite = root.isFinite,
|
||||
nativeMin = Math.min;
|
||||
|
||||
/** Used as references for the maximum length and index of an array. */
|
||||
var MAX_ARRAY_LENGTH = Math.pow(2, 32) - 1;
|
||||
|
||||
/**
|
||||
* Invokes the iteratee function `n` times, returning an array of the results
|
||||
* of each invocation. The `iteratee` is bound to `thisArg` and invoked with
|
||||
* one argument; (index).
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Utility
|
||||
* @param {number} n The number of times to invoke `iteratee`.
|
||||
* @param {Function} [iteratee=_.identity] The function invoked per iteration.
|
||||
* @param {*} [thisArg] The `this` binding of `iteratee`.
|
||||
* @returns {Array} Returns the array of results.
|
||||
* @example
|
||||
*
|
||||
* var diceRolls = _.times(3, _.partial(_.random, 1, 6, false));
|
||||
* // => [3, 6, 4]
|
||||
*
|
||||
* _.times(3, function(n) { mage.castSpell(n); });
|
||||
* // => invokes `mage.castSpell(n)` three times with `n` of `0`, `1`, and `2` respectively
|
||||
*
|
||||
* _.times(3, function(n) { this.cast(n); }, mage);
|
||||
* // => also invokes `mage.castSpell(n)` three times
|
||||
*/
|
||||
function times(n, iteratee, thisArg) {
|
||||
n = +n;
|
||||
|
||||
// Exit early to avoid a JSC JIT bug in Safari 8
|
||||
// where `Array(0)` is treated as `Array(1)`.
|
||||
if (n < 1 || !nativeIsFinite(n)) {
|
||||
return [];
|
||||
}
|
||||
var index = -1,
|
||||
result = Array(nativeMin(n, MAX_ARRAY_LENGTH));
|
||||
|
||||
iteratee = bindCallback(iteratee, thisArg, 1);
|
||||
while (++index < n) {
|
||||
if (index < MAX_ARRAY_LENGTH) {
|
||||
result[index] = iteratee(index);
|
||||
} else {
|
||||
iteratee(index);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export default times;
|
||||
27
utility/uniqueId.js
Normal file
27
utility/uniqueId.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import baseToString from '../internal/baseToString';
|
||||
|
||||
/** Used to generate unique IDs. */
|
||||
var idCounter = 0;
|
||||
|
||||
/**
|
||||
* Generates a unique ID. If `prefix` is provided the ID is appended to it.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Utility
|
||||
* @param {string} [prefix] The value to prefix the ID with.
|
||||
* @returns {string} Returns the unique ID.
|
||||
* @example
|
||||
*
|
||||
* _.uniqueId('contact_');
|
||||
* // => 'contact_104'
|
||||
*
|
||||
* _.uniqueId();
|
||||
* // => '105'
|
||||
*/
|
||||
function uniqueId(prefix) {
|
||||
var id = ++idCounter;
|
||||
return baseToString(prefix) + id;
|
||||
}
|
||||
|
||||
export default uniqueId;
|
||||
Reference in New Issue
Block a user