@polka/url
Advanced tools
Comparing version 1.0.0-next.0 to 1.0.0-next.1
48
index.js
@@ -1,2 +0,11 @@ | ||
module.exports = function (req) { | ||
function parse(str) { | ||
let out={}, arr=str.split('&'); | ||
for (let i=0, k, v; i < arr.length; i++) { | ||
[k, v=''] = arr[i].split('='); | ||
out[k] = out[k] !== void 0 ? [].concat(out[k], v) : v; | ||
} | ||
return out; | ||
} | ||
module.exports = function (req, toDecode) { | ||
let url = req.url; | ||
@@ -8,21 +17,30 @@ if (url == null) return; | ||
if (url.length > 1 && !req._decoded) { | ||
url = req.url = decodeURIComponent(url); | ||
req._decoded = true; | ||
} | ||
obj = { | ||
path: url, | ||
pathname: url, | ||
search: null, | ||
query: null, | ||
href: url, | ||
_raw: url | ||
}; | ||
obj = {}; | ||
obj.query = obj.search = null; | ||
obj.href = obj.path = obj.pathname = url; | ||
if (url.length > 1) { | ||
if (toDecode && !req._decoded && !!~url.indexOf('%', 1)) { | ||
url = req.url = obj.href = obj.path = obj.pathname = obj._raw = decodeURIComponent(url); | ||
req._decoded = true; | ||
} | ||
let idx = url.indexOf('?', 1); | ||
if (idx !== -1) { | ||
obj.search = url.substring(idx); | ||
obj.query = obj.search.substring(1); | ||
obj.pathname = url.substring(0, idx); | ||
let idx = url.indexOf('?', 1); | ||
if (idx !== -1) { | ||
obj.search = url.substring(idx); | ||
obj.query = obj.search.substring(1); | ||
obj.pathname = url.substring(0, idx); | ||
if (toDecode && obj.query.length > 0) { | ||
obj.query = parse(obj.query); | ||
} | ||
} | ||
} | ||
obj._raw = url; | ||
return (req._parsedUrl = obj); | ||
} |
{ | ||
"version": "1.0.0-next.0", | ||
"version": "1.0.0-next.1", | ||
"name": "@polka/url", | ||
@@ -18,3 +18,3 @@ "repository": "lukeed/polka", | ||
}, | ||
"gitHead": "3598f0c5a1b343b6a37f661084c1643e832ff701" | ||
"gitHead": "8d32406bd3fa924b648df3e67ed32da502293904" | ||
} |
@@ -33,2 +33,15 @@ # @polka/url [![npm](https://badgen.now.sh/npm/v/@polka/url)](https://npmjs.org/package/@polka/url) | ||
assert.deepEqual(foo, req._parsedUrl); //=> true | ||
// Example with `toDecode` param | ||
req = { url: '/f%C3%B8%C3%B8%C3%9F%E2%88%82r?phone=%2b8675309' }; | ||
parse(req, true); | ||
//=> { search: '?phone=+8675309', | ||
//=> query: { phone: '+8675309' }, | ||
//=> pathname: '/føøß∂r', | ||
//=> path: '/føøß∂r?phone=+8675309', | ||
//=> href: '/føøß∂r?phone=+8675309', | ||
//=> _raw: '/føøß∂r?phone=+8675309' } | ||
// Attaches awareness key | ||
assert(req._decoded); //=> true | ||
``` | ||
@@ -38,3 +51,3 @@ | ||
### url(req) | ||
### url(req, toDecode) | ||
Returns: `Object` or `undefined` | ||
@@ -51,28 +64,18 @@ | ||
#### toDecode | ||
Type: `Boolean`<br> | ||
Default: `false` | ||
## Benchmarks | ||
If enabled, the `url` will be fully decoded (via [`decodeURIComponent`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent)) and the output keys will be slightly different: | ||
> Running the `parseurl` benchmark suite on Node 10.9.0 | ||
* `path`, `pathname`, `href`, `_raw` — will be the decoded strings | ||
* `search` — if there is a value, will be decoded string, else remain `null` | ||
* `query` — if there is a value, will be a decoded **object**, else remain `null` | ||
``` | ||
Parsing: "/foo/bar?user=tj&pet=fluffy" | ||
nativeurl x 3,496,593 ops/sec ±0.78% (194 runs sampled) | ||
parseurl x 5,702,515 ops/sec ±0.59% (194 runs sampled) | ||
@polka/url x 11,510,281 ops/sec ±1.93% (192 runs sampled) | ||
Additionally, the `req` is mutated with `req._decoded = true` so as to prevent repetitive decoding. | ||
REPEAT: "/foo/bar?user=tj&pet=fluffy" | ||
nativeurl x 3,344,884 ops/sec ±0.13% (191 runs sampled) | ||
parseurl x 20,386,848 ops/sec ±0.22% (192 runs sampled) | ||
@polka/url x 21,088,923 ops/sec ±0.58% (191 runs sampled) | ||
Parsing: "/foo/bar" | ||
nativeurl x 9,808,119 ops/sec ±0.51% (190 runs sampled) | ||
parseurl x 26,186,627 ops/sec ±0.16% (195 runs sampled) | ||
@polka/url x 43,946,765 ops/sec ±0.55% (194 runs sampled) | ||
## Benchmarks | ||
Parsing: "/" | ||
nativeurl x 15,698,746 ops/sec ±0.79% (192 runs sampled) | ||
parseurl x 36,861,339 ops/sec ±0.19% (195 runs sampled) | ||
@polka/url x 48,295,119 ops/sec ±0.51% (194 runs sampled) | ||
``` | ||
Check out the [`bench`](/bench) directory for in-depth benchmark results and comparisons. | ||
@@ -79,0 +82,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5631
38
90