Cleanup type coercions.

This commit is contained in:
John-David Dalton
2014-03-12 00:29:31 -07:00
parent 2970f66218
commit 029aac8059

View File

@@ -1327,7 +1327,7 @@
iterable = collection, iterable = collection,
length = collection ? collection.length : 0; length = collection ? collection.length : 0;
if (typeof length == 'number') { if (typeof length == 'number' && length > -1) {
length |= 0; length |= 0;
if (support.unindexedChars && isString(iterable)) { if (support.unindexedChars && isString(iterable)) {
iterable = iterable.split(''); iterable = iterable.split('');
@@ -1356,7 +1356,7 @@
var iterable = collection, var iterable = collection,
length = collection ? collection.length : 0; length = collection ? collection.length : 0;
if (typeof length == 'number') { if (typeof length == 'number' && length > -1) {
length = (length |= 0) < 0 ? 0 : length; length = (length |= 0) < 0 ? 0 : length;
if (support.unindexedChars && isString(iterable)) { if (support.unindexedChars && isString(iterable)) {
iterable = iterable.split(''); iterable = iterable.split('');
@@ -2855,11 +2855,13 @@
*/ */
function range(start, end, step) { function range(start, end, step) {
start = +start || 0; start = +start || 0;
step = typeof step == 'number' ? step : (+step || 1); step = step == null ? 1 : (+step || 0);
if (end == null) { if (end == null) {
end = start; end = start;
start = 0; start = 0;
} else {
end = +end || 0;
} }
// use `Array(length)` so engines like Chakra and V8 avoid slower modes // use `Array(length)` so engines like Chakra and V8 avoid slower modes
// http://youtu.be/XAqIpGU8ZZk#t=17m25s // http://youtu.be/XAqIpGU8ZZk#t=17m25s
@@ -3602,20 +3604,19 @@
*/ */
function contains(collection, target, fromIndex) { function contains(collection, target, fromIndex) {
var length = collection ? collection.length : 0; var length = collection ? collection.length : 0;
fromIndex = typeof fromIndex == 'number' ? fromIndex | 0 : 0; fromIndex = (typeof fromIndex == 'number' && fromIndex) | 0;
if (typeof length == 'number') { if (typeof length == 'number' && length > -1) {
length = (length |= 0) < 0 ? 0 : length;
if (fromIndex >= length) {
return false;
}
if (typeof collection == 'string' || !isArray(collection) && isString(collection)) { if (typeof collection == 'string' || !isArray(collection) && isString(collection)) {
if (fromIndex >= length) {
return false;
}
return nativeContains return nativeContains
? nativeContains.call(collection, target, fromIndex) ? nativeContains.call(collection, target, fromIndex)
: collection.indexOf(target, fromIndex) > -1; : collection.indexOf(target, fromIndex) > -1;
} }
var indexOf = getIndexOf(); var indexOf = getIndexOf();
fromIndex = fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex; fromIndex = fromIndex < 0 ? nativeMax(0, (length | 0) + fromIndex) : fromIndex;
return indexOf(collection, target, fromIndex) > -1; return indexOf(collection, target, fromIndex) > -1;
} }
var index = -1, var index = -1,
@@ -4079,7 +4080,7 @@
var args = slice(arguments, 2), var args = slice(arguments, 2),
index = -1, index = -1,
isFunc = typeof methodName == 'function', isFunc = typeof methodName == 'function',
length = collection ? collection.length | 0 : 0, length = (collection && collection.length) | 0,
result = Array(length < 0 ? 0 : length); result = Array(length < 0 ? 0 : length);
baseEach(collection, function(value) { baseEach(collection, function(value) {
@@ -4129,7 +4130,7 @@
*/ */
function map(collection, callback, thisArg) { function map(collection, callback, thisArg) {
var index = -1, var index = -1,
length = collection ? collection.length | 0 : 0, length = (collection && collection.length) | 0,
result = Array(length < 0 ? 0 : length); result = Array(length < 0 ? 0 : length);
callback = lodash.createCallback(callback, thisArg, 3); callback = lodash.createCallback(callback, thisArg, 3);
@@ -4518,7 +4519,7 @@
collection = collection.split(''); collection = collection.split('');
} }
if (n == null || guard) { if (n == null || guard) {
var length = collection ? collection.length | 0 : 0; var length = collection && collection.length | 0;
return length > 0 ? collection[baseRandom(0, length - 1)] : undefined; return length > 0 ? collection[baseRandom(0, length - 1)] : undefined;
} }
var result = shuffle(collection); var result = shuffle(collection);
@@ -4543,7 +4544,7 @@
*/ */
function shuffle(collection) { function shuffle(collection) {
var index = -1, var index = -1,
length = collection ? collection.length | 0 : 0, length = (collection && collection.length) | 0,
result = Array(length < 0 ? 0 : length); result = Array(length < 0 ? 0 : length);
baseEach(collection, function(value) { baseEach(collection, function(value) {
@@ -4694,8 +4695,8 @@
*/ */
function sortBy(collection, callback, thisArg) { function sortBy(collection, callback, thisArg) {
var index = -1, var index = -1,
length = (collection && collection.length) | 0,
multi = callback && isArray(callback), multi = callback && isArray(callback),
length = collection ? collection.length | 0 : 0,
result = Array(length < 0 ? 0 : length); result = Array(length < 0 ? 0 : length);
if (!multi) { if (!multi) {