https://github.com/blackglory/object-path-operator
🌳
https://github.com/blackglory/object-path-operator
browser library nodejs npm-package typescript
Last synced: 2 months ago
JSON representation
🌳
- Host: GitHub
- URL: https://github.com/blackglory/object-path-operator
- Owner: BlackGlory
- License: mit
- Created: 2021-11-19T03:12:47.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-06-11T04:38:51.000Z (about 3 years ago)
- Last Synced: 2025-04-07T16:53:23.426Z (over 1 year ago)
- Topics: browser, library, nodejs, npm-package, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/object-path-operator
- Size: 1.09 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# object-path-operator
## Install
```sh
npm install --save object-path-operator
# or
yarn add object-path-operator
```
## API
### getProp
```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
```
### tryGetProp
```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
```
### getOwnProp
```ts
function getOwnProp(obj: object, path: PropertyKey[]): unknown
```
### tryGetOwnProp
```ts
function tryGetOwnProp(obj: object, path: PropertyKey[], defaultValue?: unknown): unknown
```
### setProp
```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
```
### trySetProp
```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
```
### setOwnProp
```ts
function setOwnProp(obj: object, path: PropertyKey[], value: unknown): boolean
```
### trySetOwnProp
```ts
function trySetOwnProp(obj: object, path: PropertyKey[], value: unknown): boolean
```
### removeProp
```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
```
### tryRemoveProp
```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
```
### removeOwnProp
```ts
function removeOwnProp(obj: object, path: PropertyKey[]): boolean
```
### tryRemoveOwnProp
```ts
function tryRemoveOwnProp(obj: object, path: PropertyKey[]): boolean
```
### propExists
```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
```
### tryPropExists
```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
```
### ownPropExists
```ts
function ownPropExists(obj: object, path: PropertyKey[]): boolean
```
### tryOwnPropExists
```ts
function tryOwnPropExists(obj: object, path: PropertyKey[]): boolean
```