-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathis-plain-object.ts
31 lines (29 loc) · 1.03 KB
/
is-plain-object.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
28
29
30
31
/**
* Please refer to the terms of the license agreement in the root of the project
*
* (c) 2024 Feedzai
*/
/**
* Checks if value is a plain object, that is, an object created by the Object constructor or one with a [[Prototype]] of null.
*
* @param value - The value to check
* @returns True if the value is a plain object, false otherwise
*
* @example
* isPlainObject({}) // true
* isPlainObject(Object.create(null)) // true
* isPlainObject([]) // false
* isPlainObject(null) // false
* isPlainObject(undefined) // false
* isPlainObject(new Date()) // false
* isPlainObject('string') // false
* isPlainObject(123) // false
* isPlainObject(function(){}) // false
*/
export function isPlainObject(value: unknown): value is Record<string, unknown> {
if (typeof value !== "object" || value === null) {
return false;
}
const prototype = Object.getPrototypeOf(value);
return prototype === null || prototype === Object.prototype;
}