mirror of
https://github.com/prymitive/karma
synced 2026-05-05 03:16:51 +00:00
Rewrite querystring.js as CommonJS, add basic tests
This commit is contained in:
@@ -1,54 +1,49 @@
|
||||
/* exported QueryString */
|
||||
var QueryString = (function() {
|
||||
const $ = require("jquery");
|
||||
|
||||
var parse = function() {
|
||||
var vars = [],
|
||||
hash;
|
||||
var q = document.URL.split("?")[1];
|
||||
if (q !== undefined) {
|
||||
q = q.split("&");
|
||||
for (var i = 0; i < q.length; i++) {
|
||||
hash = q[i].split("=");
|
||||
vars[hash[0]] = hash.slice(1).join("=");
|
||||
}
|
||||
function parse() {
|
||||
var vars = {},
|
||||
hash;
|
||||
var q = document.URL.split("?")[1];
|
||||
if (q !== undefined) {
|
||||
q = q.split("&");
|
||||
for (var i = 0; i < q.length; i++) {
|
||||
hash = q[i].split("=");
|
||||
vars[hash[0]] = hash.slice(1).join("=");
|
||||
}
|
||||
return vars;
|
||||
};
|
||||
}
|
||||
return vars;
|
||||
}
|
||||
|
||||
var update = function(key, value) {
|
||||
/* https://gist.github.com/excalq/2961415 */
|
||||
var baseUrl = [ location.protocol, "//", location.host, location.pathname ].join(""),
|
||||
urlQueryString = document.location.search,
|
||||
newParam = key + "=" + value,
|
||||
params = "?" + newParam;
|
||||
function update(key, value) {
|
||||
/* https://gist.github.com/excalq/2961415 */
|
||||
var baseUrl = [ location.protocol, "//", location.host, location.pathname ].join(""),
|
||||
urlQueryString = document.location.search,
|
||||
newParam = key + "=" + value,
|
||||
params = "?" + newParam;
|
||||
|
||||
// If the "search" string exists, then build params from it
|
||||
if (urlQueryString) {
|
||||
var keyRegex = new RegExp("([?&])" + key + "[^&]*");
|
||||
// If the "search" string exists, then build params from it
|
||||
if (urlQueryString) {
|
||||
var keyRegex = new RegExp("([?&])" + key + "[^&]*");
|
||||
|
||||
// If param exists already, update it
|
||||
if (urlQueryString.match(keyRegex) !== null) {
|
||||
params = urlQueryString.replace(keyRegex, "$1" + newParam);
|
||||
} else { // Otherwise, add it to end of query string
|
||||
params = urlQueryString + "&" + newParam;
|
||||
}
|
||||
// If param exists already, update it
|
||||
if (urlQueryString.match(keyRegex) !== null) {
|
||||
params = urlQueryString.replace(keyRegex, "$1" + newParam);
|
||||
} else { // Otherwise, add it to end of query string
|
||||
params = urlQueryString + "&" + newParam;
|
||||
}
|
||||
window.history.replaceState({}, "", baseUrl + params);
|
||||
};
|
||||
}
|
||||
window.history.replaceState({}, "", baseUrl + params);
|
||||
}
|
||||
|
||||
var remove = function(key) {
|
||||
var baseUrl = [ location.protocol, "//", location.host, location.pathname ].join(""),
|
||||
q = QueryString.Parse();
|
||||
if (q[key] !== undefined) {
|
||||
delete q[key];
|
||||
window.history.replaceState({}, "", baseUrl + "?" + $.param(q));
|
||||
}
|
||||
};
|
||||
function remove(key) {
|
||||
var baseUrl = [ location.protocol, "//", location.host, location.pathname ].join(""),
|
||||
q = parse();
|
||||
if (q[key] !== undefined) {
|
||||
delete q[key];
|
||||
window.history.replaceState({}, "", baseUrl + "?" + $.param(q));
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
Parse: parse,
|
||||
Set: update,
|
||||
Remove: remove
|
||||
};
|
||||
|
||||
}());
|
||||
exports.parse = parse;
|
||||
exports.update = update;
|
||||
exports.remove = remove;
|
||||
|
||||
23
assets/static/querystring.test.js
Normal file
23
assets/static/querystring.test.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const querystring = require("./querystring");
|
||||
|
||||
test("querystring parse()", () => {
|
||||
expect(querystring.parse()).toEqual({});
|
||||
Object.defineProperty(document, "URL", {
|
||||
value: "http://example.com?foo=bar",
|
||||
configurable: true,
|
||||
});
|
||||
expect(querystring.parse()).toEqual({"foo": "bar"});
|
||||
});
|
||||
|
||||
test("querystring update()", () => {
|
||||
Object.defineProperty(document, "URL", {
|
||||
value: "http://example.com?foo=bar",
|
||||
configurable: true,
|
||||
});
|
||||
expect(querystring.parse()).toEqual({"foo": "bar"});
|
||||
/*
|
||||
FIXME disabled as it requires mocking browser history
|
||||
querystring.update("foo", "notbar");
|
||||
expect(querystring.parse()).toEqual({"foo": "notbar"});
|
||||
*/
|
||||
});
|
||||
Reference in New Issue
Block a user