Skip to content

Commit

Permalink
adding optional config for indent
Browse files Browse the repository at this point in the history
  • Loading branch information
luizstacio committed Sep 28, 2015
1 parent e2c8c7b commit 1836c50
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 14 deletions.
49 changes: 43 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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
}
```
```

#### Default sizes
```
{
tab: { size: 1 },
space: { size: 4 }
}
```

#### Config default
```
{
type: 'tab'
}
```

[Based in this project](https://github.com/phoboslab/json-format).
30 changes: 23 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 += ": ";
Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
23 changes: 23 additions & 0 deletions sample.js
Original file line number Diff line number Diff line change
@@ -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');
});

0 comments on commit 1836c50

Please sign in to comment.