Comparing version 2.1.1 to 2.1.2
39
index.js
const build = function(base, options) { | ||
let urlOut = ""; | ||
const params = options.params; | ||
let path = options.path; | ||
let path = options.path || []; | ||
const canonical = !!options.canonical; | ||
@@ -10,7 +10,13 @@ const anchor = options.anchor; | ||
if (str[str.length - 1] == "/") { | ||
if (str == null || str === "") { | ||
return null; | ||
} | ||
str = str.toString().trim(); | ||
if (str[str.length - 1] === "/") { | ||
str = str.slice(0, str.length - 1); | ||
} | ||
if (str[0] == "/") { | ||
if (str[0] === "/") { | ||
str = str.slice(1, str.length); | ||
@@ -22,25 +28,16 @@ } | ||
if (base){ | ||
urlOut = urlOut.concat(base); | ||
if (typeof(path) === "string") { | ||
path = path.split("/"); | ||
} | ||
if (path){ | ||
if (typeof path == "string") { | ||
path = [path]; | ||
} | ||
path = [base || ""].concat(path).map(trimSlashes).filter(i => !!i); | ||
path = path.map(item => trimSlashes(item.toString().trim())).join("/"); | ||
urlOut = path.join("/"); | ||
if (canonical) { | ||
const last = path.split("/").pop(); | ||
const isFile = last.includes("."); | ||
if (canonical && !path[path.length - 1].includes(".")) { // Not a file | ||
urlOut = urlOut + "/"; | ||
} | ||
if (!isFile) { | ||
path = path + "/"; | ||
} | ||
} | ||
if (path.length) { | ||
urlOut = trimSlashes(base).concat("/" + path.toString()); | ||
} | ||
if (!base || base === "/") { | ||
urlOut = "/" + urlOut; | ||
} | ||
@@ -47,0 +44,0 @@ |
{ | ||
"name": "brembo", | ||
"version": "2.1.1", | ||
"version": "2.1.2", | ||
"description": "A simple utility for building URLs", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
5077
71