-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.ts
More file actions
42 lines (35 loc) · 1.27 KB
/
query.ts
File metadata and controls
42 lines (35 loc) · 1.27 KB
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
const DEFAULT_SEP: string = '&';
const DEFAULT_EQ: string = '=';
import { StringDictionary } from "./types.ts";
type QueryDictionary = StringDictionary;
function parse(queryString: string, sep: string=DEFAULT_SEP, eq: string=DEFAULT_EQ): QueryDictionary {
const pairs = queryString.replace(/^\?/, '').split(sep);
const r:QueryDictionary = {};
pairs.forEach((pair) => {
const [key, value=''] = pair.split(eq);
if (key) {
if (!r[key]) {
r[key] = decodeURIComponent(value);
} else {
const arr: Array<string> = [];
r[key] = arr.concat(r[key], decodeURIComponent(value));
}
}
});
return r;
};
function stringify (obj: object, sep: string=DEFAULT_SEP, eq: string=DEFAULT_EQ): string {
let pairs = [];
for(let [key, value] of Object.entries(obj)) {
if (typeof value === 'object' && value.length) {
pairs.push(toValuesString(key, value, sep));
} else {
pairs.push(`${key}${eq}${encodeURIComponent(value.toString())}`);
}
}
return pairs.join(sep);
};
function toValuesString(key: string, values: Array<string|number>, sep: string=DEFAULT_SEP, eq: string=DEFAULT_EQ): string {
return values.map(value => `${key}${eq}${encodeURIComponent(value.toString())}`).join(sep);
}
export default { parse, stringify };