Skip to content
This repository has been archived by the owner on Jan 25, 2018. It is now read-only.

Commit

Permalink
Merge pull request #26 from metal/release/v3.1.0
Browse files Browse the repository at this point in the history
3.1.0
  • Loading branch information
jbalsas authored Nov 2, 2017
2 parents b291c60 + 9288c89 commit eafcfb6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/Uri.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import parse from './parse';
import resolvePathname from 'resolve-pathname';
import { MultiMap } from 'metal-structs';
import { isDef, string } from 'metal';
import { isDef, isNumber, string } from 'metal';

class Uri {

Expand All @@ -22,8 +22,13 @@ class Uri {
* @param {*=} opt_uri Optional string URI to parse
* @constructor
*/
constructor(opt_uri = '') {
this.url = parse(this.maybeAddProtocolAndHostname_(opt_uri));
constructor(opt_uri = '', opt_addProtocol = true) {
this.addProtocol_ = opt_addProtocol;

opt_uri = opt_addProtocol ?
this.maybeAddProtocolAndHostname_(opt_uri) : opt_uri;

this.url = parse(opt_uri);
this.ensurePathname_();
}

Expand Down Expand Up @@ -411,6 +416,10 @@ class Uri {
* @override
*/
toString() {
if (!this.addProtocol_) {
return this.url.toString();
}

var href = '';
var host = this.getHost();
if (host) {
Expand All @@ -427,10 +436,14 @@ class Uri {
* @static
*/
static joinPaths(basePath, ...paths) {
basePath = isNumber(basePath) ? basePath.toString() : basePath;
if (basePath.charAt(basePath.length - 1) === '/') {
basePath = basePath.substring(0, basePath.length - 1);
}
paths = paths.map(path => path.charAt(0) === '/' ? path.substring(1) : path);
paths = paths.map(path => {
path = isNumber(path) ? path.toString() : path;
return path.charAt(0) === '/' ? path.substring(1) : path;
});
return [basePath].concat(paths).join('/').replace(/\/$/, '');
}

Expand Down
47 changes: 47 additions & 0 deletions test/Uri.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ describe('Uri', function() {
assert.strictEqual('http://localhost:123', Uri.joinPaths('http://localhost:123', ''));
});

it('should convert numbers to strings before joining paths', function() {
assert.strictEqual('foo/123', Uri.joinPaths('foo', 123));
assert.strictEqual('123/foo', Uri.joinPaths(123, 'foo'));
assert.strictEqual('1/2/3', Uri.joinPaths(1, 2, 3));
});

it('should make urls unique by adding a random param', function() {
var uri = new Uri('foo.bar/path');
var uri2 = new Uri('foo.bar/path');
Expand Down Expand Up @@ -284,4 +290,45 @@ describe('Uri', function() {
assert.strictEqual('https://hostname:8080/foo/', uri.toString());
assert.strictEqual(false, uri.isUsingDefaultProtocol());
});

it('should set protocol http: on localhost uri', function() {
var uri = new Uri('localhost:8080');
assert.strictEqual('http:', uri.getProtocol());
assert.strictEqual('8080', uri.getPort());
});

it('should support useful types of uri schemes like "tel"', function() {
var uri = new Uri('tel:pathname', false);
assert.strictEqual('tel:', uri.getProtocol());
assert.strictEqual('pathname', uri.getPathname());
assert.strictEqual('tel:pathname', uri.toString());
});

it('should support uri schemes that include hyphens', function() {
var uri = new Uri('ms-excel:pathname', false);
assert.strictEqual('ms-excel:', uri.getProtocol());
assert.strictEqual('pathname', uri.getPathname());
assert.strictEqual('ms-excel:pathname', uri.toString());
});

it('should support uri schemes that include numerical values', function() {
var uri = new Uri('pkcs11:pathname', false);
assert.strictEqual('pkcs11:', uri.getProtocol());
assert.strictEqual('pathname', uri.getPathname());
assert.strictEqual('pkcs11:pathname', uri.toString());
});

it('should support uri schemes that include periods', function() {
var uri = new Uri('iris.beep:pathname', false);
assert.strictEqual('iris.beep:', uri.getProtocol());
assert.strictEqual('pathname', uri.getPathname());
assert.strictEqual('iris.beep:pathname', uri.toString());
});

it('should support uri schemes that include pluses', function() {
var uri = new Uri('some+scheme:pathname', false);
assert.strictEqual('some+scheme:', uri.getProtocol());
assert.strictEqual('pathname', uri.getPathname());
assert.strictEqual('some+scheme:pathname', uri.toString());
});
});

0 comments on commit eafcfb6

Please sign in to comment.