@wxhccc/es-util 1.3.0 → 1.5.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 -443
- package/dist/array-tree-switch.d.ts +49 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.esm.js +190 -62
- package/dist/index.js +194 -61
- package/dist/index.min.js +2 -2
- package/dist/object-array.d.ts +33 -0
- package/dist/object-array.js +1 -1
- package/dist/optional.d.ts +45 -0
- package/dist/promise.d.ts +37 -0
- package/dist/promise.js +82 -59
- package/dist/validate.d.ts +2 -0
- package/dist/validate.js +1 -1
- package/dist/value-string-switch/byte-string.d.ts +12 -0
- package/dist/value-string-switch/index.d.ts +1 -0
- package/dist/value-string-switch.js +1 -1
- package/package.json +67 -68
- package/types/index.d.ts +1 -0
- package/types/optional.d.ts +45 -0
- package/types/promise.d.ts +29 -8
- package/dist/object-array.es.js +0 -46
- package/dist/promise.es.js +0 -154
- package/dist/validate.es.js +0 -71
- package/dist/value-string-switch.es.js +0 -66
- package/types/tools.d.ts +0 -1
- package/types/value-string-switch/case-switch.d.ts +0 -7
package/dist/object-array.es.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* @wxhccc/es-util v1.2.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,154 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* @wxhccc/es-util v1.2.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
|
-
if (checkLock())
|
|
112
|
-
return;
|
|
113
|
-
corePromsie = wrap ? awaitWrapper(promise()) : promise();
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
corePromsie = wrap ? awaitWrapper(promise) : promise;
|
|
117
|
-
}
|
|
118
|
-
Object.defineProperties(corePromsie, {
|
|
119
|
-
__lockValue: { get: checkLock },
|
|
120
|
-
lock: {
|
|
121
|
-
value: (keyOrHookOrHandle, syncRefHandle) => {
|
|
122
|
-
const isRefHandle = (val) => Array.isArray(val) && val.length === 2;
|
|
123
|
-
if (typeof keyOrHookOrHandle === 'string') {
|
|
124
|
-
lockKey = keyOrHookOrHandle.split('.');
|
|
125
|
-
}
|
|
126
|
-
else if (isRefHandle(keyOrHookOrHandle)) {
|
|
127
|
-
lockRefHandle = keyOrHookOrHandle;
|
|
128
|
-
}
|
|
129
|
-
else if (typeof keyOrHookOrHandle === 'function') {
|
|
130
|
-
lockSwitchHook = keyOrHookOrHandle;
|
|
131
|
-
if (isRefHandle(syncRefHandle)) {
|
|
132
|
-
lockRefHandle = syncRefHandle;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
stateLock(true);
|
|
136
|
-
return corePromsie;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
corePromsie.finally(() => stateLock(false));
|
|
141
|
-
return corePromsie;
|
|
142
|
-
}
|
|
143
|
-
const wpVuePlugin = {
|
|
144
|
-
install(appOrVue) {
|
|
145
|
-
if (appOrVue.config && 'globalProperties' in appOrVue.config) {
|
|
146
|
-
appOrVue.config.globalProperties.$wp = wp;
|
|
147
|
-
}
|
|
148
|
-
else {
|
|
149
|
-
appOrVue.prototype.$wp = wp;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
export { awaitWrapper, objType, wp, wpVuePlugin };
|
package/dist/validate.es.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* @wxhccc/es-util v1.2.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.2.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 };
|
package/types/tools.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function awaitWrapper<T, K = Error>(promise: Promise<T>): Promise<[K, undefined] | [null, T]>;
|