Skip to content

Commit caaafed

Browse files
authored
Change distributed code to use ECMAScript modules (#76)
1 parent 86eeb03 commit caaafed

File tree

6 files changed

+115
-136
lines changed

6 files changed

+115
-136
lines changed

README.md

+11-29
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,28 @@
1-
Join all arguments together and normalize the resulting url.
1+
Join all arguments together and normalize the resulting URL.
22

33
## Install
44

5-
~~~
5+
```bash
66
npm install url-join
7-
~~~
7+
```
8+
9+
If you want to use it directly in a browser use a CDN like [Skypack](https://www.skypack.dev/view/url-join).
810

911
## Usage
1012

11-
~~~javascript
12-
var urljoin = require('url-join');
13+
```javascript
14+
import urlJoin from 'url-join';
1315

14-
var fullUrl = urljoin('http://www.google.com', 'a', '/b/cd', '?foo=123');
16+
const fullUrl = urlJoin('http://www.google.com', 'a', '/b/cd', '?foo=123');
1517

1618
console.log(fullUrl);
17-
18-
~~~
19+
```
1920

2021
Prints:
2122

22-
~~~
23+
```
2324
'http://www.google.com/a/b/cd?foo=123'
24-
~~~
25-
26-
## Browser and AMD
27-
28-
It also works in the browser, you can either include ```lib/url-join.js``` in your page:
29-
30-
~~~html
31-
<script src="url-join.js"></script>
32-
<script type="text/javascript">
33-
urljoin('http://blabla.com', 'foo?a=1')
34-
</script>
35-
~~~
36-
37-
Or using an AMD module system like requirejs:
38-
39-
~~~javascript
40-
define(['path/url-join.js'], function (urljoin) {
41-
urljoin('http://blabla.com', 'foo?a=1');
42-
});
43-
~~~
25+
```
4426

4527
## License
4628

bin/changelog renamed to bin/changelog.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
2+
import changelog from 'conventional-changelog';
23

3-
var changelog = require('conventional-changelog');
44
var semver_regex = /\bv?(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)(?:-[\da-z\-]+(?:\.[\da-z\-]+)*)?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?\b/ig;
55

66
const commitPartial = ` - {{header}}

lib/url-join.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
declare function urlJoin(...parts: string[]): string;
77
declare function urlJoin(parts: string[]): string;
88

9-
export = urlJoin;
9+
export default urlJoin;

lib/url-join.js

+54-62
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,70 @@
1-
(function (name, context, definition) {
2-
if (typeof module !== 'undefined' && module.exports) module.exports = definition();
3-
else if (typeof define === 'function' && define.amd) define(definition);
4-
else context[name] = definition();
5-
})('urljoin', this, function () {
6-
7-
function normalize (strArray) {
8-
var resultArray = [];
9-
if (strArray.length === 0) { return ''; }
10-
11-
if (typeof strArray[0] !== 'string') {
12-
throw new TypeError('Url must be a string. Received ' + strArray[0]);
13-
}
14-
15-
// If the first part is a plain protocol, we combine it with the next part.
16-
if (strArray[0].match(/^[^/:]+:\/*$/) && strArray.length > 1) {
17-
var first = strArray.shift();
18-
strArray[0] = first + strArray[0];
19-
}
1+
function normalize (strArray) {
2+
var resultArray = [];
3+
if (strArray.length === 0) { return ''; }
204

21-
// There must be two or three slashes in the file protocol, two slashes in anything else.
22-
if (strArray[0].match(/^file:\/\/\//)) {
23-
strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1:///');
24-
} else {
25-
strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1://');
26-
}
5+
if (typeof strArray[0] !== 'string') {
6+
throw new TypeError('Url must be a string. Received ' + strArray[0]);
7+
}
278

28-
for (var i = 0; i < strArray.length; i++) {
29-
var component = strArray[i];
9+
// If the first part is a plain protocol, we combine it with the next part.
10+
if (strArray[0].match(/^[^/:]+:\/*$/) && strArray.length > 1) {
11+
var first = strArray.shift();
12+
strArray[0] = first + strArray[0];
13+
}
3014

31-
if (typeof component !== 'string') {
32-
throw new TypeError('Url must be a string. Received ' + component);
33-
}
15+
// There must be two or three slashes in the file protocol, two slashes in anything else.
16+
if (strArray[0].match(/^file:\/\/\//)) {
17+
strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1:///');
18+
} else {
19+
strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1://');
20+
}
3421

35-
if (component === '') { continue; }
22+
for (var i = 0; i < strArray.length; i++) {
23+
var component = strArray[i];
3624

37-
if (i > 0) {
38-
// Removing the starting slashes for each component but the first.
39-
component = component.replace(/^[\/]+/, '');
40-
}
41-
if (i < strArray.length - 1) {
42-
// Removing the ending slashes for each component but the last.
43-
component = component.replace(/[\/]+$/, '');
44-
} else {
45-
// For the last component we will combine multiple slashes to a single one.
46-
component = component.replace(/[\/]+$/, '/');
47-
}
25+
if (typeof component !== 'string') {
26+
throw new TypeError('Url must be a string. Received ' + component);
27+
}
4828

49-
resultArray.push(component);
29+
if (component === '') { continue; }
5030

31+
if (i > 0) {
32+
// Removing the starting slashes for each component but the first.
33+
component = component.replace(/^[\/]+/, '');
34+
}
35+
if (i < strArray.length - 1) {
36+
// Removing the ending slashes for each component but the last.
37+
component = component.replace(/[\/]+$/, '');
38+
} else {
39+
// For the last component we will combine multiple slashes to a single one.
40+
component = component.replace(/[\/]+$/, '/');
5141
}
5242

53-
var str = resultArray.join('/');
54-
// Each input component is now separated by a single slash except the possible first plain protocol part.
43+
resultArray.push(component);
5544

56-
// remove trailing slash before parameters or hash
57-
str = str.replace(/\/(\?|&|#[^!])/g, '$1');
45+
}
5846

59-
// replace ? in parameters with &
60-
var parts = str.split('?');
61-
str = parts.shift() + (parts.length > 0 ? '?': '') + parts.join('&');
47+
var str = resultArray.join('/');
48+
// Each input component is now separated by a single slash except the possible first plain protocol part.
6249

63-
return str;
64-
}
50+
// remove trailing slash before parameters or hash
51+
str = str.replace(/\/(\?|&|#[^!])/g, '$1');
6552

66-
return function () {
67-
var input;
53+
// replace ? in parameters with &
54+
var parts = str.split('?');
55+
str = parts.shift() + (parts.length > 0 ? '?': '') + parts.join('&');
6856

69-
if (typeof arguments[0] === 'object') {
70-
input = arguments[0];
71-
} else {
72-
input = [].slice.call(arguments);
73-
}
57+
return str;
58+
}
7459

75-
return normalize(input);
76-
};
60+
export default function urlJoin() {
61+
var input;
62+
63+
if (typeof arguments[0] === 'object') {
64+
input = arguments[0];
65+
} else {
66+
input = [].slice.call(arguments);
67+
}
7768

78-
});
69+
return normalize(input);
70+
}

package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
"name": "url-join",
33
"version": "4.0.1",
44
"description": "Join urls and normalize as in path.join.",
5-
"main": "lib/url-join.js",
6-
"types": "lib/url-join.d.ts",
5+
"type": "module",
6+
"main": "./lib/url-join.js",
7+
"exports": "./lib/url-join.js",
8+
"types": "./lib/url-join.d.ts",
79
"sideEffects": false,
10+
"engines": {
11+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
12+
},
813
"scripts": {
914
"test": "mocha --require should"
1015
},

0 commit comments

Comments
 (0)