-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy patharg-1.2.js
329 lines (266 loc) · 10.1 KB
/
arg-1.2.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
arg.js - v1.2
JavaScript URL argument processing once and for all.
by Mat Ryer and Ryan Quinn
Copyright (c) 2013 Stretchr, Inc.
Please consider promoting this project if you find it useful.
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
(function(global){
/**
* MakeArg makes the Arg namespace.
* var Arg = MakeArg();
*/
global.MakeArg = function(){
/** @namespace
*/
var Arg = function(){
return Arg.get.apply(global, arguments);
};
Arg.version = "1.2.0";
/**
* Parses the arg string into an Arg.Arg object.
*/
Arg.parse = function(s){
if (!s) return {};
if (s.indexOf("=")===-1 && s.indexOf("&")===-1) return {};
s = Arg._cleanParamStr(s);
var obj = {};
var pairs = s.split("&");
for (var pi in pairs) {
if(pairs.hasOwnProperty(pi)){
var kvsegs = pairs[pi].split("=");
var key = decodeURIComponent(kvsegs[0]), val = Arg.__decode(kvsegs[1]);
Arg._access(obj, key, val);
}
}
return obj;
};
/**
* Decodes a URL component (including resolving + to spaces)
*/
Arg.__decode = function(s) {
while (s && s.indexOf("+")>-1) {
s = s.replace("+", " ");
};
s = decodeURIComponent(s);
return s;
};
/**
* Helper method to get/set deep nested values in an object based on a string selector
*
* @param {Object} obj Based object to either get/set selector on
* @param {String} selector Object selector ie foo[0][1].bar[0].baz.foobar
* @param {Mixed} value (optional) Value to set leaf located at `selector` to.
* If value is undefined, operates in 'get' mode to return value at obj->selector
* @return {Mixed}
*/
Arg._access = function(obj, selector, value) {
var shouldSet = typeof value !== "undefined";
var selectorBreak = -1;
var coerce_types = {
'true' : true,
'false' : false,
'null' : null
};
// selector could be a number if we're at a numerical index leaf in which case selector.search is not valid
if (typeof selector == 'string' || toString.call(selector) == '[object String]') {
selectorBreak = selector.search(/[\.\[]/);
}
// No dot or array notation so we're at a leaf, set value
if (selectorBreak === -1) {
if (Arg.coerceMode) {
value = value && !isNaN(value) ? +value // number
: value === 'undefined' ? undefined // undefined
: coerce_types[value] !== undefined ? coerce_types[value] // true, false, null
: value; // string
}
return shouldSet ? (obj[selector] = value) : obj[selector];
}
// Example:
// selector = 'foo[0].bar.baz[2]'
// currentRoot = 'foo'
// nextSelector = '0].bar.baz[2]' -> will be converted to '0.bar.baz[2]' in below switch statement
var currentRoot = selector.substr(0, selectorBreak);
var nextSelector = selector.substr(selectorBreak + 1);
switch (selector.charAt(selectorBreak)) {
case '[':
// Intialize node as an array if we haven't visted it before
obj[currentRoot] = obj[currentRoot] || [];
nextSelector = nextSelector.replace(']', '');
if (nextSelector.search(/[\.\[]/) === -1) {
nextSelector = parseInt(nextSelector, 10);
}
return Arg._access(obj[currentRoot], nextSelector, value);
case '.':
// Intialize node as an object if we haven't visted it before
obj[currentRoot] = obj[currentRoot] || {};
return Arg._access(obj[currentRoot], nextSelector, value);
}
return obj;
};
/**
* Turns the specified object into a URL parameter string.
*/
Arg.stringify = function(obj, keyPrefix) {
switch (typeof(obj)) {
case "object":
var segs = [];
var thisKey;
for (var key in obj) {
if (!obj.hasOwnProperty(key)) continue;
var val = obj[key];
if (typeof(key) === "undefined" || key.length === 0 || typeof(val) === "undefined") continue;
thisKey = keyPrefix ? keyPrefix+"."+key : key;
if (typeof obj.length !== "undefined") {
thisKey = keyPrefix ? keyPrefix+"["+key+"]" : key;
}
if (typeof val === "object") {
segs.push(Arg.stringify(val, thisKey));
} else {
segs.push(encodeURIComponent(thisKey)+"="+encodeURIComponent(val));
}
}
return segs.join("&");
}
return encodeURIComponent(obj);
};
/**
* Generates a URL with the given parameters.
* (object) = A URL to the current page with the specified parameters.
* (path, object) = A URL to the specified path, with the object of parameters.
* (path, object, object) = A URL to the specified path with the first object as query parameters,
* and the second object as hash parameters.
*/
Arg.url = function(){
var sep = (Arg.urlUseHash ? Arg.hashQuerySeperator : Arg.querySeperator);
var segs = [location.pathname, sep];
var args = {};
switch (arguments.length) {
case 1: // Arg.url(params)
segs.push(Arg.stringify(arguments[0]));
break;
case 2: // Arg.url(path, params)
segs[0] = Arg._cleanPath(arguments[0]);
args = Arg.parse(arguments[0]);
args = Arg.merge(args, arguments[1]);
segs.push(Arg.stringify(args));
break;
case 3: // Arg.url(path, query, hash)
segs[0] = Arg._cleanPath(arguments[0]);
segs[1] = Arg.querySeperator;
segs.push(Arg.stringify(arguments[1]));
(typeof(arguments[2])==="string") ? segs.push(Arg.hashSeperator) : segs.push(Arg.hashQuerySeperator);
segs.push(Arg.stringify(arguments[2]));
}
var s = segs.join("");
// trim off sep if it's the last thing
if (s.indexOf(sep) == s.length - sep.length) {
s = s.substr(0, s.length - sep.length);
}
return s;
};
/** urlUseHash tells the Arg.url method to always put the parameters in the hash. */
Arg.urlUseHash = false;
/** The string that seperates the path and query parameters. */
Arg.querySeperator = "?";
/** The string that seperates the path or query, and the hash property. */
Arg.hashSeperator = "#";
/** The string that seperates the the path or query, and the hash query parameters. */
Arg.hashQuerySeperator = "#?";
/** When parsing values if they should be coerced into primitive types, ie Number, Boolean, Undefined */
Arg.coerceMode = true;
/**
* Gets all parameters from the current URL.
*/
Arg.all = function(){
var merged = Arg.parse(Arg.querystring() + "&" + Arg.hashstring());
return Arg._all ? Arg._all : Arg._all = merged;
};
/**
* Gets a parameter from the URL.
*/
Arg.get = function(selector, def){
var val = Arg._access(Arg.all(), selector);
return typeof(val) === "undefined" ? def : val;
};
/**
* Gets the query string parameters from the current URL.
*/
Arg.query = function(){
return Arg._query ? Arg._query : Arg._query = Arg.parse(Arg.querystring());
};
/**
* Gets the hash string parameters from the current URL.
*/
Arg.hash = function(){
return Arg._hash ? Arg._hash : Arg._hash = Arg.parse(Arg.hashstring());
};
/**
* Gets the query string from the URL (the part after the ?).
*/
Arg.querystring = function(){
return Arg._cleanParamStr(location.search);
};
/**
* Gets the hash param string from the URL (the part after the #).
*/
Arg.hashstring = function(){
return Arg._cleanParamStr(location.hash)
};
/*
* Cleans the URL parameter string stripping # and ? from the beginning.
*/
Arg._cleanParamStr = function(s){
if (s.indexOf(Arg.querySeperator)>-1)
s = s.split(Arg.querySeperator)[1];
if (s.indexOf(Arg.hashSeperator)>-1)
s = s.split(Arg.hashSeperator)[1];
if (s.indexOf("=")===-1 && s.indexOf("&")===-1)
return "";
while (s.indexOf(Arg.hashSeperator) == 0 || s.indexOf(Arg.querySeperator) == 0)
s = s.substr(1);
return s;
};
Arg._cleanPath = function(p){
if (p.indexOf(Arg.querySeperator)>-1)
p = p.substr(0,p.indexOf(Arg.querySeperator));
if (p.indexOf(Arg.hashSeperator)>-1)
p = p.substr(0,p.indexOf(Arg.hashSeperator));
return p;
};
/**
* Merges all the arguments into a new object.
*/
Arg.merge = function(){
var all = {};
for (var ai in arguments){
if(arguments.hasOwnProperty(ai)){
for (var k in arguments[ai]){
if(arguments[ai].hasOwnProperty(k)){
all[k] = arguments[ai][k];
}
}
}
}
return all;
};
return Arg;
};
/** @namespace
* Arg is the root namespace for all arg.js functionality.
*/
global.Arg = MakeArg();
})(window);