Skip to content

Commit e6815c9

Browse files
rprietoJames Halliday
authored andcommitted
New “space” option to enable pretty printing (same as ES5)
1 parent 20a2e83 commit e6815c9

File tree

3 files changed

+103
-6
lines changed

3 files changed

+103
-6
lines changed

index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var json = typeof JSON !== 'undefined' ? JSON : require('jsonify');
33
module.exports = function (obj, opts) {
44
if (!opts) opts = {};
55
if (typeof opts === 'function') opts = { cmp: opts };
6+
var space = opts.space || '';
7+
if (typeof space === 'number') space = Array(space+1).join(' ');
68
var cmp = opts.cmp && (function (f) {
79
return function (node) {
810
return function (a, b) {
@@ -13,27 +15,31 @@ module.exports = function (obj, opts) {
1315
};
1416
})(opts.cmp);
1517

16-
return (function stringify (node) {
18+
return (function stringify (node, level) {
19+
var indent = space ? ('\n' + new Array(level + 1).join(space)) : '';
20+
var colonSeparator = space ? ': ' : ':';
1721
if (typeof node !== 'object' || node === null) {
1822
return json.stringify(node);
1923
}
2024
if (isArray(node)) {
2125
var out = [];
2226
for (var i = 0; i < node.length; i++) {
23-
out.push(stringify(node[i]));
27+
var item = stringify(node[i], level+1);
28+
out.push(indent + space + item);
2429
}
25-
return '[' + out.join(',') + ']';
30+
return '[' + out.join(',') + indent + ']';
2631
}
2732
else {
2833
var keys = objectKeys(node).sort(cmp && cmp(node));
2934
var out = [];
3035
for (var i = 0; i < keys.length; i++) {
3136
var key = keys[i];
32-
out.push(stringify(key) + ':' + stringify(node[key]));
37+
var keyValue = stringify(key,0) + colonSeparator + stringify(node[key],level+1);
38+
out.push(indent + space + keyValue);
3339
}
34-
return '{' + out.join(',') + '}';
40+
return '{' + out.join(',') + indent + '}';
3541
}
36-
})(obj);
42+
})(obj, 0);
3743
};
3844

3945
var isArray = Array.isArray || function (x) {

readme.markdown

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ var stringify = require('json-stable-stringify')
3333

3434
Return a deterministic stringified string `str` from the object `obj`.
3535

36+
## options
37+
38+
### cmp
39+
3640
If `opts` is given, you can supply an `opts.cmp` to have a custom comparison
3741
function for object keys. Your function `opts.cmp` is called with these
3842
parameters:
@@ -77,6 +81,34 @@ which outputs:
7781
{"d":6,"c":5,"b":[{"z":3,"y":2,"x":1},9],"a":10}
7882
```
7983

84+
### space
85+
86+
If you specify `opts.space`, it will indent the output for pretty-printing. Valid values are strings (e.g. `{space: \t}`) or a number of spaces (`{space: 3}`).
87+
88+
For example:
89+
90+
```js
91+
var obj = { b: 1, a: { foo: 'bar', and: [1, 2, 3] } };
92+
var s = stringify(obj, { space: ' ' });
93+
console.log(s);
94+
```
95+
96+
which outputs:
97+
98+
```
99+
{
100+
"a": {
101+
"and": [
102+
1,
103+
2,
104+
3
105+
],
106+
"foo": "bar"
107+
},
108+
"b": 1
109+
}
110+
```
111+
80112
# install
81113

82114
With [npm](https://npmjs.org) do:

test/space.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
var test = require('tape');
2+
var stringify = require('../');
3+
4+
test('space parameter', function (t) {
5+
t.plan(1);
6+
var obj = { one: 1, two: 2 };
7+
t.equal(stringify(obj, {space: ' '}), ''
8+
+ '{\n'
9+
+ ' "one": 1,\n'
10+
+ ' "two": 2\n'
11+
+ '}'
12+
);
13+
});
14+
15+
test('space parameter (with tabs)', function (t) {
16+
t.plan(1);
17+
var obj = { one: 1, two: 2 };
18+
t.equal(stringify(obj, {space: '\t'}), ''
19+
+ '{\n'
20+
+ '\t"one": 1,\n'
21+
+ '\t"two": 2\n'
22+
+ '}'
23+
);
24+
});
25+
26+
test('space parameter (with a number)', function (t) {
27+
t.plan(1);
28+
var obj = { one: 1, two: 2 };
29+
t.equal(stringify(obj, {space: 3}), ''
30+
+ '{\n'
31+
+ ' "one": 1,\n'
32+
+ ' "two": 2\n'
33+
+ '}'
34+
);
35+
});
36+
37+
test('space parameter (nested objects)', function (t) {
38+
t.plan(1);
39+
var obj = { one: 1, two: { b: 4, a: [2,3] } };
40+
t.equal(stringify(obj, {space: ' '}), ''
41+
+ '{\n'
42+
+ ' "one": 1,\n'
43+
+ ' "two": {\n'
44+
+ ' "a": [\n'
45+
+ ' 2,\n'
46+
+ ' 3\n'
47+
+ ' ],\n'
48+
+ ' "b": 4\n'
49+
+ ' }\n'
50+
+ '}'
51+
);
52+
});
53+
54+
test('space parameter (same as native)', function (t) {
55+
t.plan(1);
56+
// for this test, properties need to be in alphabetical order
57+
var obj = { one: 1, two: { a: [2,3], b: 4 } };
58+
t.equal(stringify(obj, {space: ' '}), JSON.stringify(obj, null, ' '));
59+
});

0 commit comments

Comments
 (0)