-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
174 lines (156 loc) · 4.72 KB
/
index.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
/**
* This is port of the library jerrybendy/url-search-params-polyfill
* It has been converted into a JavaScript class.
*
* @author Eric Richer <[email protected]>
* @licence MIT
*/
export class URLSearchParams {
/**
*
* @param {string|null} search
*/
constructor(search=null) {
search = search || "";
if (search instanceof URLSearchParams) {
search = search.toString();
}
this.dict = this.parseToDict(search);
}
dict = {};
/**
* Appends a specified key/value pair as a new search parameter
* @param {string} name
* @param {string} value
*/
append( name, value) {
this.appendTo(this.dict, name, value);
};
/**
* Deletes this give search parameter, and its associated value, from the list of all search parameters
* @param {string} name
*/
delete(name) {
delete this.dict[name];
};
/**
* Returns the first value associated to the given search parameter
* @param {string} name
* @return {string|null}
*/
get(name) {
return name in this.dict ? this.dict[name][0] : null;
};
/**
* Returns all the values association with a given parameter
* @param {string} name
* @return {Array}
*/
getAll(name) {
return name in this.dict ? this.dict[name].slice(0) : []
};
/**
* Test if the search parameter exists
* @param {string} name
* @return {boolean}
*/
has(name) {
return name in this.dict;
};
/**
* Sets the value associated to a given search parameter to
* the given value. If there were several values, delete the others.
* @param {string} name
* @param {string} value
*/
set(name, value) {
this.dict[name] = [''+value];
};
/**
* Returns a string containing a query string suitable for use in a URL
* @return {string}
*/
toString() {
const dict = this.dict,
query = [];
let key, name, i, value;
for (key in dict) {
name = this.encode(key);
for (i=0, value = dict[key]; i<value.length; i++) {
query.push(name+'='+this.encode(value[i]));
}
}
return query.join('&');
};
/**
*
* @param {string|object|array} search
*/
parseToDict(search) {
const dict = {};
if (typeof search === 'object') {
// if 'search' is an array, treat it as a sequence
if (Array.isArray(search)) {
for (let i=0; i<search.length; i++) {
const item = search[i];
if (Array.isArray(item) && item.length === 2) {
this.appendTo(dict, item[0], item[1]);
} else {
throw new TypeError("Failed to construct 'URLSearchParams': Sequence initalizer must only contain pair elements");
}
}
} else {
for (let key in search) {
if (search.hasOwnProperty(key)) {
this.appendTo(dict, key, search[key]);
}
}
}
} else {
// remove 1st ?
if (search.indexOf('?') === 0) {
search = search.slice(1);
}
const pairs = search.split("&");
for (let j=0; j<pairs.length; j++) {
const value = pairs[j],
index = value.indexOf('=');
if (-1 <index) {
this.appendTo(dict, this.decode(value.slice(0, index)), this.decode(value.slice(index+1)));
} else {
if (value) {
this.appendTo(dict, this.decode(value), '');
}
}
}
}
return dict;
}
appendTo(dict, name, value) {
const val = typeof value === 'string' ? value: (
value !== null && value !== undefined && typeof value.toString === 'function' ? value.toString() : JSON.stringify(value)
);
if (name in dict) {
dict[name].push(value);
} else {
dict[name] = [val];
}
}
decode(str) {
return str
.replace(/[ +]/g, '%20')
.replace(/(%[a-f0-9]{2})+/ig, (match) => {return decodeURIComponent(match)})
}
encode(str) {
const replace = {
'!': '%21',
"'": '%27',
'(': '%28',
')': '%29',
'~': '%7E',
'%20': '+',
'%00': '\x00'
};
return encodeURIComponent(str).replace(/[!'\(\)~]|%20|%00/g, match => replace[match]);
}
}