parent
63ac2864d4
commit
36d0e59c4b
@ -1,38 +1,41 @@ |
||||
function getQueryParams () { |
||||
var qs = window.location.hash.substr(1); |
||||
// Allowing window to be overriden for testing
|
||||
function QueryParams (_window) { |
||||
if (_window === undefined) _window = window; |
||||
|
||||
if (window.location.search.length > 0) { |
||||
// use legacy query params instead of hash
|
||||
window.location.hash = window.location.search.substr(1); |
||||
window.location.search = ''; |
||||
} |
||||
this.get = function () { |
||||
var qs = _window.location.hash.substr(1); |
||||
|
||||
var params = {}; |
||||
var parts = qs.split('&'); |
||||
for (var x in parts) { |
||||
var keyValue = parts[x].split('='); |
||||
if (keyValue[0] !== '') { |
||||
params[keyValue[0]] = keyValue[1]; |
||||
if (_window.location.search.length > 0) { |
||||
// use legacy query params instead of hash
|
||||
_window.location.hash = _window.location.search.substr(1); |
||||
_window.location.search = ''; |
||||
} |
||||
} |
||||
return params; |
||||
} |
||||
|
||||
function updateQueryParams (params) { |
||||
var currentParams = getQueryParams(); |
||||
var keys = Object.keys(params); |
||||
for (var x in keys) { |
||||
currentParams[keys[x]] = params[keys[x]]; |
||||
var params = {}; |
||||
var parts = qs.split('&'); |
||||
for (var x in parts) { |
||||
var keyValue = parts[x].split('='); |
||||
if (keyValue[0] !== '') { |
||||
params[keyValue[0]] = keyValue[1]; |
||||
} |
||||
} |
||||
return params; |
||||
} |
||||
var queryString = '#'; |
||||
var updatedKeys = Object.keys(currentParams); |
||||
for (var y in updatedKeys) { |
||||
queryString += updatedKeys[y] + '=' + currentParams[updatedKeys[y]] + '&'; |
||||
|
||||
this.update = function (params) { |
||||
var currentParams = this.get(); |
||||
var keys = Object.keys(params); |
||||
for (var x in keys) { |
||||
currentParams[keys[x]] = params[keys[x]]; |
||||
} |
||||
var queryString = '#'; |
||||
var updatedKeys = Object.keys(currentParams); |
||||
for (var y in updatedKeys) { |
||||
queryString += updatedKeys[y] + '=' + currentParams[updatedKeys[y]] + '&'; |
||||
} |
||||
_window.location.hash = queryString.slice(0, -1); |
||||
} |
||||
window.location.hash = queryString.slice(0, -1); |
||||
|
||||
} |
||||
|
||||
module.exports = { |
||||
get: getQueryParams, |
||||
update: updateQueryParams |
||||
}; |
||||
module.exports = QueryParams; |
||||
|
@ -0,0 +1 @@ |
||||
require('./query-params-test'); |
@ -0,0 +1,21 @@ |
||||
var test = require('tape'); |
||||
|
||||
var QueryParams = require('../src/app/query-params'); |
||||
|
||||
test('queryParams.get', function (t) { |
||||
t.plan(2); |
||||
|
||||
var fakeWindow = {location: {hash: '#wat=sup&foo=bar', search: ''}}; |
||||
var params = new QueryParams(fakeWindow).get(); |
||||
t.equal(params.wat, 'sup'); |
||||
t.equal(params.foo, 'bar'); |
||||
}); |
||||
|
||||
test('queryParams.update', function (t) { |
||||
t.plan(1); |
||||
|
||||
var fakeWindow = {location: {hash: '#wat=sup', search: ''}}; |
||||
var qp = new QueryParams(fakeWindow); |
||||
qp.update({foo: 'bar'}); |
||||
t.equal(fakeWindow.location.hash, '#wat=sup&foo=bar'); |
||||
}); |
Loading…
Reference in new issue