From 1509533bc0b36fd2abd809a87a936b785983df0a Mon Sep 17 00:00:00 2001 From: wxhccc Date: Fri, 11 Feb 2022 13:47:10 +0800 Subject: [PATCH 1/2] 1.6.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e225ceb..71e549f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@wxhccc/es-util", - "version": "1.5.2", + "version": "1.6.0", "description": "A library that contains some useful methods", "main": "dist/index.js", "module": "dist/index.esm.js", From caedb2c0acfdbbbdd64da34d451db334309298c9 Mon Sep 17 00:00:00 2001 From: wxhccc Date: Fri, 11 Feb 2022 16:05:49 +0800 Subject: [PATCH 2/2] release: v1.6.0 --- __tests__/index.spec.js | 28 ++++++++++++++ package.json | 3 +- src/value-string-switch/index.ts | 1 + src/value-string-switch/mask-data.ts | 57 ++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/value-string-switch/mask-data.ts diff --git a/__tests__/index.spec.js b/__tests__/index.spec.js index 8510be4..b8eb3d6 100644 --- a/__tests__/index.spec.js +++ b/__tests__/index.spec.js @@ -132,6 +132,34 @@ describe('#value-string-switch', function () { }) }) + describe('#mask-data', function () { + describe('#maskData', function () { + const { maskData } = esUtil + it(`should return '*********' if run maskData('123456789')`, function () { + expect(maskData('123456789')).toBe('*********') + }) + it(`should return '123******' if run maskData('123456789', { startCharIndex: 3 })`, function () { + expect(maskData('123456789', { startCharIndex: 3 })).toBe('123******') + }) + it(`should return '123***789' if run maskData('123456789', { startCharIndex: 3, endCharIndex: 5 })`, function () { + expect(maskData('123456789', { startCharIndex: 3, endCharIndex: 5 })).toBe('123***789') + }) + it(`should return '1234567**' if run maskData('123456789', { startCharIndex: -2 })`, function () { + expect(maskData('123456789', { startCharIndex: -2 })).toBe('1234567**') + }) + it(`should return '123***789' if run maskData('123456789', { startCharIndex: -5, endCharIndex: -3 })`, function () { + expect(maskData('123456789', { startCharIndex: -6, endCharIndex: -4 })).toBe('123***789') + }) + it(`should return 'ab****34@sine.com' if run maskData('abcd1234@sine.com', { startCharIndex: 2, endCharIndex: -3, endUntil: '@' })`, function () { + expect(maskData('abcd1234@sine.com', { startCharIndex: 2, endCharIndex: -3, endUntil: '@' })).toBe('ab****34@sine.com') + }) + it(`should return '138****8888' if run maskData('13888888888', { mode: 'telphone' })`, function () { + expect(maskData('13888888888', { mode: 'telphone' })).toBe('138****8888') + }) + + }) + + }) }) describe('#promise', function () { diff --git a/package.json b/package.json index 71e549f..0147f9e 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "idCard", "validate", "mapToObject", - "string-switch" + "string-switch", + "mask data" ], "author": "wxhccc", "license": "MIT", diff --git a/src/value-string-switch/index.ts b/src/value-string-switch/index.ts index ed4b382..1ba54fe 100644 --- a/src/value-string-switch/index.ts +++ b/src/value-string-switch/index.ts @@ -1 +1,2 @@ export * from './byte-string'; +export * from './mask-data'; \ No newline at end of file diff --git a/src/value-string-switch/mask-data.ts b/src/value-string-switch/mask-data.ts new file mode 100644 index 0000000..3bc1d4c --- /dev/null +++ b/src/value-string-switch/mask-data.ts @@ -0,0 +1,57 @@ +interface MaskDataOptions { + /** 替换的字符,默认为* */ + maskWith?: string + /** 起始字符索引,为负数时从字符串结尾起倒数第几个数开始 */ + startCharIndex?: number + /** 结束字符索引,包含在处理字符内,缺省时会替换到字符串结尾,为负数时从字符串结尾起倒数第几个数开始 */ + endCharIndex?: number + /** 待处理字符串起始处理字符(串),不包含在待处理片段内 */ + startFrom?: string + /** 待处理字符串结束处理字符(串),不包含在待处理片段内 */ + endUntil?: string + /** 内置的默认的的处理模式,会预设一些属性值,属性值可以被覆盖 */ + mode?: 'telphone' | 'idcard' +} +const modeIndexs: Record, MaskDataOptions> = { + telphone: { startCharIndex: -8, endCharIndex: -5 }, + idcard: { startCharIndex: -12, endCharIndex: -5 } +} +/** + * 对给定的字符串进行脱敏处理 + * @param data 需要处理的字符串 + * @param options 配置参数对象 + * @returns + */ +export function maskData(data: string, options?: MaskDataOptions) { + if (typeof data !== 'string' || !data) { + return '' + } + const modeOpts = options && options.mode && modeIndexs[options.mode] ? modeIndexs[options.mode] : {} + const { maskWith, startCharIndex: start = 0, endCharIndex: end, startFrom, endUntil } = { maskWith: '*', ...modeOpts, ...options } + let handleData = data + let beforeChars = '' + let afterChars = '' + const startFromIndex = startFrom ? data.indexOf(startFrom) : -1 + if (startFromIndex > -1) { + const index = startFromIndex + (startFrom as string).length + handleData = handleData.slice(index) + beforeChars = data.slice(0, index) + } + const endUtilIndex = endUntil ? data.lastIndexOf(endUntil) : -1 + if (endUtilIndex > -1) { + handleData = handleData.slice(0, endUtilIndex) + afterChars = data.slice(endUtilIndex) + } + const chars = handleData.split('') + const newChars = [beforeChars] + const charLen = chars.length + const indexs = [start >= 0 ? start : charLen + start, end !== undefined ? (end >= 0 ? end : charLen + end) : charLen - 1] + let [startIdx, endIdx] = indexs + endIdx = endIdx > 0 && endIdx < charLen ? endIdx : charLen - 1 + startIdx = startIdx >= 0 && startIdx <= endIdx ? startIdx : 0 + chars.forEach((char, idx) => { + newChars.push(idx >= startIdx && idx <= endIdx ? maskWith : char) + }) + newChars.push(afterChars) + return newChars.join('') +} \ No newline at end of file