Skip to content

Commit 1836c50

Browse files
committed
adding optional config for indent
1 parent e2c8c7b commit 1836c50

File tree

4 files changed

+90
-14
lines changed

4 files changed

+90
-14
lines changed

README.md

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
json-format
22
==========
33

4-
Change for using with npm modules. [original version](https://github.com/phoboslab/json-format).
4+
Parse JavaScript Object to a JSON String indented.
55

6-
#### Instaling ####
6+
#### Instaling
77
```
88
npm install json-format
99
```
1010

11-
#### Usage ###
11+
#### Usage
1212
```
1313
var jsonFormat = require('./');
1414
var fs = require('fs');
@@ -17,16 +17,53 @@ Change for using with npm modules. [original version](https://github.com/phobosl
1717
b: 2
1818
}
1919
20-
fs.writeFile('example.json', jsonFormat(obj), function(err){
20+
/* using config default, indent with tabs */
21+
fs.writeFile('example_tabs.json', jsonFormat(obj), function(err){
22+
if (err) throw err;
23+
console.log('saved');
24+
});
25+
26+
/* using indent with spaces */
27+
var config = {
28+
type: 'space',
29+
size: 2
30+
}
31+
32+
fs.writeFile('example_spaces.json', jsonFormat(obj, config), function(err){
2133
if (err) throw err;
2234
console.log('saved');
2335
});
2436
```
2537

26-
#### Result ####
38+
##### Result `example_tabs.json`
39+
```
40+
{
41+
"a": 1,
42+
"b": 2
43+
}
44+
```
45+
46+
##### Result `example_spaces.json`
2747
```
2848
{
2949
"a": 1,
3050
"b": 2
3151
}
32-
```
52+
```
53+
54+
#### Default sizes
55+
```
56+
{
57+
tab: { size: 1 },
58+
space: { size: 4 }
59+
}
60+
```
61+
62+
#### Config default
63+
```
64+
{
65+
type: 'tab'
66+
}
67+
```
68+
69+
[Based in this project](https://github.com/phoboslab/json-format).

index.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@
99
http://www.opensource.org/licenses/mit-license.php
1010
*/
1111
var p = [],
12+
indentConfig = {
13+
tab: { char: '\t', size: 1 },
14+
space: { char: ' ', size: 4 }
15+
},
16+
configDefault = {
17+
type: 'tab'
18+
},
1219
push = function( m ) { return '\\' + p.push( m ) + '\\'; },
1320
pop = function( m, i ) { return p[i-1] },
14-
tabs = function( count ) { return new Array( count + 1 ).join( '\t' ); };
21+
tabs = function( count, indentType) { return new Array( count + 1 ).join( indentType ); };
1522

16-
function JSONFormat ( json ) {
23+
function JSONFormat ( json, indentType ) {
1724
p = [];
1825
var out = "",
1926
indent = 0;
@@ -31,14 +38,14 @@ function JSONFormat ( json ) {
3138
switch(c) {
3239
case '{':
3340
case '[':
34-
out += c + "\n" + tabs(++indent);
41+
out += c + "\n" + tabs(++indent, indentType);
3542
break;
3643
case '}':
3744
case ']':
38-
out += "\n" + tabs(--indent) + c;
45+
out += "\n" + tabs(--indent, indentType) + c;
3946
break;
4047
case ',':
41-
out += ",\n" + tabs(indent);
48+
out += ",\n" + tabs(indent, indentType);
4249
break;
4350
case ':':
4451
out += ": ";
@@ -59,6 +66,15 @@ function JSONFormat ( json ) {
5966
return out;
6067
};
6168

62-
module.exports = function(json){
63-
return JSONFormat(JSON.stringify(json));
69+
module.exports = function(json, config){
70+
config = config || configDefault;
71+
var indent = indentConfig[config.type];
72+
73+
if ( indent == null ) {
74+
throw('[' + config.type + '] is not compatible!');
75+
} else {
76+
indentType = new Array((config.size || indent.size) + 1).join(indent.char);
77+
}
78+
79+
return JSONFormat(JSON.stringify(json), indentType);
6480
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-format",
3-
"version": "0.0.1",
3+
"version": "0.1.1",
44
"description": "JSON format for good presentation",
55
"main": "index.js",
66
"scripts": {

sample.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var jsonFormat = require('json-format');
2+
var fs = require('fs');
3+
var obj = {
4+
a: 1,
5+
b: 2
6+
}
7+
8+
/* using config default, indent with tabs */
9+
fs.writeFile('example_tabs.json', jsonFormat(obj), function(err){
10+
if (err) throw err;
11+
console.log('saved');
12+
});
13+
14+
/* using indent with spaces */
15+
var config = {
16+
type: 'space',
17+
size: 2
18+
}
19+
20+
fs.writeFile('example_spaces.json', jsonFormat(obj, config), function(err){
21+
if (err) throw err;
22+
console.log('saved');
23+
});

0 commit comments

Comments
 (0)