Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ export function unflatten(parsed, revivers) {
break;
}

case 'URL': {
const url = new URL(value[1]);
hydrated[index] = url;
break;
}

case 'URLSearchParams': {
const url = new URLSearchParams(value[1]);
hydrated[index] = url;
break;
}

default:
throw new Error(`Unknown type ${type}`);
}
Expand Down
8 changes: 8 additions & 0 deletions src/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ export function stringify(value, reducers) {
str = `["Date","${valid ? thing.toISOString() : ''}"]`;
break;

case 'URL':
str = `["URL",${stringify_string(thing.toString())}]`;
break;

case 'URLSearchParams':
str = `["URLSearchParams",${stringify_string(thing.toString())}]`;
break;

case 'RegExp':
const { source, flags } = thing;
str = flags
Expand Down
8 changes: 8 additions & 0 deletions src/uneval.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export function uneval(value, replacer) {
case 'Boolean':
case 'Date':
case 'RegExp':
case 'URL':
case 'URLSearchParams':
return;

case 'Array':
Expand Down Expand Up @@ -167,6 +169,12 @@ export function uneval(value, replacer) {
case 'Date':
return `new Date(${thing.getTime()})`;

case 'URL':
return `new URL(${stringify_string(thing.href)})`;

case 'URLSearchParams':
return `new URLSearchParams(${stringify_string(thing.toString())})`;

case 'Array':
const members = /** @type {any[]} */ (thing).map((v, i) =>
i in thing ? stringify(v) : ''
Expand Down
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ const fixtures = {
value: new Uint8Array([1, 2, 3]).buffer,
js: 'new Uint8Array([1,2,3]).buffer',
json: '[["ArrayBuffer","AQID"]]'
},
{
name: 'URL',
value: new URL('https://user:[email protected]/<script>/path?foo=bar#hash'),
js: 'new URL("https://user:[email protected]/%3Cscript%3E/path?foo=bar#hash")',
json: '[["URL","https://user:[email protected]/%3Cscript%3E/path?foo=bar#hash"]]'
},
{
name: 'URLSearchParams',
value: new URLSearchParams('foo=1&foo=2&baz=<+>'),
js: 'new URLSearchParams("foo=1&foo=2&baz=%3C+%3E")',
json: '[["URLSearchParams","foo=1&foo=2&baz=%3C+%3E"]]'
}
],

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"diagnostics": true,
"noImplicitThis": true,
"noEmitOnError": true,
"lib": ["es6", "es2020"],
"lib": ["es6", "es2020", "dom"],
"target": "es2020"
},
"module": "ES6",
Expand Down