# object-path-operator

> Operate object specific path

Latest version **5.2.2** (published 2023-06-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install object-path-operator
pnpm add object-path-operator
yarn add object-path-operator
bun add object-path-operator
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.2.2 |
| Published | 2023-06-11 |
| First published | 2017-04-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 24.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | BlackGlory |
| Maintainers | black_glory |

## Links

- npm: https://www.npmjs.com/package/object-path-operator
- Repository: https://github.com/BlackGlory/object-path-operator
- Homepage: https://github.com/BlackGlory/object-path-operator#readme
- Issues: https://github.com/BlackGlory/object-path-operator/issues
- npm.io page: https://npm.io/package/object-path-operator

## Recent versions

- 5.2.2 (latest) — 2023-06-11
- 5.2.1 — 2022-08-01
- 5.2.0 — 2022-05-20
- 5.1.0 — 2022-05-20
- 5.0.0 — 2021-11-19
- 4.0.1 — 2021-11-19
- 4.0.0 — 2021-11-19
- 3.0.0 — 2020-12-22
- 2.0.0 — 2020-12-22
- 1.0.0 — 2018-05-08
- 0.3.0 — 2018-04-28
- 0.2.1 — 2018-01-01
- 0.2.0 — 2018-01-01
- 0.1.0 — 2017-04-28

## 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
```

---
_Source: https://npm.io/package/object-path-operator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
