@wxhccc/es-util 1.5.1 → 1.5.2
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/dist/index.esm.js +8 -6
- package/dist/index.js +8 -5
- package/dist/index.min.js +2 -2
- package/dist/object-array.js +1 -1
- package/dist/optional.d.ts +8 -3
- package/dist/promise.js +1 -1
- package/dist/validate.js +1 -1
- package/dist/value-string-switch.js +1 -1
- package/package.json +1 -1
- package/types/optional.d.ts +8 -3
package/dist/index.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @wxhccc/es-util v1.5.
|
|
2
|
+
* @wxhccc/es-util v1.5.2
|
|
3
3
|
* (c) 2021 wxhccc
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
@@ -473,7 +473,7 @@ const switchNullToUndefined = (data) => {
|
|
|
473
473
|
* 将搜素条件里的数组字段转换为其他格式,通常用于搜索表单的字段转换
|
|
474
474
|
* @param formData 传入参数
|
|
475
475
|
* @param maps 转换的映射关系,eg:{ date: ['startTime', 'endTime'] }
|
|
476
|
-
* @param
|
|
476
|
+
* @param options 额外的配置参数
|
|
477
477
|
*/
|
|
478
478
|
const formParamsSwitch = (formData, maps, options) => {
|
|
479
479
|
const dateKeys = Object.keys(maps);
|
|
@@ -495,10 +495,10 @@ const formParamsSwitch = (formData, maps, options) => {
|
|
|
495
495
|
const { filterNullable: filterNullableValue, dateFormat } = { filterNullable: true, ...options };
|
|
496
496
|
const handleData = accessKeys.reduce((acc, key) => {
|
|
497
497
|
const values = dateData[key];
|
|
498
|
-
|
|
498
|
+
const mapValue = maps[key];
|
|
499
|
+
if (!values || !mapValue) {
|
|
499
500
|
return acc;
|
|
500
501
|
}
|
|
501
|
-
const mapValue = maps[key];
|
|
502
502
|
// 数组合并操作/取最后一位操作
|
|
503
503
|
if (mapValue === 'join' || mapValue === 'pop') {
|
|
504
504
|
if (Array.isArray(values) && values.length) {
|
|
@@ -523,6 +523,8 @@ const formParamsSwitch = (formData, maps, options) => {
|
|
|
523
523
|
...handleData
|
|
524
524
|
};
|
|
525
525
|
return filterNullableValue ? filterNullable(result) : result;
|
|
526
|
-
};
|
|
526
|
+
};
|
|
527
|
+
/** 创建一个绑定了默认时间格式转换函数的formParamsSwitch函数 */
|
|
528
|
+
const formParamsSwitchCreator = (dateFormat) => (formData, maps, options) => formParamsSwitch(formData, maps, { dateFormat, ...options });
|
|
527
529
|
|
|
528
|
-
export { ChinaIdCardValid, array2tree, awaitWrapper, byteParse, byteStringify, checkoutBy, filterNullable, formParamsSwitch, formulaValidate, isNullable, mapToObject, objType, pickRenameKeys, safeJsonParse, switchNullToUndefined, tree2array, wp, wpVuePlugin };
|
|
530
|
+
export { ChinaIdCardValid, array2tree, awaitWrapper, byteParse, byteStringify, checkoutBy, filterNullable, formParamsSwitch, formParamsSwitchCreator, formulaValidate, isNullable, mapToObject, objType, pickRenameKeys, safeJsonParse, switchNullToUndefined, tree2array, wp, wpVuePlugin };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @wxhccc/es-util v1.5.
|
|
2
|
+
* @wxhccc/es-util v1.5.2
|
|
3
3
|
* (c) 2021 wxhccc
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
@@ -477,7 +477,7 @@ const switchNullToUndefined = (data) => {
|
|
|
477
477
|
* 将搜素条件里的数组字段转换为其他格式,通常用于搜索表单的字段转换
|
|
478
478
|
* @param formData 传入参数
|
|
479
479
|
* @param maps 转换的映射关系,eg:{ date: ['startTime', 'endTime'] }
|
|
480
|
-
* @param
|
|
480
|
+
* @param options 额外的配置参数
|
|
481
481
|
*/
|
|
482
482
|
const formParamsSwitch = (formData, maps, options) => {
|
|
483
483
|
const dateKeys = Object.keys(maps);
|
|
@@ -499,10 +499,10 @@ const formParamsSwitch = (formData, maps, options) => {
|
|
|
499
499
|
const { filterNullable: filterNullableValue, dateFormat } = { filterNullable: true, ...options };
|
|
500
500
|
const handleData = accessKeys.reduce((acc, key) => {
|
|
501
501
|
const values = dateData[key];
|
|
502
|
-
|
|
502
|
+
const mapValue = maps[key];
|
|
503
|
+
if (!values || !mapValue) {
|
|
503
504
|
return acc;
|
|
504
505
|
}
|
|
505
|
-
const mapValue = maps[key];
|
|
506
506
|
// 数组合并操作/取最后一位操作
|
|
507
507
|
if (mapValue === 'join' || mapValue === 'pop') {
|
|
508
508
|
if (Array.isArray(values) && values.length) {
|
|
@@ -527,7 +527,9 @@ const formParamsSwitch = (formData, maps, options) => {
|
|
|
527
527
|
...handleData
|
|
528
528
|
};
|
|
529
529
|
return filterNullableValue ? filterNullable(result) : result;
|
|
530
|
-
};
|
|
530
|
+
};
|
|
531
|
+
/** 创建一个绑定了默认时间格式转换函数的formParamsSwitch函数 */
|
|
532
|
+
const formParamsSwitchCreator = (dateFormat) => (formData, maps, options) => formParamsSwitch(formData, maps, { dateFormat, ...options });
|
|
531
533
|
|
|
532
534
|
exports.ChinaIdCardValid = ChinaIdCardValid;
|
|
533
535
|
exports.array2tree = array2tree;
|
|
@@ -537,6 +539,7 @@ exports.byteStringify = byteStringify;
|
|
|
537
539
|
exports.checkoutBy = checkoutBy;
|
|
538
540
|
exports.filterNullable = filterNullable;
|
|
539
541
|
exports.formParamsSwitch = formParamsSwitch;
|
|
542
|
+
exports.formParamsSwitchCreator = formParamsSwitchCreator;
|
|
540
543
|
exports.formulaValidate = formulaValidate;
|
|
541
544
|
exports.isNullable = isNullable;
|
|
542
545
|
exports.mapToObject = mapToObject;
|
package/dist/index.min.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @wxhccc/es-util v1.5.
|
|
2
|
+
* @wxhccc/es-util v1.5.2
|
|
3
3
|
* (c) 2021 wxhccc
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
6
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Smartfetch={})}(this,(function(e){"use strict";const{hasOwnProperty:t}=Object.prototype;function n(e,t=[]){return{[e]:t}}const r=/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i;const{hasOwnProperty:i,toString:c}=Object.prototype,o=e=>{const t=c.call(e).match(/^\[object (.*)\]$/);return t?t[1]:""};async function
|
|
6
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Smartfetch={})}(this,(function(e){"use strict";const{hasOwnProperty:t}=Object.prototype;function n(e,t=[]){return{[e]:t}}const r=/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i;const{hasOwnProperty:i,toString:c}=Object.prototype,o=e=>{const t=c.call(e).match(/^\[object (.*)\]$/);return t?t[1]:""};async function a(e){try{return[null,await e]}catch(e){return[e,void 0]}}const s={};function l(e,t){const n=function(e){return e?e._isVue||e.$&&e.$.vnode?"vue":"setState"in e?"react":"unknown":"unknown"}(this),r="unknown"!==n,c=r?this:s,l="react"===n?"state":"",u=l?c[l]:c,{wrap:f,lock:p,syncRefHandle:y,manualUnlock:d}="boolean"==typeof t?{wrap:t}:{...t};let b,h,j=[],g=!1,O=!1;const m=(e,t)=>!!e&&i.call(e,t),B=e=>"Object"===o(e),x=e=>{j.length?r?((e,t,r)=>{if("vue"===n&&c.$set&&!m(e,t[0]))return;const{$set:i=((e,t,n)=>{e[t]=n})}=c,o="react"===n,a=o?{...e}:e;let s=a,l=!1;for(let e=0;e<t.length;e++){const n=t[e],c=m(s,n);if(e===t.length-1){const e="boolean"==typeof s[n];l=!c||e,l&&i(s,n,r)}else{if(!c&&i(s,n,{}),!B(s[n]))break;o&&(s[n]={...s[n]}),s=s[n]}}o&&l&&c.setState({[t[0]]:a[t[0]]})})(u,j,e):s[p]=e:(h&&(h[0][h[1]]=e),b&&b(e))},A=()=>h?h[0][h[1]]:r||"string"!=typeof p?((e,t)=>{let n=!1;if(e&&B(e)&&Array.isArray(t)&&t.length){let r=e;for(let e=0;e<t.length;e++){const i=t[e];if("object"!=typeof r||!m(r,i))break;r=r[i],e===t.length-1&&(n="boolean"==typeof r&&r)}}return n})(u,j):u[p];if(p){const e=e=>Array.isArray(e)&&2===e.length;"string"==typeof p?j=p.split("."):e(p)?h=p:"function"==typeof p&&(b=p,e(y)&&(h=y)),g=j.length>0||!!h||!!b,g&&(O=A())}let k;k="function"==typeof e?O?(()=>{const e=Promise.resolve(void 0);return Object.assign(e,{then(...t){const[n]=t;return 2===t.length&&"function"==typeof n?n(void 0):e},catch:t=>e,finally:()=>e})})():f?a(e()):e():f?a(e):e;const K=()=>x(!1);return Object.defineProperties(k,{__lockValue:{get:A},_checkLockKey:{value:e=>s[e]},unlock:{value:K}}),g&&!O&&(x(!0),!d&&k.finally(K)),k}const u=Object.defineProperty(l,"_checkLockKey",{value:e=>s[e]}),f={install(e,t="$wp"){e.config&&"globalProperties"in e.config?e.config.globalProperties[t]=l:e.prototype[t]=l}};const p=(e,t)=>{const n={};return Object.keys(e).forEach((r=>{const i=e[r];""!==i&&null!=i&&(n[r]=t&&"[object Object]"===Object.prototype.toString.call(i)?p(i):i)})),n},{toString:y}=Object.prototype,d=e=>{if(null!==e)return Array.isArray(e)?e.map(d):("[object Object]"===y.call(e)&&Object.keys(e).forEach((t=>{e[t]=d(e[t])})),e)},b=(e,t,n)=>{const r=Object.keys(t),i={},c={};Object.keys(e).forEach((t=>{r.includes(t)?i[t]=e[t]:c[t]=e[t]}));const o=Object.keys(i);if(0===o.length)return e;const{filterNullable:a,dateFormat:s}={filterNullable:!0,...n},l=o.reduce(((e,n)=>{const r=i[n],c=t[n];if(!r||!c)return e;if("join"===c||"pop"===c)Array.isArray(r)&&r.length&&(e[n]="join"===c?r.join(","):r.slice(-1)[0]);else if(2===c.length||3===c.length){if(!r||2!==r.length)return e;const[t,n,i]=c,[o,a]=r;Object.assign(e,{[t]:s instanceof Function?s(o,i):o,[n]:s instanceof Function?s(a,i):a})}return e}),{}),u={...c,...l};return a?p(u):u};e.ChinaIdCardValid=function(e){const t=e.toString().toUpperCase();return r.test(t)&&["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"].includes(t.substr(0,2))&&(()=>{if(18===t.length){const e=t.split(""),n=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2],r=[1,0,"X",9,8,7,6,5,4,3,2];let i=0,c=0,o=0;for(let t=0;t<17;t++)c=parseInt(e[t]),o=n[t],i+=c*o;return r[i%11].toString()===e[17]}return!0})()},e.array2tree=function(e,r={}){const{primaryKey:i,parentKey:c,childrenKey:o,createRoot:a,parentRefKey:s}=Object.assign({primaryKey:"id",parentKey:"pid",childrenKey:"children",createRoot:!1,parentRefKey:!1},r),l=s&&("string"==typeof s?s:"_parent");let u={},f=[];return Array.isArray(e)&&e.forEach((e=>{!t.call(u,e[i])&&(u[e[i]]=Object.assign({},e,n(o))),t.call(u,e[c])&&u[e[c]][o].push(Object.assign(u[e[i]],l?{[l]:u[e[c]]}:{})),!e[c]&&f.push(u[e[i]])})),"function"==typeof a?a(f):a?{[o]:f}:f},e.awaitWrapper=a,e.byteParse=function(e,t={}){},e.byteStringify=function(e,t){const n=Object.assign({standard:"jedec",unitLvl:"auto",precision:1,detail:!1},t),{standard:r,unitLvl:i,precision:c,detail:o}=n;let a=parseFloat(e);if(!Number.isFinite(a))return;const s=a<0?-1:1;a=Math.abs(a);const{base:l,unitMap:u}=((e,t)=>{const n={B:{expMax:0,units:{metric:"B",iec:"B",jedec:"B"}},K:{expMax:1,units:{metric:"kB",iec:"KiB",jedec:"KB"}},M:{expMax:2,units:{metric:"MB",iec:"MiB",jedec:"MB"}},G:{expMax:3,units:{metric:"GB",iec:"GiB",jedec:"GB"}},T:{expMax:4,units:{metric:"TB",iec:"TiB",jedec:"TB"}},P:{expMax:5,units:{metric:"PB",iec:"PiB",jedec:"PB"}},E:{expMax:6,units:{metric:"EB",iec:"EiB",jedec:"EB"}},Z:{expMax:7,units:{metric:"ZB",iec:"ZiB",jedec:"ZB"}},Y:{expMax:8,units:{metric:"YB",iec:"YiB",jedec:"YB"}}},r={metric:1e3,iec:1024,jedec:1024};return{base:r[e]||r.jedec,unitMap:n[t]||Object.values(n)}})(r,i),f=e=>Number.isInteger(e)?e:e.toFixed(c);let p="",y=0;if(Array.isArray(u))u.some((e=>{const t=Math.pow(l,e.expMax),n=a/t;return!(n>l)&&(p=e.units[r],y=f(n*s),!0)}));else{const e=Math.pow(l,u.expMax);p=u.units[r];y=f(a/e*s)}return o?{value:y,unit:p}:`${y} ${p}`},e.checkoutBy=function(e,t,n){return e instanceof Object?Array.isArray(t)?t.map((t=>e[t])):t&&t instanceof Object?Object.keys(t).map((r=>e[r]instanceof Object&&t[r]instanceof Object?n?n({},e[r],t[r]):Object.assign({},e[r],t[r]):t[r]||e[r])):Object.values(e):[]},e.filterNullable=p,e.formParamsSwitch=b,e.formParamsSwitchCreator=e=>(t,n,r)=>b(t,n,{dateFormat:e,...r}),e.formulaValidate=function(e,t){return e=e.replace(/\s/g,""),!/[+\-*/]{2,}/.test(e)&&!/\(\)/.test(e)&&!/\([+\-*/]/.test(e)&&!/[+\-*/]\)/.test(e)&&!/[^+\-*/]\(/.test(e)&&!/\)[^+\-*/]/.test(e)&&(()=>{let t=[];for(let n,r=0;r<e.length;r++)if(n=e.charAt(r),"("===n)t.push("(");else if(")"===n){if(!(t.length>0))return!1;t.pop()}return!t.length})()&&(!Array.isArray(t)||(n=t,e.replace(/[()+\-*/]{1,}/g,"`").split("`").every((e=>!/[A-Z]/i.test(e)||n.includes(e)))));var n},e.isNullable=e=>""===e||null==e,e.mapToObject=function(e,t="key",n="value"){let r={};return Array.isArray(e)&&t&&n&&[t,n].every((e=>["string","function"].includes(typeof e)))&&e.forEach(((e,i)=>{const c="string"==typeof t?e[t]:t(e,i),o="string"==typeof n?e[n]:n(e,i);["string","number"].includes(typeof c)&&(r[c]=o)})),r},e.objType=o,e.pickRenameKeys=function(e,t){if(!(e&&e instanceof Object&&t&&t instanceof Object))return;let n={};for(let r in e)r in t&&(n[t[r]]=e[r]);return n},e.safeJsonParse=function(e="",t){try{return JSON.parse(e)}catch(e){return t}},e.switchNullToUndefined=d,e.tree2array=function(e,t={}){const{primaryKey:r,parentKey:i,childrenKey:c,hasParentKey:o,returnObject:a}=Object.assign({hasParentKey:!0,primaryKey:"id",parentKey:"pid",childrenKey:"children",returnObject:!1},t);let s=a?{}:[];return Array.isArray(e)||"object"==typeof e?(function e(t,n,l=!1){let u=o?{}:{[i]:n};if(!l){const e=Object.assign(u,function(e,t){let n={};for(let r in e)r!==t&&(n[r]=e[r]);return n}(t,c));a?s[t[r]]=e:s.push(e)}Array.isArray(t[c])&&t[c].length&&t[c].forEach((n=>e(n,t[r])))}(Array.isArray(e)?n(c,e):e,null,!0),s):s},e.wp=u,e.wpVuePlugin=f,Object.defineProperty(e,"__esModule",{value:!0})}));
|
package/dist/object-array.js
CHANGED
package/dist/optional.d.ts
CHANGED
|
@@ -32,14 +32,19 @@ export declare type ParamsSwitchMaps<DK extends string = string, RK extends stri
|
|
|
32
32
|
RK,
|
|
33
33
|
RK
|
|
34
34
|
] | [RK, RK, string] | 'join' | 'pop'>;
|
|
35
|
+
export declare type DateFormat = (date: any, format?: string) => string;
|
|
35
36
|
/**
|
|
36
37
|
* 将搜素条件里的数组字段转换为其他格式,通常用于搜索表单的字段转换
|
|
37
38
|
* @param formData 传入参数
|
|
38
39
|
* @param maps 转换的映射关系,eg:{ date: ['startTime', 'endTime'] }
|
|
39
|
-
* @param
|
|
40
|
+
* @param options 额外的配置参数
|
|
40
41
|
*/
|
|
41
|
-
export declare const formParamsSwitch: <
|
|
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
|
+
/** 是否需要过滤空数据 */
|
|
42
44
|
filterNullable?: boolean | undefined;
|
|
43
|
-
|
|
45
|
+
/** 时间格式换函数 */
|
|
46
|
+
dateFormat?: DateFormat | undefined;
|
|
44
47
|
} | undefined) => R;
|
|
48
|
+
/** 创建一个绑定了默认时间格式转换函数的formParamsSwitch函数 */
|
|
49
|
+
export declare const formParamsSwitchCreator: (dateFormat: DateFormat) => typeof formParamsSwitch;
|
|
45
50
|
export {};
|
package/dist/promise.js
CHANGED
package/dist/validate.js
CHANGED
package/package.json
CHANGED
package/types/optional.d.ts
CHANGED
|
@@ -32,14 +32,19 @@ export declare type ParamsSwitchMaps<DK extends string = string, RK extends stri
|
|
|
32
32
|
RK,
|
|
33
33
|
RK
|
|
34
34
|
] | [RK, RK, string] | 'join' | 'pop'>;
|
|
35
|
+
export declare type DateFormat = (date: any, format?: string) => string;
|
|
35
36
|
/**
|
|
36
37
|
* 将搜素条件里的数组字段转换为其他格式,通常用于搜索表单的字段转换
|
|
37
38
|
* @param formData 传入参数
|
|
38
39
|
* @param maps 转换的映射关系,eg:{ date: ['startTime', 'endTime'] }
|
|
39
|
-
* @param
|
|
40
|
+
* @param options 额外的配置参数
|
|
40
41
|
*/
|
|
41
|
-
export declare const formParamsSwitch: <
|
|
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
|
+
/** 是否需要过滤空数据 */
|
|
42
44
|
filterNullable?: boolean | undefined;
|
|
43
|
-
|
|
45
|
+
/** 时间格式换函数 */
|
|
46
|
+
dateFormat?: DateFormat | undefined;
|
|
44
47
|
} | undefined) => R;
|
|
48
|
+
/** 创建一个绑定了默认时间格式转换函数的formParamsSwitch函数 */
|
|
49
|
+
export declare const formParamsSwitchCreator: (dateFormat: DateFormat) => typeof formParamsSwitch;
|
|
45
50
|
export {};
|