Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lukeed/url-shim
A 1.5kB browser polyfill for the Node.js `URL` and `URLSearchParams` classes.
https://github.com/lukeed/url-shim
Last synced: 3 months ago
JSON representation
A 1.5kB browser polyfill for the Node.js `URL` and `URLSearchParams` classes.
- Host: GitHub
- URL: https://github.com/lukeed/url-shim
- Owner: lukeed
- License: mit
- Created: 2019-12-12T18:38:31.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2019-12-17T05:41:41.000Z (almost 5 years ago)
- Last Synced: 2024-07-05T06:02:48.607Z (4 months ago)
- Language: JavaScript
- Homepage:
- Size: 35.2 KB
- Stars: 123
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
- awesome-list - url-shim
README
# url-shim [![codecov](https://badgen.now.sh/codecov/c/github/lukeed/url-shim)](https://codecov.io/gh/lukeed/url-shim)
> A 1.53kB browser polyfill for the Node.js `URL` and `URLSearchParams` classes.
**Why?**
* All browser implementations *are not* 100% identical to the Node.js implementation.
_For example, browsers have issue with custom protocols, which affects the `origin` and `pathname` parsing._* Most polyfills match the browser implementations.
_But what if you have a "universal app" and want to guarantee client/server uniformity?_* Most polyfills immediately (albeit, conditionally) mutate global scope.
_You can't declaratively import their implementations for standalone usage._> **Note:** The only other library that satisfies these requirements is [`whatwg-url`](https://github.com/jsdom/whatwg-url), but it [weighs **87.6 kB (gzip)**](https://bundlephobia.com/[email protected])!
This module is available in three formats:
* **ES Module**: `dist/urlshim.mjs`
* **CommonJS**: `dist/urlshim.js`
* **UMD**: `dist/urlshim.min.js`## Install
```
$ npm install --save url-shim
```## Usage
```js
import { URL, URLSearchParams } from 'url-shim';// composition
new URL('/foo', 'https://example.org/').href;
//=> "https://example.org/foo"// unicode -> ASCII conversion
new URL('https://測試').href;
//=> "https://xn--g6w251d/"// custom protocols w/ path
new URL('webpack:///src/bundle.js');
//=> { protocol: "webpack:", pathname: "/src/bundle.js", ... }// custom protocols w/ hostname
new URL('git://github.com/lukeed/url-shim');
//=> { protocol: "git:", hostname: "github.com", pathname: "/lukeed/url-shim", ... }new URL('http://foobar.com/123?a=1&b=2').searchParams instanceof URLSearchParams;
//=> trueconst params = new URLSearchParams('foo=bar&xyz=baz');
for (const [name, value] of params) {
console.log(name, value);
}
// Prints:
// foo bar
// xyz baz
```## API
### URL(input, base?)
> **Size (gzip):** `1.53 kB`See [Node.js documentation](https://nodejs.org/dist/latest-v12.x/docs/api/url.html#url_class_url) for info.
> **Important:** Requires a browser environment because `document.createElement` is used for URL parsing.
### URLSearchParams(input?)
> **Size (gzip):** `944 B`See [Node.js documentation](https://nodejs.org/dist/latest-v12.x/docs/api/url.html#url_class_urlsearchparams) for info.
## License
MIT © [Luke Edwards](https://lukeed.com)