url-assembler
Advanced tools
Comparing version 1.0.0 to 1.1.0
17
index.js
@@ -38,3 +38,3 @@ var extend = require('extend'); | ||
if(baseUrl) { | ||
if (baseUrl) { | ||
extend(this, url.parse(baseUrl)); | ||
@@ -47,3 +47,3 @@ this._prefix = this.pathname; | ||
} | ||
if(params) { | ||
if (params) { | ||
extend(this, params.value); | ||
@@ -54,2 +54,3 @@ this._prefix = params.prefix; | ||
this.href = this.toString(); | ||
} | ||
@@ -93,8 +94,10 @@ | ||
methods.param = function param (key, value) { | ||
methods.param = function param (key, value, strict) { | ||
var chainable = this._chain(); | ||
if(!value && typeof key === 'object') { | ||
if (typeof key === 'object') { | ||
var hash = key; | ||
for(key in hash) { | ||
chainable = chainable.param(key, hash[key]); | ||
strict = (value === true); | ||
for (key in hash) { | ||
chainable = chainable.param(key, hash[key], strict); | ||
} | ||
@@ -107,3 +110,3 @@ return chainable; | ||
chainable.pathname = this.pathname.replace(symbol, value); | ||
if(chainable.pathname === previous) { | ||
if (!strict && chainable.pathname === previous) { | ||
return chainable.query(key, value); | ||
@@ -110,0 +113,0 @@ } |
{ | ||
"name": "url-assembler", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Assemble urls from route-like templates (/path/:param)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -26,3 +26,3 @@ var expect = require('chai').expect | ||
describe('.prefix()', function () { | ||
describe('.prefix(prefix)', function () { | ||
it('adds a prefix to the result of toString()', function () { | ||
@@ -33,3 +33,3 @@ expect(myUrl.prefix('/coucou').toString()).to.equal('/coucou/hello'); | ||
describe('.param()', function () { | ||
describe('.param(key, value)', function () { | ||
it('adds the parameter as a query parameter', function () { | ||
@@ -40,3 +40,9 @@ expect(myUrl.param('a', 'bc').toString()).to.equal('/hello?a=bc'); | ||
describe('.query()', function () { | ||
describe('.param(key, value, true)', function () { | ||
it('does not add the parameter as query parameter', function () { | ||
expect(myUrl.param('a', 'bc', true).toString()).to.equal('/hello'); | ||
}); | ||
}); | ||
describe('.query(key, value)', function () { | ||
it('add the parameter as a query parameter', function () { | ||
@@ -53,43 +59,43 @@ expect(myUrl.query('param', 12345).toString()).to.equal('/hello?param=12345'); | ||
}); | ||
}); | ||
describe('called with a hash map', function () { | ||
it('adds each of it to the query string', function () { | ||
myUrl = myUrl.query({ | ||
'hello': 'goodbye', | ||
'one': 1 | ||
}) | ||
expect(myUrl.toString()).to.equal('/hello?hello=goodbye&one=1'); | ||
describe('.query({key: value})', function () { | ||
it('adds each of it to the query string', function () { | ||
myUrl = myUrl.query({ | ||
'hello': 'goodbye', | ||
'one': 1 | ||
}) | ||
expect(myUrl.toString()).to.equal('/hello?hello=goodbye&one=1'); | ||
}) | ||
it('does not add the query parameters which are null', function () { | ||
myUrl = myUrl.query({ | ||
'hello': 'goodbye', | ||
'one': null, | ||
'goodbye': 'hello' | ||
}) | ||
expect(myUrl.toString()).to.equal('/hello?hello=goodbye&goodbye=hello'); | ||
}); | ||
it('does not add the query parameters which are null', function () { | ||
myUrl = myUrl.query({ | ||
'hello': 'goodbye', | ||
'one': null, | ||
'goodbye': 'hello' | ||
}) | ||
expect(myUrl.toString()).to.equal('/hello?hello=goodbye&goodbye=hello'); | ||
}); | ||
it('keeps falsy values if they are correct', function () { | ||
it('keeps falsy values if they are correct', function () { | ||
myUrl = myUrl.query({ | ||
'hello': 'goodbye', | ||
'two': '', | ||
'three': 0, | ||
'goodbye': 'hello' | ||
}) | ||
expect(myUrl.toString()).to.equal('/hello?hello=goodbye&two=&three=0&goodbye=hello'); | ||
}); | ||
describe('when some query param have already been set', function () { | ||
beforeEach(function () { | ||
myUrl = myUrl.query('yes', 'no'); | ||
}) | ||
it('keeps the previously set query params', function () { | ||
myUrl = myUrl.query({ | ||
'hello': 'goodbye', | ||
'two': '', | ||
'three': 0, | ||
'goodbye': 'hello' | ||
'one': 1 | ||
}) | ||
expect(myUrl.toString()).to.equal('/hello?hello=goodbye&two=&three=0&goodbye=hello'); | ||
expect(myUrl.toString()).to.equal('/hello?yes=no&hello=goodbye&one=1') | ||
}); | ||
describe('when some query param have already been set', function () { | ||
beforeEach(function () { | ||
myUrl = myUrl.query('yes', 'no'); | ||
}) | ||
it('keeps the previously set query params', function () { | ||
myUrl = myUrl.query({ | ||
'hello': 'goodbye', | ||
'one': 1 | ||
}) | ||
expect(myUrl.toString()).to.equal('/hello?yes=no&hello=goodbye&one=1') | ||
}); | ||
}) | ||
}) | ||
@@ -103,3 +109,3 @@ }) | ||
}); | ||
describe('.param()', function () { | ||
describe('.param(key, value)', function () { | ||
it('replaces the parameter in the template', function () { | ||
@@ -110,3 +116,3 @@ expect(myUrl.param('myparam', 'hello').toString()).to.equal('/path/hello'); | ||
describe('.segment()', function () { | ||
describe('.segment(subpath)', function () { | ||
beforeEach(function () { | ||
@@ -137,4 +143,24 @@ myUrl = myUrl | ||
}); | ||
it('puts parameters which are not substituted in the path to query params', function () { | ||
var actual = myUrl.param({ | ||
group: 'A', | ||
user: 9, | ||
something: 'else' | ||
}).toString(); | ||
expect(actual).to.equal('/groups/A/users/9?something=else'); | ||
}); | ||
}) | ||
describe('.param({...}, true)', function () { | ||
it('does not put unused parameters in query params', function () { | ||
var actual = myUrl.param({ | ||
group: 'A', | ||
user: 9, | ||
something: 'else' | ||
}, true).toString(); | ||
expect(actual).to.equal('/groups/A/users/9'); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -19,3 +19,3 @@ var expect = require('chai').expect | ||
describe('.prefix()', function () { | ||
describe('.prefix(prefix)', function () { | ||
it('adds a prefix in addition to the exsting one', function () { | ||
@@ -22,0 +22,0 @@ expect(myUrl.prefix('/v2').toString()).to.equal('http://hello.com:8989/api/v2/hello/world'); |
14652
307