whatwg-url-fns
v1.3.0
Published
Delightful WHATWG URL transform functions.
Maintainers
Readme
whatwg-url-fns
Delightful WHATWG URL transform functions.
Given a URL, produce another URL safely and concisely, declaring the next fields to apply over the provided URL.
install
pnpm install --save whatwg-url-fns
npm install --save whatwg-url-fnsusage
A quick example:
import { transform } from "whatwg-url-fns";
const url: URL = transform(
"https://example.com:8080/path?baz=1#hash", // URL | string
{
pathname: "/next-page", // or { append: "/next-page" } // yields /path/next-page
hash: "", // clear out the hash
searchParams: {
set: { foo: "bar" },
unset: ["baz"],
// [clear: true] // empty the searchParams before running set operations
},
},
);
url.toString(); // https://example.com:8080/next-page?foo=bar- The implementation is thoughtfully typed and prevents conflicting URL inputs,
such as
originandhostname. searchParamsacceptset/unset/clearcollections. Precedence matters.clearoccurs first, thenset, thenunset.- Stringy-fields can be cleared by passing the empty string.
- Browser and node.js friendly.
See the tests for more examples.
