mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 02:17:48 +00:00
Revert removing falsey guards.
Former-commit-id: b5eeb5d4a0896eb030f20e7e91e54bf101535abc
This commit is contained in:
72
lodash.js
72
lodash.js
@@ -315,6 +315,8 @@
|
|||||||
'var index, value, iteratee = <%= firstArg %>, ' +
|
'var index, value, iteratee = <%= firstArg %>, ' +
|
||||||
// assign the `result` variable an initial value
|
// assign the `result` variable an initial value
|
||||||
'result<% if (init) { %> = <%= init %><% } %>;\n' +
|
'result<% if (init) { %> = <%= init %><% } %>;\n' +
|
||||||
|
// exit early if the first argument is falsey
|
||||||
|
'if (!<%= firstArg %>) return result;\n' +
|
||||||
// add code before the iteration branches
|
// add code before the iteration branches
|
||||||
'<%= top %>;\n' +
|
'<%= top %>;\n' +
|
||||||
|
|
||||||
@@ -981,7 +983,6 @@
|
|||||||
var shimKeys = createIterator({
|
var shimKeys = createIterator({
|
||||||
'args': 'object',
|
'args': 'object',
|
||||||
'init': '[]',
|
'init': '[]',
|
||||||
'top': 'if (!(object && objectTypes[typeof object])) throw TypeError()',
|
|
||||||
'inLoop': 'result.push(index)'
|
'inLoop': 'result.push(index)'
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -1250,7 +1251,7 @@
|
|||||||
* // => true
|
* // => true
|
||||||
*/
|
*/
|
||||||
function has(object, property) {
|
function has(object, property) {
|
||||||
return hasOwnProperty.call(object, property);
|
return object ? hasOwnProperty.call(object, property) : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1329,7 +1330,6 @@
|
|||||||
'args': 'value',
|
'args': 'value',
|
||||||
'init': 'true',
|
'init': 'true',
|
||||||
'top':
|
'top':
|
||||||
'if (!value) return result;\n' +
|
|
||||||
'var className = toString.call(value),\n' +
|
'var className = toString.call(value),\n' +
|
||||||
' length = value.length;\n' +
|
' length = value.length;\n' +
|
||||||
'if (arrayLikeClasses[className]' +
|
'if (arrayLikeClasses[className]' +
|
||||||
@@ -1690,10 +1690,15 @@
|
|||||||
* // => ['one', 'two', 'three'] (order is not guaranteed)
|
* // => ['one', 'two', 'three'] (order is not guaranteed)
|
||||||
*/
|
*/
|
||||||
var keys = !nativeKeys ? shimKeys : function(object) {
|
var keys = !nativeKeys ? shimKeys : function(object) {
|
||||||
|
var type = typeof object;
|
||||||
|
|
||||||
// avoid iterating over the `prototype` property
|
// avoid iterating over the `prototype` property
|
||||||
return typeof object == 'function' && propertyIsEnumerable.call(object, 'prototype')
|
if (type == 'function' && propertyIsEnumerable.call(object, 'prototype')) {
|
||||||
? shimKeys(object)
|
return shimKeys(object);
|
||||||
: nativeKeys(object);
|
}
|
||||||
|
return object && objectTypes[type]
|
||||||
|
? nativeKeys(object)
|
||||||
|
: [];
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2198,7 +2203,7 @@
|
|||||||
*/
|
*/
|
||||||
function reduceRight(collection, callback, accumulator, thisArg) {
|
function reduceRight(collection, callback, accumulator, thisArg) {
|
||||||
var iteratee = collection,
|
var iteratee = collection,
|
||||||
length = collection.length,
|
length = collection ? collection.length : 0,
|
||||||
noaccum = arguments.length < 3;
|
noaccum = arguments.length < 3;
|
||||||
|
|
||||||
if (length !== +length) {
|
if (length !== +length) {
|
||||||
@@ -2349,7 +2354,10 @@
|
|||||||
* // => [2, 3, 4]
|
* // => [2, 3, 4]
|
||||||
*/
|
*/
|
||||||
function toArray(collection) {
|
function toArray(collection) {
|
||||||
var length = collection ? collection.length : 0;
|
if (!collection) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
var length = collection.length;
|
||||||
if (length === +length) {
|
if (length === +length) {
|
||||||
return (noArraySliceOnStrings ? toString.call(collection) == stringClass : typeof collection == 'string')
|
return (noArraySliceOnStrings ? toString.call(collection) == stringClass : typeof collection == 'string')
|
||||||
? collection.split('')
|
? collection.split('')
|
||||||
@@ -2411,7 +2419,7 @@
|
|||||||
*/
|
*/
|
||||||
function compact(array) {
|
function compact(array) {
|
||||||
var index = -1,
|
var index = -1,
|
||||||
length = array.length,
|
length = array ? array.length : 0,
|
||||||
result = [];
|
result = [];
|
||||||
|
|
||||||
while (++index < length) {
|
while (++index < length) {
|
||||||
@@ -2439,11 +2447,14 @@
|
|||||||
* // => [1, 3, 4]
|
* // => [1, 3, 4]
|
||||||
*/
|
*/
|
||||||
function difference(array) {
|
function difference(array) {
|
||||||
|
var result = [];
|
||||||
|
if (!array) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
var index = -1,
|
var index = -1,
|
||||||
length = array.length,
|
length = array.length,
|
||||||
flattened = concat.apply(ArrayProto, arguments),
|
flattened = concat.apply(ArrayProto, arguments),
|
||||||
contains = cachedContains(flattened, length),
|
contains = cachedContains(flattened, length);
|
||||||
result = [];
|
|
||||||
|
|
||||||
while (++index < length) {
|
while (++index < length) {
|
||||||
var value = array[index];
|
var value = array[index];
|
||||||
@@ -2474,7 +2485,9 @@
|
|||||||
* // => 5
|
* // => 5
|
||||||
*/
|
*/
|
||||||
function first(array, n, guard) {
|
function first(array, n, guard) {
|
||||||
return (n == null || guard) ? array[0] : slice.call(array, 0, n);
|
if (array) {
|
||||||
|
return (n == null || guard) ? array[0] : slice.call(array, 0, n);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2498,7 +2511,7 @@
|
|||||||
function flatten(array, shallow) {
|
function flatten(array, shallow) {
|
||||||
var value,
|
var value,
|
||||||
index = -1,
|
index = -1,
|
||||||
length = array.length,
|
length = array ? array.length : 0,
|
||||||
result = [];
|
result = [];
|
||||||
|
|
||||||
while (++index < length) {
|
while (++index < length) {
|
||||||
@@ -2540,7 +2553,7 @@
|
|||||||
*/
|
*/
|
||||||
function indexOf(array, value, fromIndex) {
|
function indexOf(array, value, fromIndex) {
|
||||||
var index = -1,
|
var index = -1,
|
||||||
length = array.length;
|
length = array ? array.length : 0;
|
||||||
|
|
||||||
if (fromIndex) {
|
if (fromIndex) {
|
||||||
if (typeof fromIndex == 'number') {
|
if (typeof fromIndex == 'number') {
|
||||||
@@ -2576,7 +2589,9 @@
|
|||||||
* // => [3, 2]
|
* // => [3, 2]
|
||||||
*/
|
*/
|
||||||
function initial(array, n, guard) {
|
function initial(array, n, guard) {
|
||||||
return slice.call(array, 0, -((n == null || guard) ? 1 : n));
|
return array
|
||||||
|
? slice.call(array, 0, -((n == null || guard) ? 1 : n))
|
||||||
|
: [];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2598,7 +2613,7 @@
|
|||||||
var argsLength = arguments.length,
|
var argsLength = arguments.length,
|
||||||
cache = [],
|
cache = [],
|
||||||
index = -1,
|
index = -1,
|
||||||
length = array.length,
|
length = array ? array.length : 0,
|
||||||
result = [];
|
result = [];
|
||||||
|
|
||||||
array: while (++index < length) {
|
array: while (++index < length) {
|
||||||
@@ -2634,8 +2649,10 @@
|
|||||||
* // => 1
|
* // => 1
|
||||||
*/
|
*/
|
||||||
function last(array, n, guard) {
|
function last(array, n, guard) {
|
||||||
var length = array.length;
|
if (array) {
|
||||||
return (n == null || guard) ? array[length - 1] : slice.call(array, -n || length);
|
var length = array.length;
|
||||||
|
return (n == null || guard) ? array[length - 1] : slice.call(array, -n || length);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2658,7 +2675,7 @@
|
|||||||
* // => 1
|
* // => 1
|
||||||
*/
|
*/
|
||||||
function lastIndexOf(array, value, fromIndex) {
|
function lastIndexOf(array, value, fromIndex) {
|
||||||
var index = array.length;
|
var index = array ? array.length : 0;
|
||||||
if (fromIndex && typeof fromIndex == 'number') {
|
if (fromIndex && typeof fromIndex == 'number') {
|
||||||
index = (fromIndex < 0 ? nativeMax(0, index + fromIndex) : nativeMin(fromIndex, index - 1)) + 1;
|
index = (fromIndex < 0 ? nativeMax(0, index + fromIndex) : nativeMin(fromIndex, index - 1)) + 1;
|
||||||
}
|
}
|
||||||
@@ -2767,7 +2784,7 @@
|
|||||||
*/
|
*/
|
||||||
function object(keys, values) {
|
function object(keys, values) {
|
||||||
var index = -1,
|
var index = -1,
|
||||||
length = keys.length,
|
length = keys ? keys.length : 0,
|
||||||
result = {};
|
result = {};
|
||||||
|
|
||||||
while (++index < length) {
|
while (++index < length) {
|
||||||
@@ -2849,7 +2866,9 @@
|
|||||||
* // => [2, 1]
|
* // => [2, 1]
|
||||||
*/
|
*/
|
||||||
function rest(array, n, guard) {
|
function rest(array, n, guard) {
|
||||||
return slice.call(array, (n == null || guard) ? 1 : n);
|
return array
|
||||||
|
? slice.call(array, (n == null || guard) ? 1 : n)
|
||||||
|
: [];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2869,7 +2888,7 @@
|
|||||||
function shuffle(array) {
|
function shuffle(array) {
|
||||||
var rand,
|
var rand,
|
||||||
index = -1,
|
index = -1,
|
||||||
length = array.length,
|
length = array ? array.length : 0,
|
||||||
result = Array(length);
|
result = Array(length);
|
||||||
|
|
||||||
while (++index < length) {
|
while (++index < length) {
|
||||||
@@ -2923,7 +2942,7 @@
|
|||||||
function sortedIndex(array, value, callback, thisArg) {
|
function sortedIndex(array, value, callback, thisArg) {
|
||||||
var mid,
|
var mid,
|
||||||
low = 0,
|
low = 0,
|
||||||
high = array.length;
|
high = array ? array.length : low;
|
||||||
|
|
||||||
callback = createCallback(callback, thisArg);
|
callback = createCallback(callback, thisArg);
|
||||||
value = callback(value);
|
value = callback(value);
|
||||||
@@ -2996,7 +3015,7 @@
|
|||||||
function uniq(array, isSorted, callback, thisArg) {
|
function uniq(array, isSorted, callback, thisArg) {
|
||||||
var computed,
|
var computed,
|
||||||
index = -1,
|
index = -1,
|
||||||
length = array.length,
|
length = array ? array.length : 0,
|
||||||
result = [],
|
result = [],
|
||||||
seen = [];
|
seen = [];
|
||||||
|
|
||||||
@@ -3037,7 +3056,7 @@
|
|||||||
*/
|
*/
|
||||||
function without(array) {
|
function without(array) {
|
||||||
var index = -1,
|
var index = -1,
|
||||||
length = array.length,
|
length = array ? array.length : 0,
|
||||||
contains = cachedContains(arguments, 1, 20),
|
contains = cachedContains(arguments, 1, 20),
|
||||||
result = [];
|
result = [];
|
||||||
|
|
||||||
@@ -3068,7 +3087,7 @@
|
|||||||
*/
|
*/
|
||||||
function zip(array) {
|
function zip(array) {
|
||||||
var index = -1,
|
var index = -1,
|
||||||
length = max(pluck(arguments, 'length')),
|
length = array ? max(pluck(arguments, 'length')) : 0,
|
||||||
result = Array(length);
|
result = Array(length);
|
||||||
|
|
||||||
while (++index < length) {
|
while (++index < length) {
|
||||||
@@ -3730,6 +3749,7 @@
|
|||||||
// http://ejohn.org/blog/javascript-micro-templating/
|
// http://ejohn.org/blog/javascript-micro-templating/
|
||||||
// and Laura Doktorova's doT.js
|
// and Laura Doktorova's doT.js
|
||||||
// https://github.com/olado/doT
|
// https://github.com/olado/doT
|
||||||
|
text += '';
|
||||||
options || (options = {});
|
options || (options = {});
|
||||||
|
|
||||||
var isEvaluating,
|
var isEvaluating,
|
||||||
|
|||||||
37
test/test.js
37
test/test.js
@@ -1702,6 +1702,43 @@
|
|||||||
QUnit.module('lodash methods');
|
QUnit.module('lodash methods');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
test('should allow falsey arguments', function() {
|
||||||
|
var funcs = _.without.apply(_, [_.functions(_)].concat([
|
||||||
|
'_',
|
||||||
|
'_iteratorTemplate',
|
||||||
|
'_shimKeys',
|
||||||
|
'after',
|
||||||
|
'bind',
|
||||||
|
'bindAll',
|
||||||
|
'compose',
|
||||||
|
'debounce',
|
||||||
|
'defer',
|
||||||
|
'delay',
|
||||||
|
'functions',
|
||||||
|
'memoize',
|
||||||
|
'once',
|
||||||
|
'partial',
|
||||||
|
'tap',
|
||||||
|
'throttle',
|
||||||
|
'wrap'
|
||||||
|
]));
|
||||||
|
|
||||||
|
_.each(funcs, function(methodName) {
|
||||||
|
var func = _[methodName],
|
||||||
|
pass = true;
|
||||||
|
|
||||||
|
_.each(falsey, function(value, index) {
|
||||||
|
try {
|
||||||
|
index ? func(value) : func();
|
||||||
|
} catch(e) {
|
||||||
|
pass = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ok(pass, '_.' + methodName + ' allows falsey arguments');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('should handle `null` `thisArg` arguments', function() {
|
test('should handle `null` `thisArg` arguments', function() {
|
||||||
var thisArg,
|
var thisArg,
|
||||||
array = ['a'],
|
array = ['a'],
|
||||||
|
|||||||
Reference in New Issue
Block a user