@nsnanocat/util 2.6.7 → 2.6.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -1
- package/package.json +1 -1
- package/polyfill/qs.mjs +23 -1
package/README.md
CHANGED
|
@@ -655,9 +655,11 @@ console.log(value); // 1
|
|
|
655
655
|
|
|
656
656
|
当前行为:
|
|
657
657
|
- 当 `query` 为 `string` 时:
|
|
658
|
+
- 支持前导 `?`(会先去掉)。
|
|
658
659
|
- 按 `&` / `=` 切分。
|
|
660
|
+
- key 与 value 都会先执行 URL 解码(`decodeURIComponent`,并将 `+` 视为空格)。
|
|
659
661
|
- 去掉值中的双引号。
|
|
660
|
-
-
|
|
662
|
+
- 支持点路径、数组下标路径,以及方括号 key(如 `a[b]`)展开对象。
|
|
661
663
|
- 当 `query` 为 `object` 时:
|
|
662
664
|
- 将 key 当路径写入新对象(`{"a.b":"1"}` -> `{ a: { b: "1" } }`)。
|
|
663
665
|
- 当 `query` 为 `null` 或 `undefined` 时:
|
|
@@ -669,6 +671,9 @@ import { qs } from "@nsnanocat/util";
|
|
|
669
671
|
console.log(qs.parse("mode=on&a.b=1"));
|
|
670
672
|
// { mode: "on", a: { b: "1" } }
|
|
671
673
|
|
|
674
|
+
console.log(qs.parse("a%5Bb%5D=c%20d"));
|
|
675
|
+
// { a: { b: "c d" } }
|
|
676
|
+
|
|
672
677
|
console.log(qs.parse({ "list[0]": "x", "list[1]": "y" }));
|
|
673
678
|
// { list: ["x", "y"] }
|
|
674
679
|
```
|
package/package.json
CHANGED
package/polyfill/qs.mjs
CHANGED
|
@@ -31,7 +31,18 @@ export class qs {
|
|
|
31
31
|
let result = {};
|
|
32
32
|
switch (typeof query) {
|
|
33
33
|
case "string": {
|
|
34
|
-
const
|
|
34
|
+
const source = query.replace(/^\?/, "");
|
|
35
|
+
if (!source) break;
|
|
36
|
+
const obj = Object.fromEntries(
|
|
37
|
+
source
|
|
38
|
+
.split("&")
|
|
39
|
+
.filter(Boolean)
|
|
40
|
+
.map(item => {
|
|
41
|
+
const [rawKey = "", rawValue = ""] = item.split("=", 2);
|
|
42
|
+
const key = qs.#decode(rawKey).replace(/\[([^\[\]]+)\]/g, ".$1");
|
|
43
|
+
return [key, qs.#decode(rawValue).replace(/\"/g, "")];
|
|
44
|
+
}),
|
|
45
|
+
);
|
|
35
46
|
Object.keys(obj).forEach(key => _.set(result, key, obj[key]));
|
|
36
47
|
break;
|
|
37
48
|
}
|
|
@@ -139,4 +150,15 @@ export class qs {
|
|
|
139
150
|
static #encode(value) {
|
|
140
151
|
return encodeURIComponent(value);
|
|
141
152
|
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* 解码查询字符串片段。
|
|
156
|
+
* Decode a query-string fragment.
|
|
157
|
+
*
|
|
158
|
+
* @param {string} value 编码值 / Encoded value.
|
|
159
|
+
* @returns {string}
|
|
160
|
+
*/
|
|
161
|
+
static #decode(value) {
|
|
162
|
+
return decodeURIComponent(value.replace(/\+/g, " "));
|
|
163
|
+
}
|
|
142
164
|
}
|