Adjust fromIndex and length coercions.

This commit is contained in:
John-David Dalton
2014-03-13 09:28:38 -07:00
parent 7e04c9a909
commit a90453461e
7 changed files with 184 additions and 184 deletions

View File

@@ -273,7 +273,7 @@
* @returns {number} Returns the index of the matched value, else `-1`.
*/
function baseIndexOf(array, value, fromIndex) {
var index = (fromIndex | 0) - 1,
var index = (+fromIndex || 0) - 1,
length = array ? array.length : 0;
while (++index < length) {
@@ -1396,7 +1396,7 @@
* @returns {Array} Returns the new flattened array.
*/
function baseFlatten(array, isShallow, isStrict, fromIndex) {
var index = (fromIndex | 0) - 1,
var index = (+fromIndex || 0) - 1,
length = array ? array.length : 0,
result = [];
@@ -2629,7 +2629,7 @@
function indexOf(array, value, fromIndex) {
var length = array ? array.length : 0;
if (typeof fromIndex == 'number') {
fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex) | 0;
fromIndex = fromIndex < 0 ? nativeMax(0, length + fromIndex) : (fromIndex || 0);
} else if (fromIndex) {
var index = sortedIndex(array, value);
return (length && array[index] === value) ? index : -1;
@@ -2932,13 +2932,13 @@
var index = -1,
length = array ? array.length : 0;
start |= 0;
start = +start || 0;
if (start < 0) {
start = nativeMax(length + start, 0);
} else if (start > length) {
start = length;
}
end = typeof end == 'undefined' ? length : (end | 0);
end = typeof end == 'undefined' ? length : (+end || 0);
if (end < 0) {
end = nativeMax(length + end, 0);
} else if (end > length) {
@@ -3558,10 +3558,10 @@
* // => true
*/
function contains(collection, target, fromIndex) {
var length = collection ? collection.length : 0;
fromIndex = (typeof fromIndex == 'number' && fromIndex) | 0;
var length = (length = collection && collection.length, length > -1 && length >>> 0);
fromIndex = (typeof fromIndex == 'number' && +fromIndex) || 0;
if (typeof length == 'number' && length > -1) {
if (length) {
if (typeof collection == 'string' || !isArray(collection) && isString(collection)) {
if (fromIndex >= length) {
return false;
@@ -3571,7 +3571,7 @@
: collection.indexOf(target, fromIndex) > -1;
}
var indexOf = getIndexOf();
fromIndex = fromIndex < 0 ? nativeMax(0, (length | 0) + fromIndex) : fromIndex;
fromIndex = fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex;
return indexOf(collection, target, fromIndex) > -1;
}
var index = -1,
@@ -4016,8 +4016,8 @@
var args = slice(arguments, 2),
index = -1,
isFunc = typeof methodName == 'function',
length = (collection && collection.length) | 0,
result = Array(length < 0 ? 0 : length);
length = collection && collection.length,
result = Array(length < 0 ? 0 : length >>> 0);
baseEach(collection, function(value) {
result[++index] = (isFunc ? methodName : value[methodName]).apply(value, args);
@@ -4066,8 +4066,8 @@
*/
function map(collection, callback, thisArg) {
var index = -1,
length = (collection && collection.length) | 0,
result = Array(length < 0 ? 0 : length);
length = collection && collection.length,
result = Array(length < 0 ? 0 : length >>> 0);
callback = lodash.createCallback(callback, thisArg, 3);
if (isArray(collection)) {
@@ -4455,7 +4455,7 @@
collection = collection.split('');
}
if (n == null || guard) {
var length = (collection && collection.length) | 0;
var length = (length = collection && collection.length, length > -1 && length >>> 0);
return length > 0 ? collection[baseRandom(0, length - 1)] : undefined;
}
var result = shuffle(collection);
@@ -4480,8 +4480,8 @@
*/
function shuffle(collection) {
var index = -1,
length = (collection && collection.length) | 0,
result = Array(length < 0 ? 0 : length);
length = collection && collection.length,
result = Array(length < 0 ? 0 : length >>> 0);
baseEach(collection, function(value) {
var rand = baseRandom(0, ++index);
@@ -4631,9 +4631,9 @@
*/
function sortBy(collection, callback, thisArg) {
var index = -1,
length = (collection && collection.length) | 0,
length = collection && collection.length,
multi = callback && isArray(callback),
result = Array(length < 0 ? 0 : length);
result = Array(length < 0 ? 0 : length >>> 0);
if (!multi) {
callback = lodash.createCallback(callback, thisArg, 3);
@@ -6949,7 +6949,7 @@
target = String(target);
var length = string.length;
position = (typeof position == 'number' ? nativeMin(nativeMax(position | 0, 0), length) : length) - target.length;
position = (typeof position == 'number' ? nativeMin(nativeMax(+position || 0, 0), length) : length) - target.length;
return position >= 0 && string.indexOf(target, position) == position;
}
@@ -7202,7 +7202,7 @@
*/
function startsWith(string, target, position) {
string = string == null ? '' : String(string);
position = typeof position == 'number' ? nativeMin(nativeMax(position | 0, 0), string.length) : 0;
position = typeof position == 'number' ? nativeMin(nativeMax(+position || 0, 0), string.length) : 0;
return string.lastIndexOf(target, position) == position;
}
@@ -7508,11 +7508,11 @@
if (options && isObject(options)) {
var separator = 'separator' in options ? options.separator : separator;
length = 'length' in options ? options.length | 0 : length;
length = 'length' in options ? +options.length || 0 : length;
omission = 'omission' in options ? String(options.omission) : omission;
}
else if (options != null) {
length = options | 0;
length = +options || 0;
}
string = string == null ? '' : String(string);
if (length >= string.length) {