mirror of
https://github.com/toss/es-toolkit.git
synced 2024-12-03 01:42:44 +03:00
caedf69aed
* feat(predicate, compat, isLength): impl isLength Signed-off-by: sunrabbit123 <qudwls185@naver.com> * docs: change compatibility mark Signed-off-by: sunrabbit123 <qudwls185@naver.com> * test(isLength, compat): add test case link Signed-off-by: sunrabbit123 <qudwls185@naver.com> * docs(predicate, isLength): add route path on vitepress Signed-off-by: sunrabbit123 <qudwls185@naver.com> * Update docs/ko/reference/predicate/isLength.md --------- Signed-off-by: sunrabbit123 <qudwls185@naver.com> Co-authored-by: Sojin Park <raon0211@gmail.com> Co-authored-by: Sojin Park <raon0211@toss.im>
1.2 KiB
1.2 KiB
isLength
Checks if a given value is a valid length.
This function tests whether the provided value is of type number
, is a non-negative integer, and is less than or equal to JavaScript's maximum safe integer (Number.MAX_SAFE_INTEGER
). It returns true
if the value is a valid length, and false
otherwise.
This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to a valid length (number
).
Signature
function isLength(value: unknown): value is number;
Parameters
value
(unknown
): The value to check if it is a valid length.
Returns
(value is number
): Returns true
if the value is a valid length, otherwise false
.
Examples
import { isLength } from 'es-toolkit/predicate';
const value1 = 0;
const value2 = 42;
const value3 = -1;
const value4 = 1.5;
const value5 = Number.MAX_SAFE_INTEGER;
const value6 = Number.MAX_SAFE_INTEGER + 1;
console.log(isLength(value1)); // true
console.log(isLength(value2)); // true
console.log(isLength(value3)); // false
console.log(isLength(value4)); // false
console.log(isLength(value5)); // true
console.log(isLength(value6)); // false