@vope/utils 1.4.22 → 1.4.23
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/cjs/index.js +105 -2
- package/lib/index.js +3 -0
- package/lib/serialize/url-params.js +103 -0
- package/package.json +58 -58
- package/types/index.d.ts +1 -0
- package/types/serialize/url-params.d.ts +36 -0
package/cjs/index.js
CHANGED
|
@@ -34,11 +34,13 @@ __export(src_exports, {
|
|
|
34
34
|
compareVersion: () => compareVersion,
|
|
35
35
|
connectURLParams: () => connectURLParams,
|
|
36
36
|
css: () => css,
|
|
37
|
+
cubeURLParams: () => cubeURLParams,
|
|
37
38
|
deconPowersSum: () => deconPowersSum,
|
|
38
39
|
deepWeakEqual: () => deepWeakEqual,
|
|
39
40
|
desensDecrypt: () => desensDecrypt,
|
|
40
41
|
desensEncrypt: () => desensEncrypt,
|
|
41
42
|
desensReplace: () => desensReplace,
|
|
43
|
+
flatURLParams: () => flatURLParams,
|
|
42
44
|
getEncoding: () => getEncoding,
|
|
43
45
|
hash8json: () => hash8json,
|
|
44
46
|
hash8object: () => hash8object,
|
|
@@ -973,8 +975,109 @@ var parseNum = (num) => {
|
|
|
973
975
|
// src/rotate/angle.ts
|
|
974
976
|
var rotateAngle = (from, angle, visit) => rotateRad(from, angle / 180 * Math.PI, visit);
|
|
975
977
|
|
|
976
|
-
// src/
|
|
978
|
+
// src/serialize/url-params.ts
|
|
977
979
|
var import_lodash_es4 = require("lodash-es");
|
|
980
|
+
var cubeURLParams = (text, serializes) => {
|
|
981
|
+
const params = new URLSearchParams(text);
|
|
982
|
+
const result = {};
|
|
983
|
+
for (const [key, value] of params) {
|
|
984
|
+
const serialize = serializes[key];
|
|
985
|
+
switch (serialize) {
|
|
986
|
+
case Boolean:
|
|
987
|
+
result[key] = value === "true";
|
|
988
|
+
break;
|
|
989
|
+
case Number:
|
|
990
|
+
result[key] = Number(value);
|
|
991
|
+
break;
|
|
992
|
+
case Date:
|
|
993
|
+
result[key] = new Date(value);
|
|
994
|
+
break;
|
|
995
|
+
case BigInt:
|
|
996
|
+
result[key] = BigInt(value);
|
|
997
|
+
break;
|
|
998
|
+
case RegExp:
|
|
999
|
+
result[key] = new RegExp(value);
|
|
1000
|
+
break;
|
|
1001
|
+
default:
|
|
1002
|
+
if ((0, import_lodash_es4.isFunction)(serialize)) {
|
|
1003
|
+
result[key] = serialize(value);
|
|
1004
|
+
} else if ((0, import_lodash_es4.isObject)(serialize) && (0, import_lodash_es4.isFunction)(serialize.cube)) {
|
|
1005
|
+
result[key] = serialize.cube(value);
|
|
1006
|
+
} else {
|
|
1007
|
+
result[key] = value;
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
return result;
|
|
1012
|
+
};
|
|
1013
|
+
var re_flat = /\+/g;
|
|
1014
|
+
var flatEncode = (text) => text.replace(re_flat, () => "%2B");
|
|
1015
|
+
var flatValue = (value) => {
|
|
1016
|
+
switch (typeof value) {
|
|
1017
|
+
case "boolean":
|
|
1018
|
+
case "bigint":
|
|
1019
|
+
case "number":
|
|
1020
|
+
case "symbol":
|
|
1021
|
+
case "function":
|
|
1022
|
+
return value.toString();
|
|
1023
|
+
case "string":
|
|
1024
|
+
return flatEncode(value);
|
|
1025
|
+
case "undefined":
|
|
1026
|
+
return "";
|
|
1027
|
+
default:
|
|
1028
|
+
if (value === null) {
|
|
1029
|
+
return "";
|
|
1030
|
+
}
|
|
1031
|
+
if (value instanceof Date) {
|
|
1032
|
+
return value.toJSON();
|
|
1033
|
+
} else if (value instanceof RegExp) {
|
|
1034
|
+
return flatEncode(value.source);
|
|
1035
|
+
} else {
|
|
1036
|
+
return flatEncode(value.toString());
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
};
|
|
1040
|
+
var flatURLParams = (data, serializes) => {
|
|
1041
|
+
const params = new URLSearchParams();
|
|
1042
|
+
for (const key in data) {
|
|
1043
|
+
const value = data[key];
|
|
1044
|
+
const serialize = serializes[key];
|
|
1045
|
+
let result;
|
|
1046
|
+
switch (serialize) {
|
|
1047
|
+
case Boolean:
|
|
1048
|
+
result = typeof value === "boolean" ? `${value}` : "";
|
|
1049
|
+
break;
|
|
1050
|
+
case Number:
|
|
1051
|
+
result = typeof value === "number" ? `${value}` : "";
|
|
1052
|
+
break;
|
|
1053
|
+
case Date:
|
|
1054
|
+
result = value instanceof Date ? value.toJSON() : "";
|
|
1055
|
+
break;
|
|
1056
|
+
case BigInt:
|
|
1057
|
+
result = typeof value === "bigint" ? `${value}` : "";
|
|
1058
|
+
break;
|
|
1059
|
+
case RegExp:
|
|
1060
|
+
result = value instanceof RegExp ? flatEncode(value.source) : "";
|
|
1061
|
+
break;
|
|
1062
|
+
case String:
|
|
1063
|
+
result = typeof value === "string" ? flatEncode(value) : "";
|
|
1064
|
+
break;
|
|
1065
|
+
default:
|
|
1066
|
+
if ((0, import_lodash_es4.isFunction)(serialize)) {
|
|
1067
|
+
result = flatValue(value);
|
|
1068
|
+
} else if ((0, import_lodash_es4.isObject)(serialize) && (0, import_lodash_es4.isFunction)(serialize.flat)) {
|
|
1069
|
+
result = serialize.flat(value);
|
|
1070
|
+
} else {
|
|
1071
|
+
result = flatValue(value);
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
params.append(key, result);
|
|
1075
|
+
}
|
|
1076
|
+
return params.toString();
|
|
1077
|
+
};
|
|
1078
|
+
|
|
1079
|
+
// src/style/css.ts
|
|
1080
|
+
var import_lodash_es5 = require("lodash-es");
|
|
978
1081
|
var hump = /[A-Z]/g;
|
|
979
1082
|
var css = (...args) => {
|
|
980
1083
|
const list = [];
|
|
@@ -982,7 +1085,7 @@ var css = (...args) => {
|
|
|
982
1085
|
const data = args[i];
|
|
983
1086
|
if (!data)
|
|
984
1087
|
continue;
|
|
985
|
-
if ((0,
|
|
1088
|
+
if ((0, import_lodash_es5.isString)(data)) {
|
|
986
1089
|
list.push(data);
|
|
987
1090
|
} else if (Array.isArray(data)) {
|
|
988
1091
|
const result = css(...data);
|
package/lib/index.js
CHANGED
|
@@ -24,6 +24,7 @@ import { parseNum } from "./parse/num";
|
|
|
24
24
|
import { deconPowersSum } from "./power/decon";
|
|
25
25
|
import { rotateAngle } from "./rotate/angle";
|
|
26
26
|
import { rotateRad } from "./rotate/rad";
|
|
27
|
+
import { cubeURLParams, flatURLParams } from "./serialize/url-params";
|
|
27
28
|
import { css } from "./style/css";
|
|
28
29
|
import { default as default4 } from "./subscriber";
|
|
29
30
|
import { FlexTable } from "./table/flex";
|
|
@@ -46,11 +47,13 @@ export {
|
|
|
46
47
|
compareVersion,
|
|
47
48
|
connectURLParams,
|
|
48
49
|
css,
|
|
50
|
+
cubeURLParams,
|
|
49
51
|
deconPowersSum,
|
|
50
52
|
deepWeakEqual,
|
|
51
53
|
desensDecrypt,
|
|
52
54
|
desensEncrypt,
|
|
53
55
|
desensReplace,
|
|
56
|
+
flatURLParams,
|
|
54
57
|
getEncoding,
|
|
55
58
|
hash8json,
|
|
56
59
|
hash8object,
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { isFunction, isObject } from "lodash-es";
|
|
2
|
+
const cubeURLParams = (text, serializes) => {
|
|
3
|
+
const params = new URLSearchParams(text);
|
|
4
|
+
const result = {};
|
|
5
|
+
for (const [key, value] of params) {
|
|
6
|
+
const serialize = serializes[key];
|
|
7
|
+
switch (serialize) {
|
|
8
|
+
case Boolean:
|
|
9
|
+
result[key] = value === "true";
|
|
10
|
+
break;
|
|
11
|
+
case Number:
|
|
12
|
+
result[key] = Number(value);
|
|
13
|
+
break;
|
|
14
|
+
case Date:
|
|
15
|
+
result[key] = new Date(value);
|
|
16
|
+
break;
|
|
17
|
+
case BigInt:
|
|
18
|
+
result[key] = BigInt(value);
|
|
19
|
+
break;
|
|
20
|
+
case RegExp:
|
|
21
|
+
result[key] = new RegExp(value);
|
|
22
|
+
break;
|
|
23
|
+
default:
|
|
24
|
+
if (isFunction(serialize)) {
|
|
25
|
+
result[key] = serialize(value);
|
|
26
|
+
} else if (isObject(serialize) && isFunction(serialize.cube)) {
|
|
27
|
+
result[key] = serialize.cube(value);
|
|
28
|
+
} else {
|
|
29
|
+
result[key] = value;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
35
|
+
const re_flat = /\+/g;
|
|
36
|
+
const flatEncode = (text) => text.replace(re_flat, () => "%2B");
|
|
37
|
+
const flatValue = (value) => {
|
|
38
|
+
switch (typeof value) {
|
|
39
|
+
case "boolean":
|
|
40
|
+
case "bigint":
|
|
41
|
+
case "number":
|
|
42
|
+
case "symbol":
|
|
43
|
+
case "function":
|
|
44
|
+
return value.toString();
|
|
45
|
+
case "string":
|
|
46
|
+
return flatEncode(value);
|
|
47
|
+
case "undefined":
|
|
48
|
+
return "";
|
|
49
|
+
default:
|
|
50
|
+
if (value === null) {
|
|
51
|
+
return "";
|
|
52
|
+
}
|
|
53
|
+
if (value instanceof Date) {
|
|
54
|
+
return value.toJSON();
|
|
55
|
+
} else if (value instanceof RegExp) {
|
|
56
|
+
return flatEncode(value.source);
|
|
57
|
+
} else {
|
|
58
|
+
return flatEncode(value.toString());
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
const flatURLParams = (data, serializes) => {
|
|
63
|
+
const params = new URLSearchParams();
|
|
64
|
+
for (const key in data) {
|
|
65
|
+
const value = data[key];
|
|
66
|
+
const serialize = serializes[key];
|
|
67
|
+
let result;
|
|
68
|
+
switch (serialize) {
|
|
69
|
+
case Boolean:
|
|
70
|
+
result = typeof value === "boolean" ? `${value}` : "";
|
|
71
|
+
break;
|
|
72
|
+
case Number:
|
|
73
|
+
result = typeof value === "number" ? `${value}` : "";
|
|
74
|
+
break;
|
|
75
|
+
case Date:
|
|
76
|
+
result = value instanceof Date ? value.toJSON() : "";
|
|
77
|
+
break;
|
|
78
|
+
case BigInt:
|
|
79
|
+
result = typeof value === "bigint" ? `${value}` : "";
|
|
80
|
+
break;
|
|
81
|
+
case RegExp:
|
|
82
|
+
result = value instanceof RegExp ? flatEncode(value.source) : "";
|
|
83
|
+
break;
|
|
84
|
+
case String:
|
|
85
|
+
result = typeof value === "string" ? flatEncode(value) : "";
|
|
86
|
+
break;
|
|
87
|
+
default:
|
|
88
|
+
if (isFunction(serialize)) {
|
|
89
|
+
result = flatValue(value);
|
|
90
|
+
} else if (isObject(serialize) && isFunction(serialize.flat)) {
|
|
91
|
+
result = serialize.flat(value);
|
|
92
|
+
} else {
|
|
93
|
+
result = flatValue(value);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
params.append(key, result);
|
|
97
|
+
}
|
|
98
|
+
return params.toString();
|
|
99
|
+
};
|
|
100
|
+
export {
|
|
101
|
+
cubeURLParams,
|
|
102
|
+
flatURLParams
|
|
103
|
+
};
|
package/package.json
CHANGED
|
@@ -1,58 +1,58 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@vope/utils",
|
|
3
|
-
"version": "1.4.
|
|
4
|
-
"description": "",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"publishConfig": {
|
|
7
|
-
"access": "public",
|
|
8
|
-
"registry": "https://registry.npmjs.org/"
|
|
9
|
-
},
|
|
10
|
-
"types": "./types/index.d.ts",
|
|
11
|
-
"typings": "./types/index.d.ts",
|
|
12
|
-
"module": "./lib/index.js",
|
|
13
|
-
"main": "./cjs/index.js",
|
|
14
|
-
"scripts": {
|
|
15
|
-
"start": "esbuild ./pre.ts --outfile=pre.cjs --format=cjs & node ./pre.cjs",
|
|
16
|
-
"prebuild": "npm run start & esbuild ./build.ts --outfile=build.cjs --format=cjs",
|
|
17
|
-
"build": "node ./build.cjs",
|
|
18
|
-
"premocha": "node ./build.cjs --type=test",
|
|
19
|
-
"mocha": "mocha __tests/**/*.js",
|
|
20
|
-
"test": "npm run mocha",
|
|
21
|
-
"prepublishOnly": "npm run build & npm run test"
|
|
22
|
-
},
|
|
23
|
-
"exports": {
|
|
24
|
-
".": {
|
|
25
|
-
"import": "./lib/index.js",
|
|
26
|
-
"require": "./cjs/index.js"
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
|
-
"author": "vope",
|
|
30
|
-
"license": "ISC",
|
|
31
|
-
"files": [
|
|
32
|
-
"cjs",
|
|
33
|
-
"lib",
|
|
34
|
-
"types"
|
|
35
|
-
],
|
|
36
|
-
"devDependencies": {
|
|
37
|
-
"@babel/parser": "^7.23.4",
|
|
38
|
-
"@babel/traverse": "^7.23.4",
|
|
39
|
-
"@babel/types": "^7.23.4",
|
|
40
|
-
"@types/babel__traverse": "^7.20.4",
|
|
41
|
-
"@types/mocha": "^10.0.6",
|
|
42
|
-
"@types/node": "^20.4.5",
|
|
43
|
-
"esbuild": "^0.19.4",
|
|
44
|
-
"glob": "^10.3.10",
|
|
45
|
-
"iconv-lite": "^0.6.3",
|
|
46
|
-
"mocha": "^10.2.0",
|
|
47
|
-
"rimraf": "^5.0.5",
|
|
48
|
-
"tslib": "^2.6.1",
|
|
49
|
-
"typescript": "^5.3.3"
|
|
50
|
-
},
|
|
51
|
-
"dependencies": {
|
|
52
|
-
"@type-challenges/utils": "^0.1.1",
|
|
53
|
-
"@types/lodash-es": "^4.17.9",
|
|
54
|
-
"@vope/types": "^1.0.
|
|
55
|
-
"csstype": "^3.1.3",
|
|
56
|
-
"lodash-es": "^4.17.21"
|
|
57
|
-
}
|
|
58
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@vope/utils",
|
|
3
|
+
"version": "1.4.23",
|
|
4
|
+
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public",
|
|
8
|
+
"registry": "https://registry.npmjs.org/"
|
|
9
|
+
},
|
|
10
|
+
"types": "./types/index.d.ts",
|
|
11
|
+
"typings": "./types/index.d.ts",
|
|
12
|
+
"module": "./lib/index.js",
|
|
13
|
+
"main": "./cjs/index.js",
|
|
14
|
+
"scripts": {
|
|
15
|
+
"start": "esbuild ./pre.ts --outfile=pre.cjs --format=cjs & node ./pre.cjs",
|
|
16
|
+
"prebuild": "npm run start & esbuild ./build.ts --outfile=build.cjs --format=cjs",
|
|
17
|
+
"build": "node ./build.cjs",
|
|
18
|
+
"premocha": "node ./build.cjs --type=test",
|
|
19
|
+
"mocha": "mocha __tests/**/*.js",
|
|
20
|
+
"test": "npm run mocha",
|
|
21
|
+
"prepublishOnly": "npm run build & npm run test"
|
|
22
|
+
},
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"import": "./lib/index.js",
|
|
26
|
+
"require": "./cjs/index.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"author": "vope",
|
|
30
|
+
"license": "ISC",
|
|
31
|
+
"files": [
|
|
32
|
+
"cjs",
|
|
33
|
+
"lib",
|
|
34
|
+
"types"
|
|
35
|
+
],
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@babel/parser": "^7.23.4",
|
|
38
|
+
"@babel/traverse": "^7.23.4",
|
|
39
|
+
"@babel/types": "^7.23.4",
|
|
40
|
+
"@types/babel__traverse": "^7.20.4",
|
|
41
|
+
"@types/mocha": "^10.0.6",
|
|
42
|
+
"@types/node": "^20.4.5",
|
|
43
|
+
"esbuild": "^0.19.4",
|
|
44
|
+
"glob": "^10.3.10",
|
|
45
|
+
"iconv-lite": "^0.6.3",
|
|
46
|
+
"mocha": "^10.2.0",
|
|
47
|
+
"rimraf": "^5.0.5",
|
|
48
|
+
"tslib": "^2.6.1",
|
|
49
|
+
"typescript": "^5.3.3"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"@type-challenges/utils": "^0.1.1",
|
|
53
|
+
"@types/lodash-es": "^4.17.9",
|
|
54
|
+
"@vope/types": "^1.0.13",
|
|
55
|
+
"csstype": "^3.1.3",
|
|
56
|
+
"lodash-es": "^4.17.21"
|
|
57
|
+
}
|
|
58
|
+
}
|
package/types/index.d.ts
CHANGED
|
@@ -24,6 +24,7 @@ export { parseNum } from './parse/num';
|
|
|
24
24
|
export { deconPowersSum } from './power/decon';
|
|
25
25
|
export { rotateAngle } from './rotate/angle';
|
|
26
26
|
export { rotateRad } from './rotate/rad';
|
|
27
|
+
export { type URLParamsValues, cubeURLParams, type URLParamsFlats, flatURLParams } from './serialize/url-params';
|
|
27
28
|
export { type CSSProperties, type StyleValue, css } from './style/css';
|
|
28
29
|
export { type SModel, default as Subscriber } from './subscriber';
|
|
29
30
|
export { FlexTable } from './table/flex';
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Assign } from '@vope/types';
|
|
2
|
+
type NativeValue = BooleanConstructor | NumberConstructor | DateConstructor | BigIntConstructor | RegExpConstructor | StringConstructor;
|
|
3
|
+
interface AccessorValue<T = any> {
|
|
4
|
+
cube(text: string): T;
|
|
5
|
+
flat?(value: T): string;
|
|
6
|
+
}
|
|
7
|
+
type SimplifyValue = ((text: string) => any);
|
|
8
|
+
type Value = NativeValue | SimplifyValue | AccessorValue;
|
|
9
|
+
/**
|
|
10
|
+
* 转换套件类型
|
|
11
|
+
*/
|
|
12
|
+
type Values = Record<string, Value>;
|
|
13
|
+
export { Values as URLParamsValues };
|
|
14
|
+
type SimplifyBranch<T, Branch extends boolean> = Branch extends true ? T extends object ? Assign<T, {
|
|
15
|
+
toString?(this: T): string;
|
|
16
|
+
}> : T : T;
|
|
17
|
+
type Cube<T, Branch extends boolean> = [T] extends [DateConstructor] ? Date : [T] extends [NativeValue] ? T extends ((value: string) => infer A) ? A : string : [T] extends [(text: string) => infer S] ? SimplifyBranch<S, Branch> : [T] extends [AccessorValue<infer B>] ? B : string;
|
|
18
|
+
type Cubes<T extends Values, Branch extends boolean = false> = {
|
|
19
|
+
[P in keyof T]: Cube<T[P], Branch>;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* 将URL查询参数转换成(复杂)对象
|
|
23
|
+
* @param text URL查询参数字符串
|
|
24
|
+
* @param serializes 转换套件
|
|
25
|
+
*/
|
|
26
|
+
export declare const cubeURLParams: <T extends Values>(text: string, serializes: T) => Cubes<T, false>;
|
|
27
|
+
/**
|
|
28
|
+
* 符合套件的(复杂)数据类型
|
|
29
|
+
*/
|
|
30
|
+
export type URLParamsFlats<T extends Values> = Cubes<T, true>;
|
|
31
|
+
/**
|
|
32
|
+
* 将(复杂)对象转换成URL查询参数
|
|
33
|
+
* @param data 符合套件格式的(复杂)对象
|
|
34
|
+
* @param serializes 转换套件
|
|
35
|
+
*/
|
|
36
|
+
export declare const flatURLParams: <T extends Values>(data: Cubes<T, true>, serializes: T) => string;
|