fix: persist RegExp flags during serialization

This commit is contained in:
Nate Moore 2022-09-09 09:11:02 -05:00
parent d136864bff
commit 546a5b73cb
4 changed files with 16 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Persist RegExp flags during serialization

View file

@ -20,7 +20,10 @@ declare const Astro: {
const propTypes: PropTypeSelector = {
0: (value) => value,
1: (value) => JSON.parse(value, reviver),
2: (value) => new RegExp(value),
2: (value) => {
const [source, flags] = JSON.parse(value);
return new RegExp(source, flags);
},
3: (value) => new Date(value),
4: (value) => new Map(JSON.parse(value, reviver)),
5: (value) => new Set(JSON.parse(value, reviver)),

View file

@ -62,7 +62,7 @@ function convertToSerializedForm(
return [PROP_TYPE.Date, (value as Date).toISOString()];
}
case '[object RegExp]': {
return [PROP_TYPE.RegExp, (value as RegExp).source];
return [PROP_TYPE.RegExp, JSON.stringify([(value as RegExp).source, (value as RegExp).flags])];
}
case '[object Map]': {
return [

View file

@ -14,7 +14,12 @@ describe('serialize', () => {
});
it('serializes a regular expression', () => {
const input = { a: /b/ };
const output = `{"a":[2,"b"]}`;
const output = `{"a":[2,"[\\"b\\",\\"\\"]"]}`;
expect(serializeProps(input)).to.equal(output);
});
it('serializes a regular expression with flags', () => {
const input = { a: /b/gim };
const output = `{"a":[2,"[\\"b\\",\\"gim\\"]"]}`;
expect(serializeProps(input)).to.equal(output);
});
it('serializes a Date', () => {