Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jridgewell/safe-decode-uri-component
Safely decode URI components, leaving invalid sequences as they are
https://github.com/jridgewell/safe-decode-uri-component
Last synced: 2 months ago
JSON representation
Safely decode URI components, leaving invalid sequences as they are
- Host: GitHub
- URL: https://github.com/jridgewell/safe-decode-uri-component
- Owner: jridgewell
- License: mit
- Created: 2017-10-24T03:59:11.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-10-26T19:10:43.000Z (about 1 year ago)
- Last Synced: 2024-10-03T09:18:27.089Z (3 months ago)
- Language: JavaScript
- Size: 193 KB
- Stars: 6
- Watchers: 3
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# safe-decode-uri-component
[![Build Status](https://travis-ci.org/jridgewell/safe-decode-uri-component.svg?branch=master)](https://travis-ci.org/jridgewell/safe-decode-uri-component)
Decodes strings that were encoded by `encodeURI` and
`encodeURIComponent`, without throwing errors on invalid escapes.```js
const base = "http://github.com";
const query = `?value=${encodeURIComponent('test ⚡')}`;let url = base + query; // => "http://github.com?value=test%20%E2%9A%A1"
// Now, something happens and the url gets truncated:
// url = "http://github.com?value=test%20%E2%9A%A"decodeURIComponent(url); // THROWS ERROR
```Truncating "useless" characters from a URL happens for any number of
reasons. Or, maybe your user just typed in a bad URL? Either way, it's
annoying when all you want is the decoded value, treating invalid
escapes as if they were just regular characters.```js
const decode = require('safe-decode-uri-component');decode(url); // => "http://github.com?value=test %E2%9A%A"
```Notice that `%20` was decoded to a space, and the invalid sequence
`%E2%9A%A` remains. This is exactly like `decodeURIComponent`, except we
don't balk at an invalid sequence.```js
decode("value=test%20%E2%9A%A1"); // => "value=test ⚡"
decode("value=test%20%E2%9A%A"); // => "value=test %E2%9A%A"
decode("value=test%20%E2%9A%"); // => "value=test %E2%9A%"
decode("value=test%20%E2%9A"); // => "value=test %E2%9A"
decode("value=test%20%E2%9"); // => "value=test %E2%9"
decode("value=test%20%E2%"); // => "value=test %E2%"
decode("value=test%20%E2"); // => "value=test %E2"
decode("value=test%20%E"); // => "value=test %E"
decode("value=test%20%"); // => "value=test %"
decode("value=test%20"); // => "value=test "
decode("value=test%2"); // => "value=test%2"
decode("value=test%"); // => "value=test%"
decode("value=test"); // => "value=test"
```## Installation
```bash
npm install --save safe-decode-uri-component
```## Node.js
We also provide a native Node.js module, available as the `native` tag:
```bash
npm install --save safe-decode-uri-component@native
```It's way faster, faster than even native `decodeURIComponent`.
```
$ npm run benchmarkShort String (native) x 1,448,425 ops/sec ±0.97% (85 runs sampled)
Short String (safe) x 3,449,002 ops/sec ±0.73% (88 runs sampled)
Fastest is Short String (safe)Medium String (native) x 18,491 ops/sec ±0.84% (90 runs sampled)
Medium String (safe) x 22,695 ops/sec ±0.81% (88 runs sampled)
Fastest is Medium String (safe)Long String (native) x 33.56 ops/sec ±1.01% (58 runs sampled)
Long String (safe) x 41.83 ops/sec ±0.73% (54 runs sampled)
Fastest is Long String (safe)
```