formatter-json

0.1.1 • Public • Published

Just pass any data to it, it will format JSON for you.

install

$ npm install formatter-json

Usage

const fj = require('formatter-json');
 
const testObj = {
    msg: '',
    success: true,
    deepData: {
        author: 'Chvin',
        github: 'https://github.com/chvin',
        address: 'China',
        age: 0,
        favorite: [
            'run', 'movie', 'coding', {eat: 'rice'}
        ]
    },
    someReg: /hello world/g,
    someDate: new Date,
    someNull: null,
    someUndefined: undefined,
    someSymbol: Symbol()
};
 
const strTestObj = JSON.stringify(testObj);
 
console.log(fj(testObj));
 
console.log(fj(strTestObj));
 

You will get the same result.

{
  "msg": "",
  "success": true,
  "deepData": {
    "author": "Chvin",
    "github": "https://github.com/chvin",
    "address": "China",
    "age": 0,
    "favorite": [
      "run",
      "movie",
      "coding",
      {
        "eat": "rice"
      }
    ]
  },
  "someReg": {},
  "someDate": "2018-11-12T02:48:28.156Z",
  "someNull": null
}

Set indentation

const fj = require('formatter-json');
const strTestObj = JSON.stringify(testObj, 4); // default 2

You will get:

{
    "msg": "",
    "success": true,
    "deepData": {
        "author": "Chvin",
        "github": "https://github.com/chvin",
        "address": "China",
        "age": 0,
        "favorite": [
            "run",
            "movie",
            "coding",
            {
                "eat": "rice"
            }
        ]
    },
    "someReg": {},
    "someDate": "2018-11-12T02:51:37.590Z",
    "someNull": null
}

Any type of result

const fj = require('formatter-json');
 
console.log(fj(1)); // '1'
 
console.log(fj(null)); // 'null'
 
console.log(fj(undefined)); // undefined
 
console.log(fj('abc')); // '"abc"'
 
console.log(fj(/reg/)); // '{}'
 
console.log(fj(new Date)); // '2017-09-12T09:09:49.460Z'
 
console.log(fj(Symbol())); // undefined
 
console.log(fj({a: 'a', b: 'b'}));
/*
`
{
  "a": "a",
  "b": "b"
}
`
*/
 
console.log(fj([1, 2, 3]));
/*
`
[
  1,
  2,
  3
]
`
*/

Readme

Keywords

Package Sidebar

Install

npm i formatter-json

Weekly Downloads

3

Version

0.1.1

License

MIT

Unpacked Size

9.59 kB

Total Files

5

Last publish

Collaborators

  • chvin