# path-operation

> path-operation is a tool for operating object

Latest version **0.2.4** (published 2019-12-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install path-operation
pnpm add path-operation
yarn add path-operation
bun add path-operation
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.4 |
| Published | 2019-12-10 |
| First published | 2019-12-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.0.0 |
| Dependencies | 0 |
| Unpacked size | 21.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | xyk0279 |

## Links

- npm: https://www.npmjs.com/package/path-operation
- Repository: https://github.com/panzer-vor/path-operation
- Homepage: https://github.com/panzer-vor/path-operation#readme
- Issues: https://github.com/panzer-vor/path-operation/issues
- npm.io page: https://npm.io/package/path-operation

## Recent versions

- 0.2.4 (latest) — 2019-12-10
- 0.2.3 — 2019-12-09
- 0.2.2 — 2019-12-07
- 0.2.1 — 2019-12-07
- 0.2.0 — 2019-12-07
- 0.1.1 — 2019-12-05
- 0.1.0 — 2019-12-04
- 0.0.5 — 2019-12-03
- 0.0.4 — 2019-12-03
- 0.0.3 — 2019-12-03
- 0.0.2 — 2019-12-03
- 0.0.1 — 2019-12-02

## README

# About Path Operation
path-operation is a tool for operating object

# Why use Path Operation
path-operation makes complex object easier to operating

# Usage
## install
```js
npm install path-operation
```

- Common Usage
```javascript
import po from 'path-operation'

const obj = {
  a: {
    b: 2,
    c: {
      d: 4
    }
  }
}

const pathObj = po(obj)

const bPath = pathObj(['a', 'b'])
const cPath = pathObj(['a', 'c'])
const dPath = pathObj(['a', 'c', 'd'])

bPath(v => v + 1) // { a: { b: 3, c: { d: 4 } } }
cPath(v => ({...v, f: 8})) // { a: { b: 3, c: { d: 4, f: 8 } } }
dPath(10) // { a: { b: 3, c: { d: 10, f: 8 } } }
```
- Redux Usage
```javascript
const state = {
  app: {
    name: 'puppy',
    age: 18
  }
}

const pathState(state)
const pathName = pathState(['app', 'name'])
const pathAge = pathState(['app', 'age'])

// reducer
(state, {path, payload}) => path(payload)

dispatch({
  path: pathName,
  payload: 'jack'
})
/**
state = {
  app: {
    name: 'jack',
    age: 18
  } 
}
*/
dispatch({
  path: pathAge,
  payload: pathAge.getValue() + 1
})
/**
state = {
  app: {
    name: 'jack',
    age: 19
  } 
}
*/
dispatch({
  path: pathAge,
  payload: pathAge.getValue() + 1
})
/**
state = {
  app: {
    name: 'jack',
    age: 20
  } 
}
*/
```
- Redux Middleware
[redux-po](https://github.com/panzer-vor/path-operation/tree/master/packages/redux-path-operation)

# API
```javascript
// - Config default: {defaultPath: [], mutable: true, pathMap: null}
const state = {
  app: {
    name: 'puppy',
    age: 18
  },
}
const path = Po(state, {defaultPath: ['app'], mutable: false})
// if not set default path, equal path(['app', 'age']) 👇
const pathAge = path(['age'])
// if config mutable is false, inner state is immutable
pathAge(
  pathAge.getValue() + 1 // path can use path.getValue to get path current value
)
// {...xxx, age: 19}
pathAge(pathAge.getValue() + 1) // {...xxx, age: 19}
```
- use path map config
```javascript
const path = Po(state, {
  pathMap: {
    userAge: ['app','age'],
    userName: ['app', 'name'],
  }
})
pathName(['userAge'])('jack') // {...xxx, name: 'jack'}
pathAge(['userAge'])(20) // {...xxx, age: 20}
```
# More Usage
[Test Use Case Link](https://github.com/panzer-vor/path-operation/blob/master/packages/path-operation/test/index.spec.ts)

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