-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl-query-helper.js
158 lines (143 loc) · 3.96 KB
/
url-query-helper.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* UrlQueryHelper JavaScript Library v1.0
* https://github.com/toddw/url-query-helper
*
* Requires Underscore.js
* http://underscorejs.org/
*
* Copyright 2013 Todd Williams
* Released under the MIT license
* http://opensource.org/licenses/MIT
*
* Date: 2013-10-22
*/
/**
* Creates an instance of UrlQueryHelper
*
* @param {string} url The url that we will be parseing
* @param {object} updatedParams An optional object of new or updated parameters that will modify the url
*/
UrlQueryHelper = function (url, updatedParams) {
if(this.init) {
this.init(url, updatedParams);
}
};
UrlQueryHelper.prototype = {
/**
* The initializing script that gets all of the params from the url query string
*
* @param {string} url The url that we will be parseing
* @param {object} updatedParams An optional object of new or updated parameters that will modify the url
*/
init: function (url, updatedParams) {
var self = this,
oldParamsArray;
// set the constant variables
self.originalUrl = url;
self.updateParams = updatedParams;
self.params = {};
// convert all of the params in the url to a hash
if(self.originalUrl.split('?').length > 1) {
originalParamsArray = self.originalUrl.split('?')[1].split('&');
_.each(originalParamsArray, function (param) {
var paramParts = param.split('=');
self.params[paramParts[0]] = decodeURI(paramParts[1]);
});
}
// update params hash with new params if any passed in
if(updatedParams) {
self.setParams(updatedParams);
}
return self;
},
/**
* Builds and returns a url with the current params for this instance
*
* @return {string} The url
*/
getUrl: function () {
var self = this;
// build new url
if(_.size(self.params) > 0) {
return self.originalUrl.split('?')[0] + '?' + _.map(self.params, function (value, key) { return key + '=' + encodeURI(value) }).join('&');
}
else {
return self.originalUrl.split('?')[0];
}
},
/**
* Returns the params object which shows the current state of params for this instance
*
* @return {object} All of the params for this helper
*/
getParams: function () {
return this.params;
},
/**
* Return s a single param from this instance
*
* @param {string} param The name of the parameter to get
* @return {string} The value of the requested param
*/
getParam: function (param) {
return this.params[param];
},
/**
* Update or add a param to this instance
*
* @param {string} param The name of the parameter to add
* @param {string} value The desired value for the given parameter
* @return {UrlQueryHelper}
*/
setParam: function (param, value) {
var self = this;
self.params[param] = value;
return self;
},
/**
* Update or add multiple parameters at once
*
* @param {object} paramsHash An object with all of the paremters to update or add
* @return {UrlQueryHelper}
*/
setParams: function (paramsHash) {
var self = this;
_.each(paramsHash, function (value, param) {
self.setParam(param, value);
});
return self;
},
/**
* Remove a parameter from this instance
*
* @param {string} param the name of the parameter to remove
* @return {UrlQueryHelper}
*/
removeParam: function (param) {
var self = this;
self.params = _.omit(self.params, param);
return self;
},
/**
* Remove a list of paremters from this instance
*
* @param {...string} Arguments as a list of param names to remove from this instance
* @return {UrlQueryHelper}
*/
removeParams: function () {
var self = this;
_.each(arguments, function (param) {
self.removeParam(param);
});
return self;
},
/**
* Create a String representation of the UrlQueryHelper
*
* @this {UrlQueryHelper}
* @return {string} Human-readable representation of UrlQuerHelper
*/
toString: function () {
return this.getUrl();
}
};