Skip to content

Commit 1802836

Browse files
authored
Fix output of symbols to make them legal JS (#81)
1 parent 8b89474 commit 1802836

3 files changed

Lines changed: 31 additions & 6 deletions

File tree

index.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,26 @@ export default function stringifyObject(input, options, pad) {
5555
|| typeof input === 'number'
5656
|| typeof input === 'boolean'
5757
|| typeof input === 'function'
58-
|| typeof input === 'symbol'
5958
|| isRegexp(input)
6059
) {
6160
return String(input);
6261
}
6362

63+
if (typeof input === 'symbol') {
64+
const t = input.description;
65+
if (t === undefined) return 'Symbol()';
66+
if (t.slice(0,7) === 'Symbol.'
67+
&& Symbol[t.slice(7)] === input
68+
) {
69+
return t;
70+
}
71+
const q = stringify(t, options);
72+
if (Symbol.keyFor(input) !== undefined) {
73+
return `Symbol.for(${q})`;
74+
}
75+
return `Symbol(${q})`;
76+
}
77+
6478
if (input instanceof Date) {
6579
return `new Date('${input.toISOString()}')`;
6680
}
@@ -106,7 +120,9 @@ export default function stringifyObject(input, options, pad) {
106120
const eol = objectKeys.length - 1 === index ? tokens.newline : ',' + tokens.newlineOrSpace;
107121
const isSymbol = typeof element === 'symbol';
108122
const isClassic = !isSymbol && /^[a-z$_][$\w]*$/i.test(element);
109-
const key = isSymbol || isClassic ? element : stringify(element, options);
123+
let key = element;
124+
if (!isClassic) key = stringify(element, options);
125+
if (isSymbol) key = '[' + key + ']';
110126

111127
let value = stringify(input[element], options, pad + indent);
112128
if (options.transform) {

test/fixtures/object.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
Infinity: Infinity,
2525
newlines: "foo\nbar\r\nbaz",
2626
circular: "[Circular]",
27-
Symbol(): Symbol(),
28-
Symbol(foo): Symbol(foo),
29-
Symbol(foo): Symbol(foo)
27+
[Symbol()]: Symbol(),
28+
[Symbol("foo")]: Symbol("foo"),
29+
[Symbol.for("foo")]: Symbol.for("foo")
3030
}

test/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,5 +199,14 @@ test('don\'t stringify non-enumerable symbols', t => {
199199
const symbol = Symbol('for non-enumerable key');
200200
Object.defineProperty(object, symbol, {enumerable: false});
201201

202-
t.is(stringifyObject(object), '{\n\tSymbol(for enumerable key): undefined\n}');
202+
t.is(stringifyObject(object), '{\n\t[Symbol(\'for enumerable key\')]: undefined\n}');
203+
});
204+
test('handle symbols', t => {
205+
const object = {
206+
[Symbol('unique')]: Symbol('unique'),
207+
[Symbol.for('registry')]: [Symbol.for('registry'), 2],
208+
[Symbol.iterator]: {k: Symbol.iterator},
209+
[Symbol()]: 'undef'
210+
};
211+
t.is(stringifyObject(object), '{\n\t[Symbol(\'unique\')]: Symbol(\'unique\'),\n\t[Symbol.for(\'registry\')]: [\n\t\tSymbol.for(\'registry\'),\n\t\t2\n\t],\n\t[Symbol.iterator]: {\n\t\tk: Symbol.iterator\n\t},\n\t[Symbol()]: \'undef\'\n}');
203212
});

0 commit comments

Comments
 (0)