mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
Add isIndex helper.
This commit is contained in:
42
lodash.js
42
lodash.js
@@ -718,6 +718,19 @@
|
|||||||
};
|
};
|
||||||
}());
|
}());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if `value` is valid array-like index.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {*} value The value to check.
|
||||||
|
* @param {number} [length] The upper bound of a valid index.
|
||||||
|
* @returns {boolean} Returns `true` if `value` is a valid index, else `false`.
|
||||||
|
*/
|
||||||
|
function isIndex(value, length) {
|
||||||
|
value = +value;
|
||||||
|
return value > -1 && value % 1 == 0 && (length == null || value < length);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used by `trimmedLeftIndex` and `trimmedRightIndex` to determine if a
|
* Used by `trimmedLeftIndex` and `trimmedRightIndex` to determine if a
|
||||||
* character code is whitespace.
|
* character code is whitespace.
|
||||||
@@ -1422,7 +1435,7 @@
|
|||||||
var key = props[index];
|
var key = props[index];
|
||||||
if (isArr) {
|
if (isArr) {
|
||||||
key = parseFloat(key);
|
key = parseFloat(key);
|
||||||
result[index] = (key > -1 && key < length && key % 1 == 0) ? collection[key] : undefined;
|
result[index] = isIndex(key, length) ? collection[key] : undefined;
|
||||||
} else {
|
} else {
|
||||||
result[index] = collection[key];
|
result[index] = collection[key];
|
||||||
}
|
}
|
||||||
@@ -2235,7 +2248,7 @@
|
|||||||
indexes.sort(baseCompareAscending);
|
indexes.sort(baseCompareAscending);
|
||||||
while (length--) {
|
while (length--) {
|
||||||
var index = parseFloat(indexes[length]);
|
var index = parseFloat(indexes[length]);
|
||||||
if (index != previous && index > -1 && index % 1 == 0) {
|
if (index != previous && isIndex(index)) {
|
||||||
var previous = index;
|
var previous = index;
|
||||||
splice.call(array, index, 1);
|
splice.call(array, index, 1);
|
||||||
}
|
}
|
||||||
@@ -3098,12 +3111,12 @@
|
|||||||
if (!isObject(object)) {
|
if (!isObject(object)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
var type = typeof index,
|
var type = typeof index;
|
||||||
prereq = type == 'string';
|
|
||||||
|
|
||||||
if (type == 'number') {
|
if (type == 'number') {
|
||||||
var length = object.length;
|
var length = object.length,
|
||||||
prereq = (isLength(length) && index > -1 && index < length && index % 1 == 0);
|
prereq = isLength(length) && isIndex(index, length);
|
||||||
|
} else {
|
||||||
|
prereq = type == 'string';
|
||||||
}
|
}
|
||||||
return prereq && object[index] === value;
|
return prereq && object[index] === value;
|
||||||
}
|
}
|
||||||
@@ -3193,9 +3206,7 @@
|
|||||||
|
|
||||||
while (length--) {
|
while (length--) {
|
||||||
var index = indexes[length];
|
var index = indexes[length];
|
||||||
array[length] = (index > -1 && index < arrLength && index % 1 == 0)
|
array[length] = isIndex(index, arrLength) ? oldArray[index] : undefined;
|
||||||
? oldArray[index]
|
|
||||||
: undefined;
|
|
||||||
}
|
}
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
@@ -3291,14 +3302,12 @@
|
|||||||
(isArray(object) || (support.nonEnumStrings && isString(object)) ||
|
(isArray(object) || (support.nonEnumStrings && isString(object)) ||
|
||||||
(support.nonEnumArgs && isArguments(object)));
|
(support.nonEnumArgs && isArguments(object)));
|
||||||
|
|
||||||
var keyIndex,
|
var index = -1,
|
||||||
index = -1,
|
|
||||||
result = [];
|
result = [];
|
||||||
|
|
||||||
while (++index < propsLength) {
|
while (++index < propsLength) {
|
||||||
var key = props[index];
|
var key = props[index];
|
||||||
if ((allowIndexes && (keyIndex = +key, keyIndex > -1 && keyIndex < length && keyIndex % 1 == 0)) ||
|
if ((allowIndexes && isIndex(key, length)) || hasOwnProperty.call(object, key)) {
|
||||||
hasOwnProperty.call(object, key)) {
|
|
||||||
result.push(key);
|
result.push(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8082,8 +8091,7 @@
|
|||||||
(isArray(object) || (support.nonEnumStrings && isString(object)) ||
|
(isArray(object) || (support.nonEnumStrings && isString(object)) ||
|
||||||
(support.nonEnumArgs && isArguments(object))) && length) || 0;
|
(support.nonEnumArgs && isArguments(object))) && length) || 0;
|
||||||
|
|
||||||
var keyIndex,
|
var Ctor = object.constructor,
|
||||||
Ctor = object.constructor,
|
|
||||||
index = -1,
|
index = -1,
|
||||||
proto = (typeof Ctor == 'function' && Ctor.prototype) || objectProto,
|
proto = (typeof Ctor == 'function' && Ctor.prototype) || objectProto,
|
||||||
isProto = proto === object,
|
isProto = proto === object,
|
||||||
@@ -8102,7 +8110,7 @@
|
|||||||
for (var key in object) {
|
for (var key in object) {
|
||||||
if (!(skipProto && key == 'prototype') &&
|
if (!(skipProto && key == 'prototype') &&
|
||||||
!(skipErrorProps && (key == 'message' || key == 'name')) &&
|
!(skipErrorProps && (key == 'message' || key == 'name')) &&
|
||||||
!(skipIndexes && (keyIndex = +key, keyIndex > -1 && keyIndex < length && keyIndex % 1 == 0)) &&
|
!(skipIndexes && isIndex(key, length)) &&
|
||||||
!(key == 'constructor' && (isProto || !hasOwnProperty.call(object, key)))) {
|
!(key == 'constructor' && (isProto || !hasOwnProperty.call(object, key)))) {
|
||||||
result.push(key);
|
result.push(key);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user