1.0.0-beta • Published 4 years ago

fs-json-writer v1.0.0-beta

Weekly downloads
3
License
BSD-2-CLAUSE
Repository
github
Last release
4 years ago

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

1.0.0-beta

4 years ago

0.9.2

4 years ago

0.9.1

4 years ago

0.9.0

4 years ago

0.9.0-beta

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago