@wxhccc/es-util 1.6.0 → 2.0.1

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.
Files changed (39) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +286 -57
  3. package/dist/array-tree-switch.d.ts +18 -2
  4. package/dist/date-time.d.ts +14 -0
  5. package/dist/event-target-emitter.d.ts +45 -0
  6. package/dist/event-target-emitter.js +147 -0
  7. package/dist/index.d.ts +4 -0
  8. package/dist/index.esm.js +499 -216
  9. package/dist/index.js +505 -218
  10. package/dist/index.min.js +2 -2
  11. package/dist/object-array.d.ts +7 -9
  12. package/dist/object-array.js +34 -16
  13. package/dist/optional.d.ts +6 -9
  14. package/dist/optional.js +136 -0
  15. package/dist/page-communicate.d.ts +27 -0
  16. package/dist/page-communicate.js +66 -0
  17. package/dist/promise.d.ts +21 -24
  18. package/dist/promise.js +24 -132
  19. package/dist/raf-timer.d.ts +36 -0
  20. package/dist/raf-timer.js +103 -0
  21. package/dist/types.d.ts +4 -0
  22. package/dist/utils.d.ts +10 -0
  23. package/dist/utils.js +30 -0
  24. package/dist/validate.js +54 -6
  25. package/dist/value-string-switch/byte-string.d.ts +9 -4
  26. package/dist/value-string-switch/index.d.ts +1 -0
  27. package/dist/value-string-switch/mask-data.d.ts +1 -2
  28. package/dist/value-string-switch/uri-b64.d.ts +8 -0
  29. package/dist/value-string-switch.js +28 -11
  30. package/package.json +19 -19
  31. package/types/array-tree-switch.d.ts +0 -49
  32. package/types/index.d.ts +0 -6
  33. package/types/object-array.d.ts +0 -33
  34. package/types/optional.d.ts +0 -50
  35. package/types/promise.d.ts +0 -44
  36. package/types/validate.d.ts +0 -2
  37. package/types/value-string-switch/byte-string.d.ts +0 -12
  38. package/types/value-string-switch/index.d.ts +0 -2
  39. package/types/value-string-switch/mask-data.d.ts +0 -22
package/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "@wxhccc/es-util",
3
- "version": "1.6.0",
3
+ "version": "2.0.1",
4
4
  "description": "A library that contains some useful methods",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",
7
7
  "unpkg": "dist/index.min.js",
8
- "types": "types/index.d.ts",
8
+ "types": "dist/index.d.ts",
9
9
  "scripts": {
10
10
  "start": "npm run dev",
11
11
  "test": "jest",
12
12
  "dev": "rollup -w -c --environment NODE_ENV:development",
13
- "build": "rollup -c"
13
+ "clean": "rimraf dist/**",
14
+ "build": "yarn clean && rollup -c"
14
15
  },
15
16
  "keywords": [
16
17
  "array-tree-switch",
@@ -42,27 +43,26 @@
42
43
  "@rollup/plugin-commonjs": "^18.0.0",
43
44
  "@rollup/plugin-node-resolve": "^11.2.1",
44
45
  "@types/jest": "^26.0.23",
45
- "@typescript-eslint/eslint-plugin": "^2.19.0",
46
- "@typescript-eslint/parser": "^2.19.0",
47
- "@vue/test-utils": "^2.0.0-rc.6",
46
+ "@types/lodash": "^4.14.182",
47
+ "@typescript-eslint/eslint-plugin": "^5.30.7",
48
+ "@typescript-eslint/parser": "^5.30.7",
48
49
  "babel-eslint": "^8.2.6",
49
- "enzyme": "^3.11.0",
50
- "enzyme-adapter-react-16": "^1.15.6",
51
- "eslint": "^4.19.1",
52
- "jest": "^26.6.3",
53
- "lodash": "^4.17.15",
54
- "react": "^17.0.2",
55
- "react-dom": "^17.0.2",
56
- "rollup": "^2.45.1",
50
+ "eslint": "^8.20.0",
51
+ "eslint-config-prettier": "^8.5.0",
52
+ "eslint-plugin-prettier": "^4.2.1",
53
+ "jest": "^28.1.3",
54
+ "jest-environment-jsdom": "^28.1.3",
55
+ "prettier": "^2.7.1",
56
+ "rimraf": "^3.0.2",
57
+ "rollup": "^2.77.0",
57
58
  "rollup-plugin-terser": "^7.0.2",
58
- "rollup-plugin-typescript2": "^0.30.0",
59
+ "rollup-plugin-typescript2": "^0.32.1",
60
+ "ts-jest": "^28.0.7",
59
61
  "tslib": "^1.10.0",
60
- "typescript": "^4.2.4",
61
- "vue": "^3.0.11"
62
+ "typescript": "^4.7.4"
62
63
  },
63
64
  "files": [
64
- "dist",
65
- "types"
65
+ "dist"
66
66
  ],
67
67
  "dependencies": {}
68
68
  }
@@ -1,49 +0,0 @@
1
- export interface Node {
2
- [key: string]: any;
3
- }
4
- export interface TreeNode {
5
- [key: string]: any;
6
- }
7
- interface Options {
8
- primaryKey?: string;
9
- parentKey?: string;
10
- childrenKey?: string;
11
- }
12
- interface TreeOptions extends Options {
13
- createRoot?: boolean | ((nodes: TreeNode[]) => any);
14
- parentRefKey?: boolean | string;
15
- }
16
- interface NodeOptions extends Options {
17
- hasParentKey?: boolean;
18
- returnObject?: boolean;
19
- }
20
- declare type Tree = TreeNode[] | {
21
- [children: string]: TreeNode[];
22
- };
23
- /**
24
- * translate array to tree
25
- * @param {Array} array the node array need to be translate
26
- * @param {Object} options the options
27
- * @param {string} primaryKey the primary key of node
28
- * @param {string} parentKey the parent's key of node
29
- * @param {string} childrenKey the child's key of node
30
- * @param {boolean | Function} createNode whether create root node, or function which can return root node
31
- * @param {boolean | string} parentRefKey parent reference key of node
32
- */
33
- export declare function array2tree(array: Node[], options?: TreeOptions): Tree;
34
- declare type Nodes = Node[];
35
- declare type NodeMap = {
36
- [id: string]: Node;
37
- };
38
- /**
39
- * translate tree to array
40
- * @param {Array/Object} tree the node tree need to be translate
41
- * @param {Object} options the options
42
- * @param {string} primaryKey the primary key of tree node
43
- * @param {string} parentKey the parent's key of tree node
44
- * @param {string} childrenKey the child's key of tree node
45
- * @param {boolean} hasParentKey whether create parent id in node
46
- * @param {boolean} returnObject whether return object or array
47
- */
48
- export declare function tree2array(tree: Tree, options?: NodeOptions): NodeMap | Nodes;
49
- export {};
package/types/index.d.ts DELETED
@@ -1,6 +0,0 @@
1
- export * from './array-tree-switch';
2
- export * from './validate';
3
- export * from './object-array';
4
- export * from './value-string-switch';
5
- export * from './promise';
6
- export * from './optional';
@@ -1,33 +0,0 @@
1
- declare type Obj = {
2
- [key: string]: any;
3
- };
4
- /**
5
- * translate object array to key-value map object
6
- *
7
- * @param {Object[]} objectArray
8
- * @param {string} keyProp
9
- * @param {string} valueProp
10
- */
11
- interface PropHandler {
12
- (item: any, index: number): string;
13
- }
14
- export declare function mapToObject(objectArray: any[], keyProp?: string | PropHandler, valueProp?: string | PropHandler): Obj;
15
- /**
16
- * checkout values form object as an array
17
- *
18
- * @param {Object} object
19
- * @param {string[] | Object} keys
20
- * @param {string} valueProp
21
- */
22
- declare type MergeFn = <T>(...args: T[]) => T;
23
- export declare function checkoutBy(object: Obj, keys?: string[] | Obj, mergeFn?: MergeFn): any[];
24
- /**
25
- * pick and rename object values
26
- *
27
- * @param {Object} object
28
- * @param {Object} keysMap
29
- */
30
- export declare function pickRenameKeys(object: Obj, keysMap: {
31
- [key: string]: string;
32
- }): Obj | undefined;
33
- export {};
@@ -1,50 +0,0 @@
1
- declare type AnyObject = Record<string | number, any>;
2
- /**
3
- * JSON.parse string safely
4
- * @param json string
5
- * @param faileValue any
6
- * @returns any
7
- */
8
- export declare function safeJsonParse<T = AnyObject>(json?: string): T | undefined;
9
- export declare function safeJsonParse<T = AnyObject>(json: string | undefined, faileValue: T): T;
10
- /**
11
- * check value whether empty text
12
- * 判断给定变量是否是字面量空值 */
13
- export declare const isNullable: (value: unknown) => value is "" | null | undefined;
14
- /**
15
- * 过滤对象中属性为空('' | undefined | null)的字段
16
- * @param obj 给定对象
17
- * @param deep 是否需要递归过滤
18
- * @returns 过滤后的对象
19
- */
20
- export declare const filterNullable: <T extends AnyObject>(obj: T, deep?: boolean | undefined) => T;
21
- /**
22
- * 将对象中属性值为null的转换为undefined
23
- * @param data 对象数据
24
- * @returns
25
- */
26
- export declare const switchNullToUndefined: (data: any) => any;
27
- /**
28
- * 参数转换规则的配置对象
29
- * 值可以是时间范围转换所需的开始,结束时间字段的名称,或者需要进行拼接的数组
30
- */
31
- export declare type ParamsSwitchMaps<DK extends string = string, RK extends string = string> = Record<DK, [
32
- RK,
33
- RK
34
- ] | [RK, RK, string] | 'join' | 'pop'>;
35
- export declare type DateFormat = (date: any, format?: string) => string;
36
- /**
37
- * 将搜素条件里的数组字段转换为其他格式,通常用于搜索表单的字段转换
38
- * @param formData 传入参数
39
- * @param maps 转换的映射关系,eg:{ date: ['startTime', 'endTime'] }
40
- * @param options 额外的配置参数
41
- */
42
- export declare const formParamsSwitch: <R extends AnyObject = AnyObject, T extends AnyObject = AnyObject>(formData: T, maps: Partial<ParamsSwitchMaps<Exclude<keyof T, number | symbol>, Exclude<keyof R, number | symbol>>>, options?: {
43
- /** 是否需要过滤空数据 */
44
- filterNullable?: boolean | undefined;
45
- /** 时间格式换函数 */
46
- dateFormat?: DateFormat | undefined;
47
- } | undefined) => R;
48
- /** 创建一个绑定了默认时间格式转换函数的formParamsSwitch函数 */
49
- export declare const formParamsSwitchCreator: (dateFormat: DateFormat) => typeof formParamsSwitch;
50
- export {};
@@ -1,44 +0,0 @@
1
- export declare const objType: (val: unknown) => string;
2
- /**
3
- * wrap promise and handle reject or err by return an array like [error, undefined]
4
- * @param promise promise
5
- * @returns Promise<[K, undefined] | [null, T]>
6
- */
7
- export declare function awaitWrapper<T, K = Error>(promise: Promise<T>): Promise<[null, T] | [K, undefined]>;
8
- export declare type SyncRefHandle = [Record<string, boolean>, string];
9
- export declare type LockSwitchHook = (val: boolean) => unknown;
10
- export interface PromiseWithLock<T> extends Promise<T> {
11
- readonly __lockValue: boolean;
12
- unlock: () => void;
13
- }
14
- export declare type ContextType = 'react' | 'vue' | 'unknown';
15
- /**
16
- * wrap promise with lock, support reactive environment like react or vue
17
- * @param promise promise
18
- * @param wrap whether use awaitWrap to wrap result
19
- */
20
- export interface WpOptions {
21
- wrap?: boolean;
22
- lock?: string | LockSwitchHook | SyncRefHandle;
23
- syncRefHandle?: SyncRefHandle;
24
- manualUnlock?: boolean;
25
- }
26
- declare type WpReturn<T> = PromiseWithLock<T | undefined>;
27
- declare type WpArrReturn<T> = PromiseWithLock<[null, T] | [Error, undefined]>;
28
- declare type WpPromise<T> = Promise<T> | (() => Promise<T>);
29
- declare function wrapPromise<T>(this: any, promise: WpPromise<T>, wrap?: false): WpReturn<T>;
30
- declare function wrapPromise<T>(this: any, promise: WpPromise<T>, wrap: true): WpArrReturn<T>;
31
- declare function wrapPromise<T>(this: any, promise: WpPromise<T>, options?: WpOptions & {
32
- wrap?: false;
33
- }): WpReturn<T>;
34
- declare function wrapPromise<T>(this: any, promise: WpPromise<T>, options?: WpOptions & {
35
- wrap: true;
36
- }): WpArrReturn<T>;
37
- declare type WP = typeof wrapPromise & {
38
- _checkLockKey: (key: string) => boolean;
39
- };
40
- export declare const wp: WP;
41
- export declare const wpVuePlugin: {
42
- install(appOrVue: any, key?: string): void;
43
- };
44
- export {};
@@ -1,2 +0,0 @@
1
- export declare function ChinaIdCardValid(idNo: string): boolean;
2
- export declare function formulaValidate(string: string, variables?: string[]): boolean;
@@ -1,12 +0,0 @@
1
- interface Options {
2
- standard?: 'metric' | 'iec' | 'jedec';
3
- unitLvl?: string;
4
- precision?: number;
5
- detail?: boolean;
6
- }
7
- export declare function byteStringify(byteNum: number | string, options?: Options): string | {
8
- value: string | number;
9
- unit: string;
10
- } | undefined;
11
- export declare function byteParse(byteStr: string, options?: {}): void;
12
- export {};
@@ -1,2 +0,0 @@
1
- export * from './byte-string';
2
- export * from './mask-data';
@@ -1,22 +0,0 @@
1
- interface MaskDataOptions {
2
- /** 替换的字符,默认为* */
3
- maskWith?: string;
4
- /** 起始字符索引,为负数时从字符串结尾起倒数第几个数开始 */
5
- startCharIndex?: number;
6
- /** 结束字符索引,包含在处理字符内,缺省时会替换到字符串结尾,为负数时从字符串结尾起倒数第几个数开始 */
7
- endCharIndex?: number;
8
- /** 待处理字符串起始处理字符(串),不包含在待处理片段内 */
9
- startFrom?: string;
10
- /** 待处理字符串结束处理字符(串),不包含在待处理片段内 */
11
- endUntil?: string;
12
- /** 内置的默认的的处理模式,会预设一些属性值,属性值可以被覆盖 */
13
- mode?: 'telphone' | 'idcard';
14
- }
15
- /**
16
- * 对给定的字符串进行脱敏处理
17
- * @param data 需要处理的字符串
18
- * @param options 配置参数对象
19
- * @returns
20
- */
21
- export declare function maskData(data: string, options?: MaskDataOptions): string;
22
- export {};