pugjs / js-stringify

Stringify an object so it can be safely inlined in JavaScript code
MIT License
19 stars 4 forks source link

Allow pretty-printing JSON output #5

Open cspotcode opened 7 years ago

cspotcode commented 7 years ago

This patch allows emitting objects over multiple lines with indentation, for applications that might need it. For example, a CLI tool might have a --pretty or --debug flag which generates more readable output.

It adds a second, optional argument to stringify. It's passed as JSON.stringify's third argument, which controls indentation.

stringify({foo: 'bar', baz: true});
/* {"foo":"bar","baz":true} */
stringify({foo: 'bar', baz: true}, '  ');
/* {
  "foo": "bar",
  "baz": true
}*/
stringify({foo: 'bar', baz: true}, 4);
/* {
    "foo": "bar",
    "baz": true
}*/