toPlainObject
lang
Converts value to a plain object flattening inherited enumerable string keyed properties of value to own properties of the plain object.
Installation
Import
import { toPlainObject } from '@tulx/utils';Source Code
Implementation
/**
* Converts value to a plain object flattening inherited enumerable string keyed properties of value to own properties of the plain object.
*
* @param value - The value to convert.
* @returns Returns the converted plain object.
*
* @example
* ```ts
* function Foo() {
* this.b = 2;
* }
* Foo.prototype.c = 3;
* toPlainObject(new Foo); // { 'b': 2, 'c': 3 }
* ```
*/
export function toPlainObject(value: unknown): Record<string, unknown> {
const result: Record<string, unknown> = {};
if (value === null || value === undefined) {
return result;
}
for (const key in value as Record<string, unknown>) {
result[key] = (value as Record<string, unknown>)[key];
}
return result;
}
Example
import { toPlainObject } from '@tulx/utils';
function Foo() {
this.b = 2;
}
Foo.prototype.c = 3;
toPlainObject(new Foo); // { 'b': 2, 'c': 3 }Related 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.