Skip to content

Commit

Permalink
Merge pull request #16 from wxhccc/dev
Browse files Browse the repository at this point in the history
v1.6.0
  • Loading branch information
wxhccc authored Feb 11, 2022
2 parents d42f77a + 08109e4 commit 221ac6d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
28 changes: 28 additions & 0 deletions __tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -21,7 +21,8 @@
"idCard",
"validate",
"mapToObject",
"string-switch"
"string-switch",
"mask data"
],
"author": "wxhccc",
"license": "MIT",
Expand Down
1 change: 1 addition & 0 deletions src/value-string-switch/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './byte-string';
export * from './mask-data';
57 changes: 57 additions & 0 deletions src/value-string-switch/mask-data.ts
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('')
}

0 comments on commit 221ac6d

Please sign in to comment.