Skip to content

Commit

Permalink
Merge pull request #22 from twada/symbol-as-object-key
Browse files Browse the repository at this point in the history
fix: Symbol as object key
  • Loading branch information
twada committed Feb 5, 2024
2 parents cf72b2b + 2f3b9b4 commit 8d09e52
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function createStringifier (customOptions) {
return function stringifyAny (push, x) {
const context = this;
let handler = handlerFor(context.node, options, handlers);
const currentPath = '/' + context.path.join('/');
const currentPath = '/' + context.path.map(String).join('/');
const customization = handlers[currentPath];
const acc = {
context: context,
Expand Down
6 changes: 5 additions & 1 deletion strategies.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,11 @@ function decorateObject () {
}

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

function afterAllChildren (context, push, options) {
Expand Down
8 changes: 8 additions & 0 deletions test/es6_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ describe('ES6 features', () => {
it('Symbol', () => {
assert.strictEqual(stringify(FOO), 'Symbol(FOO)');
});
it('Symbol as Object key', () => {
const id = Symbol("id");
const user = {
name: "John",
[id]: 123
};
assert.strictEqual(stringify(user), 'Object{name:"John",Symbol(id):123}');
});
});

0 comments on commit 8d09e52

Please sign in to comment.