Skip to content

Commit 0fe6257

Browse files
feat(json): add RegExp handling in replacer and reviver
1 parent 427cc44 commit 0fe6257

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

packages/operators/src/json.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,21 @@ describe('log', () => {
4141
bigInt: BigInt(123),
4242
date: new Date(),
4343
url: new URL('https://example.com'),
44+
regexp: /\w/g,
4445
image: readFile('./packages/operators/fixtures/images/test_image.jpg'),
4546
array: [
4647
Promise.resolve('hello world'),
4748
BigInt(123),
4849
new Date(),
49-
new URL('https://example.com')
50+
new URL('https://example.com'),
51+
/\w/g
5052
],
5153
nested: Promise.resolve({
5254
text: Promise.resolve('hello world'),
5355
bigInt: BigInt(123),
5456
date: new Date(),
5557
url: new URL('https://example.com'),
58+
regexp: Promise.resolve(new RegExp('\\w', 'g')),
5659
image: from(readFile('./packages/operators/fixtures/images/test_image.jpg'))
5760
})
5861
});

packages/operators/src/json/replacer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const syncReplacer = [
88
{ validator: value => isURL(value), handler: value => value.toString() },
99
{ validator: value => isDate(value), handler: value => value.toISOString() },
1010
{ validator: value => isBigInt(value), handler: value => `${value.toString()}n` },
11+
{ validator: value => isRegExp(value), handler: value => value.toString() },
1112
{ validator: () => true, handler: value => value }
1213
];
1314

@@ -17,3 +18,4 @@ export const createAsyncReplacer = (transforms = []) => [...transforms, ...async
1718
const isURL = value => value?.constructor === URL;
1819
const isDate = value => value?.constructor === Date;
1920
const isBigInt = value => value?.constructor === BigInt;
21+
const isRegExp = value => value?.constructor === RegExp;

packages/operators/src/json/reviver.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const syncReviver = [
88
{ validator: value => isValidUrl(value), handler: value => new URL(value) },
99
{ validator: value => isValidISODateString(value), handler: value => new Date(value) },
1010
{ validator: value => isBigInt(value), handler: value => BigInt(value.slice(0, -1)) },
11+
{ validator: value => isRegExp(value), handler: value => regExpFromString(value) },
1112
{ validator: () => true, handler: value => value }
1213
];
1314

@@ -18,12 +19,25 @@ const isValidUrl = value => {
1819
return URL.canParse(value) && /^[\w]+:\/\/\S+$/gm.test(value);
1920
};
2021

21-
function isValidISODateString(value) {
22+
const isValidISODateString = value => {
2223
if (!/\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
25-
}
26+
};
2627

27-
function isBigInt(value) {
28+
const isBigInt = value => {
2829
return value?.constructor === String && /^\d+n$/.test(value);
29-
}
30+
};
31+
32+
const isRegExp = value => value?.constructor === String && /^\/.*\/[gimuy]*$/.test(value);
33+
34+
const regExpFromString = q => {
35+
const match = q.match(/^\/(.*)\/([gimuy]*)$/);
36+
if (!match) return null;
37+
const [, pattern, flags] = match;
38+
try {
39+
return new RegExp(pattern, flags);
40+
} catch {
41+
return null;
42+
}
43+
};

0 commit comments

Comments
 (0)