UNPKG

866 BJavaScriptView Raw
1import baseClamp from './_baseClamp.js';
2import toInteger from './toInteger.js';
3
4/** Used as references for the maximum length and index of an array. */
5var MAX_ARRAY_LENGTH = 4294967295;
6
7/**
8 * Converts `value` to an integer suitable for use as the length of an
9 * array-like object.
10 *
11 * **Note:** This method is based on
12 * [`ToLength`](http://ecma-international.org/ecma-262/7.0/#sec-tolength).
13 *
14 * @static
15 * @memberOf _
16 * @since 4.0.0
17 * @category Lang
18 * @param {*} value The value to convert.
19 * @returns {number} Returns the converted integer.
20 * @example
21 *
22 * _.toLength(3.2);
23 * // => 3
24 *
25 * _.toLength(Number.MIN_VALUE);
26 * // => 0
27 *
28 * _.toLength(Infinity);
29 * // => 4294967295
30 *
31 * _.toLength('3.2');
32 * // => 3
33 */
34function toLength(value) {
35 return value ? baseClamp(toInteger(value), 0, MAX_ARRAY_LENGTH) : 0;
36}
37
38export default toLength;