Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

qss.ts

53 lines1.1 KB

1// @ts-nocheck
2​
3// We're inlining qss here for compression's sake, but we've included it as a hard dependency for the MIT license it requires.
4​
5export function encode(obj, pfx?: string) {
6 var k,
7 i,
8 tmp,
9 str = ''
10​
11 for (k in obj) {
12 if ((tmp = obj[k]) !== void 0) {
13 if (Array.isArray(tmp)) {
14 for (i = 0; i < tmp.length; i++) {
15 str && (str += '&')
16 str += encodeURIComponent(k) + '=' + encodeURIComponent(tmp[i])
17 }
18 } else {
19 str && (str += '&')
20 str += encodeURIComponent(k) + '=' + encodeURIComponent(tmp)
21 }
22 }
23 }
24​
25 return (pfx || '') + str
27​
28function toValue(mix) {
29 if (!mix) return ''
30 var str = decodeURIComponent(mix)
31 if (str === 'false') return false
32 if (str === 'true') return true
33 return +str * 0 === 0 ? +str : str
35​
36export function decode(str) {
37 var tmp,
38 k,
39 out = {},
40 arr = str.split('&')
41​
42 while ((tmp = arr.shift())) {
43 tmp = tmp.split('=')
44 k = tmp.shift()
45 if (out[k] !== void 0) {
46 out[k] = [].concat(out[k], toValue(tmp.shift()))
47 } else {
48 out[k] = toValue(tmp.shift())
49 }
50 }
51​
52 return out