Skip to content

Commit

Permalink
improve path.replace to replace segments and provide uriToString for …
Browse files Browse the repository at this point in the history
…Path and Query
  • Loading branch information
robhicks committed May 6, 2017
1 parent 244958c commit d1d7538
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 56 deletions.
41 changes: 29 additions & 12 deletions dist/Uri.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@
var Path = function Path(f, ctx) {
if ( ctx === void 0 ) ctx = {};

Object.assign(this, ctx);
this.ctx = ctx;
this._path = [];
return this.parse(f);
};

Path.prototype.append = function append (s) {
this._path.push(s);
return this;
};

Path.prototype.delete = function delete$1 (loc) {
if (!loc) {
this._path.pop();
return this;
}
};

Path.prototype.get = function get () {
return this._path;
};
Expand All @@ -25,21 +37,21 @@ Path.prototype.parse = function parse (f) {
return this;
};

Path.prototype.replace = function replace (f) {
Path.prototype.replace = function replace (f, loc) {
if (loc === 'file') {
this._path.splice(this._path.length - 1, 1, f);
return this;
} else if (Number.isInteger(loc)) {
this._path.splice(loc, 1, f);
return this;
}
return this.parse(f);
};

Path.prototype.removeFilename = function removeFilename () {
this._path.splice(this._path.length - 1, 1);
return this;
Path.prototype.uriToString = function uriToString () {
return this.ctx.toString();
};

Path.prototype.replaceFilename = function replaceFilename (n) {
this._path.splice(this._path.length - 1, 1, n);
return this;
};


Path.prototype.toString = function toString () {
return Array.isArray(this._path) ? this._path.join('/') : '';
};
Expand All @@ -48,6 +60,7 @@ var Query = function Query(f, ctx) {
if ( ctx === void 0 ) ctx = {};

Object.assign(this, ctx);
this.ctx = ctx;
this.set(f);
return this;
};
Expand Down Expand Up @@ -185,6 +198,10 @@ Query.prototype.toString = function toString () {
return pairs.join('&');
};

Query.prototype.uriToString = function uriToString () {
return this.ctx.toString();
};

var StringBuilder = function StringBuilder(string) {
if (!string || typeof string === 'undefined') { this.string = String(""); }
else { this.string = String(string); }
Expand Down Expand Up @@ -291,7 +308,7 @@ Uri.prototype.toString = function toString () {
var f = this.fragment();
var s = this.scheme();
var str = new StringBuilder();
return str.append(s ? s + '://' : "").append(this.authority()).append('/').append(p).append('?').append(q).toString();
return str.append(s ? s + '://' : "").append(this.authority()).append('/').append(p).append(q !== '' ? '?' : '').append(q).toString();
};

module.exports = Uri;
41 changes: 29 additions & 12 deletions dist/Uri.es.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
var Path = function Path(f, ctx) {
if ( ctx === void 0 ) ctx = {};

Object.assign(this, ctx);
this.ctx = ctx;
this._path = [];
return this.parse(f);
};

Path.prototype.append = function append (s) {
this._path.push(s);
return this;
};

Path.prototype.delete = function delete$1 (loc) {
if (!loc) {
this._path.pop();
return this;
}
};

Path.prototype.get = function get () {
return this._path;
};
Expand All @@ -23,21 +35,21 @@ Path.prototype.parse = function parse (f) {
return this;
};

Path.prototype.replace = function replace (f) {
Path.prototype.replace = function replace (f, loc) {
if (loc === 'file') {
this._path.splice(this._path.length - 1, 1, f);
return this;
} else if (Number.isInteger(loc)) {
this._path.splice(loc, 1, f);
return this;
}
return this.parse(f);
};

Path.prototype.removeFilename = function removeFilename () {
this._path.splice(this._path.length - 1, 1);
return this;
Path.prototype.uriToString = function uriToString () {
return this.ctx.toString();
};

Path.prototype.replaceFilename = function replaceFilename (n) {
this._path.splice(this._path.length - 1, 1, n);
return this;
};


Path.prototype.toString = function toString () {
return Array.isArray(this._path) ? this._path.join('/') : '';
};
Expand All @@ -46,6 +58,7 @@ var Query = function Query(f, ctx) {
if ( ctx === void 0 ) ctx = {};

Object.assign(this, ctx);
this.ctx = ctx;
this.set(f);
return this;
};
Expand Down Expand Up @@ -183,6 +196,10 @@ Query.prototype.toString = function toString () {
return pairs.join('&');
};

Query.prototype.uriToString = function uriToString () {
return this.ctx.toString();
};

var StringBuilder = function StringBuilder(string) {
if (!string || typeof string === 'undefined') { this.string = String(""); }
else { this.string = String(string); }
Expand Down Expand Up @@ -289,7 +306,7 @@ Uri.prototype.toString = function toString () {
var f = this.fragment();
var s = this.scheme();
var str = new StringBuilder();
return str.append(s ? s + '://' : "").append(this.authority()).append('/').append(p).append('?').append(q).toString();
return str.append(s ? s + '://' : "").append(this.authority()).append('/').append(p).append(q !== '' ? '?' : '').append(q).toString();
};

export default Uri;
41 changes: 29 additions & 12 deletions dist/Uri.iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@ var Uri = (function () {
var Path = function Path(f, ctx) {
if ( ctx === void 0 ) ctx = {};

Object.assign(this, ctx);
this.ctx = ctx;
this._path = [];
return this.parse(f);
};

Path.prototype.append = function append (s) {
this._path.push(s);
return this;
};

Path.prototype.delete = function delete$1 (loc) {
if (!loc) {
this._path.pop();
return this;
}
};

Path.prototype.get = function get () {
return this._path;
};
Expand All @@ -26,21 +38,21 @@ Path.prototype.parse = function parse (f) {
return this;
};

Path.prototype.replace = function replace (f) {
Path.prototype.replace = function replace (f, loc) {
if (loc === 'file') {
this._path.splice(this._path.length - 1, 1, f);
return this;
} else if (Number.isInteger(loc)) {
this._path.splice(loc, 1, f);
return this;
}
return this.parse(f);
};

Path.prototype.removeFilename = function removeFilename () {
this._path.splice(this._path.length - 1, 1);
return this;
Path.prototype.uriToString = function uriToString () {
return this.ctx.toString();
};

Path.prototype.replaceFilename = function replaceFilename (n) {
this._path.splice(this._path.length - 1, 1, n);
return this;
};


Path.prototype.toString = function toString () {
return Array.isArray(this._path) ? this._path.join('/') : '';
};
Expand All @@ -49,6 +61,7 @@ var Query = function Query(f, ctx) {
if ( ctx === void 0 ) ctx = {};

Object.assign(this, ctx);
this.ctx = ctx;
this.set(f);
return this;
};
Expand Down Expand Up @@ -186,6 +199,10 @@ Query.prototype.toString = function toString () {
return pairs.join('&');
};

Query.prototype.uriToString = function uriToString () {
return this.ctx.toString();
};

var StringBuilder = function StringBuilder(string) {
if (!string || typeof string === 'undefined') { this.string = String(""); }
else { this.string = String(string); }
Expand Down Expand Up @@ -292,7 +309,7 @@ Uri.prototype.toString = function toString () {
var f = this.fragment();
var s = this.scheme();
var str = new StringBuilder();
return str.append(s ? s + '://' : "").append(this.authority()).append('/').append(p).append('?').append(q).toString();
return str.append(s ? s + '://' : "").append(this.authority()).append('/').append(p).append(q !== '' ? '?' : '').append(q).toString();
};

return Uri;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Uri",
"version": "1.1.0",
"version": "1.1.1",
"description": "Lightweight Javascript library for handling URLs",
"keywords": [
"Lightweight",
Expand Down
50 changes: 43 additions & 7 deletions spec/Uri.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,41 @@ describe('Uri', () => {
expect(uri.path.replace('different/path/to/file.json').toString()).toEqual('different/path/to/file.json');
});

it('should remove the file part of the path', () => {
it('should replace the file part of the path', () => {
let url = 'https://user:[email protected]/path/to/file.xml';
let uri = new Uri(url);

expect(uri.path.removeFilename().toString()).toEqual('path/to');
expect(uri.path.replace('file.json', 'file').toString()).toEqual('path/to/file.json');
});

it('should replace the file part of the path', () => {
it('should remove the last segment of the path', () => {
let url = 'https://user:[email protected]/path/to/file.xml';
let uri = new Uri(url);

expect(uri.path.delete().toString()).toEqual('path/to');
});

it('should replace the first segment of the path', () => {
let url = 'https://user:[email protected]/path/to/file.xml';
let uri = new Uri(url);

expect(uri.path.replace('new-path', 0).toString()).toEqual('new-path/to/file.xml');
});

it('should replace the second segment of the path', () => {
let url = 'https://user:[email protected]/path/to/file.xml';
let uri = new Uri(url);

expect(uri.path.replace('new-to', 1).toString()).toEqual('path/new-to/file.xml');
});

it('should return the uri as a string', () => {
let url = 'https://user:[email protected]/path/to/file.xml';
let uri = new Uri(url);

expect(uri.path.replaceFilename('goober.json').toString()).toEqual('path/to/goober.json');
expect(uri.path.replace('new-to', 1).uriToString()).toEqual('https://user:[email protected]/path/new-to/file.xml');
});

});

describe('Query', () => {
Expand All @@ -66,6 +88,12 @@ describe('Uri', () => {
expect(uri.query.toString()).toBe('foo=bar');
});

it('should clear to the query string', () => {
let url = 'https://user:[email protected]/path/to/file.xml?context=foo&credentials=bar';
let uri = new Uri(url);
expect(uri.query.clear().toString()).toBe('');
});

it('should append to the query string', () => {
let url = 'https://user:[email protected]/path/to/file.xml?context=foo&credentials=bar';
let uri = new Uri(url);
Expand All @@ -79,6 +107,13 @@ describe('Uri', () => {
uri.query.merge({context: 'bar'});
expect(uri.query.toString()).toBe('context=bar&credentials=bar');
});

it('should, when cleared, return a proper url', () => {
let url = 'https://user:[email protected]/path/to/file.xml?context=foo&credentials=bar';
let uri = new Uri(url);
uri.query.clear();
expect(uri.toString()).toEqual('https://user:[email protected]/path/to/file.xml');
});
});

it('should change the host', () => {
Expand All @@ -104,9 +139,10 @@ describe('Uri', () => {
expect(Array.isArray(uri.path.get())).toEqual(true);
expect(uri.path.toString()).toEqual('path/to/file.xml');
expect(uri.query).toEqual(jasmine.any(Object));
expect(uri.query.add({foo: 'bar'}).toString()).toEqual('context=foo&credentials=bar&foo=bar')
expect(uri.query.add({foo: 'bar'}).merge({foo: 'bars'}).toString()).toEqual('context=foo&credentials=bar&foo=bars')
expect(uri.query.clear().add({foo: 'bar'}).merge({foo: 'bars'}).toString()).toEqual('foo=bars')
expect(uri.query.add({foo: 'bar'}).toString()).toEqual('context=foo&credentials=bar&foo=bar');
expect(uri.query.add({foo: 'bar'}).merge({foo: 'bars'}).toString()).toEqual('context=foo&credentials=bar&foo=bars');
expect(uri.query.clear().add({foo: 'bar'}).merge({foo: 'bars'}).toString()).toEqual('foo=bars');
expect(uri.query.clear().add({foo: 'bar'}).merge({foo: 'bars'}).uriToString()).toEqual('https://user:[email protected]/path/to/file.xml?foo=bars');
});

});
Loading

0 comments on commit d1d7538

Please sign in to comment.