mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 02:17:50 +00:00
More adjustments to coerced values.
This commit is contained in:
26
lodash.js
26
lodash.js
@@ -2026,9 +2026,9 @@
|
|||||||
*/
|
*/
|
||||||
function createPad(string, length, chars) {
|
function createPad(string, length, chars) {
|
||||||
var strLength = string.length;
|
var strLength = string.length;
|
||||||
length |= 0;
|
length = +length;
|
||||||
|
|
||||||
if (strLength >= length) {
|
if (strLength >= length || !nativeIsFinite(length)) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
var padLength = length - strLength;
|
var padLength = length - strLength;
|
||||||
@@ -2854,8 +2854,7 @@
|
|||||||
function lastIndexOf(array, value, fromIndex) {
|
function lastIndexOf(array, value, fromIndex) {
|
||||||
var index = array ? array.length : 0;
|
var index = array ? array.length : 0;
|
||||||
if (typeof fromIndex == 'number') {
|
if (typeof fromIndex == 'number') {
|
||||||
fromIndex |= 0;
|
index = (fromIndex < 0 ? nativeMax(index + fromIndex, 0) : nativeMin(fromIndex || 0, index - 1)) + 1;
|
||||||
index = (fromIndex < 0 ? nativeMax(index + fromIndex, 0) : nativeMin(fromIndex, index - 1)) + 1;
|
|
||||||
}
|
}
|
||||||
while (index--) {
|
while (index--) {
|
||||||
if (array[index] === value) {
|
if (array[index] === value) {
|
||||||
@@ -4525,7 +4524,7 @@
|
|||||||
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);
|
||||||
result.length = nativeMin(nativeMax(n, 0), result.length);
|
result.length = nativeMin(n < 0 ? 0 : (+n || 0), result.length);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4808,6 +4807,7 @@
|
|||||||
if (!isFunction(func)) {
|
if (!isFunction(func)) {
|
||||||
throw new TypeError;
|
throw new TypeError;
|
||||||
}
|
}
|
||||||
|
n = nativeIsFinite(n = +n) ? n : 0;
|
||||||
return function() {
|
return function() {
|
||||||
if (--n < 1) {
|
if (--n < 1) {
|
||||||
return func.apply(this, arguments);
|
return func.apply(this, arguments);
|
||||||
@@ -5080,7 +5080,7 @@
|
|||||||
trailing = false;
|
trailing = false;
|
||||||
} else if (isObject(options)) {
|
} else if (isObject(options)) {
|
||||||
leading = options.leading;
|
leading = options.leading;
|
||||||
maxWait = 'maxWait' in options && (nativeMax(wait, options.maxWait) || 0);
|
maxWait = 'maxWait' in options && nativeMax(wait, +options.maxWait || 0);
|
||||||
trailing = 'trailing' in options ? options.trailing : trailing;
|
trailing = 'trailing' in options ? options.trailing : trailing;
|
||||||
}
|
}
|
||||||
var delayed = function() {
|
var delayed = function() {
|
||||||
@@ -7016,7 +7016,7 @@
|
|||||||
target = String(target);
|
target = String(target);
|
||||||
|
|
||||||
var length = string.length;
|
var length = string.length;
|
||||||
position = (typeof position == 'number' ? nativeMin(nativeMax(+position || 0, 0), length) : length) - target.length;
|
position = (typeof position == 'undefined' ? length : nativeMin(position < 0 ? 0 : (+position || 0), length)) - target.length;
|
||||||
return position >= 0 && string.indexOf(target, position) == position;
|
return position >= 0 && string.indexOf(target, position) == position;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -7113,10 +7113,10 @@
|
|||||||
*/
|
*/
|
||||||
function pad(string, length, chars) {
|
function pad(string, length, chars) {
|
||||||
string = string == null ? '' : String(string);
|
string = string == null ? '' : String(string);
|
||||||
length |= 0;
|
length = +length;
|
||||||
|
|
||||||
var strLength = string.length;
|
var strLength = string.length;
|
||||||
if (strLength >= length) {
|
if (strLength >= length || !nativeIsFinite(length)) {
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
var mid = (length - strLength) / 2,
|
var mid = (length - strLength) / 2,
|
||||||
@@ -7205,9 +7205,9 @@
|
|||||||
*/
|
*/
|
||||||
function repeat(string, n) {
|
function repeat(string, n) {
|
||||||
var result = '';
|
var result = '';
|
||||||
n |= 0;
|
n = +n;
|
||||||
|
|
||||||
if (n < 1 || string == null) {
|
if (n < 1 || string == null || !nativeIsFinite(n)) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
string = String(string);
|
string = String(string);
|
||||||
@@ -7269,7 +7269,7 @@
|
|||||||
*/
|
*/
|
||||||
function startsWith(string, target, position) {
|
function startsWith(string, target, position) {
|
||||||
string = string == null ? '' : String(string);
|
string = string == null ? '' : String(string);
|
||||||
position = typeof position == 'number' ? nativeMin(nativeMax(+position || 0, 0), string.length) : 0;
|
position = typeof position == 'undefined' ? 0 : nativeMin(position < 0 ? 0 : (+position || 0), string.length);
|
||||||
return string.lastIndexOf(target, position) == position;
|
return string.lastIndexOf(target, position) == position;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -8151,7 +8151,7 @@
|
|||||||
* // => also calls `mage.castSpell(n)` three times
|
* // => also calls `mage.castSpell(n)` three times
|
||||||
*/
|
*/
|
||||||
function times(n, callback, thisArg) {
|
function times(n, callback, thisArg) {
|
||||||
n = (n = +n) > -1 ? n : 0;
|
n = n < 0 ? 0 : n >>> 0;
|
||||||
var index = -1,
|
var index = -1,
|
||||||
result = Array(n);
|
result = Array(n);
|
||||||
|
|
||||||
|
|||||||
36
test/test.js
36
test/test.js
@@ -601,18 +601,23 @@
|
|||||||
QUnit.module('lodash.after');
|
QUnit.module('lodash.after');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
function after(n, times) {
|
||||||
|
var count = 0;
|
||||||
|
_.times(times, _.after(n, function() { count++; }));
|
||||||
|
return count;
|
||||||
|
}
|
||||||
test('should create a function that executes `func` after `n` calls', 4, function() {
|
test('should create a function that executes `func` after `n` calls', 4, function() {
|
||||||
function after(n, times) {
|
|
||||||
var count = 0;
|
|
||||||
_.times(times, _.after(n, function() { count++; }));
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
strictEqual(after(5, 5), 1, 'after(n) should execute `func` after being called `n` times');
|
strictEqual(after(5, 5), 1, 'after(n) should execute `func` after being called `n` times');
|
||||||
strictEqual(after(5, 4), 0, 'after(n) should not execute `func` unless called `n` times');
|
strictEqual(after(5, 4), 0, 'after(n) should not execute `func` unless called `n` times');
|
||||||
strictEqual(after(0, 0), 0, 'after(0) should not execute `func` immediately');
|
strictEqual(after(0, 0), 0, 'after(0) should not execute `func` immediately');
|
||||||
strictEqual(after(0, 1), 1, 'after(0) should execute `func` when called once');
|
strictEqual(after(0, 1), 1, 'after(0) should execute `func` when called once');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should coerce non-finite `n` values to `0`', 3, function() {
|
||||||
|
_.forEach([-Infinity, NaN, Infinity], function(n) {
|
||||||
|
strictEqual(after(n, 1), 1);
|
||||||
|
});
|
||||||
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
@@ -7378,14 +7383,14 @@
|
|||||||
deepEqual(actual.sort(), array);
|
deepEqual(actual.sort(), array);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should return an empty array when `n` < `1`', 3, function() {
|
test('should return an empty array when `n` < `1` or `NaN`', 4, function() {
|
||||||
_.forEach([0, -1, -2], function(n) {
|
_.forEach([0, -1, NaN, -Infinity], function(n) {
|
||||||
deepEqual(_.sample(array, n), []);
|
deepEqual(_.sample(array, n), []);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should return all elements when `n` >= `array.length`', 2, function() {
|
test('should return all elements when `n` >= `array.length`', 4, function() {
|
||||||
_.forEach([3, 4], function(n) {
|
_.forEach([3, 4, Math.pow(2, 32), Infinity], function(n) {
|
||||||
deepEqual(_.sample(array, n).sort(), array);
|
deepEqual(_.sample(array, n).sort(), array);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -8708,6 +8713,17 @@
|
|||||||
QUnit.module('lodash.times');
|
QUnit.module('lodash.times');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
test('should rollover large `n` values', 1, function() {
|
||||||
|
var actual = _.times(Math.pow(2, 32) + 1);
|
||||||
|
deepEqual(actual, [0]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should coerce non-finite `n` values to `0`', 3, function() {
|
||||||
|
_.forEach([-Infinity, NaN, Infinity], function(n) {
|
||||||
|
deepEqual(_.times(n), []);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('should pass the correct `callback` arguments', 1, function() {
|
test('should pass the correct `callback` arguments', 1, function() {
|
||||||
var args;
|
var args;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user