From 1836c5083457de5743f55c7f76899f2e61697aac Mon Sep 17 00:00:00 2001 From: luizstacio Date: Mon, 28 Sep 2015 02:07:49 -0300 Subject: [PATCH] adding optional config for indent --- README.md | 49 +++++++++++++++++++++++++++++++++++++++++++------ index.js | 30 +++++++++++++++++++++++------- package.json | 2 +- sample.js | 23 +++++++++++++++++++++++ 4 files changed, 90 insertions(+), 14 deletions(-) create mode 100644 sample.js diff --git a/README.md b/README.md index 64a766d..0fdc845 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,14 @@ json-format ========== -Change for using with npm modules. [original version](https://github.com/phoboslab/json-format). +Parse JavaScript Object to a JSON String indented. -#### Instaling #### +#### Instaling ``` npm install json-format ``` -#### Usage ### +#### Usage ``` var jsonFormat = require('./'); var fs = require('fs'); @@ -17,16 +17,53 @@ Change for using with npm modules. [original version](https://github.com/phobosl b: 2 } - fs.writeFile('example.json', jsonFormat(obj), function(err){ + /* using config default, indent with tabs */ + fs.writeFile('example_tabs.json', jsonFormat(obj), function(err){ + if (err) throw err; + console.log('saved'); + }); + + /* using indent with spaces */ + var config = { + type: 'space', + size: 2 + } + + fs.writeFile('example_spaces.json', jsonFormat(obj, config), function(err){ if (err) throw err; console.log('saved'); }); ``` -#### Result #### +##### Result `example_tabs.json` +``` +{ + "a": 1, + "b": 2 +} +``` + +##### Result `example_spaces.json` ``` { "a": 1, "b": 2 } -``` \ No newline at end of file +``` + +#### Default sizes +``` +{ + tab: { size: 1 }, + space: { size: 4 } +} +``` + +#### Config default +``` +{ + type: 'tab' +} +``` + +[Based in this project](https://github.com/phoboslab/json-format). \ No newline at end of file diff --git a/index.js b/index.js index 23b4b44..71d5f3f 100644 --- a/index.js +++ b/index.js @@ -9,11 +9,18 @@ http://www.opensource.org/licenses/mit-license.php */ var p = [], + indentConfig = { + tab: { char: '\t', size: 1 }, + space: { char: ' ', size: 4 } + }, + configDefault = { + type: 'tab' + }, push = function( m ) { return '\\' + p.push( m ) + '\\'; }, pop = function( m, i ) { return p[i-1] }, - tabs = function( count ) { return new Array( count + 1 ).join( '\t' ); }; + tabs = function( count, indentType) { return new Array( count + 1 ).join( indentType ); }; -function JSONFormat ( json ) { +function JSONFormat ( json, indentType ) { p = []; var out = "", indent = 0; @@ -31,14 +38,14 @@ function JSONFormat ( json ) { switch(c) { case '{': case '[': - out += c + "\n" + tabs(++indent); + out += c + "\n" + tabs(++indent, indentType); break; case '}': case ']': - out += "\n" + tabs(--indent) + c; + out += "\n" + tabs(--indent, indentType) + c; break; case ',': - out += ",\n" + tabs(indent); + out += ",\n" + tabs(indent, indentType); break; case ':': out += ": "; @@ -59,6 +66,15 @@ function JSONFormat ( json ) { return out; }; -module.exports = function(json){ - return JSONFormat(JSON.stringify(json)); +module.exports = function(json, config){ + config = config || configDefault; + var indent = indentConfig[config.type]; + + if ( indent == null ) { + throw('[' + config.type + '] is not compatible!'); + } else { + indentType = new Array((config.size || indent.size) + 1).join(indent.char); + } + + return JSONFormat(JSON.stringify(json), indentType); } \ No newline at end of file diff --git a/package.json b/package.json index 0d471a3..79b421b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "json-format", - "version": "0.0.1", + "version": "0.1.1", "description": "JSON format for good presentation", "main": "index.js", "scripts": { diff --git a/sample.js b/sample.js new file mode 100644 index 0000000..bfd37e8 --- /dev/null +++ b/sample.js @@ -0,0 +1,23 @@ +var jsonFormat = require('json-format'); +var fs = require('fs'); +var obj = { + a: 1, + b: 2 +} + +/* using config default, indent with tabs */ +fs.writeFile('example_tabs.json', jsonFormat(obj), function(err){ + if (err) throw err; + console.log('saved'); +}); + +/* using indent with spaces */ +var config = { + type: 'space', + size: 2 +} + +fs.writeFile('example_spaces.json', jsonFormat(obj, config), function(err){ + if (err) throw err; + console.log('saved'); +}); \ No newline at end of file