object-path-operator
TypeScript icon, indicating that this package has built-in type declarations

5.2.2 • Public • Published

object-path-operator

Install

npm install --save object-path-operator
# or
yarn add object-path-operator

API

getProp

function getProp(obj: object, path: PropertyKey[]): unknown

Get object property by path.

const obj = {
  key: ['value']
}

getProp(obj, []) // throw error
getProp(obj, ['key', 0]) // value
getProp(obj, ['key-does-not-exist']) // throw error

tryGetProp

function tryGetProp(obj: object, path: PropertyKey[], defaultValue?: unknown): unknown
const obj = {
  key: ['value']
}

tryGetProp(obj, []) // undefined
tryGetProp(obj, ['key', 0]) // value
tryGetProp(obj, ['key-does-not-exist']) // undefined

getOwnProp

function getOwnProp(obj: object, path: PropertyKey[]): unknown

tryGetOwnProp

function tryGetOwnProp(obj: object, path: PropertyKey[], defaultValue?: unknown): unknown

setProp

function setProp(obj: object, path: PropertyKey[], value: unknown): boolean

Set object property by path.

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

trySetProp

function trySetProp(
  obj: object
, path: [PropertyKey, ...PropertyKey[]]
, value: unknown
): boolean
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

setOwnProp

function setOwnProp(obj: object, path: PropertyKey[], value: unknown): boolean

trySetOwnProp

function trySetOwnProp(obj: object, path: PropertyKey[], value: unknown): boolean

removeProp

function removeProp(obj: object, path: PropertyKey[]): boolean

Remove object property by path.

const obj = {
  key: ['value']
}

removeProp(obj, []) // throw error
removeProp(obj, ['key', 0]) // true
removeProp(obj, ['key-does-not-exist']) // throw error

tryRemoveProp

function tryRemoveProp(obj: object, path: PropertyKey[]): boolean
const obj = {
  key: ['value']
}

tryRemoveProp(obj, []) // false
tryRemoveProp(obj, ['key', 0]) // true
tryRemoveProp(obj, ['key-does-not-exist']) // false

removeOwnProp

function removeOwnProp(obj: object, path: PropertyKey[]): boolean

tryRemoveOwnProp

function tryRemoveOwnProp(obj: object, path: PropertyKey[]): boolean

propExists

function propExists(obj: object, path: PropertyKey[]): boolean
const obj = {
  key: ['value']
}

propExists(obj, []) // throw error
propExists(obj, ['key', 0]) // true
propExists(obj, ['key-does-not-exist']) // false

tryPropExists

function tryPropExists(obj: object, path: PropertyKey[]): boolean
const obj = {
  key: ['value]
}

tryPropExists(obj, []) // false
tryPropExists(obj, ['key', 0]) // true
tryPropExists(obj, ['key-does-not-exist']) // false

ownPropExists

function ownPropExists(obj: object, path: PropertyKey[]): boolean

tryOwnPropExists

function tryOwnPropExists(obj: object, path: PropertyKey[]): boolean

Readme

Keywords

none

Package Sidebar

Install

npm i object-path-operator

Weekly Downloads

246

Version

5.2.2

License

MIT

Unpacked Size

25.4 kB

Total Files

23

Last publish

Collaborators

  • black_glory