forked from remeda/remeda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isObject.ts
27 lines (27 loc) · 1.1 KB
/
isObject.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* eslint @typescript-eslint/ban-types: ["error",{types:{Function: false},extendDefaults:true}] --
* Function is used generically in this file to define any type of function, so
* this lint error is not relevant for it.
*/
type DefinitelyObject<T> = Exclude<
Extract<T, object>,
Array<any> | Function | ReadonlyArray<any>
> extends never
? Record<string, unknown>
: Exclude<Extract<T, object>, Array<any> | Function | ReadonlyArray<any>>;
/**
* A function that checks if the passed parameter is of type Object and narrows its type accordingly
* @param data the variable to check
* @signature
* R.isObject(data)
* @returns true if the passed input is an Object, Promise, Date or Error, false otherwise
* @example
* R.isObject({}) //=> true
* R.isObject(Promise.resolve("something")) //=> true
* R.isObject(new Date()) //=> true
* R.isObject(new Error("error")) //=> true
* R.isObject('somethingElse') //=> false
* @category Guard
*/
export function isObject<T>(data: T | object): data is DefinitelyObject<T> {
return !!data && !Array.isArray(data) && typeof data === 'object';
}