-
Notifications
You must be signed in to change notification settings - Fork 51
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
feat: support Date object serialization, make json more consistent with JSON.stringify #58
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -342,6 +342,18 @@ describe('common usage', function() { | |
'new Map([\n\t[\'a\', 1],\n\t[\'b\', new Map([\n\t\t[\'x\', 2],\n\t\t[\'y\', 3]\n\t])]\n])', | ||
'Stringifying a Map with `compact: false`' | ||
); | ||
assert.equal( | ||
jsesc( | ||
new Map([ | ||
['a', 1] | ||
]), | ||
{ | ||
'json': true | ||
} | ||
), | ||
'{}', | ||
'Stringifying a Map with `json: true`' | ||
); | ||
assert.equal( | ||
jsesc( | ||
new Set([]) | ||
|
@@ -377,6 +389,19 @@ describe('common usage', function() { | |
'new Set([\n\t[\n\t\t\'a\'\n\t],\n\t\'b\',\n\t{}\n])', | ||
'Stringifying a Set with `compact: false`' | ||
); | ||
assert.equal( | ||
jsesc( | ||
new Set([ | ||
['a'], | ||
'b' | ||
]), | ||
{ | ||
'json': true | ||
} | ||
), | ||
'{}', | ||
'Stringifying a Set with `json: true`' | ||
); | ||
// Buffer | ||
assert.equal( | ||
jsesc( | ||
|
@@ -395,6 +420,31 @@ describe('common usage', function() { | |
'Buffer.from([\n\t19,\n\t55,\n\t66\n])', | ||
'Stringifying a Buffer with `compact: false`' | ||
); | ||
// Date | ||
assert.equal( | ||
jsesc( | ||
new Date('2020-04-03T15:34:00Z') | ||
), | ||
'new Date(1585928040000)' | ||
); | ||
assert.equal( | ||
jsesc( | ||
new Date('2020-04-03T15:34:00Z'), | ||
{ | ||
'numbers': 'hexadecimal' | ||
} | ||
), | ||
'new Date(0x17140AD6E40)' | ||
); | ||
assert.equal( | ||
jsesc( | ||
new Date('2020-04-03T15:34:00Z'), | ||
{ | ||
'json': true | ||
} | ||
), | ||
'"2020-04-03T15:34:00.000Z"' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This depends on how we want to deal with #57, Is this what you'd expect when passing a Date/Map/Set while using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either way this would need to be documented in the README under the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the behavior of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could include an I don't need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The way I've used So I defer to you on this. If you have a use case for it, that's great! |
||
); | ||
// JSON | ||
assert.equal( | ||
jsesc('foo\x00bar\xFF\uFFFDbaz', { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A potential solution for #57;
Map
andSet
don't havetoJSON
methods - and even if they did, they probably wouldn't return the same type, so the expectation here is that we never process aMap
orSet
in a special way when targetting JSON.