Operate object specific path
npm install object-path-operatorsh
npm install --save object-path-operator
or
yarn add object-path-operator
`API
$3
`ts
function getProp(obj: object, path: PropertyKey[]): unknown
`Get object property by path.
`ts
const obj = {
key: ['value']
}getProp(obj, []) // throw error
getProp(obj, ['key', 0]) // value
getProp(obj, ['key-does-not-exist']) // throw error
`$3
`ts
function tryGetProp(obj: object, path: PropertyKey[], defaultValue?: unknown): unknown
``ts
const obj = {
key: ['value']
}tryGetProp(obj, []) // undefined
tryGetProp(obj, ['key', 0]) // value
tryGetProp(obj, ['key-does-not-exist']) // undefined
`$3
`ts
function getOwnProp(obj: object, path: PropertyKey[]): unknown
`$3
`ts
function tryGetOwnProp(obj: object, path: PropertyKey[], defaultValue?: unknown): unknown
`$3
`ts
function setProp(obj: object, path: PropertyKey[], value: unknown): boolean
`Set object property by path.
`ts
const obj = {
key: ['value']
}setProp(obj, [], 'new-value') // false
setProp(obj, ['key', 0], 'new-value') // true
setProp(obj, ['newKey'], 'new-value') // true
setProp(obj, ['path', 'does', 'not', 'exist'], 'new-value') // throw error
`$3
`ts
function trySetProp(
obj: object
, path: [PropertyKey, ...PropertyKey[]]
, value: unknown
): boolean
``ts
const obj = {
key: ['value']
}trySetProp(obj, [], 'new-value') // false
trySetProp(obj, ['key', 0], 'new-value') // true
trySetProp(obj, ['newKey'], 'new-value') // true
trySetProp(obj, ['path', 'does', 'not', 'exist'], 'new-value') // false
`$3
`ts
function setOwnProp(obj: object, path: PropertyKey[], value: unknown): boolean
`$3
`ts
function trySetOwnProp(obj: object, path: PropertyKey[], value: unknown): boolean
`$3
`ts
function removeProp(obj: object, path: PropertyKey[]): boolean
`Remove object property by path.
`ts
const obj = {
key: ['value']
}removeProp(obj, []) // throw error
removeProp(obj, ['key', 0]) // true
removeProp(obj, ['key-does-not-exist']) // throw error
`$3
`ts
function tryRemoveProp(obj: object, path: PropertyKey[]): boolean
``ts
const obj = {
key: ['value']
}tryRemoveProp(obj, []) // false
tryRemoveProp(obj, ['key', 0]) // true
tryRemoveProp(obj, ['key-does-not-exist']) // false
`$3
`ts
function removeOwnProp(obj: object, path: PropertyKey[]): boolean
`$3
`ts
function tryRemoveOwnProp(obj: object, path: PropertyKey[]): boolean
`$3
`ts
function propExists(obj: object, path: PropertyKey[]): boolean
``ts
const obj = {
key: ['value']
}propExists(obj, []) // throw error
propExists(obj, ['key', 0]) // true
propExists(obj, ['key-does-not-exist']) // false
`$3
`ts
function tryPropExists(obj: object, path: PropertyKey[]): boolean
``ts
const obj = {
key: ['value]
}tryPropExists(obj, []) // false
tryPropExists(obj, ['key', 0]) // true
tryPropExists(obj, ['key-does-not-exist']) // false
`$3
`ts
function ownPropExists(obj: object, path: PropertyKey[]): boolean
`$3
`ts
function tryOwnPropExists(obj: object, path: PropertyKey[]): boolean
``