qs.js
3.18 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
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
export const formats = {
default: "RFC3986",
formatters: {
RFC1738(value) {
return String(value).replaceAll("%20", "+");
},
RFC3986: String,
},
RFC1738: "RFC1738",
RFC3986: "RFC3986",
};
function decodeValue(input) {
try {
return decodeURIComponent(String(input).replaceAll("+", " "));
} catch {
return String(input);
}
}
function encodeValue(input) {
return encodeURIComponent(String(input));
}
function isPlainObject(value) {
if (!value || typeof value !== "object") return false;
const proto = Object.getPrototypeOf(value);
return proto === Object.prototype || proto === null;
}
function addEntry(entries, key, value, options) {
if (value === undefined) return;
if (options.skipNulls && value === null) return;
const keyOut = options.encodeValuesOnly ? key : encodeValue(key);
if (value === null && options.strictNullHandling) {
entries.push(keyOut);
return;
}
const valueOut = encodeValue(value === null ? "" : value);
entries.push(`${keyOut}=${valueOut}`);
}
function buildEntries(entries, value, key, options) {
if (value === undefined) return;
if (value instanceof Date) {
addEntry(entries, key, options.serializeDate(value), options);
return;
}
if (Array.isArray(value)) {
value.forEach((v, idx) => {
let nextKey = key;
if (options.arrayFormat === "indices") nextKey = `${key}[${idx}]`;
else if (options.arrayFormat === "brackets") nextKey = `${key}[]`;
buildEntries(entries, v, nextKey, options);
});
return;
}
if (isPlainObject(value)) {
for (const [k, v] of Object.entries(value)) {
const nextKey = options.allowDots ? `${key}.${k}` : `${key}[${k}]`;
buildEntries(entries, v, nextKey, options);
}
return;
}
addEntry(entries, key, value, options);
}
export function stringify(object, opts = {}) {
const options = {
delimiter: "&",
allowDots: false,
encodeValuesOnly: false,
arrayFormat: "indices",
addQueryPrefix: false,
skipNulls: false,
strictNullHandling: false,
serializeDate: (d) => d.toISOString(),
...opts,
};
if (!object || typeof object !== "object") return "";
const entries = [];
const keys = Object.keys(object);
if (typeof options.sort === "function") keys.sort(options.sort);
for (const key of keys) {
buildEntries(entries, object[key], key, options);
}
const joined = entries.join(options.delimiter);
if (!joined) return "";
return options.addQueryPrefix ? `?${joined}` : joined;
}
export function parse(str, opts = {}) {
const options = {
ignoreQueryPrefix: false,
delimiter: "&",
...opts,
};
const input = options.ignoreQueryPrefix ? String(str).replace(/^\?/, "") : String(str);
if (!input) return {};
const out = {};
const parts = input.split(options.delimiter);
for (const part of parts) {
if (!part) continue;
const idx = part.indexOf("=");
const rawKey = idx === -1 ? part : part.slice(0, idx);
const rawValue = idx === -1 ? "" : part.slice(idx + 1);
const key = decodeValue(rawKey);
const value = decodeValue(rawValue);
if (Object.hasOwn(out, key)) {
const prev = out[key];
out[key] = Array.isArray(prev) ? [...prev, value] : [prev, value];
} else {
out[key] = value;
}
}
return out;
}
const Qs = { formats, parse, stringify };
export default Qs;