Skip to content

Commit 8d09e52

Browse files
authored
Merge pull request #22 from twada/symbol-as-object-key
fix: Symbol as object key
2 parents cf72b2b + 2f3b9b4 commit 8d09e52

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function createStringifier (customOptions) {
5353
return function stringifyAny (push, x) {
5454
const context = this;
5555
let handler = handlerFor(context.node, options, handlers);
56-
const currentPath = '/' + context.path.join('/');
56+
const currentPath = '/' + context.path.map(String).join('/');
5757
const customization = handlers[currentPath];
5858
const acc = {
5959
context: context,

strategies.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,11 @@ function decorateObject () {
244244
}
245245

246246
function sanitizeKey (key) {
247-
return /^[A-Za-z_]+$/.test(key) ? key : JSON.stringify(key);
247+
if (typeof key === 'symbol') {
248+
return key.toString();
249+
} else {
250+
return /^[A-Za-z_]+$/.test(key) ? key : JSON.stringify(key);
251+
}
248252
}
249253

250254
function afterAllChildren (context, push, options) {

test/es6_test.js

+8
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,12 @@ describe('ES6 features', () => {
99
it('Symbol', () => {
1010
assert.strictEqual(stringify(FOO), 'Symbol(FOO)');
1111
});
12+
it('Symbol as Object key', () => {
13+
const id = Symbol("id");
14+
const user = {
15+
name: "John",
16+
[id]: 123
17+
};
18+
assert.strictEqual(stringify(user), 'Object{name:"John",Symbol(id):123}');
19+
});
1220
});

0 commit comments

Comments
 (0)