Skip to content

Commit 0d9a51e

Browse files
fix(json): enhance Symbol handling in serialization and deserialization
1 parent 1fb4435 commit 0d9a51e

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

packages/operators/src/json.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,18 @@ describe('log', () => {
5050
BigInt(123),
5151
new Date(),
5252
new URL('https://example.com'),
53-
/\w/g
53+
/\w/g,
54+
Symbol.for('foo'),
55+
Symbol('bar')
5456
],
55-
nested: Promise.resolve({
57+
nested: of({
5658
text: Promise.resolve('hello world'),
5759
bigInt: BigInt(123),
5860
date: new Date(),
5961
url: new URL('https://example.com'),
6062
regexp: Promise.resolve(new RegExp('\\w', 'g')),
63+
globalSymbol: Symbol.for('foo'),
64+
symbol: Symbol('bar'),
6165
image: from(readFile('./packages/operators/fixtures/images/test_image.jpg'))
6266
})
6367
});

packages/operators/src/json/replacer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ const isBigInt = value => value?.constructor === BigInt;
2323
const isRegExp = value => value?.constructor === RegExp;
2424
const isSymbol = value => value?.constructor === Symbol;
2525

26-
const symbolToString = value => `${Symbol.keyFor(value) && 'g'}${value.toString()}`;
26+
const symbolToString = value => `${(Symbol.keyFor(value) && 'g') || ''}${value.toString()}`;

packages/operators/src/json/reviver.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@ export const syncReviver = [
1616
export const createSyncReviver = (transforms = []) => [...transforms, ...syncReviver];
1717
export const createAsyncReviver = (transforms = []) => [...transforms, ...asyncReviver];
1818

19-
const isValidUrl = value => URL.canParse(value) && /^[\w]+:\/\/\S+$/gm.test(value);
19+
const isValidUrl = value =>
20+
isString(value) && URL.canParse(value) && /^[\w]+:\/\/\S+$/gm.test(value);
2021

2122
const isValidISODateString = value => {
22-
if (!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(value)) return false;
23+
if (!isString(value) || !/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(value)) return false;
2324
const d = new Date(value);
2425
return d instanceof Date && !isNaN(d.getTime()) && d.toISOString() === value; // valid date
2526
};
2627

27-
const isBigInt = value => value?.constructor === String && /^\d+n$/.test(value);
28-
const isRegExp = value => value?.constructor === String && /^\/.*\/[gimuy]*$/.test(value);
29-
const isSymbol = value => value?.constructor === String && /(\w?)Symbol\((\w+)\)/g.test(value);
28+
const isString = value => value?.constructor === String;
29+
const isBigInt = value => isString(value) && /^\d+n$/.test(value);
30+
const isRegExp = value => isString(value) && /^\/.*\/[gimuy]*$/.test(value);
31+
const isSymbol = value => isString(value) && /(\w?)Symbol\((\w+)\)/g.test(value);
3032

3133
const regExpFromString = value => {
3234
const match = value.match(/^\/(.*)\/([gimuy]*)$/);

0 commit comments

Comments
 (0)