json-chain
Version:
fluent chaining for json with dot-prop access
66 lines (51 loc) • 1.74 kB
Markdown
for json with dot-prop access
[![NPM version][json-chain-npm-image]][json-chain-npm-url]
[![MIT License][license-image]][license-url]
[![fliphub][gitter-badge]][gitter-url]
[![flipfam][flipfam-image]][flipfam-url]
[ ]: https://img.shields.io/npm/v/json-chain.svg
[ ]: https://npmjs.org/package/json-chain
[ ]: http://img.shields.io/badge/license-MIT-blue.svg?style=flat
[ ]: https://spdx.org/licenses/MIT
[ ]: https://img.shields.io/gitter/room/fliphub/pink.svg
[ ]: https://gitter.im/fliphub/Lobby
[ ]: https://img.shields.io/badge/%F0%9F%8F%97%20%F0%9F%92%A0-flipfam-9659F7.svg
[ ]: https://www.npmjs.com/package/flipfam
```bash
yarn add json-chain
npm i json-chain --save
```
```js
const JSONChain = require('json-chain')
```
```js
const data = {
eh: ['og'],
canada: true,
}
const chain = JSONChain.init(data).update('eh', ['some values'])
```
```js
const {readFileSync} = require('fs')
const pkg = readFileSync('./package.json', 'utf8')
const chain = new JSONChain(pkg)
.parse() // will be done automatically, is optional
.set('eh', ['some values']) // also as .update
.del('eh') // also as .delete, .remove
// also as .val
const test = chain.get('scripts.test')
const has = chain.has('version')
```
- has `.toString` and `.toJSON` methods for auto-stringifying when cast to `string` or `JSON.stringify`
```js
const chain = new JSONChain(pkg)
.updateIfNotEmpty('scripts.test', 'ava --verbose')
.updateIfNotEmpty('scripts.devDependencies', {'ava': '*'})
.write()
```
> fluent chaining