Skip to content

Commit

Permalink
feat(esl-utils): fix isPrimitive check and isObject return type, …
Browse files Browse the repository at this point in the history
…add `isArrayLike`
  • Loading branch information
ala-n committed Nov 19, 2021
1 parent 45c50a0 commit 80b8aa0
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/modules/esl-utils/misc/object.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
export const isObject = (obj: any): obj is Record<string, any> => obj && typeof obj === 'object';
/** Checks that passed value is object, but not a callable-object (function) */
export const isObject = (obj: any): obj is Record<string, any> => !!obj && typeof obj === 'object';
/** Check that passed value is an object or function */
export const isObjectLike = (obj: any) => isObject(obj) || typeof obj === 'function';
export const isPrimitive = (obj: any): obj is string | number | boolean =>
typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean';

/** Checks if the passed value is primitive */
export const isPrimitive = (obj: any): obj is string | number | boolean | symbol | undefined | null =>
obj === null ||
typeof obj === 'undefined' ||
typeof obj === 'string' ||
typeof obj === 'number' ||
typeof obj === 'boolean' ||
typeof obj === 'symbol';
/** Check that passed object is prototype of some class */
export const isPrototype = (obj: any) => Object.hasOwnProperty.call(obj, 'constructor');

/** Array-like type definition */
export type ArrayLike<T = any> = {
[key: number]: T;
length: number;
};
/** Checks that passed object is array-like */
export const isArrayLike = (value: any): value is ArrayLike => {
if (!value || typeof value !== 'object') return false;
if (Array.isArray(value)) return true;
if (typeof value.length !== 'number' || value.length < 0) return false;
return !value.length || Object.hasOwnProperty.call(value, value.length - 1);
};

export type CopyPredicate = (key: string, value: any) => boolean;

/** Deep object compare */
Expand Down

0 comments on commit 80b8aa0

Please sign in to comment.