-
Notifications
You must be signed in to change notification settings - Fork 1
/
query-handler.js
117 lines (108 loc) · 3.65 KB
/
query-handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//! QueryHandler.js v1.2.0 | (c) 2015 Zach Dahl | MIT License
(function (root, factory) {
if (typeof define === "function" && define.amd) {
define(["exports"], factory);
} else if (typeof exports !== "undefined") {
factory(exports);
} else {
factory(root);
}
})(this, function (exports) {
"use strict";
var _createClass = (function () { function defineProperties(target, props) { for (var key in props) { var prop = props[key]; prop.configurable = true; if (prop.value) prop.writable = true; } Object.defineProperties(target, props); } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };
exports.QueryHandler = QueryHandler;
Object.defineProperty(exports, "__esModule", {
value: true
});
var Query = (function () {
function Query(string) {
_classCallCheck(this, Query);
this.initialString = string;
this.generateQueryObject(string);
}
_createClass(Query, {
generateQueryObject: {
value: function generateQueryObject(str) {
this.query = {};
var arr = str.substr(1).split("&");
for (var i = 0, il = arr.length; i < il; ++i) {
var current = arr[i].split("=");
var key = decodeURIComponent(current[0]);
var value = this.smartParse(current[1]);
if (typeof this.query[key] === "undefined") this.query[key] = value;else if (this.query[key] instanceof Array) this.query[key].push(value);else this.query[key] = [this.query[key], value];
}
}
},
smartParse: {
value: function smartParse(val) {
if (typeof val === "undefined") {
return null;
}if (/^(true|false|null)$/i.test(val)) {
return JSON.parse(val);
}if (!isNaN(+val)) {
return +val;
}return decodeURIComponent(val);
}
},
getQueryString: {
value: function getQueryString() {
var self = this;
var q = this.query;
var result = Object.keys(q).map(function (key, _) {
var val = q[key];
if (val instanceof Array) {
return val.map(function (v, i) {
return self.pairToString(key, v);
}).join("&");
}
return self.pairToString(key, val);
});
return "?" + result.join("&");
}
},
string: {
get: function () {
return this.getQueryString();
}
},
initial: {
get: function () {
return this.initialString;
}
},
has: {
value: function has(key) {
return this.query.hasOwnProperty(key);
}
},
get: {
value: function get(key, def) {
return this.has(key) ? this.query[key] : typeof def === "undefined" ? null : def;
}
},
set: {
value: function set(key, val) {
this.query[key] = val;
}
},
remove: {
value: function remove(key) {
delete this.query[key];
}
},
pairToString: {
value: function pairToString(key, val) {
key = encodeURIComponent(key);
if (val === null) {
return key;
}return key + "=" + encodeURIComponent(val);
}
}
});
return Query;
})();
function QueryHandler(q) {
return new Query(q);
}
});