tiny-parse
Advanced tools
Comparing version 1.0.3 to 2.0.0
@@ -0,0 +0,0 @@ "use strict"; |
114
lib/parse.js
"use strict"; | ||
const coerce = require("tiny-coerce"), | ||
url = require("url"), | ||
space = /\s+/, | ||
basic = /^Basic\s/; | ||
each = require("retsu").each, | ||
url = require("url"); | ||
@@ -16,50 +15,26 @@ function trim (obj) { | ||
function iterate (obj, fn) { | ||
if (obj instanceof Object) { | ||
Object.keys(obj).forEach(i => { | ||
fn.call(obj, obj[i], i); | ||
}); | ||
} else { | ||
obj.forEach(fn); | ||
} | ||
} | ||
function queryString (arg = "") { | ||
const obj = {}, | ||
result = (arg.split("?")[1] || "").split("&"); | ||
function queryString (qstring = "") { | ||
let obj = {}, | ||
aresult = qstring.split("?"), | ||
result; | ||
each(result, prop => { | ||
const aitem = prop.replace(/\+/g, " ").split("="), | ||
item = aitem.length > 2 ? [aitem.shift(), aitem.join("=")] : aitem; | ||
if (aresult.length > 1) { | ||
aresult.shift(); | ||
} | ||
if (!isEmpty(item[0])) { | ||
if (item[1] === undefined) { | ||
item[1] = ""; | ||
} else { | ||
item[1] = coerce(decodeURIComponent(item[1])); | ||
} | ||
result = aresult.join("?"); | ||
result.split("&").forEach(prop => { | ||
let aitem = prop.replace(/\+/g, " ").split("="), | ||
item; | ||
if (aitem.length > 2) { | ||
item = [aitem.shift(), aitem.join("=")]; | ||
} else { | ||
item = aitem; | ||
if (obj[item[0]] === undefined) { | ||
obj[item[0]] = item[1]; | ||
} else if (obj[item[0]] instanceof Array === false) { | ||
obj[item[0]] = [obj[item[0]]]; | ||
obj[item[0]].push(item[1]); | ||
} else { | ||
obj[item[0]].push(item[1]); | ||
} | ||
} | ||
if (isEmpty(item[0])) { | ||
return; | ||
} | ||
if (item[1] === undefined) { | ||
item[1] = ""; | ||
} else { | ||
item[1] = coerce(decodeURIComponent(item[1])); | ||
} | ||
if (obj[item[0]] === undefined) { | ||
obj[item[0]] = item[1]; | ||
} else if (obj[item[0]] instanceof Array === false) { | ||
obj[item[0]] = [obj[item[0]]]; | ||
obj[item[0]].push(item[1]); | ||
} else { | ||
obj[item[0]].push(item[1]); | ||
} | ||
}); | ||
@@ -70,50 +45,23 @@ | ||
function uri (req) { | ||
let header = req.headers.authorization || "", | ||
auth = "", | ||
token; | ||
if (!isEmpty(header) && basic.test(header)) { | ||
token = header.split(space).pop() || ""; | ||
auth = new Buffer(token, "base64").toString(); | ||
if (!isEmpty(auth)) { | ||
auth += "@"; | ||
} | ||
} | ||
return "http://" + auth + req.headers.host + req.url; | ||
} | ||
function parse (arg) { | ||
let luri = arg.url ? uri(arg) : arg, | ||
let uri = arg.url || arg, | ||
idxAscii, idxQ, parsed; | ||
if (luri === undefined || luri === null) { | ||
luri = ""; | ||
if (uri === undefined || uri === null) { | ||
uri = ""; | ||
} else { | ||
idxAscii = luri.indexOf("%3F"); | ||
idxQ = luri.indexOf("?"); | ||
idxAscii = uri.indexOf("%3F"); | ||
idxQ = uri.indexOf("?"); | ||
switch (true) { | ||
case idxQ === -1 && idxAscii > -1: | ||
case idxAscii < idxQ: | ||
luri = luri.replace("%3F", "?"); | ||
break; | ||
default: | ||
void 0; | ||
if (idxAscii < idxQ || idxQ === -1 && idxAscii > -1) { | ||
uri = uri.replace("%3F", "?"); | ||
} | ||
} | ||
parsed = url.parse(luri); | ||
parsed.pathname = parsed.pathname.replace(/%20/g, " "); | ||
parsed = url.parse(uri); | ||
parsed.pathname = (parsed.pathname || "").replace(/%20/g, " "); | ||
parsed.path = parsed.pathname + (parsed.search || ""); | ||
parsed.query = parsed.search ? queryString(parsed.search) : {}; | ||
iterate(parsed, (v, k) => { | ||
if (v === null) { | ||
parsed[k] = ""; | ||
} | ||
}); | ||
return parsed; | ||
@@ -120,0 +68,0 @@ } |
{ | ||
"name": "tiny-parse", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"description": "URL parsing with coercion of `query`", | ||
@@ -25,2 +25,3 @@ "main": "index.js", | ||
}, | ||
"engineStrict": true, | ||
"engines": { | ||
@@ -31,2 +32,3 @@ "node": ">=6.0.0" | ||
"dependencies": { | ||
"retsu": "~2.0.3", | ||
"tiny-coerce": "~1.0.1" | ||
@@ -33,0 +35,0 @@ }, |
@@ -0,0 +0,0 @@ # tiny-parse |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
4463
2
57
+ Addedretsu@~2.0.3
+ Addedretsu@2.0.3(transitive)