Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

url-join

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

url-join - npm Package Compare versions

Comparing version 0.0.1 to 1.0.0

bower.json

36

lib/url-join.js

@@ -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')
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc