mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 06:27:49 +00:00
663 lines
16 KiB
JavaScript
663 lines
16 KiB
JavaScript
/**
|
|
* lodash 4.1.1 (Custom Build) <https://lodash.com/>
|
|
* Build: `lodash modularize exports="npm" -o ./`
|
|
* Copyright 2012-2016 The Dojo Foundation <http://dojofoundation.org/>
|
|
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
|
|
* Copyright 2009-2016 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
|
|
* Available under MIT license <https://lodash.com/license>
|
|
*/
|
|
|
|
/** Used as the size to enable large array optimizations. */
|
|
var LARGE_ARRAY_SIZE = 200;
|
|
|
|
/** Used to stand-in for `undefined` hash values. */
|
|
var HASH_UNDEFINED = '__lodash_hash_undefined__';
|
|
|
|
/** `Object#toString` result references. */
|
|
var funcTag = '[object Function]',
|
|
genTag = '[object GeneratorFunction]';
|
|
|
|
/** Used to match `RegExp` [syntax characters](http://ecma-international.org/ecma-262/6.0/#sec-patterns). */
|
|
var reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
|
|
|
|
/** Used to detect host constructors (Safari > 5). */
|
|
var reIsHostCtor = /^\[object .+?Constructor\]$/;
|
|
|
|
/** Used to determine if values are of the language type `Object`. */
|
|
var objectTypes = {
|
|
'function': true,
|
|
'object': true
|
|
};
|
|
|
|
/** Detect free variable `exports`. */
|
|
var freeExports = (objectTypes[typeof exports] && exports && !exports.nodeType)
|
|
? exports
|
|
: undefined;
|
|
|
|
/** Detect free variable `module`. */
|
|
var freeModule = (objectTypes[typeof module] && module && !module.nodeType)
|
|
? module
|
|
: undefined;
|
|
|
|
/** Detect free variable `global` from Node.js. */
|
|
var freeGlobal = checkGlobal(freeExports && freeModule && typeof global == 'object' && global);
|
|
|
|
/** Detect free variable `self`. */
|
|
var freeSelf = checkGlobal(objectTypes[typeof self] && self);
|
|
|
|
/** Detect free variable `window`. */
|
|
var freeWindow = checkGlobal(objectTypes[typeof window] && window);
|
|
|
|
/** Detect `this` as the global object. */
|
|
var thisGlobal = checkGlobal(objectTypes[typeof this] && this);
|
|
|
|
/**
|
|
* Used as a reference to the global object.
|
|
*
|
|
* The `this` value is used if it's the global object to avoid Greasemonkey's
|
|
* restricted `window` object, otherwise the `window` object is used.
|
|
*/
|
|
var root = freeGlobal ||
|
|
((freeWindow !== (thisGlobal && thisGlobal.window)) && freeWindow) ||
|
|
freeSelf || thisGlobal || Function('return this')();
|
|
|
|
/**
|
|
* Checks if `value` is a global object.
|
|
*
|
|
* @private
|
|
* @param {*} value The value to check.
|
|
* @returns {null|Object} Returns `value` if it's a global object, else `null`.
|
|
*/
|
|
function checkGlobal(value) {
|
|
return (value && value.Object === Object) ? value : null;
|
|
}
|
|
|
|
/**
|
|
* Checks if `value` is a host object in IE < 9.
|
|
*
|
|
* @private
|
|
* @param {*} value The value to check.
|
|
* @returns {boolean} Returns `true` if `value` is a host object, else `false`.
|
|
*/
|
|
function isHostObject(value) {
|
|
// Many host objects are `Object` objects that can coerce to strings
|
|
// despite having improperly defined `toString` methods.
|
|
var result = false;
|
|
if (value != null && typeof value.toString != 'function') {
|
|
try {
|
|
result = !!(value + '');
|
|
} catch (e) {}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/** Used for built-in method references. */
|
|
var arrayProto = Array.prototype,
|
|
objectProto = Object.prototype;
|
|
|
|
/** Used to resolve the decompiled source of functions. */
|
|
var funcToString = Function.prototype.toString;
|
|
|
|
/** Used to check objects for own properties. */
|
|
var hasOwnProperty = objectProto.hasOwnProperty;
|
|
|
|
/**
|
|
* Used to resolve the [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
|
|
* of values.
|
|
*/
|
|
var objectToString = objectProto.toString;
|
|
|
|
/** Used to detect if a method is native. */
|
|
var reIsNative = RegExp('^' +
|
|
funcToString.call(hasOwnProperty).replace(reRegExpChar, '\\$&')
|
|
.replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
|
|
);
|
|
|
|
/** Built-in value references. */
|
|
var splice = arrayProto.splice;
|
|
|
|
/* Built-in method references that are verified to be native. */
|
|
var Map = getNative(root, 'Map'),
|
|
nativeCreate = getNative(Object, 'create');
|
|
|
|
/**
|
|
* Creates an hash object.
|
|
*
|
|
* @private
|
|
* @constructor
|
|
* @returns {Object} Returns the new hash object.
|
|
*/
|
|
function Hash() {}
|
|
|
|
/**
|
|
* Removes `key` and its value from the hash.
|
|
*
|
|
* @private
|
|
* @param {Object} hash The hash to modify.
|
|
* @param {string} key The key of the value to remove.
|
|
* @returns {boolean} Returns `true` if the entry was removed, else `false`.
|
|
*/
|
|
function hashDelete(hash, key) {
|
|
return hashHas(hash, key) && delete hash[key];
|
|
}
|
|
|
|
/**
|
|
* Gets the hash value for `key`.
|
|
*
|
|
* @private
|
|
* @param {Object} hash The hash to query.
|
|
* @param {string} key The key of the value to get.
|
|
* @returns {*} Returns the entry value.
|
|
*/
|
|
function hashGet(hash, key) {
|
|
if (nativeCreate) {
|
|
var result = hash[key];
|
|
return result === HASH_UNDEFINED ? undefined : result;
|
|
}
|
|
return hasOwnProperty.call(hash, key) ? hash[key] : undefined;
|
|
}
|
|
|
|
/**
|
|
* Checks if a hash value for `key` exists.
|
|
*
|
|
* @private
|
|
* @param {Object} hash The hash to query.
|
|
* @param {string} key The key of the entry to check.
|
|
* @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
|
|
*/
|
|
function hashHas(hash, key) {
|
|
return nativeCreate ? hash[key] !== undefined : hasOwnProperty.call(hash, key);
|
|
}
|
|
|
|
/**
|
|
* Sets the hash `key` to `value`.
|
|
*
|
|
* @private
|
|
* @param {Object} hash The hash to modify.
|
|
* @param {string} key The key of the value to set.
|
|
* @param {*} value The value to set.
|
|
*/
|
|
function hashSet(hash, key, value) {
|
|
hash[key] = (nativeCreate && value === undefined) ? HASH_UNDEFINED : value;
|
|
}
|
|
|
|
/**
|
|
* Creates a map cache object to store key-value pairs.
|
|
*
|
|
* @private
|
|
* @constructor
|
|
* @param {Array} [values] The values to cache.
|
|
*/
|
|
function MapCache(values) {
|
|
var index = -1,
|
|
length = values ? values.length : 0;
|
|
|
|
this.clear();
|
|
while (++index < length) {
|
|
var entry = values[index];
|
|
this.set(entry[0], entry[1]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Removes all key-value entries from the map.
|
|
*
|
|
* @private
|
|
* @name clear
|
|
* @memberOf MapCache
|
|
*/
|
|
function mapClear() {
|
|
this.__data__ = {
|
|
'hash': new Hash,
|
|
'map': Map ? new Map : [],
|
|
'string': new Hash
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Removes `key` and its value from the map.
|
|
*
|
|
* @private
|
|
* @name delete
|
|
* @memberOf MapCache
|
|
* @param {string} key The key of the value to remove.
|
|
* @returns {boolean} Returns `true` if the entry was removed, else `false`.
|
|
*/
|
|
function mapDelete(key) {
|
|
var data = this.__data__;
|
|
if (isKeyable(key)) {
|
|
return hashDelete(typeof key == 'string' ? data.string : data.hash, key);
|
|
}
|
|
return Map ? data.map['delete'](key) : assocDelete(data.map, key);
|
|
}
|
|
|
|
/**
|
|
* Gets the map value for `key`.
|
|
*
|
|
* @private
|
|
* @name get
|
|
* @memberOf MapCache
|
|
* @param {string} key The key of the value to get.
|
|
* @returns {*} Returns the entry value.
|
|
*/
|
|
function mapGet(key) {
|
|
var data = this.__data__;
|
|
if (isKeyable(key)) {
|
|
return hashGet(typeof key == 'string' ? data.string : data.hash, key);
|
|
}
|
|
return Map ? data.map.get(key) : assocGet(data.map, key);
|
|
}
|
|
|
|
/**
|
|
* Checks if a map value for `key` exists.
|
|
*
|
|
* @private
|
|
* @name has
|
|
* @memberOf MapCache
|
|
* @param {string} key The key of the entry to check.
|
|
* @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
|
|
*/
|
|
function mapHas(key) {
|
|
var data = this.__data__;
|
|
if (isKeyable(key)) {
|
|
return hashHas(typeof key == 'string' ? data.string : data.hash, key);
|
|
}
|
|
return Map ? data.map.has(key) : assocHas(data.map, key);
|
|
}
|
|
|
|
/**
|
|
* Sets the map `key` to `value`.
|
|
*
|
|
* @private
|
|
* @name set
|
|
* @memberOf MapCache
|
|
* @param {string} key The key of the value to set.
|
|
* @param {*} value The value to set.
|
|
* @returns {Object} Returns the map cache object.
|
|
*/
|
|
function mapSet(key, value) {
|
|
var data = this.__data__;
|
|
if (isKeyable(key)) {
|
|
hashSet(typeof key == 'string' ? data.string : data.hash, key, value);
|
|
} else if (Map) {
|
|
data.map.set(key, value);
|
|
} else {
|
|
assocSet(data.map, key, value);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Creates a stack cache object to store key-value pairs.
|
|
*
|
|
* @private
|
|
* @constructor
|
|
* @param {Array} [values] The values to cache.
|
|
*/
|
|
function Stack(values) {
|
|
var index = -1,
|
|
length = values ? values.length : 0;
|
|
|
|
this.clear();
|
|
while (++index < length) {
|
|
var entry = values[index];
|
|
this.set(entry[0], entry[1]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Removes all key-value entries from the stack.
|
|
*
|
|
* @private
|
|
* @name clear
|
|
* @memberOf Stack
|
|
*/
|
|
function stackClear() {
|
|
this.__data__ = { 'array': [], 'map': null };
|
|
}
|
|
|
|
/**
|
|
* Removes `key` and its value from the stack.
|
|
*
|
|
* @private
|
|
* @name delete
|
|
* @memberOf Stack
|
|
* @param {string} key The key of the value to remove.
|
|
* @returns {boolean} Returns `true` if the entry was removed, else `false`.
|
|
*/
|
|
function stackDelete(key) {
|
|
var data = this.__data__,
|
|
array = data.array;
|
|
|
|
return array ? assocDelete(array, key) : data.map['delete'](key);
|
|
}
|
|
|
|
/**
|
|
* Gets the stack value for `key`.
|
|
*
|
|
* @private
|
|
* @name get
|
|
* @memberOf Stack
|
|
* @param {string} key The key of the value to get.
|
|
* @returns {*} Returns the entry value.
|
|
*/
|
|
function stackGet(key) {
|
|
var data = this.__data__,
|
|
array = data.array;
|
|
|
|
return array ? assocGet(array, key) : data.map.get(key);
|
|
}
|
|
|
|
/**
|
|
* Checks if a stack value for `key` exists.
|
|
*
|
|
* @private
|
|
* @name has
|
|
* @memberOf Stack
|
|
* @param {string} key The key of the entry to check.
|
|
* @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
|
|
*/
|
|
function stackHas(key) {
|
|
var data = this.__data__,
|
|
array = data.array;
|
|
|
|
return array ? assocHas(array, key) : data.map.has(key);
|
|
}
|
|
|
|
/**
|
|
* Sets the stack `key` to `value`.
|
|
*
|
|
* @private
|
|
* @name set
|
|
* @memberOf Stack
|
|
* @param {string} key The key of the value to set.
|
|
* @param {*} value The value to set.
|
|
* @returns {Object} Returns the stack cache object.
|
|
*/
|
|
function stackSet(key, value) {
|
|
var data = this.__data__,
|
|
array = data.array;
|
|
|
|
if (array) {
|
|
if (array.length < (LARGE_ARRAY_SIZE - 1)) {
|
|
assocSet(array, key, value);
|
|
} else {
|
|
data.array = null;
|
|
data.map = new MapCache(array);
|
|
}
|
|
}
|
|
var map = data.map;
|
|
if (map) {
|
|
map.set(key, value);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Removes `key` and its value from the associative array.
|
|
*
|
|
* @private
|
|
* @param {Array} array The array to query.
|
|
* @param {string} key The key of the value to remove.
|
|
* @returns {boolean} Returns `true` if the entry was removed, else `false`.
|
|
*/
|
|
function assocDelete(array, key) {
|
|
var index = assocIndexOf(array, key);
|
|
if (index < 0) {
|
|
return false;
|
|
}
|
|
var lastIndex = array.length - 1;
|
|
if (index == lastIndex) {
|
|
array.pop();
|
|
} else {
|
|
splice.call(array, index, 1);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Gets the associative array value for `key`.
|
|
*
|
|
* @private
|
|
* @param {Array} array The array to query.
|
|
* @param {string} key The key of the value to get.
|
|
* @returns {*} Returns the entry value.
|
|
*/
|
|
function assocGet(array, key) {
|
|
var index = assocIndexOf(array, key);
|
|
return index < 0 ? undefined : array[index][1];
|
|
}
|
|
|
|
/**
|
|
* Checks if an associative array value for `key` exists.
|
|
*
|
|
* @private
|
|
* @param {Array} array The array to query.
|
|
* @param {string} key The key of the entry to check.
|
|
* @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
|
|
*/
|
|
function assocHas(array, key) {
|
|
return assocIndexOf(array, key) > -1;
|
|
}
|
|
|
|
/**
|
|
* Gets the index at which the first occurrence of `key` is found in `array`
|
|
* of key-value pairs.
|
|
*
|
|
* @private
|
|
* @param {Array} array The array to search.
|
|
* @param {*} key The key to search for.
|
|
* @returns {number} Returns the index of the matched value, else `-1`.
|
|
*/
|
|
function assocIndexOf(array, key) {
|
|
var length = array.length;
|
|
while (length--) {
|
|
if (eq(array[length][0], key)) {
|
|
return length;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
/**
|
|
* Sets the associative array `key` to `value`.
|
|
*
|
|
* @private
|
|
* @param {Array} array The array to modify.
|
|
* @param {string} key The key of the value to set.
|
|
* @param {*} value The value to set.
|
|
*/
|
|
function assocSet(array, key, value) {
|
|
var index = assocIndexOf(array, key);
|
|
if (index < 0) {
|
|
array.push([key, value]);
|
|
} else {
|
|
array[index][1] = value;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets the native function at `key` of `object`.
|
|
*
|
|
* @private
|
|
* @param {Object} object The object to query.
|
|
* @param {string} key The key of the method to get.
|
|
* @returns {*} Returns the function if it's native, else `undefined`.
|
|
*/
|
|
function getNative(object, key) {
|
|
var value = object[key];
|
|
return isNative(value) ? value : undefined;
|
|
}
|
|
|
|
/**
|
|
* Checks if `value` is suitable for use as unique object key.
|
|
*
|
|
* @private
|
|
* @param {*} value The value to check.
|
|
* @returns {boolean} Returns `true` if `value` is suitable, else `false`.
|
|
*/
|
|
function isKeyable(value) {
|
|
var type = typeof value;
|
|
return type == 'number' || type == 'boolean' ||
|
|
(type == 'string' && value != '__proto__') || value == null;
|
|
}
|
|
|
|
/**
|
|
* Performs a [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
|
|
* comparison between two values to determine if they are equivalent.
|
|
*
|
|
* @static
|
|
* @memberOf _
|
|
* @category Lang
|
|
* @param {*} value The value to compare.
|
|
* @param {*} other The other value to compare.
|
|
* @returns {boolean} Returns `true` if the values are equivalent, else `false`.
|
|
* @example
|
|
*
|
|
* var object = { 'user': 'fred' };
|
|
* var other = { 'user': 'fred' };
|
|
*
|
|
* _.eq(object, object);
|
|
* // => true
|
|
*
|
|
* _.eq(object, other);
|
|
* // => false
|
|
*
|
|
* _.eq('a', 'a');
|
|
* // => true
|
|
*
|
|
* _.eq('a', Object('a'));
|
|
* // => false
|
|
*
|
|
* _.eq(NaN, NaN);
|
|
* // => true
|
|
*/
|
|
function eq(value, other) {
|
|
return value === other || (value !== value && other !== other);
|
|
}
|
|
|
|
/**
|
|
* Checks if `value` is classified as a `Function` object.
|
|
*
|
|
* @static
|
|
* @memberOf _
|
|
* @category Lang
|
|
* @param {*} value The value to check.
|
|
* @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
|
|
* @example
|
|
*
|
|
* _.isFunction(_);
|
|
* // => true
|
|
*
|
|
* _.isFunction(/abc/);
|
|
* // => false
|
|
*/
|
|
function isFunction(value) {
|
|
// The use of `Object#toString` avoids issues with the `typeof` operator
|
|
// in Safari 8 which returns 'object' for typed array and weak map constructors,
|
|
// and PhantomJS 1.9 which returns 'function' for `NodeList` instances.
|
|
var tag = isObject(value) ? objectToString.call(value) : '';
|
|
return tag == funcTag || tag == genTag;
|
|
}
|
|
|
|
/**
|
|
* Checks if `value` is the [language type](https://es5.github.io/#x8) of `Object`.
|
|
* (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
|
|
*
|
|
* @static
|
|
* @memberOf _
|
|
* @category Lang
|
|
* @param {*} value The value to check.
|
|
* @returns {boolean} Returns `true` if `value` is an object, else `false`.
|
|
* @example
|
|
*
|
|
* _.isObject({});
|
|
* // => true
|
|
*
|
|
* _.isObject([1, 2, 3]);
|
|
* // => true
|
|
*
|
|
* _.isObject(_.noop);
|
|
* // => true
|
|
*
|
|
* _.isObject(null);
|
|
* // => false
|
|
*/
|
|
function isObject(value) {
|
|
var type = typeof value;
|
|
return !!value && (type == 'object' || type == 'function');
|
|
}
|
|
|
|
/**
|
|
* Checks if `value` is object-like. A value is object-like if it's not `null`
|
|
* and has a `typeof` result of "object".
|
|
*
|
|
* @static
|
|
* @memberOf _
|
|
* @category Lang
|
|
* @param {*} value The value to check.
|
|
* @returns {boolean} Returns `true` if `value` is object-like, else `false`.
|
|
* @example
|
|
*
|
|
* _.isObjectLike({});
|
|
* // => true
|
|
*
|
|
* _.isObjectLike([1, 2, 3]);
|
|
* // => true
|
|
*
|
|
* _.isObjectLike(_.noop);
|
|
* // => false
|
|
*
|
|
* _.isObjectLike(null);
|
|
* // => false
|
|
*/
|
|
function isObjectLike(value) {
|
|
return !!value && typeof value == 'object';
|
|
}
|
|
|
|
/**
|
|
* Checks if `value` is a native function.
|
|
*
|
|
* @static
|
|
* @memberOf _
|
|
* @category Lang
|
|
* @param {*} value The value to check.
|
|
* @returns {boolean} Returns `true` if `value` is a native function, else `false`.
|
|
* @example
|
|
*
|
|
* _.isNative(Array.prototype.push);
|
|
* // => true
|
|
*
|
|
* _.isNative(_);
|
|
* // => false
|
|
*/
|
|
function isNative(value) {
|
|
if (value == null) {
|
|
return false;
|
|
}
|
|
if (isFunction(value)) {
|
|
return reIsNative.test(funcToString.call(value));
|
|
}
|
|
return isObjectLike(value) &&
|
|
(isHostObject(value) ? reIsNative : reIsHostCtor).test(value);
|
|
}
|
|
|
|
// Avoid inheriting from `Object.prototype` when possible.
|
|
Hash.prototype = nativeCreate ? nativeCreate(null) : objectProto;
|
|
|
|
// Add functions to the `MapCache`.
|
|
MapCache.prototype.clear = mapClear;
|
|
MapCache.prototype['delete'] = mapDelete;
|
|
MapCache.prototype.get = mapGet;
|
|
MapCache.prototype.has = mapHas;
|
|
MapCache.prototype.set = mapSet;
|
|
|
|
// Add functions to the `Stack` cache.
|
|
Stack.prototype.clear = stackClear;
|
|
Stack.prototype['delete'] = stackDelete;
|
|
Stack.prototype.get = stackGet;
|
|
Stack.prototype.has = stackHas;
|
|
Stack.prototype.set = stackSet;
|
|
|
|
module.exports = Stack;
|