-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiny-query-string.js
137 lines (122 loc) · 3.79 KB
/
tiny-query-string.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
// TinyQueryString - A really tiny URL query string utility
// author : Richard Westenra
// license : MIT
// github.com/richardwestenra/tiny-query-string
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['tiny-query-string'], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
root['tiny-query-string'] = factory();
}
}(this, function () {
'use strict';
var getRegex = function(name) {
return new RegExp('[\?&](' + name + ')=?([^&#]*)', 'i');
};
var setDefault = function(text) {
return typeof text !== 'string' ? (
typeof window !== 'undefined' ? window.location.search : ''
) : text;
};
return {
get: function(arg1, arg2) {
if (typeof arg1 === 'object') {
return this.getMany.apply(this, arguments);
} else if (!arg1 || arg2 === false) {
return this.getAll.apply(this, arguments);
} else {
return this.getOne.apply(this, arguments);
}
},
getOne: function(name, text) {
var match = setDefault(text).match( getRegex(name) );
if (!match) {
return false;
} else if (match[2]) {
return decodeURIComponent(match[2]);
} else {
return true;
}
},
getMany: function(arr, text) {
return arr.reduce(function(obj, key) {
obj[key] = this.getOne(key, setDefault(text));
return obj;
}.bind(this), {});
},
getAll: function(text) {
text = setDefault(text).match(/\?(.+)/);
if (!text) {
return {};
} else {
var keys = text[1].split('&').map(function(pair) {
return pair.split('=')[0].toLowerCase();
});
return this.getMany.call(this, keys, text[0]);
}
},
set: function(arg) {
return (typeof arg === 'object') ?
this.setMany.apply(this, arguments) :
this.setOne.apply(this, arguments);
},
setOne: function(name, value, text) {
text = setDefault(text);
var regex = getRegex(name),
match = regex.exec(text);
if (value && (typeof value === 'string' || typeof value === 'number')) {
name = name + '=' + encodeURIComponent(value);
}
if (!text.length || text.indexOf('?') < 0) {
return (text || '') + '?' + name;
} else if (match) {
return text.replace(regex, match[0].charAt(0) + name);
} else {
return text + '&' + name;
}
},
setMany: function(values, text) {
if (Array.isArray(values)) {
return values.reduce(function(txt, d) {
return this.setOne(d, false, txt);
}.bind(this), setDefault(text));
}
return Object.keys(values).reduce(function(txt, key) {
return this.setOne(key, values[key], txt);
}.bind(this), setDefault(text));
},
remove: function(arg1, arg2) {
if (typeof arg1 === 'object') {
return this.removeMany.apply(this, arguments);
} else if (!arg1 || arg2 === false) {
return this.removeAll.apply(this, arguments);
} else {
return this.removeOne.apply(this, arguments);
}
},
removeOne: function(name, text) {
text = setDefault(text).match(/([^\?]*)(\?*.*)/);
if (!text) {
return false;
} else if (!text[2].length) {
return text[1];
} else {
var remainingKeys = Object.keys(this.getAll(text[2]))
.filter(function(key){
return key !== name;
});
return this.setMany(remainingKeys, text[1]);
}
},
removeMany: function(arr, text) {
return arr.reduce(function(txt, d) {
return this.removeOne(d, txt);
}.bind(this), setDefault(text));
},
removeAll: function(text) {
return setDefault(text).split('?')[0];
}
};
}));