Rich-Harris/devalue

[Feature Request] Support URLs

Opened this issue · 1 comments

I was surprised to learn that this package didn't support URL types, especially as they have a toJSON method natively.

Date and RegExp types are supported, so I figured URLs were a pretty clear case.

Is this something that can be added?

For those wondering how to add URL types today: you can pass a custom record to the stringify() and parse() functions to specify parsing behavior. The following will encode and decode URL objects as expected:

import { stringify, parse } from 'devalue';

const data = { url: new URL('https://example.com') }
const stringified = stringify(data, {
  URL: (value) => value instanceof URL && value.href,
});

const parsed = parse(stringified, {
  URL: (href) => new URL(href),
})

I would love to see URL support added natively though! I'd argue URL is as common in a payload as other natively supported classes.