Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 11 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,28 @@
Join all arguments together and normalize the resulting url.
Join all arguments together and normalize the resulting URL.

## Install

~~~
```bash
npm install url-join
~~~
```

If you want to use it directly in a browser use a CDN like [Skypack](https://www.skypack.dev/view/url-join).

## Usage

~~~javascript
var urljoin = require('url-join');
```javascript
import urlJoin from 'url-join';

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

console.log(fullUrl);

~~~
```

Prints:

~~~
```
'http://www.google.com/a/b/cd?foo=123'
~~~

## Browser and AMD

It also works in the browser, you can either include ```lib/url-join.js``` in your page:

~~~html
<script src="url-join.js"></script>
<script type="text/javascript">
urljoin('http://blabla.com', 'foo?a=1')
</script>
~~~

Or using an AMD module system like requirejs:

~~~javascript
define(['path/url-join.js'], function (urljoin) {
urljoin('http://blabla.com', 'foo?a=1');
});
~~~
```

## License

Expand Down
2 changes: 1 addition & 1 deletion bin/changelog → bin/changelog.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import changelog from 'conventional-changelog';

var changelog = require('conventional-changelog');
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;

const commitPartial = ` - {{header}}
Expand Down
2 changes: 1 addition & 1 deletion lib/url-join.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
declare function urlJoin(...parts: string[]): string;
declare function urlJoin(parts: string[]): string;

export = urlJoin;
export default urlJoin;
116 changes: 54 additions & 62 deletions lib/url-join.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,70 @@
(function (name, context, definition) {
if (typeof module !== 'undefined' && module.exports) module.exports = definition();
else if (typeof define === 'function' && define.amd) define(definition);
else context[name] = definition();
})('urljoin', this, function () {

function normalize (strArray) {
var resultArray = [];
if (strArray.length === 0) { return ''; }

if (typeof strArray[0] !== 'string') {
throw new TypeError('Url must be a string. Received ' + strArray[0]);
}

// If the first part is a plain protocol, we combine it with the next part.
if (strArray[0].match(/^[^/:]+:\/*$/) && strArray.length > 1) {
var first = strArray.shift();
strArray[0] = first + strArray[0];
}
function normalize (strArray) {
var resultArray = [];
if (strArray.length === 0) { return ''; }

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

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

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

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

if (i > 0) {
// Removing the starting slashes for each component but the first.
component = component.replace(/^[\/]+/, '');
}
if (i < strArray.length - 1) {
// Removing the ending slashes for each component but the last.
component = component.replace(/[\/]+$/, '');
} else {
// For the last component we will combine multiple slashes to a single one.
component = component.replace(/[\/]+$/, '/');
}
if (typeof component !== 'string') {
throw new TypeError('Url must be a string. Received ' + component);
}

resultArray.push(component);
if (component === '') { continue; }

if (i > 0) {
// Removing the starting slashes for each component but the first.
component = component.replace(/^[\/]+/, '');
}
if (i < strArray.length - 1) {
// Removing the ending slashes for each component but the last.
component = component.replace(/[\/]+$/, '');
} else {
// For the last component we will combine multiple slashes to a single one.
component = component.replace(/[\/]+$/, '/');
}

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

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

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

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

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

if (typeof arguments[0] === 'object') {
input = arguments[0];
} else {
input = [].slice.call(arguments);
}
return str;
}

return normalize(input);
};
export default function urlJoin() {
var input;

if (typeof arguments[0] === 'object') {
input = arguments[0];
} else {
input = [].slice.call(arguments);
}

});
return normalize(input);
}
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
"name": "url-join",
"version": "4.0.1",
"description": "Join urls and normalize as in path.join.",
"main": "lib/url-join.js",
"types": "lib/url-join.d.ts",
"type": "module",
"main": "./lib/url-join.js",
"exports": "./lib/url-join.js",
"types": "./lib/url-join.d.ts",
"sideEffects": false,
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "mocha --require should"
},
Expand Down
Loading