mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 06:27:49 +00:00
141 lines
4.2 KiB
JavaScript
141 lines
4.2 KiB
JavaScript
/**
|
|
* lodash 3.1.1 (Custom Build) <https://lodash.com/>
|
|
* Build: `lodash modern modularize exports="npm" -o ./`
|
|
* Copyright 2012-2015 The Dojo Foundation <http://dojofoundation.org/>
|
|
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
|
|
* Copyright 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
|
|
* Available under MIT license <https://lodash.com/license>
|
|
*/
|
|
var baseIndexOf = require('lodash._baseindexof'),
|
|
baseValues = require('lodash._basevalues'),
|
|
isIterateeCall = require('lodash._isiterateecall'),
|
|
isArray = require('lodash.isarray'),
|
|
isString = require('lodash.isstring'),
|
|
keys = require('lodash.keys');
|
|
|
|
/* Native method references for those with the same name as other `lodash` methods. */
|
|
var nativeMax = Math.max;
|
|
|
|
/**
|
|
* Used as the [maximum length](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.max_safe_integer)
|
|
* of an array-like value.
|
|
*/
|
|
var MAX_SAFE_INTEGER = Math.pow(2, 53) - 1;
|
|
|
|
/**
|
|
* The base implementation of `_.property` without support for deep paths.
|
|
*
|
|
* @private
|
|
* @param {string} key The key of the property to get.
|
|
* @returns {Function} Returns the new function.
|
|
*/
|
|
function baseProperty(key) {
|
|
return function(object) {
|
|
return object == null ? undefined : object[key];
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Gets the "length" property value of `object`.
|
|
*
|
|
* **Note:** This function is used to avoid a [JIT bug](https://bugs.webkit.org/show_bug.cgi?id=142792)
|
|
* that affects Safari on at least iOS 8.1-8.3 ARM64.
|
|
*
|
|
* @private
|
|
* @param {Object} object The object to query.
|
|
* @returns {*} Returns the "length" value.
|
|
*/
|
|
var getLength = baseProperty('length');
|
|
|
|
/**
|
|
* Checks if `value` is a valid array-like length.
|
|
*
|
|
* **Note:** This function is based on [`ToLength`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength).
|
|
*
|
|
* @private
|
|
* @param {*} value The value to check.
|
|
* @returns {boolean} Returns `true` if `value` is a valid length, else `false`.
|
|
*/
|
|
function isLength(value) {
|
|
return typeof value == 'number' && value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
|
|
}
|
|
|
|
/**
|
|
* Checks if `value` is in `collection` using
|
|
* [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
|
|
* for equality comparisons. If `fromIndex` is negative, it is used as the offset
|
|
* from the end of `collection`.
|
|
*
|
|
* @static
|
|
* @memberOf _
|
|
* @alias contains, include
|
|
* @category Collection
|
|
* @param {Array|Object|string} collection The collection to search.
|
|
* @param {*} target The value to search for.
|
|
* @param {number} [fromIndex=0] The index to search from.
|
|
* @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`.
|
|
* @returns {boolean} Returns `true` if a matching element is found, else `false`.
|
|
* @example
|
|
*
|
|
* _.includes([1, 2, 3], 1);
|
|
* // => true
|
|
*
|
|
* _.includes([1, 2, 3], 1, 2);
|
|
* // => false
|
|
*
|
|
* _.includes({ 'user': 'fred', 'age': 40 }, 'fred');
|
|
* // => true
|
|
*
|
|
* _.includes('pebbles', 'eb');
|
|
* // => true
|
|
*/
|
|
function includes(collection, target, fromIndex, guard) {
|
|
var length = collection ? getLength(collection) : 0;
|
|
if (!isLength(length)) {
|
|
collection = values(collection);
|
|
length = collection.length;
|
|
}
|
|
if (!length) {
|
|
return false;
|
|
}
|
|
if (typeof fromIndex != 'number' || (guard && isIterateeCall(target, fromIndex, guard))) {
|
|
fromIndex = 0;
|
|
} else {
|
|
fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
|
|
}
|
|
return (typeof collection == 'string' || !isArray(collection) && isString(collection))
|
|
? (fromIndex < length && collection.indexOf(target, fromIndex) > -1)
|
|
: (baseIndexOf(collection, target, fromIndex) > -1);
|
|
}
|
|
|
|
/**
|
|
* Creates an array of the own enumerable property values of `object`.
|
|
*
|
|
* **Note:** Non-object values are coerced to objects.
|
|
*
|
|
* @static
|
|
* @memberOf _
|
|
* @category Object
|
|
* @param {Object} object The object to query.
|
|
* @returns {Array} Returns the array of property values.
|
|
* @example
|
|
*
|
|
* function Foo() {
|
|
* this.a = 1;
|
|
* this.b = 2;
|
|
* }
|
|
*
|
|
* Foo.prototype.c = 3;
|
|
*
|
|
* _.values(new Foo);
|
|
* // => [1, 2] (iteration order is not guaranteed)
|
|
*
|
|
* _.values('hi');
|
|
* // => ['h', 'i']
|
|
*/
|
|
function values(object) {
|
|
return baseValues(object, keys(object));
|
|
}
|
|
|
|
module.exports = includes;
|