@wxhccc/es-util 1.5.1 → 1.7.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/README.md +22 -0
- package/dist/event-target-emitter.d.ts +45 -0
- package/dist/event-target-emitter.js +126 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.esm.js +236 -18
- package/dist/index.js +240 -17
- package/dist/index.min.js +3 -3
- package/dist/object-array.d.ts +6 -8
- package/dist/object-array.js +19 -9
- package/dist/optional.d.ts +8 -3
- package/dist/page-communicate.d.ts +27 -0
- package/dist/page-communicate.js +70 -0
- package/dist/promise.js +2 -2
- package/dist/types.d.ts +4 -0
- package/dist/validate.js +2 -2
- package/dist/value-string-switch/index.d.ts +1 -0
- package/dist/value-string-switch/mask-data.d.ts +22 -0
- package/dist/value-string-switch.js +47 -2
- package/package.json +5 -5
- package/types/array-tree-switch.d.ts +0 -49
- package/types/index.d.ts +0 -6
- package/types/object-array.d.ts +0 -33
- package/types/optional.d.ts +0 -45
- package/types/promise.d.ts +0 -44
- package/types/validate.d.ts +0 -2
- package/types/value-string-switch/byte-string.d.ts +0 -12
- package/types/value-string-switch/index.d.ts +0 -1
|
@@ -0,0 +1,22 @@
|
|
|
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 {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @wxhccc/es-util v1.
|
|
3
|
-
* (c)
|
|
2
|
+
* @wxhccc/es-util v1.7.0
|
|
3
|
+
* (c) 2022 wxhccc
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
6
6
|
'use strict';
|
|
@@ -67,5 +67,50 @@ function byteStringify(byteNum, options) {
|
|
|
67
67
|
function byteParse(byteStr, options = {}) {
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
const modeIndexs = {
|
|
71
|
+
telphone: { startCharIndex: -8, endCharIndex: -5 },
|
|
72
|
+
idcard: { startCharIndex: -12, endCharIndex: -5 }
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* 对给定的字符串进行脱敏处理
|
|
76
|
+
* @param data 需要处理的字符串
|
|
77
|
+
* @param options 配置参数对象
|
|
78
|
+
* @returns
|
|
79
|
+
*/
|
|
80
|
+
function maskData(data, options) {
|
|
81
|
+
if (typeof data !== 'string' || !data) {
|
|
82
|
+
return '';
|
|
83
|
+
}
|
|
84
|
+
const modeOpts = options && options.mode && modeIndexs[options.mode] ? modeIndexs[options.mode] : {};
|
|
85
|
+
const { maskWith, startCharIndex: start = 0, endCharIndex: end, startFrom, endUntil } = { maskWith: '*', ...modeOpts, ...options };
|
|
86
|
+
let handleData = data;
|
|
87
|
+
let beforeChars = '';
|
|
88
|
+
let afterChars = '';
|
|
89
|
+
const startFromIndex = startFrom ? data.indexOf(startFrom) : -1;
|
|
90
|
+
if (startFromIndex > -1) {
|
|
91
|
+
const index = startFromIndex + startFrom.length;
|
|
92
|
+
handleData = handleData.slice(index);
|
|
93
|
+
beforeChars = data.slice(0, index);
|
|
94
|
+
}
|
|
95
|
+
const endUtilIndex = endUntil ? data.lastIndexOf(endUntil) : -1;
|
|
96
|
+
if (endUtilIndex > -1) {
|
|
97
|
+
handleData = handleData.slice(0, endUtilIndex);
|
|
98
|
+
afterChars = data.slice(endUtilIndex);
|
|
99
|
+
}
|
|
100
|
+
const chars = handleData.split('');
|
|
101
|
+
const newChars = [beforeChars];
|
|
102
|
+
const charLen = chars.length;
|
|
103
|
+
const indexs = [start >= 0 ? start : charLen + start, end !== undefined ? (end >= 0 ? end : charLen + end) : charLen - 1];
|
|
104
|
+
let [startIdx, endIdx] = indexs;
|
|
105
|
+
endIdx = endIdx > 0 && endIdx < charLen ? endIdx : charLen - 1;
|
|
106
|
+
startIdx = startIdx >= 0 && startIdx <= endIdx ? startIdx : 0;
|
|
107
|
+
chars.forEach((char, idx) => {
|
|
108
|
+
newChars.push(idx >= startIdx && idx <= endIdx ? maskWith : char);
|
|
109
|
+
});
|
|
110
|
+
newChars.push(afterChars);
|
|
111
|
+
return newChars.join('');
|
|
112
|
+
}
|
|
113
|
+
|
|
70
114
|
exports.byteParse = byteParse;
|
|
71
115
|
exports.byteStringify = byteStringify;
|
|
116
|
+
exports.maskData = maskData;
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxhccc/es-util",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
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": "
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
9
|
"scripts": {
|
|
10
10
|
"start": "npm run dev",
|
|
11
11
|
"test": "jest",
|
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
"idCard",
|
|
22
22
|
"validate",
|
|
23
23
|
"mapToObject",
|
|
24
|
-
"string-switch"
|
|
24
|
+
"string-switch",
|
|
25
|
+
"mask data"
|
|
25
26
|
],
|
|
26
27
|
"author": "wxhccc",
|
|
27
28
|
"license": "MIT",
|
|
@@ -60,8 +61,7 @@
|
|
|
60
61
|
"vue": "^3.0.11"
|
|
61
62
|
},
|
|
62
63
|
"files": [
|
|
63
|
-
"dist"
|
|
64
|
-
"types"
|
|
64
|
+
"dist"
|
|
65
65
|
],
|
|
66
66
|
"dependencies": {}
|
|
67
67
|
}
|
|
@@ -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
package/types/object-array.d.ts
DELETED
|
@@ -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 {};
|
package/types/optional.d.ts
DELETED
|
@@ -1,45 +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
|
-
/**
|
|
36
|
-
* 将搜素条件里的数组字段转换为其他格式,通常用于搜索表单的字段转换
|
|
37
|
-
* @param formData 传入参数
|
|
38
|
-
* @param maps 转换的映射关系,eg:{ date: ['startTime', 'endTime'] }
|
|
39
|
-
* @param filterNullableValue 是否需要过滤掉空数据项
|
|
40
|
-
*/
|
|
41
|
-
export declare const formParamsSwitch: <T extends AnyObject, R extends AnyObject = AnyObject>(formData: T, maps: ParamsSwitchMaps, options?: {
|
|
42
|
-
filterNullable?: boolean | undefined;
|
|
43
|
-
dateFormat?: ((date: any, format?: string | undefined) => string) | undefined;
|
|
44
|
-
} | undefined) => R;
|
|
45
|
-
export {};
|
package/types/promise.d.ts
DELETED
|
@@ -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 {};
|
package/types/validate.d.ts
DELETED
|
@@ -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 +0,0 @@
|
|
|
1
|
-
export * from './byte-string';
|