An open API service indexing awesome lists of open source software.

https://github.com/artdecocode/bosom

Read and write a JSON file in Node.js
https://github.com/artdecocode/bosom

Last synced: 3 months ago
JSON representation

Read and write a JSON file in Node.js

Awesome Lists containing this project

README

          

# bosom

[![npm version](https://badge.fury.io/js/bosom.svg)](https://npmjs.org/package/bosom)

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

```sh
yarn add bosom
```



- [API](#api)
- [`async bosom(path: string, data=: Object, options=: Options): string|void`](#async-bosompath-stringdata-objectoptions-options-stringvoid)
* [`_bosom.Options`](#type-_bosomoptions)
- [Copyright](#copyright)



## API

The package is available by importing its default function:

```js
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.

```js
/* 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)
}
})()
```
```js
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.


Name
Type & Description


space
number



How many spaces to use for indentation.


replacer
!Function



The replacer function used when serializing data (see JSON.stringify documentation).

```js
/* 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)
}
})()
```
```js
t
{ 'my-data': true, bar: 'foo' }
```



## Copyright