fs-json-writer

1.0.0-beta • Public • Published

fs-json-writer

Generate JSON file content readable by a human

The native solution fs.writeFile combined with JSON.stringify generate a minified content because JSON.stringify is designed for data transfer.

This package full rewrite a JavaScript Object Notation with resolve tabulation depth and resolve nested object/array.

installation

in depending your need you can install at dev-dependencies

> npm install --save fs-json-writer

or with yarn

> yarn add fs-json-writer

usage

json file

const jsonWriter = require('fs-json-writer');
const path = require('path');
 
 
const myHumanJson = {
 
  version: "1.0.0",
  details: "this is a stable version with goodly peoples ^.^"
};
 
jsonWriter({
 
  path: path.join( __dirname, "./file-name.json" ),
 
  state: myHumaJson
});
 

output (filename.json)

{
  "version": "1.0.0",
  "details": "this is a stable version with goodly peoples ^.^"
}

This encoding use is always UTF-8 for write a JSON content and path argument should be a absolute path

js file

const jsonWriter = require('fs-json-writer');
const path = require('path');
 
 
const myHumanJson = {
 
  version: "1.0.0",
  details: "this is a stable version with goodly peoples ^.^"
};
 
jsonWriter({
 
  path: path.join( __dirname, "./file-name.js" ),
  state: myHumaJson,
 
  isEs6: true,
  isNoQuote: true
});
 

extension of filename determined if content should be JS or JSON file.

output (filename.js)

export default {
 
  version: "1.0.0",
  details: "this is a stable version with goodly peoples ^.^"
}

async

Can asynchrone generate content with Promise API or inehrit from natively fs module callback system.

Promise

const jsonWriter = require('fs-json-writer');
const path = require('path');
 
 
const myHumanJson = {
 
  version: "1.0.0",
  details: "this is a stable version with goodly peoples ^.^"
};
 
jsonWriter.async({
 
  path: path.join( __dirname, "./file-name.js" ),
  state: myHumaJson,
 
  isEs6: true
})
.then( ({ path, content, state }) => {
 
  console.log( "success write at:" + path + " the content: " + content + " from javascript object: ", state );
 
} )
.catch( error => {
 
  console.log( error );
 
  throw "Oops, something went wrong.";
 
} );
 

Callback

const jsonWriter = require('fs-json-writer');
const path = require('path');
 
 
const myHumanJson = {
 
  version: "1.0.0",
  details: "this is a stable version with goodly peoples ^.^"
};
 
jsonWriter.legacyAsync({
 
  path: path.join( __dirname, "./file-name.js" ),
  state: myHumaJson,
 
  isEs6: true,
  isNoQuote: true,
 
  onError: error => {
 
    console.log( error );
 
    throw "Oops, something went wrong.";
  },
 
  onSuccess: ({path, content, state}) => {
 
    console.log( "success write at:" + path + " the content: " + content + " from javascript object: ", state );
 
  }
 
});
 

optionals

From a syn/async call can define optionals attributes: onReplace: () => any | string[] | number[], space: string | number this attributes is passed to arg2 and arg3 of JSON.stringify

Please if you detect any bugs/undetermined comportement open new issue

Package Sidebar

Install

npm i fs-json-writer

Weekly Downloads

1

Version

1.0.0-beta

License

BSD-2-CLAUSE

Unpacked Size

40.6 kB

Total Files

22

Last publish

Collaborators

  • orivoir21