@base-web-kits/base-tools-web 0.9.14 → 0.9.99

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/package.json CHANGED
@@ -1,32 +1,32 @@
1
- {
2
- "name": "@base-web-kits/base-tools-web",
3
- "version": "0.9.14",
4
- "sideEffects": false,
5
- "description": "Independent Web utilities package built from src/web.",
6
- "keywords": [
7
- "base-tools",
8
- "web",
9
- "utilities",
10
- "thin-wrapper"
11
- ],
12
- "license": "MIT",
13
- "main": "./dist/index.cjs",
14
- "module": "./dist/index.js",
15
- "types": "./dist/index.d.ts",
16
- "exports": {
17
- ".": {
18
- "types": "./dist/index.d.ts",
19
- "import": "./dist/index.js",
20
- "require": "./dist/index.cjs"
21
- }
22
- },
23
- "files": [
24
- "dist",
25
- "README.md",
26
- "src"
27
- ],
28
- "dependencies": {},
29
- "publishConfig": {
30
- "registry": "https://registry.npmjs.org"
31
- }
32
- }
1
+ {
2
+ "name": "@base-web-kits/base-tools-web",
3
+ "version": "0.9.99",
4
+ "sideEffects": false,
5
+ "description": "Independent Web utilities package built from src/web.",
6
+ "keywords": [
7
+ "base-tools",
8
+ "web",
9
+ "utilities",
10
+ "thin-wrapper"
11
+ ],
12
+ "license": "MIT",
13
+ "main": "./dist/index.cjs",
14
+ "module": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js",
20
+ "require": "./dist/index.cjs"
21
+ }
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "README.md",
26
+ "src"
27
+ ],
28
+ "dependencies": {},
29
+ "publishConfig": {
30
+ "registry": "https://registry.npmjs.org"
31
+ }
32
+ }
@@ -1,52 +1,64 @@
1
- import { getUrlNumber, getUrlParam, getUrlParamAll } from '../../ts';
2
-
3
1
  /**
4
2
  * 获取url的查询参数值
5
- * - 与 {@link getUrlParam} 相同,只是参数url可选,默认取当前地址
6
3
  * @param key 参数名
7
4
  * @param url 完整 URL 或仅查询串(如 "a=1&b=2")
8
5
  * @returns 解码后的参数值 (若不存在|"null"|"undefined",则返回 null)
9
6
  * @example
10
- * const q = getUrlQuery('q'); // 默认当前地址
11
- * const q = getUrlQuery('q', 'https://a.com/?q=%E6%B5%8B%E8%AF%95'); // "测试"
12
- * const a = getUrlQuery('a', 'a=1'); // "1"
13
- * const list = getUrlQuery('list', 'list=[1,2]'); // "[1,2]"
14
- * const list = getUrlQuery('list', 'list=null'); // null
15
- * const list = getUrlQuery('list', 'list=undefined'); // null
7
+ * const q = getUrlParam('q'); // 默认当前地址
8
+ * const q = getUrlParam('q', 'https://a.com/?q=%E6%B5%8B%E8%AF%95'); // "测试"
9
+ * const a = getUrlParam('a', 'a=1'); // "1"
10
+ * const list = getUrlParam('list', 'list=[1,2]'); // "[1,2]"
11
+ * const list = getUrlParam('list', 'list=null'); // null
12
+ * const list = getUrlParam('list', 'list=undefined'); // null
16
13
  */
17
- export function getUrlQuery(key: string, url = window.location.href) {
18
- return getUrlParam(key, url);
14
+ export function getUrlParam(key: string, url = window.location.href) {
15
+ const searchParams = new URL(url.includes('?') ? url : `?${url}`, 'http://localhost')
16
+ .searchParams;
17
+ const value = searchParams.get(key);
18
+ return value === 'null' || value === 'undefined' ? null : value;
19
19
  }
20
20
 
21
21
  /**
22
22
  * 获取url的查询参数值,并转为number类型
23
- * - 与 {@link getUrlNumber} 相同,只是参数url可选,默认取当前地址
24
23
  * @param key 参数名
25
24
  * @param url 完整 URL 或仅查询串(如 "a=1&b=2")
26
25
  * @returns 解码后的参数值 (若不存在|"非数字字符串",则返回 null)
27
26
  * @example
28
- * const a = getUrlQueryNumber('a'); // 默认当前地址
29
- * const a = getUrlQueryNumber('a', 'https://a.com/?a=1'); // 1
30
- * const a = getUrlQueryNumber('a', 'a=1'); // 1
31
- * const a = getUrlQueryNumber('a', 'a=1.2'); // 1.2
32
- * const a = getUrlQueryNumber('a', 'a=abc'); // null
27
+ * const a = getUrlNumber('a'); // 默认当前地址
28
+ * const a = getUrlNumber('a', 'https://a.com/?a=1'); // 1
29
+ * const a = getUrlNumber('a', 'a=1'); // 1
30
+ * const a = getUrlNumber('a', 'a=1.2'); // 1.2
31
+ * const a = getUrlNumber('a', 'a=abc'); // null
33
32
  */
34
- export function getUrlQueryNumber(key: string, url = window.location.href) {
35
- return getUrlNumber(key, url);
33
+ export function getUrlNumber(key: string, url = window.location.href) {
34
+ const str = getUrlParam(key, url);
35
+ if (!str) return null;
36
+
37
+ const num = Number(str);
38
+ return isNaN(num) ? null : num;
36
39
  }
37
40
 
38
41
  /**
39
42
  * 获取url的所有查询参数值
40
- * - 与 {@link getUrlParamAll} 相同,只是参数url可选,默认取当前地址
41
43
  * @param url 完整 URL 或仅查询串(如 "a=1&b=2")
42
44
  * @returns 解码后的键值对象(无参数返回空对象; "null"|"undefined"的参数会被忽略)
43
45
  * @example
44
- * const params = getUrlQueryAll(); // 默认当前地址
45
- * const params = getUrlQueryAll('a=1&b=2'); // { a: "1", b: "2" }
46
- * const params = getUrlQueryAll('https://a.com/?a=1&b=2'); // { a: "1", b: "2" }
47
- * const params = getUrlQueryAll('a=1&b=null'); // { a: "1" }
48
- * const params = getUrlQueryAll('a=1&b=undefined'); // { a: "1" }
46
+ * const params = getUrlParams(); // 默认当前地址
47
+ * const params = getUrlParams('a=1&b=2'); // { a: "1", b: "2" }
48
+ * const params = getUrlParams('https://a.com/?a=1&b=2'); // { a: "1", b: "2" }
49
+ * const params = getUrlParams('a=1&b=null'); // { a: "1" }
50
+ * const params = getUrlParams('a=1&b=undefined'); // { a: "1" }
49
51
  */
50
- export function getUrlQueryAll(url = window.location.href) {
51
- return getUrlParamAll(url);
52
+ export function getUrlParams(url = window.location.href) {
53
+ const searchParams = new URL(url.includes('?') ? url : `?${url}`, 'http://localhost')
54
+ .searchParams;
55
+ const result: Record<string, string> = {};
56
+
57
+ for (const [key, value] of searchParams.entries()) {
58
+ if (value !== 'null' && value !== 'undefined') {
59
+ result[key] = value;
60
+ }
61
+ }
62
+
63
+ return result;
52
64
  }