brembo 2.1.1 → 2.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +18 -21
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,46 +1,43 @@
1
1
  const build = function(base, options) {
2
2
  let urlOut = "";
3
3
  const params = options.params;
4
- let path = options.path;
4
+ let path = options.path || [];
5
5
  const canonical = !!options.canonical;
6
6
  const anchor = options.anchor;
7
7
 
8
8
  const trimSlashes = function(str){
9
9
 
10
- if (str[str.length - 1] == "/") {
10
+ if (str == null || str === "") {
11
+ return null;
12
+ }
13
+
14
+ str = str.toString().trim();
15
+
16
+ if (str[str.length - 1] === "/") {
11
17
  str = str.slice(0, str.length - 1);
12
18
  }
13
19
 
14
- if (str[0] == "/") {
20
+ if (str[0] === "/") {
15
21
  str = str.slice(1, str.length);
16
22
  }
17
23
 
18
24
  return str;
19
25
  };
20
26
 
21
- if (base){
22
- urlOut = urlOut.concat(base);
27
+ if (typeof(path) === "string") {
28
+ path = path.split("/");
23
29
  }
24
30
 
25
- if (path){
26
- if (typeof path == "string") {
27
- path = [path];
28
- }
29
-
30
- path = path.map(item => trimSlashes(item.toString().trim())).join("/");
31
+ path = [base || ""].concat(path).map(trimSlashes).filter(i => !!i);
31
32
 
32
- if (canonical) {
33
- const last = path.split("/").pop();
34
- const isFile = last.includes(".");
33
+ urlOut = path.join("/");
35
34
 
36
- if (!isFile) {
37
- path = path + "/";
38
- }
39
- }
35
+ if (canonical && path && path.length && path[path.length - 1] && !path[path.length - 1].includes(".")) { // Not a file
36
+ urlOut = urlOut + "/";
37
+ }
40
38
 
41
- if (path.length) {
42
- urlOut = trimSlashes(base).concat("/" + path.toString());
43
- }
39
+ if (!base || base === "/") {
40
+ urlOut = "/" + urlOut;
44
41
  }
45
42
 
46
43
  if (params){
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brembo",
3
- "version": "2.1.1",
3
+ "version": "2.1.3",
4
4
  "description": "A simple utility for building URLs",
5
5
  "main": "index.js",
6
6
  "scripts": {