mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-12 11:57:49 +00:00
Expose _.toLength.
This commit is contained in:
committed by
John-David Dalton
parent
c7ef030ef5
commit
2b4a10c159
23
lodash.js
23
lodash.js
@@ -1475,9 +1475,9 @@
|
|||||||
* `random`, `reduce`, `reduceRight`, `repeat`, `result`, `round`, `runInContext`,
|
* `random`, `reduce`, `reduceRight`, `repeat`, `result`, `round`, `runInContext`,
|
||||||
* `sample`, `shift`, `size`, `snakeCase`, `some`, `sortedIndex`, `sortedIndexBy`,
|
* `sample`, `shift`, `size`, `snakeCase`, `some`, `sortedIndex`, `sortedIndexBy`,
|
||||||
* `sortedLastIndex`, `sortedLastIndexBy`, `startCase`, `startsWith`, `sum`,
|
* `sortedLastIndex`, `sortedLastIndexBy`, `startCase`, `startsWith`, `sum`,
|
||||||
* `sumBy`, `template`, `toLower`, `toInteger`, `toSafeInteger`, `toString`,
|
* `sumBy`, `template`, `toLower`, `toInteger`, `toLength`, `toSafeInteger`,
|
||||||
* `toUpper`, `trim`, `trimLeft`, `trimRight`, `truncate`, `unescape`, `uniqueId`,
|
* `toString`, `toUpper`, `trim`, `trimLeft`, `trimRight`, `truncate`, `unescape`,
|
||||||
* `upperCase`, `upperFirst`, `value`, and `words`
|
* `uniqueId`, `upperCase`, `upperFirst`, `value`, and `words`
|
||||||
*
|
*
|
||||||
* @name _
|
* @name _
|
||||||
* @constructor
|
* @constructor
|
||||||
@@ -9832,6 +9832,22 @@
|
|||||||
return value === value ? (remainder ? value - remainder : value) : 0;
|
return value === value ? (remainder ? value - remainder : value) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The abstract operation toLength converts its argument to an integer
|
||||||
|
* suitable for use as the length of an array-like object.
|
||||||
|
*
|
||||||
|
* **Note:** This function is loosely based on [`ToLength`](http://ecma-international.org/ecma-262/6.0/#sec-tolength).
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @memberOf _
|
||||||
|
* @category Lang
|
||||||
|
* @param {*} value The object to be converted to a length.
|
||||||
|
* @return {number} Integer in range 0 to 2^32-1.
|
||||||
|
*/
|
||||||
|
function toLength(value) {
|
||||||
|
return clamp(toInteger(value), 0, MAX_ARRAY_LENGTH);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts `value` to a plain object flattening inherited enumerable
|
* Converts `value` to a plain object flattening inherited enumerable
|
||||||
* properties of `value` to own properties of the plain object.
|
* properties of `value` to own properties of the plain object.
|
||||||
@@ -13487,6 +13503,7 @@
|
|||||||
lodash.sumBy = sumBy;
|
lodash.sumBy = sumBy;
|
||||||
lodash.template = template;
|
lodash.template = template;
|
||||||
lodash.toInteger = toInteger;
|
lodash.toInteger = toInteger;
|
||||||
|
lodash.toLength = toLength;
|
||||||
lodash.toLower = toLower;
|
lodash.toLower = toLower;
|
||||||
lodash.toSafeInteger = toSafeInteger;
|
lodash.toSafeInteger = toSafeInteger;
|
||||||
lodash.toString = toString;
|
lodash.toString = toString;
|
||||||
|
|||||||
25
test/test.js
25
test/test.js
@@ -19338,6 +19338,29 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
QUnit.module('lodash.toLength');
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
QUnit.test('should return number literal integers in range unchanged', function(assert) {
|
||||||
|
assert.expect(3);
|
||||||
|
|
||||||
|
assert.strictEqual(_.toLength(0), 0);
|
||||||
|
assert.strictEqual(_.toLength(3), 3);
|
||||||
|
assert.strictEqual(_.toLength(MAX_ARRAY_LENGTH), MAX_ARRAY_LENGTH);
|
||||||
|
});
|
||||||
|
|
||||||
|
QUnit.test('should return number as integer clamped to range', function(assert) {
|
||||||
|
assert.expect(4);
|
||||||
|
|
||||||
|
assert.strictEqual(_.toLength(-1), 0);
|
||||||
|
assert.strictEqual(_.toLength('1'), 1);
|
||||||
|
assert.strictEqual(_.toLength(1.1), 1);
|
||||||
|
assert.strictEqual(_.toLength(MAX_INTEGER), MAX_ARRAY_LENGTH);
|
||||||
|
});
|
||||||
|
}());
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
QUnit.module('lodash.toPath');
|
QUnit.module('lodash.toPath');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
@@ -22023,7 +22046,7 @@
|
|||||||
var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey);
|
var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey);
|
||||||
|
|
||||||
QUnit.test('should accept falsey arguments', function(assert) {
|
QUnit.test('should accept falsey arguments', function(assert) {
|
||||||
assert.expect(273);
|
assert.expect(274);
|
||||||
|
|
||||||
var emptyArrays = lodashStable.map(falsey, lodashStable.constant([]));
|
var emptyArrays = lodashStable.map(falsey, lodashStable.constant([]));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user