@polyv/utils-querystring 3.0.0
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/concat.d.ts +18 -0
- package/index.cjs +1 -0
- package/index.d.ts +9 -0
- package/index.js +70 -0
- package/interfaces.d.ts +31 -0
- package/internal/util.d.ts +34 -0
- package/package.json +21 -0
- package/parse.d.ts +16 -0
- package/replace.d.ts +16 -0
- package/stringify.d.ts +14 -0
package/concat.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { IQSStringifyOptions } from './interfaces';
|
|
2
|
+
/**
|
|
3
|
+
* 把指定数据序列化为查询字符串后,拼接到指定的 URL 或路径。
|
|
4
|
+
* @since 2.0.0
|
|
5
|
+
* @param url 指定的 URL 或路径,可以包含查询字符串。
|
|
6
|
+
* @param data 指定的数据。
|
|
7
|
+
* @param options 序列化选项。
|
|
8
|
+
* @returns 处理后的 URL 或路径。
|
|
9
|
+
* @example
|
|
10
|
+
* ```javascript
|
|
11
|
+
* import { concat } from '@polyv/utils-querystring';
|
|
12
|
+
* concat('https://abc.com/?a=1#hash', {
|
|
13
|
+
* b: 2,
|
|
14
|
+
* c: 3
|
|
15
|
+
* }); // 'https://abc.com/?a=1&b=2&c=3#hash'
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function concat(url: string, data?: any, options?: Readonly<IQSStringifyOptions>): string;
|
package/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@polyv/utils-object");function t(t,n){let r=Object.create(null);if(typeof t!=`string`)return r;let i=(0,e.assignProps)({decode:decodeURIComponent},n);return t.split(`&`).forEach(function(t){if(!t)return;let n=t.split(`=`),a=n[0],o=n[1]||``;if(typeof i.decode==`function`&&(a=i.decode(a),o=i.decode(o)),(0,e.hasOwnProp)(r,a)){let e=r[a],t=Array.isArray(e)?e:[e];t.push(o),r[a]=t}else r[a]=o}),r}function n(e,t,n,r){if(n??=``,!(n===``&&r.ignoreEmpty)){if(typeof r.encode==`function`){t=r.encode(t);let e=typeof n;n=[`boolean`,`string`,`number`].indexOf(e)===-1?``:r.encode(String(n))}e.push(t+`=`+n)}}function r(e,t,r,i){r.forEach(r=>{n(e,t,r,i)})}function i(t,i){let a=(0,e.assignProps)({encode:encodeURIComponent},i),o=[],s,c;for(s in t)(0,e.hasOwnProp)(t,s)&&(c=t[s],Array.isArray(c)?r(o,s,c,a):n(o,s,c,a));return o.join(`&`)}function a(e){let t={leftContext:``},n=e.indexOf(`#`);return n!==-1&&(t.hash=e.slice(n+1),e=e.slice(0,n)),n=e.indexOf(`?`),n!==-1&&(t.search=e.slice(n+1),e=e.slice(0,n)),t.leftContext=e,t}function o(e){let t=e.leftContext;return e.search!=null&&(t+=`?`+e.search),e.hash!=null&&(t+=`#`+e.hash),t}function s(e,t,n){if(e=String(e),t==null||(t=typeof t==`string`?t.replace(/^[?&]/,``):i(t,n),!t))return e;let r=a(e),s=r.search||``;return s&&s.slice(-1)!==`&`&&(s+=`&`),s+=t,r.search=s,o(r)}function c(n,r,s){let c=a(n);if(c.search==null)return n;let l=t(c.search,s);return Object.keys(r).forEach(t=>{(0,e.hasOwnProp)(l,t)&&(l[t]=r[t])}),c.search=i(l),o(c)}exports.concat=s,exports.parse=t,exports.replace=c,exports.stringify=i;
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 调用入口。
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
export type { IQSParseOptions, IQSReplaceOptions, IQSStringifyOptions, } from './interfaces';
|
|
6
|
+
export { parse } from './parse';
|
|
7
|
+
export { stringify } from './stringify';
|
|
8
|
+
export { concat } from './concat';
|
|
9
|
+
export { replace } from './replace';
|
package/index.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { assignProps as e, hasOwnProp as t } from "@polyv/utils-object";
|
|
2
|
+
//#region src/parse.ts
|
|
3
|
+
function n(n, r) {
|
|
4
|
+
let i = Object.create(null);
|
|
5
|
+
if (typeof n != "string") return i;
|
|
6
|
+
let a = e({ decode: decodeURIComponent }, r);
|
|
7
|
+
return n.split("&").forEach(function(e) {
|
|
8
|
+
if (!e) return;
|
|
9
|
+
let n = e.split("="), r = n[0], o = n[1] || "";
|
|
10
|
+
if (typeof a.decode == "function" && (r = a.decode(r), o = a.decode(o)), t(i, r)) {
|
|
11
|
+
let e = i[r], t = Array.isArray(e) ? e : [e];
|
|
12
|
+
t.push(o), i[r] = t;
|
|
13
|
+
} else i[r] = o;
|
|
14
|
+
}), i;
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/stringify.ts
|
|
18
|
+
function r(e, t, n, r) {
|
|
19
|
+
if (n ??= "", !(n === "" && r.ignoreEmpty)) {
|
|
20
|
+
if (typeof r.encode == "function") {
|
|
21
|
+
t = r.encode(t);
|
|
22
|
+
let e = typeof n;
|
|
23
|
+
n = [
|
|
24
|
+
"boolean",
|
|
25
|
+
"string",
|
|
26
|
+
"number"
|
|
27
|
+
].indexOf(e) === -1 ? "" : r.encode(String(n));
|
|
28
|
+
}
|
|
29
|
+
e.push(t + "=" + n);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function i(e, t, n, i) {
|
|
33
|
+
n.forEach((n) => {
|
|
34
|
+
r(e, t, n, i);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function a(n, a) {
|
|
38
|
+
let o = e({ encode: encodeURIComponent }, a), s = [], c, l;
|
|
39
|
+
for (c in n) t(n, c) && (l = n[c], Array.isArray(l) ? i(s, c, l, o) : r(s, c, l, o));
|
|
40
|
+
return s.join("&");
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/internal/util.ts
|
|
44
|
+
function o(e) {
|
|
45
|
+
let t = { leftContext: "" }, n = e.indexOf("#");
|
|
46
|
+
return n !== -1 && (t.hash = e.slice(n + 1), e = e.slice(0, n)), n = e.indexOf("?"), n !== -1 && (t.search = e.slice(n + 1), e = e.slice(0, n)), t.leftContext = e, t;
|
|
47
|
+
}
|
|
48
|
+
function s(e) {
|
|
49
|
+
let t = e.leftContext;
|
|
50
|
+
return e.search != null && (t += "?" + e.search), e.hash != null && (t += "#" + e.hash), t;
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/concat.ts
|
|
54
|
+
function c(e, t, n) {
|
|
55
|
+
if (e = String(e), t == null || (t = typeof t == "string" ? t.replace(/^[?&]/, "") : a(t, n), !t)) return e;
|
|
56
|
+
let r = o(e), i = r.search || "";
|
|
57
|
+
return i && i.slice(-1) !== "&" && (i += "&"), i += t, r.search = i, s(r);
|
|
58
|
+
}
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/replace.ts
|
|
61
|
+
function l(e, r, i) {
|
|
62
|
+
let c = o(e);
|
|
63
|
+
if (c.search == null) return e;
|
|
64
|
+
let l = n(c.search, i);
|
|
65
|
+
return Object.keys(r).forEach((e) => {
|
|
66
|
+
t(l, e) && (l[e] = r[e]);
|
|
67
|
+
}), c.search = a(l), s(c);
|
|
68
|
+
}
|
|
69
|
+
//#endregion
|
|
70
|
+
export { c as concat, n as parse, l as replace, a as stringify };
|
package/interfaces.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 接口。
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* 解析查询字符串的选项。
|
|
7
|
+
*/
|
|
8
|
+
export interface IQSParseOptions {
|
|
9
|
+
/**
|
|
10
|
+
* 键和值的解码函数,默认为 decodeURIComponent。
|
|
11
|
+
*/
|
|
12
|
+
decode?: (content: string) => string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* 序列化查询字符串的选项。
|
|
16
|
+
*/
|
|
17
|
+
export interface IQSStringifyOptions {
|
|
18
|
+
/**
|
|
19
|
+
* 键和值的编码函数,默认为 encodeURIComponent。
|
|
20
|
+
*/
|
|
21
|
+
encode?: (content: string) => string;
|
|
22
|
+
/**
|
|
23
|
+
* 序列化参数时是否忽略空值(null、undefined、空字符串)参数,默认为 false。
|
|
24
|
+
*/
|
|
25
|
+
ignoreEmpty?: boolean;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 替换查询字符串的选项。
|
|
29
|
+
*/
|
|
30
|
+
export interface IQSReplaceOptions extends IQSParseOptions, IQSStringifyOptions {
|
|
31
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 内部工具函数。
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* URL 切割结果。
|
|
8
|
+
*/
|
|
9
|
+
export interface IURLSplittingResult {
|
|
10
|
+
/**
|
|
11
|
+
* 切割后的左侧上下文,包括 URL 的协议、域名、路径。
|
|
12
|
+
*/
|
|
13
|
+
leftContext: string;
|
|
14
|
+
/**
|
|
15
|
+
* 查询参数。
|
|
16
|
+
*/
|
|
17
|
+
search?: string;
|
|
18
|
+
/**
|
|
19
|
+
* 锚点。
|
|
20
|
+
*/
|
|
21
|
+
hash?: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 切割 URL 或路径。
|
|
25
|
+
* @param url 要切割的 URL 或路径。
|
|
26
|
+
* @returns 切割结果。
|
|
27
|
+
*/
|
|
28
|
+
export declare function splitURL(url: string): IURLSplittingResult;
|
|
29
|
+
/**
|
|
30
|
+
* 根据切割结果还原 URL 或路径。
|
|
31
|
+
* @param result 切割结果。
|
|
32
|
+
* @returns 还原后的 URL 或路径。
|
|
33
|
+
*/
|
|
34
|
+
export declare function joinURL(result: IURLSplittingResult): string;
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@polyv/utils-querystring",
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"description": "Query string utilities for Polyv frontend development.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./index.js",
|
|
7
|
+
"module": "./index.js",
|
|
8
|
+
"types": "./index.d.ts",
|
|
9
|
+
"miniprogram": "./",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./index.d.ts",
|
|
13
|
+
"import": "./index.js",
|
|
14
|
+
"require": "./index.cjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@polyv/utils-object": "3.0.0"
|
|
20
|
+
}
|
|
21
|
+
}
|
package/parse.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { IQSParseOptions } from './interfaces';
|
|
2
|
+
/**
|
|
3
|
+
* 把查询字符串解析为键值对集合。
|
|
4
|
+
* @param str 查询字符串。
|
|
5
|
+
* @param options 解析选项。
|
|
6
|
+
* @returns 键值对集合。
|
|
7
|
+
* @example
|
|
8
|
+
* ```javascript
|
|
9
|
+
* import { parse } from '@polyv/utils-querystring';
|
|
10
|
+
* parse('id=0&str=hello'); // { id: '0', str: 'hello' }
|
|
11
|
+
* parse('id=0&id=1'); // { id: ['0', '1'] }
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare function parse(str: string, options?: Readonly<IQSParseOptions>): {
|
|
15
|
+
[key: string]: string | string[];
|
|
16
|
+
};
|
package/replace.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { IQSReplaceOptions } from './interfaces';
|
|
2
|
+
/**
|
|
3
|
+
* 替换查询字符串中指定参数的值。
|
|
4
|
+
* @since 2.0.0
|
|
5
|
+
* @param url 包含查询字符串的 URL 或路径。
|
|
6
|
+
* @param newData 要替换的参数及其替换后的值,以键值对方式传参。
|
|
7
|
+
* @param options 操作选项。
|
|
8
|
+
* @returns 已替换参数值的查询字符串。
|
|
9
|
+
* @example
|
|
10
|
+
* ```javascript
|
|
11
|
+
* import { replace } from '@polyv/utils-querystring';
|
|
12
|
+
* replace('abc?a=1&b=2', { a: 2 }); // 'abc?a=2&b=2'
|
|
13
|
+
* replace('abc?a=1&b=2', { c: 3 }); // 'abc?a=1&b=2'
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare function replace(url: string, newData: any, options?: Readonly<IQSReplaceOptions>): string;
|
package/stringify.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { IQSStringifyOptions } from './interfaces';
|
|
2
|
+
/**
|
|
3
|
+
* 把键值对序列化为查询字符串。
|
|
4
|
+
* @param data 键值对。
|
|
5
|
+
* @param options 序列化选项。
|
|
6
|
+
* @returns 序列化结果。
|
|
7
|
+
* @example
|
|
8
|
+
* ```javascript
|
|
9
|
+
* import { stringify } from '@polyv/utils-querystring';
|
|
10
|
+
* stringify({ id: '0', str: 'hello' }); // 'id=0&str=hello'
|
|
11
|
+
* stringify({ id: ['0', '1'] }); // 'id=0&id=1'
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare function stringify(data: any, options?: Readonly<IQSStringifyOptions>): string;
|