Comparing version 0.0.1 to 1.0.0
@@ -1,12 +0,26 @@ | ||
function normalize (str) { | ||
return str | ||
.replace(/[\/]+/g, '/') | ||
.replace(/\/\?/g, '?') | ||
.replace(/\/\#/g, '#') | ||
.replace(/\:\//g, '://'); | ||
} | ||
(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 () { | ||
module.exports = function () { | ||
var joined = [].slice.call(arguments, 0).join('/'); | ||
return normalize(joined); | ||
}; | ||
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'); | ||
return str; | ||
} | ||
return function () { | ||
var joined = [].slice.call(arguments, 0).join('/'); | ||
return normalize(joined); | ||
}; | ||
}); |
{ | ||
"name": "url-join", | ||
"version": "0.0.1", | ||
"version": "1.0.0", | ||
"description": "Join urls and normalize as in path.join.", | ||
@@ -11,3 +11,3 @@ "main": "lib/url-join.js", | ||
"type": "git", | ||
"url": "git://github.com/jfromaniello/url-join.git" | ||
"url": "git://github.com/jfromaniello/url-join.git" | ||
}, | ||
@@ -14,0 +14,0 @@ "keywords": [ |
@@ -18,11 +18,31 @@ Join all arguments together and normalize the resulting url. | ||
\\will print: | ||
~~~ | ||
Prints: | ||
~~~ | ||
'http://www.google.com/a/b/cd?foo=123' | ||
~~~ | ||
This works similar to [path.join](http://nodejs.org/api/path.html#path_path_join_path1_path2) but you shouldn't use ```path.join``` for urls since it will work different depending of the operative systems but also doesn't work for some cases. | ||
## 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 |
@@ -14,2 +14,7 @@ var urljoin = require('../lib/url-join'); | ||
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 () { | ||
@@ -24,2 +29,7 @@ urljoin('http:', 'www.google.com///', 'foo/bar', '?test=123') | ||
}); | ||
}); | ||
it('should support protocol-relative urls', function () { | ||
urljoin('//www.google.com', 'foo/bar', '?test=123') | ||
.should.eql('//www.google.com/foo/bar?test=123') | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
3668
6
68
1
47