Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7cde58
commit 963e9f3
Showing
5 changed files
with
92 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
define(function () { | ||
|
||
/** | ||
* Set query string parameter value | ||
* @version 0.1.0 (2012/10/11) | ||
*/ | ||
function setParam(url, paramName, value){ | ||
url = url || ''; | ||
|
||
var re = new RegExp('(\\?|&)'+ paramName +'=[^&]*' ); | ||
var param = paramName +'='+ encodeURIComponent( value ); | ||
|
||
if ( re.test(url) ) { | ||
return url.replace(re, '$1'+ param); | ||
} else { | ||
if (url.indexOf('?') === -1) { | ||
url += '?'; | ||
} | ||
if (url.indexOf('=') !== -1) { | ||
url += '&'; | ||
} | ||
return url + param; | ||
} | ||
|
||
} | ||
|
||
return setParam; | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
define(['src/queryString/setParam'], function(setParam){ | ||
|
||
describe('queryString/setParam', function(){ | ||
|
||
it('should add value if it doesn\'t exist', function(){ | ||
expect( setParam('foo.com', 'bar', true) ).toBe( 'foo.com?bar=true' ); | ||
expect( setParam('foo.com?bar=1', 'ipsum', 'dolor') ).toBe( 'foo.com?bar=1&ipsum=dolor' ); | ||
}); | ||
|
||
it('should encode value', function () { | ||
expect( setParam('foo.com?bar=1', 'ipsum', 'dólôr amèt') ).toBe( 'foo.com?bar=1&ipsum=d%C3%B3l%C3%B4r%20am%C3%A8t' ); | ||
}); | ||
|
||
it('should update value if it exists', function(){ | ||
expect( setParam('foo.com?bar=2', 'bar', false) ).toBe( 'foo.com?bar=false' ); | ||
expect( setParam('foo.com?bar=1&ipsum=dolor%20amet&maecennas=3', 'bar', 'amet') ).toBe( 'foo.com?bar=amet&ipsum=dolor%20amet&maecennas=3' ); | ||
}); | ||
|
||
it('should work with just the query string', function () { | ||
expect( setParam('?dolor=amet', 'ipsum', 123) ).toEqual( '?dolor=amet&ipsum=123' ); | ||
expect( setParam('?dolor=amet&ipsum=5', 'ipsum', 123) ).toEqual( '?dolor=amet&ipsum=123' ); | ||
expect( setParam('?dolor=amet&ipsum=5&maecennas=ullamcor', 'ipsum', 123) ).toEqual( '?dolor=amet&ipsum=123&maecennas=ullamcor' ); | ||
}); | ||
|
||
it('should work with empty url', function () { | ||
expect( setParam('', 'foo', 'bar') ).toEqual( '?foo=bar' ); | ||
expect( setParam('?', 'foo', 'bar') ).toEqual( '?foo=bar' ); | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters