isPlainObject
lang
Checks if value is a plain object.
Installation
Import
import { isPlainObject } from '@tulx/utils';Source Code
Implementation
/**
* Checks if value is a plain object.
*
* @param value - The value to check.
* @returns Returns true if value is a plain object, else false.
*
* @example
* ```ts
* function Foo() {
* this.a = 1;
* }
* isPlainObject(new Foo); // false
* isPlainObject({ 'x': 0, 'y': 0 }); // true
* isPlainObject([1, 2, 3]); // false
* isPlainObject(Object.create(null)); // true
* ```
*/
export function isPlainObject(
value: unknown
): value is Record<string, unknown> {
if (value === null || typeof value !== 'object') {
return false;
}
if (Object.getPrototypeOf(value) === null) {
return true;
}
let proto = value;
while (Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto);
}
return Object.getPrototypeOf(value) === proto;
}
Example
import { isPlainObject } from '@tulx/utils';
function Foo() {
this.a = 1;
}
isPlainObject(new Foo); // false
isPlainObject({ 'x': 0, 'y': 0 }); // true
isPlainObject([1, 2, 3]); // false
isPlainObject(Object.create(null)); // trueRelated Functions
castArray
Casts value as an array if it's not one.
clone
Creates a shallow clone of value.
cloneDeep
This method is like clone except that it recursively clones value.
cloneDeepWith
This method is like cloneDeep except that it accepts customizer which is invoked to produce the cloned value.
cloneWith
This method is like clone except that it accepts customizer which is invoked to produce the cloned value.
conformsTo
Checks if object conforms to source by invoking the predicate properties of source with the corresponding property values of object.