Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
@gyumeijie/is-array-like
Advanced tools
check whether an object is an array-like object
$ npm install @gyumeijie/is-array-like
isArrayLike(obj: any, similarity?: number): boolean
obj:
the object to be tested
similarity:
measure how much the obj is similar to the array object, there are three levels: 0, 1, 2:
0: return true iff obj has a valid `length` property, this is the default setting
1: return true iff obj has a valid `length` property and properties: [0, ..., length-1]
2: return true iff obj has and only has a valid `length` property and properties: [0, ..., length-1],
obj as such also called `strict array-like object`
a strict array-like object can be safely converted to array using
Array.from
method
let obj;
obj = {
0: 'zero',
2: 'two',
3: 'three',
length: 3,
};
Array.from(obj);
// ["zero", undefined, "two"] => missing one value, not safe
obj = {
0: 'zero',
2: 'two',
3: 'three',
length: -3,
};
Array.from(obj);
// [] => missing all values, not safe
obj = {
zero: 0,
one: 1,
two: 2,
length: 3,
};
Array.from(obj);
// [undefined, undefined, undefined] => missing all values, not safe
obj = {
0: 'zero',
1: 'one',
2: 'two',
name: 'obj',
length: 3,
};
Array.from(obj);
// ["zero", "one", "two"] => missing `name` property, not safe
obj = {
0: 'zero',
1: 'one',
2: 'two',
length: 3,
};
Array.from(obj);
// ["zero", "one", "two"] => containing all values, safely
const isArrayLike = require('@gyumeijie/is-array-like');
// test truly array
isArrayLike([]); // true
isArrayLike([1, 2, 3, 4]); // true
// test non-array object
isArrayLike({}); // false
isArrayLike(1); // false
isArrayLike('string'); // false
isArrayLike(undefined); // false
isArrayLike(null); // false
isArrayLike(new Date()); // false
let fn = (arg1, arg2) => {};
isArrayLike(fn); // false
let re = /regexp/;
re.length = 1;
re[0] = 'zero';
isArrayLike(re); // false
let obj = {
0: 'zero',
2: 'two',
3: 'three',
length: 3,
};
isArrayLike(obj); // true
isArrayLike(obj, 1); // false
obj = {
0: 'zero',
1: 'one',
2: 'two',
length: 3,
};
isArrayLike(obj); // true
isArrayLike(obj, 1); // true
obj = {
zero: 0,
one: 1,
two: 2,
length: 3,
};
isArrayLike(obj); //true
isArrayLike(obj, 1); //false
obj = {
0: 'zero',
1: 'one',
2: 'two',
length: -3,
};
isArrayLike(obj); //false
isArrayLike(obj, 1); //false
obj = {
0: 'zero',
1: 'one',
2: 'two',
name: 'obj',
length: 3,
};
isArrayLike(obj); //true
isArrayLike(obj, 1); //true
isArrayLike(obj, 2); //false
MIT © Gyumeijie
FAQs
check whether an object is an array-like object
The npm package @gyumeijie/is-array-like receives a total of 2 weekly downloads. As such, @gyumeijie/is-array-like popularity was classified as not popular.
We found that @gyumeijie/is-array-like demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.