-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support serialization of RegExp (#102)
- Loading branch information
Showing
7 changed files
with
63 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export function replacer(_key, value) { | ||
if (value instanceof RegExp) { | ||
return { | ||
__serialized_type: 'RegExp', | ||
source: value.source, | ||
flags: value.flags, | ||
}; | ||
} | ||
return value; | ||
} | ||
|
||
export function reviver(_key, value) { | ||
if (typeof value === 'object' && value !== null) { | ||
// eslint-disable-next-line no-underscore-dangle | ||
if (value.__serialized_type === 'RegExp') { | ||
return new RegExp(value.source, value.flags); | ||
} | ||
} | ||
|
||
return value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
$white: #FFFFFF; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
@import '_shared'; | ||
@import 'color_palette'; | ||
|
||
body { | ||
background: red; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const { replacer, reviver } = require('../src/serializer'); | ||
|
||
test('round-trips plain objects', () => { | ||
const json = JSON.stringify( | ||
{ | ||
a: 1, | ||
b: 'foo', | ||
c: [null, false], | ||
}, | ||
replacer | ||
); | ||
expect(JSON.parse(json, reviver)).toEqual({ | ||
a: 1, | ||
b: 'foo', | ||
c: [null, false], | ||
}); | ||
}); | ||
|
||
test('round-trips regular expressions', () => { | ||
const json = JSON.stringify( | ||
{ | ||
r: /hoge/g, | ||
s: /^(\w\s)+$/m, | ||
}, | ||
replacer | ||
); | ||
expect(JSON.parse(json, reviver)).toEqual({ | ||
r: /hoge/g, | ||
s: /^(\w\s)+$/m, | ||
}); | ||
}); |