This commit is contained in:
2019-09-17 13:20:42 -04:00
parent d211d1dc34
commit bef10ce4c9
8352 changed files with 568242 additions and 51 deletions
+2
View File
@@ -0,0 +1,2 @@
node_modules/*
*.log
+5
View File
@@ -0,0 +1,5 @@
language: node_js
node_js:
- "5"
- "4"
- "0.12"
+47
View File
@@ -0,0 +1,47 @@
Join all arguments together and normalize the resulting url.
## Install
~~~
npm install url-join
~~~
## Usage
~~~javascript
var urljoin = require('url-join');
var 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
MIT
+22
View File
@@ -0,0 +1,22 @@
{
"name": "url-join",
"version": "1.0.0",
"homepage": "https://github.com/jfromaniello/url-join",
"authors": [
"José F. Romaniello <jfromaniello@gmail.com> (http://joseoncode.com)"
],
"description": "Join urls and normalize as in path.join.",
"main": "lib/url-join.js",
"keywords": [
"url",
"join"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
]
}
+38
View File
@@ -0,0 +1,38 @@
(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 (str, options) {
// make sure protocol is followed by two slashes
str = str.replace(/:\//g, '://');
// remove consecutive slashes
str = str.replace(/([^:\s])\/+/g, '$1/');
// remove trailing slash before parameters or hash
str = str.replace(/\/(\?|&|#[^!])/g, '$1');
// replace ? in parameters with &
str = str.replace(/(\?.+)\?/g, '$1&');
return str;
}
return function () {
var input = arguments;
var options = {};
if (typeof arguments[0] === 'object') {
// new syntax with array and options
input = arguments[0];
options = arguments[1] || {};
}
var joined = [].slice.call(input, 0).join('/');
return normalize(joined, options);
};
});
+23
View File
@@ -0,0 +1,23 @@
{
"name": "url-join",
"version": "1.1.0",
"description": "Join urls and normalize as in path.join.",
"main": "lib/url-join.js",
"scripts": {
"test": "mocha --require should"
},
"repository": {
"type": "git",
"url": "git://github.com/jfromaniello/url-join.git"
},
"keywords": [
"url",
"join"
],
"author": "José F. Romaniello <jfromaniello@gmail.com> (http://joseoncode.com)",
"license": "MIT",
"devDependencies": {
"should": "~1.2.1",
"mocha": "~1.8.1"
}
}
+51
View File
@@ -0,0 +1,51 @@
var urljoin = require('../lib/url-join');
describe('url join', function () {
it('should work for simple case', function () {
urljoin('http://www.google.com/', 'foo/bar', '?test=123')
.should.eql('http://www.google.com/foo/bar?test=123');
});
it('should work for simple case with new syntax', function () {
urljoin(['http://www.google.com/', 'foo/bar', '?test=123'])
.should.eql('http://www.google.com/foo/bar?test=123');
});
it('should work for hashbang urls', function () {
urljoin(['http://www.google.com', '#!', 'foo/bar', '?test=123'])
.should.eql('http://www.google.com/#!/foo/bar?test=123');
});
it('should be able to join protocol', function () {
urljoin('http:', 'www.google.com/', 'foo/bar', '?test=123')
.should.eql('http://www.google.com/foo/bar?test=123');
});
it('should be able to join protocol with slashes', function () {
urljoin('http://', 'www.google.com/', 'foo/bar', '?test=123')
.should.eql('http://www.google.com/foo/bar?test=123');
});
it('should remove extra slashes', function () {
urljoin('http:', 'www.google.com///', 'foo/bar', '?test=123')
.should.eql('http://www.google.com/foo/bar?test=123');
});
it('should support anchors in urls', function () {
urljoin('http:', 'www.google.com///', 'foo/bar', '?test=123', '#faaaaa')
.should.eql('http://www.google.com/foo/bar?test=123#faaaaa');
});
it('should support protocol-relative urls', function () {
urljoin('//www.google.com', 'foo/bar', '?test=123')
.should.eql('//www.google.com/foo/bar?test=123')
});
it('should merge multiple query params properly', function () {
urljoin('http:', 'www.google.com///', 'foo/bar', '?test=123', '?key=456')
.should.eql('http://www.google.com/foo/bar?test=123&key=456');
urljoin('http:', 'www.google.com///', 'foo/bar', '?test=123', '?boom=value', '&key=456')
.should.eql('http://www.google.com/foo/bar?test=123&boom=value&key=456');
});
});