2.0.0 • Published 6 years ago

bosom v2.0.0

Weekly downloads
6
License
MIT
Repository
github
Last release
6 years ago

bosom

npm version

bosom is a Node.JS package which can read and write JSON files easily. It can be used to record and update configurations as serialised JSON data.

yarn add bosom

API

The package is available by importing its default function:

import bosom from '@rqt/namecheap-web'

async bosom(  path: string,  data=: Object,  options=: Options,): string|void

The function which can be used for both reading and writing of data. When only a single argument is passed, the read mode is assumed. To write data, it should be passed as the second argument, with an optional config.

bosom will read the file when only a path is passed.

/* eslint-disable no-console */
import bosom from 'bosom'
import { resolve } from 'path'

(async () => {
  try {
    const p = resolve(__dirname, 'example.json')
    const res = await bosom(p)
    console.log(res)
  } catch (err) {
    console.error(err)
  }
})()
t
{ hello: 'world', foo: true, bar: -1 }

When data is passed, bosom will write into that file the serialised version of the object. An optional configuration can be passed along to the JSON.stringify method.

_bosom.Options: Options for writing.

/* eslint-disable no-console */
import bosom from 'bosom'
import { resolve } from 'path'

(async () => {
  try {
    const p = resolve(__dirname, 'temp.json')
    await bosom(p, {
      'my-data': true,
      bar: 'foo',
    }, {
      space: 2,
    })

    // test the written value
    const actual = require(p)
    console.log(actual)
  } catch (err) {
    console.error(err)
  }
})()
t
{ 'my-data': true, bar: 'foo' }

Copyright