Skip to content

BlackGlory/object-path-operator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

895c058 Β· Jun 11, 2023

History

32 Commits
Dec 24, 2021
May 20, 2022
May 20, 2022
Nov 19, 2021
May 20, 2022
Nov 19, 2021
Jun 11, 2023
Nov 19, 2021
May 20, 2022
Nov 19, 2021
Dec 29, 2021
Jun 11, 2023
Nov 19, 2021
Nov 19, 2021
Nov 19, 2021
Dec 18, 2022

Repository files navigation

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