Skip to content

Commit fec694d

Browse files
GauBenRich-Harris
andauthored
feat: support URL and URLSearchParams (#92)
* feat: support URL * also support URLSearchParams * Update src/uneval.js * Create warm-books-live.md --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 2896e7b commit fec694d

File tree

6 files changed

+48
-1
lines changed

6 files changed

+48
-1
lines changed

.changeset/warm-books-live.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"devalue": minor
3+
---
4+
5+
feat: support `URL` and `URLSearchParams` objects

src/parse.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ export function unflatten(parsed, revivers) {
145145
break;
146146
}
147147

148+
case 'URL': {
149+
const url = new URL(value[1]);
150+
hydrated[index] = url;
151+
break;
152+
}
153+
154+
case 'URLSearchParams': {
155+
const url = new URLSearchParams(value[1]);
156+
hydrated[index] = url;
157+
break;
158+
}
159+
148160
default:
149161
throw new Error(`Unknown type ${type}`);
150162
}

src/stringify.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ export function stringify(value, reducers) {
9090
str = `["Date","${valid ? thing.toISOString() : ''}"]`;
9191
break;
9292

93+
case 'URL':
94+
str = `["URL",${stringify_string(thing.toString())}]`;
95+
break;
96+
97+
case 'URLSearchParams':
98+
str = `["URLSearchParams",${stringify_string(thing.toString())}]`;
99+
break;
100+
93101
case 'RegExp':
94102
const { source, flags } = thing;
95103
str = flags

src/uneval.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ export function uneval(value, replacer) {
5959
case 'Boolean':
6060
case 'Date':
6161
case 'RegExp':
62+
case 'URL':
63+
case 'URLSearchParams':
6264
return;
6365

6466
case 'Array':
@@ -178,6 +180,12 @@ export function uneval(value, replacer) {
178180
case 'Date':
179181
return `new Date(${thing.getTime()})`;
180182

183+
case 'URL':
184+
return `new URL(${stringify_string(thing.toString())})`;
185+
186+
case 'URLSearchParams':
187+
return `new URLSearchParams(${stringify_string(thing.toString())})`;
188+
181189
case 'Array':
182190
const members = /** @type {any[]} */ (thing).map((v, i) =>
183191
i in thing ? stringify(v) : ''

test/test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,20 @@ const fixtures = {
180180
js: 'new Uint8Array([1,2,3]).buffer',
181181
json: '[["ArrayBuffer","AQID"]]'
182182
},
183+
{
184+
name: 'URL',
185+
value: new URL(
186+
'https://user:[email protected]/<script>/path?foo=bar#hash'
187+
),
188+
js: 'new URL("https://user:[email protected]/%3Cscript%3E/path?foo=bar#hash")',
189+
json: '[["URL","https://user:[email protected]/%3Cscript%3E/path?foo=bar#hash"]]'
190+
},
191+
{
192+
name: 'URLSearchParams',
193+
value: new URLSearchParams('foo=1&foo=2&baz=<+>'),
194+
js: 'new URLSearchParams("foo=1&foo=2&baz=%3C+%3E")',
195+
json: '[["URLSearchParams","foo=1&foo=2&baz=%3C+%3E"]]'
196+
},
183197
{
184198
name: 'Sliced typed array',
185199
value: new Uint16Array([10, 20, 30, 40]).subarray(1, 3),

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"diagnostics": true,
1111
"noImplicitThis": true,
1212
"noEmitOnError": true,
13-
"lib": ["es6", "es2020"],
13+
"lib": ["es6", "es2020", "dom"],
1414
"target": "es2020"
1515
},
1616
"module": "ES6",

0 commit comments

Comments
 (0)