Object
Object utility class that provides various methods for objects.
The Object class is extended from the built-in Object object. You can continue to use whatever we normally use.
Usage
To use this module, you first need to import the Object class:
import { Object } from '@ponsetya/core'
Static Methods
Object.assign()
The assign() method recursively merge two objects. The resulting object has all properties from both objects.
If a property from both objects has the same name, then the value from the second object will be used.
Object.assign<T, S>(target: T, source: S): T & S
Parameters
- target:
T
- The target object to merge into.
- source:
S
- The source object to merge from.
- Returns:
T & S
- A new object that has all properties from both
target
andsource
.
Examples
const target = { a: 1, b: 2 }
const source = { c: 3, d: 4 }
console.log(Object.assign(target, source)) // { a: 1, b: 2, c: 3, d: 4 }
Object.is()
The is() method determines whether the passed value is an Object.
Object.is(value: unknown): boolean
Parameters
- value:
unknown
- The target object to merge into.
- Returns:
boolean
- A boolean indicating whether the variable is a object.
Examples
console.log(Object.is({ x: true })) // true
console.log(Object.is({})) // true
console.log(Object.is([])) // false
console.log(Object.is(null)) // false
Object.omit()
The omit() method strips the passed keys from the object.
Object.omit<T, K extends keyof T>(obj: T, ...keys: K[]): Omit<T, K>
Parameters
- obj:
T
- The object from which keys are to be omitted.
- ...keys:
K[]
- The keys to be omitted.
- Returns:
Omit<T, K>
- A new object with the same properties as the original object, except for the omitted keys.
Examples
const obj = { x: 1, y: 2, z: 3 }
console.log(Object.omit(obj, 'y', 'z')); // { x: 1 }