@wxhccc/es-util 1.5.2 → 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/dist/index.esm.js +47 -3
- package/dist/index.js +47 -2
- package/dist/index.min.js +3 -3
- package/dist/object-array.js +2 -2
- package/dist/promise.js +2 -2
- 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 +3 -2
- package/types/value-string-switch/index.d.ts +1 -0
- package/types/value-string-switch/mask-data.d.ts +22 -0
package/dist/index.esm.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @wxhccc/es-util v1.
|
|
3
|
-
* (c)
|
|
2
|
+
* @wxhccc/es-util v1.6.0
|
|
3
|
+
* (c) 2022 wxhccc
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
6
6
|
const { hasOwnProperty: hasOwnProperty$1 } = Object.prototype;
|
|
@@ -249,6 +249,50 @@ function byteStringify(byteNum, options) {
|
|
|
249
249
|
function byteParse(byteStr, options = {}) {
|
|
250
250
|
}
|
|
251
251
|
|
|
252
|
+
const modeIndexs = {
|
|
253
|
+
telphone: { startCharIndex: -8, endCharIndex: -5 },
|
|
254
|
+
idcard: { startCharIndex: -12, endCharIndex: -5 }
|
|
255
|
+
};
|
|
256
|
+
/**
|
|
257
|
+
* 对给定的字符串进行脱敏处理
|
|
258
|
+
* @param data 需要处理的字符串
|
|
259
|
+
* @param options 配置参数对象
|
|
260
|
+
* @returns
|
|
261
|
+
*/
|
|
262
|
+
function maskData(data, options) {
|
|
263
|
+
if (typeof data !== 'string' || !data) {
|
|
264
|
+
return '';
|
|
265
|
+
}
|
|
266
|
+
const modeOpts = options && options.mode && modeIndexs[options.mode] ? modeIndexs[options.mode] : {};
|
|
267
|
+
const { maskWith, startCharIndex: start = 0, endCharIndex: end, startFrom, endUntil } = { maskWith: '*', ...modeOpts, ...options };
|
|
268
|
+
let handleData = data;
|
|
269
|
+
let beforeChars = '';
|
|
270
|
+
let afterChars = '';
|
|
271
|
+
const startFromIndex = startFrom ? data.indexOf(startFrom) : -1;
|
|
272
|
+
if (startFromIndex > -1) {
|
|
273
|
+
const index = startFromIndex + startFrom.length;
|
|
274
|
+
handleData = handleData.slice(index);
|
|
275
|
+
beforeChars = data.slice(0, index);
|
|
276
|
+
}
|
|
277
|
+
const endUtilIndex = endUntil ? data.lastIndexOf(endUntil) : -1;
|
|
278
|
+
if (endUtilIndex > -1) {
|
|
279
|
+
handleData = handleData.slice(0, endUtilIndex);
|
|
280
|
+
afterChars = data.slice(endUtilIndex);
|
|
281
|
+
}
|
|
282
|
+
const chars = handleData.split('');
|
|
283
|
+
const newChars = [beforeChars];
|
|
284
|
+
const charLen = chars.length;
|
|
285
|
+
const indexs = [start >= 0 ? start : charLen + start, end !== undefined ? (end >= 0 ? end : charLen + end) : charLen - 1];
|
|
286
|
+
let [startIdx, endIdx] = indexs;
|
|
287
|
+
endIdx = endIdx > 0 && endIdx < charLen ? endIdx : charLen - 1;
|
|
288
|
+
startIdx = startIdx >= 0 && startIdx <= endIdx ? startIdx : 0;
|
|
289
|
+
chars.forEach((char, idx) => {
|
|
290
|
+
newChars.push(idx >= startIdx && idx <= endIdx ? maskWith : char);
|
|
291
|
+
});
|
|
292
|
+
newChars.push(afterChars);
|
|
293
|
+
return newChars.join('');
|
|
294
|
+
}
|
|
295
|
+
|
|
252
296
|
const { hasOwnProperty, toString: toString$1 } = Object.prototype;
|
|
253
297
|
const objType = (val) => {
|
|
254
298
|
const typeKeys = toString$1.call(val).match(/^\[object (.*)\]$/);
|
|
@@ -527,4 +571,4 @@ const formParamsSwitch = (formData, maps, options) => {
|
|
|
527
571
|
/** 创建一个绑定了默认时间格式转换函数的formParamsSwitch函数 */
|
|
528
572
|
const formParamsSwitchCreator = (dateFormat) => (formData, maps, options) => formParamsSwitch(formData, maps, { dateFormat, ...options });
|
|
529
573
|
|
|
530
|
-
export { ChinaIdCardValid, array2tree, awaitWrapper, byteParse, byteStringify, checkoutBy, filterNullable, formParamsSwitch, formParamsSwitchCreator, formulaValidate, isNullable, mapToObject, objType, pickRenameKeys, safeJsonParse, switchNullToUndefined, tree2array, wp, wpVuePlugin };
|
|
574
|
+
export { ChinaIdCardValid, array2tree, awaitWrapper, byteParse, byteStringify, checkoutBy, filterNullable, formParamsSwitch, formParamsSwitchCreator, formulaValidate, isNullable, mapToObject, maskData, objType, pickRenameKeys, safeJsonParse, switchNullToUndefined, tree2array, wp, wpVuePlugin };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @wxhccc/es-util v1.
|
|
3
|
-
* (c)
|
|
2
|
+
* @wxhccc/es-util v1.6.0
|
|
3
|
+
* (c) 2022 wxhccc
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
6
6
|
'use strict';
|
|
@@ -253,6 +253,50 @@ function byteStringify(byteNum, options) {
|
|
|
253
253
|
function byteParse(byteStr, options = {}) {
|
|
254
254
|
}
|
|
255
255
|
|
|
256
|
+
const modeIndexs = {
|
|
257
|
+
telphone: { startCharIndex: -8, endCharIndex: -5 },
|
|
258
|
+
idcard: { startCharIndex: -12, endCharIndex: -5 }
|
|
259
|
+
};
|
|
260
|
+
/**
|
|
261
|
+
* 对给定的字符串进行脱敏处理
|
|
262
|
+
* @param data 需要处理的字符串
|
|
263
|
+
* @param options 配置参数对象
|
|
264
|
+
* @returns
|
|
265
|
+
*/
|
|
266
|
+
function maskData(data, options) {
|
|
267
|
+
if (typeof data !== 'string' || !data) {
|
|
268
|
+
return '';
|
|
269
|
+
}
|
|
270
|
+
const modeOpts = options && options.mode && modeIndexs[options.mode] ? modeIndexs[options.mode] : {};
|
|
271
|
+
const { maskWith, startCharIndex: start = 0, endCharIndex: end, startFrom, endUntil } = { maskWith: '*', ...modeOpts, ...options };
|
|
272
|
+
let handleData = data;
|
|
273
|
+
let beforeChars = '';
|
|
274
|
+
let afterChars = '';
|
|
275
|
+
const startFromIndex = startFrom ? data.indexOf(startFrom) : -1;
|
|
276
|
+
if (startFromIndex > -1) {
|
|
277
|
+
const index = startFromIndex + startFrom.length;
|
|
278
|
+
handleData = handleData.slice(index);
|
|
279
|
+
beforeChars = data.slice(0, index);
|
|
280
|
+
}
|
|
281
|
+
const endUtilIndex = endUntil ? data.lastIndexOf(endUntil) : -1;
|
|
282
|
+
if (endUtilIndex > -1) {
|
|
283
|
+
handleData = handleData.slice(0, endUtilIndex);
|
|
284
|
+
afterChars = data.slice(endUtilIndex);
|
|
285
|
+
}
|
|
286
|
+
const chars = handleData.split('');
|
|
287
|
+
const newChars = [beforeChars];
|
|
288
|
+
const charLen = chars.length;
|
|
289
|
+
const indexs = [start >= 0 ? start : charLen + start, end !== undefined ? (end >= 0 ? end : charLen + end) : charLen - 1];
|
|
290
|
+
let [startIdx, endIdx] = indexs;
|
|
291
|
+
endIdx = endIdx > 0 && endIdx < charLen ? endIdx : charLen - 1;
|
|
292
|
+
startIdx = startIdx >= 0 && startIdx <= endIdx ? startIdx : 0;
|
|
293
|
+
chars.forEach((char, idx) => {
|
|
294
|
+
newChars.push(idx >= startIdx && idx <= endIdx ? maskWith : char);
|
|
295
|
+
});
|
|
296
|
+
newChars.push(afterChars);
|
|
297
|
+
return newChars.join('');
|
|
298
|
+
}
|
|
299
|
+
|
|
256
300
|
const { hasOwnProperty, toString: toString$1 } = Object.prototype;
|
|
257
301
|
const objType = (val) => {
|
|
258
302
|
const typeKeys = toString$1.call(val).match(/^\[object (.*)\]$/);
|
|
@@ -543,6 +587,7 @@ exports.formParamsSwitchCreator = formParamsSwitchCreator;
|
|
|
543
587
|
exports.formulaValidate = formulaValidate;
|
|
544
588
|
exports.isNullable = isNullable;
|
|
545
589
|
exports.mapToObject = mapToObject;
|
|
590
|
+
exports.maskData = maskData;
|
|
546
591
|
exports.objType = objType;
|
|
547
592
|
exports.pickRenameKeys = pickRenameKeys;
|
|
548
593
|
exports.safeJsonParse = safeJsonParse;
|
package/dist/index.min.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @wxhccc/es-util v1.
|
|
3
|
-
* (c)
|
|
2
|
+
* @wxhccc/es-util v1.6.0
|
|
3
|
+
* (c) 2022 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:
|
|
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 i={telphone:{startCharIndex:-8,endCharIndex:-5},idcard:{startCharIndex:-12,endCharIndex:-5}};const{hasOwnProperty:c,toString:o}=Object.prototype,s=e=>{const t=o.call(e).match(/^\[object (.*)\]$/);return t?t[1]:""};async function a(e){try{return[null,await e]}catch(e){return[e,void 0]}}const l={};function u(e,t){const n=function(e){return e?e._isVue||e.$&&e.$.vnode?"vue":"setState"in e?"react":"unknown":"unknown"}(this),r="unknown"!==n,i=r?this:l,o="react"===n?"state":"",u=o?i[o]:i,{wrap:f,lock:p,syncRefHandle:y,manualUnlock:d}="boolean"==typeof t?{wrap:t}:{...t};let h,b,j=[],g=!1,m=!1;const O=(e,t)=>!!e&&c.call(e,t),x=e=>"Object"===s(e),B=e=>{j.length?r?((e,t,r)=>{if("vue"===n&&i.$set&&!O(e,t[0]))return;const{$set:c=((e,t,n)=>{e[t]=n})}=i,o="react"===n,s=o?{...e}:e;let a=s,l=!1;for(let e=0;e<t.length;e++){const n=t[e],i=O(a,n);if(e===t.length-1){const e="boolean"==typeof a[n];l=!i||e,l&&c(a,n,r)}else{if(!i&&c(a,n,{}),!x(a[n]))break;o&&(a[n]={...a[n]}),a=a[n]}}o&&l&&i.setState({[t[0]]:s[t[0]]})})(u,j,e):l[p]=e:(b&&(b[0][b[1]]=e),h&&h(e))},k=()=>b?b[0][b[1]]:r||"string"!=typeof p?((e,t)=>{let n=!1;if(e&&x(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||!O(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)?b=p:"function"==typeof p&&(h=p,e(y)&&(b=y)),g=j.length>0||!!b||!!h,g&&(m=k())}let A;A="function"==typeof e?m?(()=>{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=()=>B(!1);return Object.defineProperties(A,{__lockValue:{get:k},_checkLockKey:{value:e=>l[e]},unlock:{value:K}}),g&&!m&&(B(!0),!d&&A.finally(K)),A}const f=Object.defineProperty(u,"_checkLockKey",{value:e=>l[e]}),p={install(e,t="$wp"){e.config&&"globalProperties"in e.config?e.config.globalProperties[t]=u:e.prototype[t]=u}};const y=(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)?y(i):i)})),n},{toString:d}=Object.prototype,h=e=>{if(null!==e)return Array.isArray(e)?e.map(h):("[object Object]"===d.call(e)&&Object.keys(e).forEach((t=>{e[t]=h(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:s,dateFormat:a}={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,s]=r;Object.assign(e,{[t]:a instanceof Function?a(o,i):o,[n]:a instanceof Function?a(s,i):s})}return e}),{}),u={...c,...l};return s?y(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:s,parentRefKey:a}=Object.assign({primaryKey:"id",parentKey:"pid",childrenKey:"children",createRoot:!1,parentRefKey:!1},r),l=a&&("string"==typeof a?a:"_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 s?s(f):s?{[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 s=parseFloat(e);if(!Number.isFinite(s))return;const a=s<0?-1:1;s=Math.abs(s);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=s/t;return!(n>l)&&(p=e.units[r],y=f(n*a),!0)}));else{const e=Math.pow(l,u.expMax);p=u.units[r];y=f(s/e*a)}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=y,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.maskData=function(e,t){if("string"!=typeof e||!e)return"";const n=t&&t.mode&&i[t.mode]?i[t.mode]:{},{maskWith:r,startCharIndex:c=0,endCharIndex:o,startFrom:s,endUntil:a}={maskWith:"*",...n,...t};let l=e,u="",f="";const p=s?e.indexOf(s):-1;if(p>-1){const t=p+s.length;l=l.slice(t),u=e.slice(0,t)}const y=a?e.lastIndexOf(a):-1;y>-1&&(l=l.slice(0,y),f=e.slice(y));const d=l.split(""),h=[u],b=d.length,j=[c>=0?c:b+c,void 0!==o?o>=0?o:b+o:b-1];let[g,m]=j;return m=m>0&&m<b?m:b-1,g=g>=0&&g<=m?g:0,d.forEach(((e,t)=>{h.push(t>=g&&t<=m?r:e)})),h.push(f),h.join("")},e.objType=s,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=h,e.tree2array=function(e,t={}){const{primaryKey:r,parentKey:i,childrenKey:c,hasParentKey:o,returnObject:s}=Object.assign({hasParentKey:!0,primaryKey:"id",parentKey:"pid",childrenKey:"children",returnObject:!1},t);let a=s?{}:[];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));s?a[t[r]]=e:a.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),a):a},e.wp=f,e.wpVuePlugin=p,Object.defineProperty(e,"__esModule",{value:!0})}));
|
package/dist/object-array.js
CHANGED
package/dist/promise.js
CHANGED
package/dist/validate.js
CHANGED
|
@@ -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.6.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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxhccc/es-util",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"description": "A library that contains some useful methods",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
@@ -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",
|
|
@@ -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 {};
|