Skip to content

Commit e61b27b

Browse files
authored
fix: use Array.isArray() for array check
1 parent 2ef5e6f commit e61b27b

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/util.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const throwOnUndef = (value: unknown, def?: unknown) => {
1717

1818
export const type = (value: unknown): string => {
1919
if (value === null) return 'null';
20-
if (value instanceof Array) return 'array';
20+
if (Array.isArray(value)) return 'array';
2121
if (value instanceof Uint8Array) return 'binary';
2222
return typeof value;
2323
};
@@ -61,7 +61,7 @@ export const len = (value: unknown): number => {
6161
case 'string':
6262
return value.length;
6363
case 'object': {
64-
if (value instanceof Array) return value.length;
64+
if (Array.isArray(value)) return value.length;
6565
if (value instanceof Uint8Array) return value.length;
6666
if (!value) return 0;
6767
return Object.keys(value).length;
@@ -80,7 +80,7 @@ export const member = (container: unknown, index: unknown): unknown => {
8080
}
8181
case 'object': {
8282
if (!container) throw new Error('NOT_CONTAINER');
83-
if (container instanceof Array || container instanceof Uint8Array) {
83+
if (Array.isArray(container) || container instanceof Uint8Array) {
8484
const i = int(index);
8585
if (i < 0 || i >= container.length) return undefined;
8686
return container[i];
@@ -199,7 +199,7 @@ export const u8 = (bin: unknown, pos: unknown) => {
199199
// ----------------------------------------------------- Array operator helpers
200200

201201
export const asArr = (value: unknown): unknown[] => {
202-
if (value instanceof Array) return value as unknown[];
202+
if (Array.isArray(value)) return value as unknown[];
203203
throw new Error('NOT_ARRAY');
204204
};
205205

@@ -348,18 +348,18 @@ export const objDelRaw = (obj: Record<string, unknown>, key: string): Record<str
348348
// -------------------------------------------------------------------- Various
349349

350350
export const isLiteral = (value: unknown): boolean => {
351-
if (value instanceof Array) return value.length === 1;
351+
if (Array.isArray(value)) return value.length === 1;
352352
else return true;
353353
};
354354

355355
export const asLiteral = <T>(value: Literal<T>): T => {
356-
if (value instanceof Array) {
356+
if (Array.isArray(value)) {
357357
if (value.length !== 1) throw new Error('Invalid literal.');
358358
return value[0];
359359
} else return value;
360360
};
361361

362-
export const literal = <T = unknown>(value: T): T | [T] => (value instanceof Array ? [value] : value);
362+
export const literal = <T = unknown>(value: T): T | [T] => (Array.isArray(value) ? [value] : value);
363363

364364
export const assertFixedArity = (operator: string, arity: number, expr: Expression): void => {
365365
if (expr.length !== arity + 1) throw new Error(`"${operator}" operator expects ${arity} operands.`);
@@ -371,7 +371,7 @@ export const assertVariadicArity = (operator: string, expr: Expression): void =>
371371

372372
export const assertArity = (operator: string, arity: number | [min: number, max: number], expr: Expression): void => {
373373
if (!arity) return;
374-
if (arity instanceof Array) {
374+
if (Array.isArray(arity)) {
375375
const [min, max] = arity;
376376
if (expr.length < min + 1) throw new Error(`"${operator}" operator expects at least ${min} operands.`);
377377
if (max !== -1 && expr.length > max + 1) throw new Error(`"${operator}" operator expects at most ${max} operands.`);

0 commit comments

Comments
 (0)