@deot/helper-route 1.1.8 → 1.1.9
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/dist/index.cjs +55 -66
- package/dist/index.iife.js +101 -105
- package/dist/index.js +54 -62
- package/dist/index.umd.cjs +103 -107
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -1,72 +1,61 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
if (value === null) return null;
|
|
17
|
-
value = helperUtils.flatten(value);
|
|
18
|
-
return typeof options$.parse === "function" ? options$.parse(value) : value;
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _deot_helper_utils = require("@deot/helper-utils");
|
|
3
|
+
//#region packages/route/src/get.ts
|
|
4
|
+
var get = (key, url, options) => {
|
|
5
|
+
const options$ = {
|
|
6
|
+
parse: _deot_helper_utils.flattenJSONParse,
|
|
7
|
+
...typeof url === "object" ? url : { url },
|
|
8
|
+
...options
|
|
9
|
+
};
|
|
10
|
+
const url$ = options$.url || (typeof window === "undefined" ? "" : window.location.search);
|
|
11
|
+
const match = url$.substring(url$.indexOf("?") + 1).match(new RegExp("(^|&)" + key + "=([^&]*)"));
|
|
12
|
+
let value = match != null ? match[2] : null;
|
|
13
|
+
if (value === null) return null;
|
|
14
|
+
value = (0, _deot_helper_utils.flatten)(value);
|
|
15
|
+
return typeof options$.parse === "function" ? options$.parse(value) : value;
|
|
19
16
|
};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
queryArr = [
|
|
34
|
-
...queryArr,
|
|
35
|
-
`${key}=${encodeURIComponent(v)}`
|
|
36
|
-
];
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
if (queryArr.length > 0) {
|
|
40
|
-
result += (result.indexOf("?") > -1 ? "&" : "?") + queryArr.join("&");
|
|
41
|
-
}
|
|
42
|
-
return result;
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region packages/route/src/merge.ts
|
|
19
|
+
var merge = (route) => {
|
|
20
|
+
const { origin = "", path = "", query } = route;
|
|
21
|
+
let result = origin;
|
|
22
|
+
result += path instanceof Array ? path.length ? `/${path.join("/")}` : "" : path;
|
|
23
|
+
let queryArr = [];
|
|
24
|
+
for (const key in query) if (query[key] || query[key] === false || query[key] === 0) {
|
|
25
|
+
const v = (0, _deot_helper_utils.flatten)(query[key]);
|
|
26
|
+
queryArr = [...queryArr, `${key}=${encodeURIComponent(v)}`];
|
|
27
|
+
}
|
|
28
|
+
if (queryArr.length > 0) result += (result.indexOf("?") > -1 ? "&" : "?") + queryArr.join("&");
|
|
29
|
+
return result;
|
|
43
30
|
};
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region packages/route/src/parse.ts
|
|
33
|
+
/**
|
|
34
|
+
* https://www.30secondsofcode.org/js/s/get-url-parameters/
|
|
35
|
+
* https://www.30secondsofcode.org/js/s/query-string-to-object/
|
|
36
|
+
*/
|
|
37
|
+
var parse = (url, options) => {
|
|
38
|
+
const options$ = {
|
|
39
|
+
parse: _deot_helper_utils.flattenJSONParse,
|
|
40
|
+
...typeof url === "object" ? url : { url },
|
|
41
|
+
...options
|
|
42
|
+
};
|
|
43
|
+
const [prefix, search] = (options$.url || (typeof window === "undefined" ? "" : `${window.location.pathname}${window.location.search}`)).split("?");
|
|
44
|
+
const query = {};
|
|
45
|
+
if (search) Array.from(search.match(/[^?=&]+=[^&]*/g) || []).forEach((v) => {
|
|
46
|
+
const [key, value] = v.split("=");
|
|
47
|
+
const value$ = (0, _deot_helper_utils.flatten)(value);
|
|
48
|
+
query[key] = typeof options$.parse === "function" ? options$.parse(value$) : value$;
|
|
49
|
+
});
|
|
50
|
+
const pIndex = prefix.indexOf("://");
|
|
51
|
+
const sIndex = prefix.indexOf("/", pIndex === -1 ? 0 : pIndex + 3);
|
|
52
|
+
return {
|
|
53
|
+
origin: (sIndex !== -1 ? prefix.slice(0, sIndex) : prefix) || "",
|
|
54
|
+
path: sIndex !== -1 ? prefix.slice(sIndex) : "",
|
|
55
|
+
query
|
|
56
|
+
};
|
|
68
57
|
};
|
|
69
|
-
|
|
58
|
+
//#endregion
|
|
70
59
|
exports.get = get;
|
|
71
60
|
exports.merge = merge;
|
|
72
61
|
exports.parse = parse;
|
package/dist/index.iife.js
CHANGED
|
@@ -1,106 +1,102 @@
|
|
|
1
|
-
var HelperRoute = (function
|
|
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
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
103
|
-
|
|
104
|
-
return exports;
|
|
105
|
-
|
|
1
|
+
var HelperRoute = (function(exports) {
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
+/* @__PURE__ */ new Date();
|
|
4
|
+
typeof window !== "undefined" && window.requestAnimationFrame;
|
|
5
|
+
typeof window !== "undefined" && window.cancelAnimationFrame;
|
|
6
|
+
typeof window !== "undefined" && window.requestIdleCallback;
|
|
7
|
+
typeof window !== "undefined" && window.cancelIdleCallback;
|
|
8
|
+
var flatten = (value, parser, exit) => {
|
|
9
|
+
let need = true;
|
|
10
|
+
let safeCount = 1;
|
|
11
|
+
let parseValue = value;
|
|
12
|
+
while (need) {
|
|
13
|
+
if (safeCount > 1e3) throw new Error(value);
|
|
14
|
+
try {
|
|
15
|
+
const next = (parser || decodeURIComponent)(parseValue);
|
|
16
|
+
if (parseValue === next || typeof exit === "function" && exit(next)) need = false;
|
|
17
|
+
parseValue = next;
|
|
18
|
+
} catch (_) {
|
|
19
|
+
need = false;
|
|
20
|
+
}
|
|
21
|
+
safeCount++;
|
|
22
|
+
}
|
|
23
|
+
return parseValue;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* 格式转换
|
|
27
|
+
* '1' -> 1
|
|
28
|
+
* '{}' -> {}
|
|
29
|
+
* '9007199254740992' -> '9007199254740992'
|
|
30
|
+
* '9007199254740991' -> 9007199254740991
|
|
31
|
+
* @param value 输入
|
|
32
|
+
* @returns ~
|
|
33
|
+
*/
|
|
34
|
+
var flattenJSONParse = (value) => {
|
|
35
|
+
if (value === null) return null;
|
|
36
|
+
if (/^\d+$/.test(value) && value.length >= 16 && +value > Number.MAX_SAFE_INTEGER) return value;
|
|
37
|
+
return flatten(value, JSON.parse, (v) => typeof v === "object");
|
|
38
|
+
};
|
|
39
|
+
var ALPHA = "abcdefghijklmnopqrstuvwxyz";
|
|
40
|
+
ALPHA + ALPHA.toUpperCase() + "";
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region packages/route/src/get.ts
|
|
43
|
+
var get = (key, url, options) => {
|
|
44
|
+
const options$ = {
|
|
45
|
+
parse: flattenJSONParse,
|
|
46
|
+
...typeof url === "object" ? url : { url },
|
|
47
|
+
...options
|
|
48
|
+
};
|
|
49
|
+
const url$ = options$.url || (typeof window === "undefined" ? "" : window.location.search);
|
|
50
|
+
const match = url$.substring(url$.indexOf("?") + 1).match(new RegExp("(^|&)" + key + "=([^&]*)"));
|
|
51
|
+
let value = match != null ? match[2] : null;
|
|
52
|
+
if (value === null) return null;
|
|
53
|
+
value = flatten(value);
|
|
54
|
+
return typeof options$.parse === "function" ? options$.parse(value) : value;
|
|
55
|
+
};
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region packages/route/src/merge.ts
|
|
58
|
+
var merge = (route) => {
|
|
59
|
+
const { origin = "", path = "", query } = route;
|
|
60
|
+
let result = origin;
|
|
61
|
+
result += path instanceof Array ? path.length ? `/${path.join("/")}` : "" : path;
|
|
62
|
+
let queryArr = [];
|
|
63
|
+
for (const key in query) if (query[key] || query[key] === false || query[key] === 0) {
|
|
64
|
+
const v = flatten(query[key]);
|
|
65
|
+
queryArr = [...queryArr, `${key}=${encodeURIComponent(v)}`];
|
|
66
|
+
}
|
|
67
|
+
if (queryArr.length > 0) result += (result.indexOf("?") > -1 ? "&" : "?") + queryArr.join("&");
|
|
68
|
+
return result;
|
|
69
|
+
};
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region packages/route/src/parse.ts
|
|
72
|
+
/**
|
|
73
|
+
* https://www.30secondsofcode.org/js/s/get-url-parameters/
|
|
74
|
+
* https://www.30secondsofcode.org/js/s/query-string-to-object/
|
|
75
|
+
*/
|
|
76
|
+
var parse = (url, options) => {
|
|
77
|
+
const options$ = {
|
|
78
|
+
parse: flattenJSONParse,
|
|
79
|
+
...typeof url === "object" ? url : { url },
|
|
80
|
+
...options
|
|
81
|
+
};
|
|
82
|
+
const [prefix, search] = (options$.url || (typeof window === "undefined" ? "" : `${window.location.pathname}${window.location.search}`)).split("?");
|
|
83
|
+
const query = {};
|
|
84
|
+
if (search) Array.from(search.match(/[^?=&]+=[^&]*/g) || []).forEach((v) => {
|
|
85
|
+
const [key, value] = v.split("=");
|
|
86
|
+
const value$ = flatten(value);
|
|
87
|
+
query[key] = typeof options$.parse === "function" ? options$.parse(value$) : value$;
|
|
88
|
+
});
|
|
89
|
+
const pIndex = prefix.indexOf("://");
|
|
90
|
+
const sIndex = prefix.indexOf("/", pIndex === -1 ? 0 : pIndex + 3);
|
|
91
|
+
return {
|
|
92
|
+
origin: (sIndex !== -1 ? prefix.slice(0, sIndex) : prefix) || "",
|
|
93
|
+
path: sIndex !== -1 ? prefix.slice(sIndex) : "",
|
|
94
|
+
query
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
//#endregion
|
|
98
|
+
exports.get = get;
|
|
99
|
+
exports.merge = merge;
|
|
100
|
+
exports.parse = parse;
|
|
101
|
+
return exports;
|
|
106
102
|
})({});
|
package/dist/index.js
CHANGED
|
@@ -1,66 +1,58 @@
|
|
|
1
|
-
import { flatten, flattenJSONParse } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import { flatten, flattenJSONParse } from "@deot/helper-utils";
|
|
2
|
+
//#region packages/route/src/get.ts
|
|
3
|
+
var get = (key, url, options) => {
|
|
4
|
+
const options$ = {
|
|
5
|
+
parse: flattenJSONParse,
|
|
6
|
+
...typeof url === "object" ? url : { url },
|
|
7
|
+
...options
|
|
8
|
+
};
|
|
9
|
+
const url$ = options$.url || (typeof window === "undefined" ? "" : window.location.search);
|
|
10
|
+
const match = url$.substring(url$.indexOf("?") + 1).match(new RegExp("(^|&)" + key + "=([^&]*)"));
|
|
11
|
+
let value = match != null ? match[2] : null;
|
|
12
|
+
if (value === null) return null;
|
|
13
|
+
value = flatten(value);
|
|
14
|
+
return typeof options$.parse === "function" ? options$.parse(value) : value;
|
|
15
15
|
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
queryArr = [
|
|
30
|
-
...queryArr,
|
|
31
|
-
`${key}=${encodeURIComponent(v)}`
|
|
32
|
-
];
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
if (queryArr.length > 0) {
|
|
36
|
-
result += (result.indexOf("?") > -1 ? "&" : "?") + queryArr.join("&");
|
|
37
|
-
}
|
|
38
|
-
return result;
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region packages/route/src/merge.ts
|
|
18
|
+
var merge = (route) => {
|
|
19
|
+
const { origin = "", path = "", query } = route;
|
|
20
|
+
let result = origin;
|
|
21
|
+
result += path instanceof Array ? path.length ? `/${path.join("/")}` : "" : path;
|
|
22
|
+
let queryArr = [];
|
|
23
|
+
for (const key in query) if (query[key] || query[key] === false || query[key] === 0) {
|
|
24
|
+
const v = flatten(query[key]);
|
|
25
|
+
queryArr = [...queryArr, `${key}=${encodeURIComponent(v)}`];
|
|
26
|
+
}
|
|
27
|
+
if (queryArr.length > 0) result += (result.indexOf("?") > -1 ? "&" : "?") + queryArr.join("&");
|
|
28
|
+
return result;
|
|
39
29
|
};
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region packages/route/src/parse.ts
|
|
32
|
+
/**
|
|
33
|
+
* https://www.30secondsofcode.org/js/s/get-url-parameters/
|
|
34
|
+
* https://www.30secondsofcode.org/js/s/query-string-to-object/
|
|
35
|
+
*/
|
|
36
|
+
var parse = (url, options) => {
|
|
37
|
+
const options$ = {
|
|
38
|
+
parse: flattenJSONParse,
|
|
39
|
+
...typeof url === "object" ? url : { url },
|
|
40
|
+
...options
|
|
41
|
+
};
|
|
42
|
+
const [prefix, search] = (options$.url || (typeof window === "undefined" ? "" : `${window.location.pathname}${window.location.search}`)).split("?");
|
|
43
|
+
const query = {};
|
|
44
|
+
if (search) Array.from(search.match(/[^?=&]+=[^&]*/g) || []).forEach((v) => {
|
|
45
|
+
const [key, value] = v.split("=");
|
|
46
|
+
const value$ = flatten(value);
|
|
47
|
+
query[key] = typeof options$.parse === "function" ? options$.parse(value$) : value$;
|
|
48
|
+
});
|
|
49
|
+
const pIndex = prefix.indexOf("://");
|
|
50
|
+
const sIndex = prefix.indexOf("/", pIndex === -1 ? 0 : pIndex + 3);
|
|
51
|
+
return {
|
|
52
|
+
origin: (sIndex !== -1 ? prefix.slice(0, sIndex) : prefix) || "",
|
|
53
|
+
path: sIndex !== -1 ? prefix.slice(sIndex) : "",
|
|
54
|
+
query
|
|
55
|
+
};
|
|
64
56
|
};
|
|
65
|
-
|
|
57
|
+
//#endregion
|
|
66
58
|
export { get, merge, parse };
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1,107 +1,103 @@
|
|
|
1
|
-
(function
|
|
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
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
106
|
-
|
|
107
|
-
}));
|
|
1
|
+
(function(global, factory) {
|
|
2
|
+
typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define(["exports"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.HelperRoute = {}));
|
|
3
|
+
})(this, function(exports) {
|
|
4
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
5
|
+
+/* @__PURE__ */ new Date();
|
|
6
|
+
typeof window !== "undefined" && window.requestAnimationFrame;
|
|
7
|
+
typeof window !== "undefined" && window.cancelAnimationFrame;
|
|
8
|
+
typeof window !== "undefined" && window.requestIdleCallback;
|
|
9
|
+
typeof window !== "undefined" && window.cancelIdleCallback;
|
|
10
|
+
var flatten = (value, parser, exit) => {
|
|
11
|
+
let need = true;
|
|
12
|
+
let safeCount = 1;
|
|
13
|
+
let parseValue = value;
|
|
14
|
+
while (need) {
|
|
15
|
+
if (safeCount > 1e3) throw new Error(value);
|
|
16
|
+
try {
|
|
17
|
+
const next = (parser || decodeURIComponent)(parseValue);
|
|
18
|
+
if (parseValue === next || typeof exit === "function" && exit(next)) need = false;
|
|
19
|
+
parseValue = next;
|
|
20
|
+
} catch (_) {
|
|
21
|
+
need = false;
|
|
22
|
+
}
|
|
23
|
+
safeCount++;
|
|
24
|
+
}
|
|
25
|
+
return parseValue;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* 格式转换
|
|
29
|
+
* '1' -> 1
|
|
30
|
+
* '{}' -> {}
|
|
31
|
+
* '9007199254740992' -> '9007199254740992'
|
|
32
|
+
* '9007199254740991' -> 9007199254740991
|
|
33
|
+
* @param value 输入
|
|
34
|
+
* @returns ~
|
|
35
|
+
*/
|
|
36
|
+
var flattenJSONParse = (value) => {
|
|
37
|
+
if (value === null) return null;
|
|
38
|
+
if (/^\d+$/.test(value) && value.length >= 16 && +value > Number.MAX_SAFE_INTEGER) return value;
|
|
39
|
+
return flatten(value, JSON.parse, (v) => typeof v === "object");
|
|
40
|
+
};
|
|
41
|
+
var ALPHA = "abcdefghijklmnopqrstuvwxyz";
|
|
42
|
+
ALPHA + ALPHA.toUpperCase() + "";
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region packages/route/src/get.ts
|
|
45
|
+
var get = (key, url, options) => {
|
|
46
|
+
const options$ = {
|
|
47
|
+
parse: flattenJSONParse,
|
|
48
|
+
...typeof url === "object" ? url : { url },
|
|
49
|
+
...options
|
|
50
|
+
};
|
|
51
|
+
const url$ = options$.url || (typeof window === "undefined" ? "" : window.location.search);
|
|
52
|
+
const match = url$.substring(url$.indexOf("?") + 1).match(new RegExp("(^|&)" + key + "=([^&]*)"));
|
|
53
|
+
let value = match != null ? match[2] : null;
|
|
54
|
+
if (value === null) return null;
|
|
55
|
+
value = flatten(value);
|
|
56
|
+
return typeof options$.parse === "function" ? options$.parse(value) : value;
|
|
57
|
+
};
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region packages/route/src/merge.ts
|
|
60
|
+
var merge = (route) => {
|
|
61
|
+
const { origin = "", path = "", query } = route;
|
|
62
|
+
let result = origin;
|
|
63
|
+
result += path instanceof Array ? path.length ? `/${path.join("/")}` : "" : path;
|
|
64
|
+
let queryArr = [];
|
|
65
|
+
for (const key in query) if (query[key] || query[key] === false || query[key] === 0) {
|
|
66
|
+
const v = flatten(query[key]);
|
|
67
|
+
queryArr = [...queryArr, `${key}=${encodeURIComponent(v)}`];
|
|
68
|
+
}
|
|
69
|
+
if (queryArr.length > 0) result += (result.indexOf("?") > -1 ? "&" : "?") + queryArr.join("&");
|
|
70
|
+
return result;
|
|
71
|
+
};
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region packages/route/src/parse.ts
|
|
74
|
+
/**
|
|
75
|
+
* https://www.30secondsofcode.org/js/s/get-url-parameters/
|
|
76
|
+
* https://www.30secondsofcode.org/js/s/query-string-to-object/
|
|
77
|
+
*/
|
|
78
|
+
var parse = (url, options) => {
|
|
79
|
+
const options$ = {
|
|
80
|
+
parse: flattenJSONParse,
|
|
81
|
+
...typeof url === "object" ? url : { url },
|
|
82
|
+
...options
|
|
83
|
+
};
|
|
84
|
+
const [prefix, search] = (options$.url || (typeof window === "undefined" ? "" : `${window.location.pathname}${window.location.search}`)).split("?");
|
|
85
|
+
const query = {};
|
|
86
|
+
if (search) Array.from(search.match(/[^?=&]+=[^&]*/g) || []).forEach((v) => {
|
|
87
|
+
const [key, value] = v.split("=");
|
|
88
|
+
const value$ = flatten(value);
|
|
89
|
+
query[key] = typeof options$.parse === "function" ? options$.parse(value$) : value$;
|
|
90
|
+
});
|
|
91
|
+
const pIndex = prefix.indexOf("://");
|
|
92
|
+
const sIndex = prefix.indexOf("/", pIndex === -1 ? 0 : pIndex + 3);
|
|
93
|
+
return {
|
|
94
|
+
origin: (sIndex !== -1 ? prefix.slice(0, sIndex) : prefix) || "",
|
|
95
|
+
path: sIndex !== -1 ? prefix.slice(sIndex) : "",
|
|
96
|
+
query
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
//#endregion
|
|
100
|
+
exports.get = get;
|
|
101
|
+
exports.merge = merge;
|
|
102
|
+
exports.parse = parse;
|
|
103
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deot/helper-route",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -19,6 +19,6 @@
|
|
|
19
19
|
"access": "public"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@deot/helper-utils": "^1.1.
|
|
22
|
+
"@deot/helper-utils": "^1.1.8"
|
|
23
23
|
}
|
|
24
24
|
}
|