@wxhccc/es-util 1.4.1 → 1.6.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/LICENSE +20 -20
- package/README.md +442 -442
- package/dist/index.d.ts +1 -0
- package/dist/index.esm.js +156 -6
- package/dist/index.js +162 -5
- package/dist/index.min.js +3 -3
- package/dist/object-array.js +2 -2
- package/dist/optional.d.ts +50 -0
- package/dist/promise.d.ts +4 -1
- package/dist/promise.js +3 -3
- 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 +68 -67
- package/types/index.d.ts +1 -0
- package/types/optional.d.ts +50 -0
- package/types/promise.d.ts +4 -1
- package/types/value-string-switch/index.d.ts +1 -0
- package/types/value-string-switch/mask-data.d.ts +22 -0
- package/dist/object-array.es.js +0 -46
- package/dist/promise.es.js +0 -152
- package/dist/validate.es.js +0 -71
- package/dist/value-string-switch.es.js +0 -66
package/package.json
CHANGED
|
@@ -1,67 +1,68 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@wxhccc/es-util",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "A library that contains some useful methods",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"module": "dist/index.esm.js",
|
|
7
|
-
"unpkg": "dist/index.min.js",
|
|
8
|
-
"types": "types/index.d.ts",
|
|
9
|
-
"scripts": {
|
|
10
|
-
"start": "npm run dev",
|
|
11
|
-
"test": "jest",
|
|
12
|
-
"dev": "rollup -w -c --environment NODE_ENV:development",
|
|
13
|
-
"build": "rollup -c"
|
|
14
|
-
},
|
|
15
|
-
"keywords": [
|
|
16
|
-
"array-tree-switch",
|
|
17
|
-
"array",
|
|
18
|
-
"tree",
|
|
19
|
-
"array2tree",
|
|
20
|
-
"tree2array",
|
|
21
|
-
"idCard",
|
|
22
|
-
"validate",
|
|
23
|
-
"mapToObject",
|
|
24
|
-
"string-switch"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
"@babel/
|
|
39
|
-
"@babel/preset-
|
|
40
|
-
"@
|
|
41
|
-
"@rollup/plugin-
|
|
42
|
-
"@rollup/plugin-
|
|
43
|
-
"@
|
|
44
|
-
"@
|
|
45
|
-
"@typescript-eslint/
|
|
46
|
-
"@
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"enzyme
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"react
|
|
55
|
-
"
|
|
56
|
-
"rollup
|
|
57
|
-
"rollup-plugin-
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
"
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@wxhccc/es-util",
|
|
3
|
+
"version": "1.6.0",
|
|
4
|
+
"description": "A library that contains some useful methods",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.esm.js",
|
|
7
|
+
"unpkg": "dist/index.min.js",
|
|
8
|
+
"types": "types/index.d.ts",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"start": "npm run dev",
|
|
11
|
+
"test": "jest",
|
|
12
|
+
"dev": "rollup -w -c --environment NODE_ENV:development",
|
|
13
|
+
"build": "rollup -c"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"array-tree-switch",
|
|
17
|
+
"array",
|
|
18
|
+
"tree",
|
|
19
|
+
"array2tree",
|
|
20
|
+
"tree2array",
|
|
21
|
+
"idCard",
|
|
22
|
+
"validate",
|
|
23
|
+
"mapToObject",
|
|
24
|
+
"string-switch",
|
|
25
|
+
"mask data"
|
|
26
|
+
],
|
|
27
|
+
"author": "wxhccc",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/wxhccc/es-util.git"
|
|
32
|
+
},
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/wxhccc/es-util/issues"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://github.com/wxhccc/es-util#readme",
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@babel/core": "^7.3.3",
|
|
39
|
+
"@babel/preset-env": "^7.3.1",
|
|
40
|
+
"@babel/preset-react": "^7.13.13",
|
|
41
|
+
"@rollup/plugin-babel": "^5.3.0",
|
|
42
|
+
"@rollup/plugin-commonjs": "^18.0.0",
|
|
43
|
+
"@rollup/plugin-node-resolve": "^11.2.1",
|
|
44
|
+
"@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",
|
|
48
|
+
"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",
|
|
57
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
58
|
+
"rollup-plugin-typescript2": "^0.30.0",
|
|
59
|
+
"tslib": "^1.10.0",
|
|
60
|
+
"typescript": "^4.2.4",
|
|
61
|
+
"vue": "^3.0.11"
|
|
62
|
+
},
|
|
63
|
+
"files": [
|
|
64
|
+
"dist",
|
|
65
|
+
"types"
|
|
66
|
+
],
|
|
67
|
+
"dependencies": {}
|
|
68
|
+
}
|
package/types/index.d.ts
CHANGED
|
@@ -0,0 +1,50 @@
|
|
|
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 {};
|
package/types/promise.d.ts
CHANGED
|
@@ -34,7 +34,10 @@ declare function wrapPromise<T>(this: any, promise: WpPromise<T>, options?: WpOp
|
|
|
34
34
|
declare function wrapPromise<T>(this: any, promise: WpPromise<T>, options?: WpOptions & {
|
|
35
35
|
wrap: true;
|
|
36
36
|
}): WpArrReturn<T>;
|
|
37
|
-
|
|
37
|
+
declare type WP = typeof wrapPromise & {
|
|
38
|
+
_checkLockKey: (key: string) => boolean;
|
|
39
|
+
};
|
|
40
|
+
export declare const wp: WP;
|
|
38
41
|
export declare const wpVuePlugin: {
|
|
39
42
|
install(appOrVue: any, key?: string): void;
|
|
40
43
|
};
|
|
@@ -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 {};
|
package/dist/object-array.es.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* @wxhccc/es-util v1.3.0
|
|
3
|
-
* (c) 2021 wxhccc
|
|
4
|
-
* @license MIT
|
|
5
|
-
*/
|
|
6
|
-
function mapToObject(objectArray, keyProp = 'key', valueProp = 'value') {
|
|
7
|
-
let result = {};
|
|
8
|
-
Array.isArray(objectArray) && keyProp && valueProp
|
|
9
|
-
&& [keyProp, valueProp].every(val => ['string', 'function'].includes(typeof val))
|
|
10
|
-
&& objectArray.forEach((item, index) => {
|
|
11
|
-
const key = typeof keyProp === 'string' ? item[keyProp] : keyProp(item, index);
|
|
12
|
-
const value = typeof valueProp === 'string' ? item[valueProp] : valueProp(item, index);
|
|
13
|
-
['string', 'number'].includes(typeof key) && (result[key] = value);
|
|
14
|
-
});
|
|
15
|
-
return result;
|
|
16
|
-
}
|
|
17
|
-
function checkoutBy(object, keys, mergeFn) {
|
|
18
|
-
if (!(object instanceof Object))
|
|
19
|
-
return [];
|
|
20
|
-
if (Array.isArray(keys)) {
|
|
21
|
-
return keys.map(item => (object[item]));
|
|
22
|
-
}
|
|
23
|
-
else if (keys && keys instanceof Object) {
|
|
24
|
-
return Object.keys(keys).map(item => (object[item] instanceof Object && keys[item] instanceof Object)
|
|
25
|
-
? (mergeFn ? mergeFn({}, object[item], keys[item]) : Object.assign({}, object[item], keys[item]))
|
|
26
|
-
: (keys[item] || object[item]));
|
|
27
|
-
}
|
|
28
|
-
return Object.values(object);
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* pick and rename object values
|
|
32
|
-
*
|
|
33
|
-
* @param {Object} object
|
|
34
|
-
* @param {Object} keysMap
|
|
35
|
-
*/
|
|
36
|
-
function pickRenameKeys(object, keysMap) {
|
|
37
|
-
if (!object || !(object instanceof Object) || !keysMap || !(keysMap instanceof Object))
|
|
38
|
-
return;
|
|
39
|
-
let result = {};
|
|
40
|
-
for (let i in object) {
|
|
41
|
-
(i in keysMap) && (result[keysMap[i]] = object[i]);
|
|
42
|
-
}
|
|
43
|
-
return result;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export { checkoutBy, mapToObject, pickRenameKeys };
|
package/dist/promise.es.js
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* @wxhccc/es-util v1.3.0
|
|
3
|
-
* (c) 2021 wxhccc
|
|
4
|
-
* @license MIT
|
|
5
|
-
*/
|
|
6
|
-
const { hasOwnProperty, toString } = Object.prototype;
|
|
7
|
-
const objType = (val) => {
|
|
8
|
-
const typeKeys = toString.call(val).match(/^\[object (.*)\]$/);
|
|
9
|
-
return typeKeys ? typeKeys[1] : '';
|
|
10
|
-
};
|
|
11
|
-
/**
|
|
12
|
-
* wrap promise and handle reject or err by return an array like [error, undefined]
|
|
13
|
-
* @param promise promise
|
|
14
|
-
* @returns Promise<[K, undefined] | [null, T]>
|
|
15
|
-
*/
|
|
16
|
-
async function awaitWrapper(promise) {
|
|
17
|
-
try {
|
|
18
|
-
const data = await promise;
|
|
19
|
-
return [null, data];
|
|
20
|
-
}
|
|
21
|
-
catch (err) {
|
|
22
|
-
return [err, undefined];
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
function checkContext(context) {
|
|
26
|
-
if (!context)
|
|
27
|
-
return 'unknown';
|
|
28
|
-
if (context._isVue || (context.$ && context.$.vnode)) {
|
|
29
|
-
return 'vue';
|
|
30
|
-
}
|
|
31
|
-
else if ('setState' in context) {
|
|
32
|
-
return 'react';
|
|
33
|
-
}
|
|
34
|
-
return 'unknown';
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* wrap promise with lock, support reactive environment like react or vue
|
|
38
|
-
* @param promise promise
|
|
39
|
-
* @param wrap whether use awaitWrap to wrap result
|
|
40
|
-
*/
|
|
41
|
-
const lockCtx = {};
|
|
42
|
-
function wp(promise, wrap) {
|
|
43
|
-
const contextType = checkContext(this);
|
|
44
|
-
const isReactiveIns = contextType !== 'unknown';
|
|
45
|
-
const context = isReactiveIns ? this : lockCtx;
|
|
46
|
-
const stateKey = contextType === 'react' ? 'state' : '';
|
|
47
|
-
const contextState = stateKey ? context[stateKey] : context;
|
|
48
|
-
const has = (val, key) => !!val && hasOwnProperty.call(val, key);
|
|
49
|
-
const isObj = (obj) => objType(obj) === 'Object';
|
|
50
|
-
const setValue = (obj, path, value) => {
|
|
51
|
-
// if vue2 and path[0] not defined, do nothing
|
|
52
|
-
if (contextType === 'vue' && context.$set && !has(obj, path[0]))
|
|
53
|
-
return;
|
|
54
|
-
const { $set = (o, key, val) => { o[key] = val; } } = context;
|
|
55
|
-
const isStateRect = contextType === 'react';
|
|
56
|
-
const originObj = isStateRect ? { ...obj } : obj;
|
|
57
|
-
let curObj = originObj;
|
|
58
|
-
let canSet = false;
|
|
59
|
-
for (let i = 0; i < path.length; i++) {
|
|
60
|
-
const key = path[i];
|
|
61
|
-
const keyExist = has(curObj, key);
|
|
62
|
-
if (i === path.length - 1) {
|
|
63
|
-
const isBool = typeof curObj[key] === 'boolean';
|
|
64
|
-
canSet = !keyExist || isBool;
|
|
65
|
-
canSet && $set(curObj, key, value);
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
!keyExist && $set(curObj, key, {});
|
|
69
|
-
if (!isObj(curObj[key]))
|
|
70
|
-
break;
|
|
71
|
-
isStateRect && (curObj[key] = { ...curObj[key] });
|
|
72
|
-
curObj = curObj[key];
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
// trigger setState when run in react class component
|
|
76
|
-
isStateRect && canSet && context.setState({ [path[0]]: originObj[path[0]] });
|
|
77
|
-
};
|
|
78
|
-
const getValue = (obj, path) => {
|
|
79
|
-
// use refHandle if contextState not update sync
|
|
80
|
-
if (lockRefHandle)
|
|
81
|
-
return lockRefHandle[0][lockRefHandle[1]];
|
|
82
|
-
let result = false;
|
|
83
|
-
if (obj && isObj(obj) && Array.isArray(path)) {
|
|
84
|
-
let curObj = obj;
|
|
85
|
-
for (let i = 0; i < path.length; i++) {
|
|
86
|
-
const key = path[i];
|
|
87
|
-
if (typeof curObj !== 'object' || !has(curObj, key)) {
|
|
88
|
-
break;
|
|
89
|
-
}
|
|
90
|
-
curObj = curObj[key];
|
|
91
|
-
i === path.length - 1 &&
|
|
92
|
-
(result = typeof curObj === 'boolean' ? curObj : false);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
return result;
|
|
96
|
-
};
|
|
97
|
-
const stateLock = (bool) => {
|
|
98
|
-
if (lockKey.length)
|
|
99
|
-
return setValue(contextState, lockKey, bool);
|
|
100
|
-
if (lockRefHandle)
|
|
101
|
-
lockRefHandle[0][lockRefHandle[1]] = bool;
|
|
102
|
-
if (lockSwitchHook)
|
|
103
|
-
lockSwitchHook(bool);
|
|
104
|
-
};
|
|
105
|
-
const checkLock = () => getValue(contextState, lockKey);
|
|
106
|
-
let lockSwitchHook;
|
|
107
|
-
let lockRefHandle;
|
|
108
|
-
let lockKey = [];
|
|
109
|
-
let corePromsie;
|
|
110
|
-
if (typeof promise === 'function') {
|
|
111
|
-
corePromsie = checkLock() ? Promise.reject(new Error('you can\'t recall promise method when it\'s locking')).catch(() => undefined) : wrap ? awaitWrapper(promise()) : promise();
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
corePromsie = wrap ? awaitWrapper(promise) : promise;
|
|
115
|
-
}
|
|
116
|
-
Object.defineProperties(corePromsie, {
|
|
117
|
-
__lockValue: { get: checkLock },
|
|
118
|
-
lock: {
|
|
119
|
-
value: (keyOrHookOrHandle, syncRefHandle) => {
|
|
120
|
-
const isRefHandle = (val) => Array.isArray(val) && val.length === 2;
|
|
121
|
-
if (typeof keyOrHookOrHandle === 'string') {
|
|
122
|
-
lockKey = keyOrHookOrHandle.split('.');
|
|
123
|
-
}
|
|
124
|
-
else if (isRefHandle(keyOrHookOrHandle)) {
|
|
125
|
-
lockRefHandle = keyOrHookOrHandle;
|
|
126
|
-
}
|
|
127
|
-
else if (typeof keyOrHookOrHandle === 'function') {
|
|
128
|
-
lockSwitchHook = keyOrHookOrHandle;
|
|
129
|
-
if (isRefHandle(syncRefHandle)) {
|
|
130
|
-
lockRefHandle = syncRefHandle;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
stateLock(true);
|
|
134
|
-
return corePromsie;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
corePromsie.finally(() => stateLock(false));
|
|
139
|
-
return corePromsie;
|
|
140
|
-
}
|
|
141
|
-
const wpVuePlugin = {
|
|
142
|
-
install(appOrVue) {
|
|
143
|
-
if (appOrVue.config && 'globalProperties' in appOrVue.config) {
|
|
144
|
-
appOrVue.config.globalProperties.$wp = wp;
|
|
145
|
-
}
|
|
146
|
-
else {
|
|
147
|
-
appOrVue.prototype.$wp = wp;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
export { awaitWrapper, objType, wp, wpVuePlugin };
|
package/dist/validate.es.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* @wxhccc/es-util v1.3.0
|
|
3
|
-
* (c) 2021 wxhccc
|
|
4
|
-
* @license MIT
|
|
5
|
-
*/
|
|
6
|
-
/* 身份证正则 */
|
|
7
|
-
const idCardReg = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i;
|
|
8
|
-
/* 身份证合法性验证 */
|
|
9
|
-
function ChinaIdCardValid(idNo) {
|
|
10
|
-
const idNumber = idNo.toString().toUpperCase();
|
|
11
|
-
const provCodes = ['11', '12', '13', '14', '15', '21', '22', '23', '31', '32', '33', '34', '35', '36', '37', '41', '42', '43', '44', '45', '46', '50', '51', '52', '53', '54', '61', '62', '63', '64', '65', '71', '81', '82', '91'];
|
|
12
|
-
const lastCodeCheck = () => {
|
|
13
|
-
if (idNumber.length === 18) {
|
|
14
|
-
const chars = idNumber.split('');
|
|
15
|
-
// ∑(ai×Wi)(mod 11)
|
|
16
|
-
// 加权因子
|
|
17
|
-
const factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
|
|
18
|
-
// 校验位
|
|
19
|
-
const parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
|
|
20
|
-
let sum = 0;
|
|
21
|
-
let ai = 0;
|
|
22
|
-
let wi = 0;
|
|
23
|
-
for (let i = 0; i < 17; i++) {
|
|
24
|
-
ai = parseInt(chars[i]);
|
|
25
|
-
wi = factor[i];
|
|
26
|
-
sum += ai * wi;
|
|
27
|
-
}
|
|
28
|
-
const last = parity[sum % 11];
|
|
29
|
-
return last.toString() === chars[17];
|
|
30
|
-
}
|
|
31
|
-
return true;
|
|
32
|
-
};
|
|
33
|
-
return idCardReg.test(idNumber) && provCodes.includes(idNumber.substr(0, 2)) && lastCodeCheck();
|
|
34
|
-
}
|
|
35
|
-
// 公式验证函数
|
|
36
|
-
function formulaValidate(string, variables) {
|
|
37
|
-
string = string.replace(/\s/g, ''); // 剔除空白符
|
|
38
|
-
const sc = /[+\-*/]{2,}/; // 错误情况,运算符连续
|
|
39
|
-
const eb = /\(\)/; // 空括号
|
|
40
|
-
const sl = /\([+\-*/]/; // 错误情况,(后面是运算符
|
|
41
|
-
const bs = /[+\-*/]\)/; // 错误情况,)前面是运算符
|
|
42
|
-
const bns = /[^+\-*/]\(/; // 错误情况,(前面不是运算符
|
|
43
|
-
const blns = /\)[^+\-*/]/; // 错误情况,)后面不是运算符
|
|
44
|
-
const bmatch = () => {
|
|
45
|
-
let stack = [];
|
|
46
|
-
for (let i = 0, item; i < string.length; i++) {
|
|
47
|
-
item = string.charAt(i);
|
|
48
|
-
if (item === '(') {
|
|
49
|
-
stack.push('(');
|
|
50
|
-
}
|
|
51
|
-
else if (item === ')') {
|
|
52
|
-
if (stack.length > 0) {
|
|
53
|
-
stack.pop();
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
return false;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return !stack.length;
|
|
61
|
-
};
|
|
62
|
-
// 错误情况,变量没有来自“待选公式变量”
|
|
63
|
-
const varInList = (vars) => {
|
|
64
|
-
const tmpStr = string.replace(/[()+\-*/]{1,}/g, '`');
|
|
65
|
-
const array = tmpStr.split('`');
|
|
66
|
-
return array.every(item => (!/[A-Z]/i.test(item) || vars.includes(item)));
|
|
67
|
-
};
|
|
68
|
-
return !sc.test(string) && !eb.test(string) && !sl.test(string) && !bs.test(string) && !bns.test(string) && !blns.test(string) && bmatch() && (!Array.isArray(variables) || varInList(variables));
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export { ChinaIdCardValid, formulaValidate };
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* @wxhccc/es-util v1.3.0
|
|
3
|
-
* (c) 2021 wxhccc
|
|
4
|
-
* @license MIT
|
|
5
|
-
*/
|
|
6
|
-
//
|
|
7
|
-
const configCreater = (standard, unit) => {
|
|
8
|
-
const unitMap = {
|
|
9
|
-
B: { expMax: 0, units: { metric: 'B', iec: 'B', jedec: 'B' } },
|
|
10
|
-
K: { expMax: 1, units: { metric: 'kB', iec: 'KiB', jedec: 'KB' } },
|
|
11
|
-
M: { expMax: 2, units: { metric: 'MB', iec: 'MiB', jedec: 'MB' } },
|
|
12
|
-
G: { expMax: 3, units: { metric: 'GB', iec: 'GiB', jedec: 'GB' } },
|
|
13
|
-
T: { expMax: 4, units: { metric: 'TB', iec: 'TiB', jedec: 'TB' } },
|
|
14
|
-
P: { expMax: 5, units: { metric: 'PB', iec: 'PiB', jedec: 'PB' } },
|
|
15
|
-
E: { expMax: 6, units: { metric: 'EB', iec: 'EiB', jedec: 'EB' } },
|
|
16
|
-
Z: { expMax: 7, units: { metric: 'ZB', iec: 'ZiB', jedec: 'ZB' } },
|
|
17
|
-
Y: { expMax: 8, units: { metric: 'YB', iec: 'YiB', jedec: 'YB' } }
|
|
18
|
-
};
|
|
19
|
-
const standBase = { metric: 1000, iec: 1024, jedec: 1024 };
|
|
20
|
-
return {
|
|
21
|
-
base: standBase[standard] || standBase['jedec'],
|
|
22
|
-
unitMap: unitMap[unit] || Object.values(unitMap)
|
|
23
|
-
};
|
|
24
|
-
};
|
|
25
|
-
// translate byte num to string
|
|
26
|
-
function byteStringify(byteNum, options) {
|
|
27
|
-
const opts = Object.assign({
|
|
28
|
-
standard: 'jedec',
|
|
29
|
-
unitLvl: 'auto',
|
|
30
|
-
precision: 1,
|
|
31
|
-
detail: false
|
|
32
|
-
}, options);
|
|
33
|
-
const { standard, unitLvl, precision, detail } = opts;
|
|
34
|
-
let byteNumber = parseFloat(byteNum);
|
|
35
|
-
if (!Number.isFinite(byteNumber))
|
|
36
|
-
return;
|
|
37
|
-
const negative = byteNumber < 0 ? -1 : 1;
|
|
38
|
-
byteNumber = Math.abs(byteNumber);
|
|
39
|
-
const { base, unitMap } = configCreater(standard, unitLvl);
|
|
40
|
-
const getValue = (value) => Number.isInteger(value) ? value : value.toFixed(precision);
|
|
41
|
-
let resUnit = '';
|
|
42
|
-
let resValue = 0;
|
|
43
|
-
if (!Array.isArray(unitMap)) {
|
|
44
|
-
const maxValue = Math.pow(base, unitMap.expMax);
|
|
45
|
-
resUnit = unitMap.units[standard];
|
|
46
|
-
const value = byteNumber / maxValue;
|
|
47
|
-
resValue = getValue(value * negative);
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
unitMap.some(item => {
|
|
51
|
-
const maxValue = Math.pow(base, item.expMax);
|
|
52
|
-
const value = byteNumber / maxValue;
|
|
53
|
-
if (value > base)
|
|
54
|
-
return false;
|
|
55
|
-
resUnit = item.units[standard];
|
|
56
|
-
resValue = getValue(value * negative);
|
|
57
|
-
return true;
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
return detail ? { value: resValue, unit: resUnit } : `${resValue} ${resUnit}`;
|
|
61
|
-
}
|
|
62
|
-
// translate string to byte num
|
|
63
|
-
function byteParse(byteStr, options = {}) {
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export { byteParse, byteStringify };
|