Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Symbol as object key #22

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}');
});
});