-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from wxhccc/dev
v1.6.0
- Loading branch information
Showing
4 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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****[email protected]' if run maskData('[email protected]', { startCharIndex: 2, endCharIndex: -3, endUntil: '@' })`, function () { | ||
expect(maskData('[email protected]', { startCharIndex: 2, endCharIndex: -3, endUntil: '@' })).toBe('ab****[email protected]') | ||
}) | ||
it(`should return '138****8888' if run maskData('13888888888', { mode: 'telphone' })`, function () { | ||
expect(maskData('13888888888', { mode: 'telphone' })).toBe('138****8888') | ||
}) | ||
|
||
}) | ||
|
||
}) | ||
}) | ||
|
||
describe('#promise', function () { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './byte-string'; | ||
export * from './mask-data'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
interface MaskDataOptions { | ||
/** 替换的字符,默认为* */ | ||
maskWith?: string | ||
/** 起始字符索引,为负数时从字符串结尾起倒数第几个数开始 */ | ||
startCharIndex?: number | ||
/** 结束字符索引,包含在处理字符内,缺省时会替换到字符串结尾,为负数时从字符串结尾起倒数第几个数开始 */ | ||
endCharIndex?: number | ||
/** 待处理字符串起始处理字符(串),不包含在待处理片段内 */ | ||
startFrom?: string | ||
/** 待处理字符串结束处理字符(串),不包含在待处理片段内 */ | ||
endUntil?: string | ||
/** 内置的默认的的处理模式,会预设一些属性值,属性值可以被覆盖 */ | ||
mode?: 'telphone' | 'idcard' | ||
} | ||
const modeIndexs: Record<NonNullable<MaskDataOptions['mode']>, 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('') | ||
} |