# histor

> object with history

Latest version **1.0.1** (published 2019-02-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install histor
pnpm add histor
yarn add histor
bun add histor
```

## 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 | 1.0.1 |
| Published | 2019-02-17 |
| First published | 2019-02-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | fi3ework |
| Maintainers | fi3ework |
| Keywords | object |

## Links

- npm: https://www.npmjs.com/package/histor
- Repository: https://github.com/fi3ework/histor
- Issues: https://github.com/fi3ework/histor
- npm.io page: https://npm.io/package/histor

## Recent versions

- 1.0.1 (latest) — 2019-02-17
- 1.0.0 — 2019-02-17

## README

# histor

histor is a enhancer to watch the deep change of an object and tell the detail of the change.

## Install

```bash
$ yarn add histor
```

or

```bash
$ npm i histor
```

## Usage

### histor(object, onChange)

Accept an object and a `onChange` callback function. The callback function will be called if any part of the object is changed.

The `onChange` callback will receive a change detail whose type is:

```ts
interface IDiff {
  path: (string | number)[] // the path chain of the changed key
  from: any // previous value before change of the prop
  to: any // current value after change of the prop
}
```

Example:

```ts
import histor from 'histor'

const person = {
  name: 'wee',
  skills: ['eat', 'sleep'],
  pets: {
    cat: {
      name: 'c1'
    },
    dog: 'd1'
  }
}

const p = histor(person, diff => {
  console.log(`$${diff.path.map(p => `[${p}]`).join('')}: ${diff.from} ==> ${diff.to}`)
})

person.pets.cat.name = 'c2'
// it will log:
// `$[pets][cat][name]: c1 ==> c2`
```

## ⚠️ CAVEAT

- The change diff will just keep a reference to the changed value. So if the new value or the new value has been changed deeply afterward. The previous diff will be changed at the same time. So it's better to serialize the diff to make it immutable.

## Thanks

Inspired by [on-change](https://github.com/sindresorhus/on-change).

## License

MIT

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