Skip to content

Commit

Permalink
fix: Always return a string for custom stringify method
Browse files Browse the repository at this point in the history
  • Loading branch information
ptang-nr committed Oct 2, 2024
1 parent 1306012 commit d6fe8f9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/common/util/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const getCircularReplacer = () => {
*/
export function stringify (val) {
try {
return JSON.stringify(val, getCircularReplacer())
return JSON.stringify(val, getCircularReplacer()) ?? ''
} catch (e) {
try {
ee.emit('internal-error', [e])
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/common/util/stringify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ test('should utilize JSON.stringify to serialize input data', () => {
test.each([
['array', [0, 1, 'asdf', undefined, null, 'weee'], '[0,1,"asdf",null,null,"weee"]'],
['object', { a: 123, c: 'b', f: 'asdf', u: undefined, n: null }, '{"a":123,"c":"b","f":"asdf","n":null}'],
['undefined', undefined, undefined],
['undefined', undefined, ''],
['null', null, 'null'],
['number', 123, '123'],
['function', function F () {}, undefined],
['string', '0', '"0"'],
['symbol', Symbol('foo'), ''],
['function', function F () {}, ''],
['object w/ prototype', (() => { function F () {}; F.prototype = { a: 123 }; const obj = new F(); obj.other = 222; return obj })(), '{"other":222}'],
['nested json', { stringified: stringify({ a: 123, c: 'b', f: 'asdf', u: undefined, n: null }) }, '{"stringified":"{\\"a\\":123,\\"c\\":\\"b\\",\\"f\\":\\"asdf\\",\\"n\\":null}"}']
])('should serialize input %s properly', (_, input, expected) => {
Expand Down

0 comments on commit d6fe8f9

Please sign in to comment.