Skip to content

Latest commit

 

History

History
71 lines (50 loc) · 1.68 KB

File metadata and controls

71 lines (50 loc) · 1.68 KB

Utilities (Type Guards)

The @jsopen/objects library provides several utility functions for type checking and identifying various JavaScript structures.

Object Checks

isObject(v)

Returns true if the value is a non-null object and NOT an array.

import { isObject } from '@jsopen/objects';

isObject({}); // true
isObject([]); // false
isObject(null); // false

isPlainObject(v)

Returns true if the value is a "plain" object (created with {} or new Object()).

import { isPlainObject } from '@jsopen/objects';

isPlainObject({}); // true
isPlainObject(new MyClass()); // false

Type Guards

isBuiltIn(v)

Returns true if the value is a built-in JavaScript object type such as Date, RegExp, Map, Set, Promise, Error, or typed arrays.

import { isBuiltIn } from '@jsopen/objects';

isBuiltIn(new Date()); // true
isBuiltIn(new Map()); // true
isBuiltIn({}); // false

isConstructor(fn)

Returns true if the value is a function that can be used as a constructor.

import { isConstructor } from '@jsopen/objects';

isConstructor(class {}); // true
isConstructor(() => {}); // false

isIterable(x)

Returns true if the value implements the Symbol.iterator protocol.

import { isIterable } from '@jsopen/objects';

isIterable([]); // true
isIterable(new Map()); // true
isIterable({}); // false

isAsyncIterable(x)

Returns true if the value implements the Symbol.asyncIterator protocol.

import { isAsyncIterable } from '@jsopen/objects';

const asyncGen = async function* () {};
isAsyncIterable(asyncGen()); // true
isAsyncIterable([]); // false