Skip to content

Commit d5efbb3

Browse files
committed
[New] add collapseEmpty option
New option to remove newlines in empty arrays and objects Fixes #15
1 parent cbe368f commit d5efbb3

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ declare namespace stableStringify {
1010

1111
type StableStringifyOptions = {
1212
cmp?: Comparator;
13+
collapseEmpty?: boolean;
1314
cycles?: boolean;
1415
replacer?: (this: Node, key: Key, value: unknown) => unknown;
1516
space?: string | number;

index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module.exports = function stableStringify(obj) {
3434
var cycles = !!opts && typeof opts.cycles === 'boolean' && opts.cycles;
3535
/** @type {undefined | typeof defaultReplacer} */
3636
var replacer = opts && opts.replacer ? callBind(opts.replacer) : defaultReplacer;
37+
var collapseEmpty = (opts && opts.collapseEmpty) || false;
3738

3839
var cmpOpt = typeof opts === 'function' ? opts : opts && opts.cmp;
3940
/** @type {undefined | (<T extends import('.').NonArrayNode>(node: T) => (a: Exclude<keyof T, symbol | number>, b: Exclude<keyof T, symbol | number>) => number)} */
@@ -73,13 +74,21 @@ module.exports = function stableStringify(obj) {
7374
if (typeof node !== 'object' || node === null) {
7475
return jsonStringify(node);
7576
}
77+
78+
/** @type {(out: any[], brackets: '[]' | '{}') => string} */
79+
var groupOutput = function (out, brackets) {
80+
return collapseEmpty && out.length === 0
81+
? brackets
82+
: brackets[0] + $join(out, ',') + indent + brackets[1];
83+
};
84+
7685
if (isArray(node)) {
7786
var out = [];
7887
for (var i = 0; i < node.length; i++) {
7988
var item = stringify(node, i, node[i], level + 1) || jsonStringify(null);
8089
out[out.length] = indent + space + item;
8190
}
82-
return '[' + $join(out, ',') + indent + ']';
91+
return groupOutput(out, '[]');
8392
}
8493

8594
if ($indexOf(seen, node) !== -1) {
@@ -107,7 +116,7 @@ module.exports = function stableStringify(obj) {
107116
out[out.length] = indent + space + keyValue;
108117
}
109118
$splice(seen, $indexOf(seen, node), 1);
110-
return '{' + $join(out, ',') + indent + '}';
119+
return groupOutput(out, '{}');
111120
}({ '': obj }, '', obj, 0)
112121
);
113122
};

test/space.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ test('space parameter (same as native)', function (t) {
7474
);
7575
});
7676

77+
test('space parameter base empty behavior: empty arrays and objects have added newline and space', function (t) {
78+
t.plan(1);
79+
var obj = { emptyArr: [], emptyObj: {} };
80+
t.equal(
81+
stringify(obj, { space: ' ' }),
82+
'{\n "emptyArr": [\n ],\n "emptyObj": {\n }\n}'
83+
);
84+
});
85+
86+
test('space parameter, with collapseEmpty: true', function (t) {
87+
t.plan(1);
88+
var obj = { emptyArr: [], emptyObj: {} };
89+
t.equal(
90+
stringify(obj, { collapseEmpty: true, space: ' ' }),
91+
'{\n "emptyArr": [],\n "emptyObj": {}\n}'
92+
);
93+
});
94+
7795
test('space parameter, on a cmp function', function (t) {
7896
t.plan(3);
7997
var obj = { one: 1, two: 2 };

0 commit comments

Comments
 (0)