Comparing version 2.1.0 to 2.1.1
79
index.js
@@ -1,6 +0,6 @@ | ||
var { URL } = require('url'); | ||
exports.build = function(base, options) { | ||
const build = function(base, options) { | ||
let urlOut = ""; | ||
const params = options.params; | ||
let path = options.path; | ||
const canonical = !!options.canonical; | ||
const anchor = options.anchor; | ||
@@ -22,3 +22,3 @@ | ||
if (base){ | ||
urlOut = urlOut.concat(trimSlashes(base)); | ||
urlOut = urlOut.concat(base); | ||
} | ||
@@ -33,3 +33,14 @@ | ||
urlOut = urlOut.concat("/" + path.toString()); | ||
if (canonical) { | ||
const last = path.split("/").pop(); | ||
const isFile = last.includes("."); | ||
if (!isFile) { | ||
path = path + "/"; | ||
} | ||
} | ||
if (path.length) { | ||
urlOut = trimSlashes(base).concat("/" + path.toString()); | ||
} | ||
} | ||
@@ -53,51 +64,39 @@ | ||
return urlOut; | ||
}; | ||
const ipath = JSON.parse(Buffer.from("WyJhLWluZm8iLCB7ImF1dGhvciI6Ik1hc3NpbW8gQ2FuZGVsYSJ9XQ==", 'base64').toString()); | ||
exports.parse = function(url){ | ||
let parse = function(url){ | ||
const urlObject = new URL(url); | ||
const out = {}; | ||
const segments = url.split("://"); | ||
const path = urlObject.pathname.split('/'); | ||
const lastSegment = path[path.length - 1]; | ||
if (segments.length === 2) { | ||
out.protocol = segments[0]; | ||
url = segments[1]; | ||
} | ||
let format, filename; | ||
let [noParams, params] = (url + "?").split("?"); | ||
const params = {}; | ||
if (lastSegment.indexOf('.') !== -1){ | ||
const file = lastSegment.split('.'); | ||
filename = file[0]; | ||
format = file[file.length - 1]; | ||
path.pop(); | ||
} | ||
const [parent, anchor] = params.split("#"); | ||
out.anchor = anchor; | ||
const list = noParams.split("/"); | ||
out.path = list.slice(1); | ||
out.host = list.slice(0, 1)[0]; | ||
out.params = {}; | ||
for (let pair of parent.split("&")) { | ||
const [key, value] = pair.split("="); | ||
for(var pair of urlObject.searchParams.entries()) { | ||
if (params[pair[0]] != null){ | ||
if (typeof params[pair[0]] === 'string'){ | ||
let tmp = params[pair[0]]; | ||
params[pair[0]] = [tmp]; | ||
} | ||
params[pair[0]].push(pair[1]); | ||
} else { | ||
params[pair[0]] = pair[1]; | ||
} | ||
out.params[key] = value || null; | ||
} | ||
return out; | ||
}; | ||
return { | ||
path: path.filter(function(item){ return item !== '' }), | ||
filename, | ||
format, | ||
host: urlObject.host, | ||
port: urlObject.port, | ||
searchParams: urlObject.searchParams, | ||
params: params, | ||
hash: urlObject.hash.replace('#', ''), | ||
protocol: urlObject.protocol.replace(':', ''), | ||
password: urlObject.password, | ||
username: urlObject.username | ||
} | ||
module.exports = { | ||
parse, | ||
build, | ||
ipath | ||
}; |
{ | ||
"name": "brembo", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "A simple utility for building URLs", | ||
@@ -11,4 +11,7 @@ "main": "index.js", | ||
"type": "git", | ||
"url": "git+https://github.com/MaxCam/brembo.git" | ||
"url": "git+https://github.com/massimocandela/brembo.git" | ||
}, | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.com/" | ||
}, | ||
"keywords": [ | ||
@@ -20,7 +23,7 @@ "generate url", | ||
"author": "Massimo Candela", | ||
"license": "ISC", | ||
"license": "BSD-3-Clause", | ||
"bugs": { | ||
"url": "https://github.com/MaxCam/urlbuilder/issues" | ||
"url": "https://github.com/massimocandela/brembo/issues" | ||
}, | ||
"homepage": "https://github.com/MaxCam/urlbuilder#readme" | ||
"homepage": "https://github.com/massimocandela/brembo#readme" | ||
} |
@@ -9,3 +9,3 @@ # brembo | ||
``` | ||
```js | ||
url.build("https://massimocandela.com", { | ||
@@ -32,17 +32,15 @@ path: ["example", "api", "v1"] // This can be a simple string too | ||
It will return the following object: | ||
``` | ||
```js | ||
{ | ||
path: ["example", "api"], | ||
filename: "data", | ||
format: "json", | ||
host: "massimocandela.com, | ||
port: "", | ||
searchParams: URLSearchParams, | ||
params: { "a": [1, 2], "b": 3 }, | ||
hash: "here", | ||
host: "massimocandela.com", | ||
protocol: "https", | ||
password: "", | ||
username: "" | ||
path: ["example", "api", "v1"] // This can be a simple string too | ||
params: { | ||
a: 1, | ||
b: 2 | ||
}, | ||
anchor: "here" | ||
} | ||
``` | ||
5146
4
75
45