Files
order_site/apps/backend/src/utils/masking.ts
T

52 lines
1.1 KiB
TypeScript

type MaskPhoneOptions = {
maskShort?: boolean
shortMask?: string
}
type MaskCodeOptions = {
shortMask?: string
}
export function maskSecret(value: unknown): string {
const normalized = String(value || '').trim()
if (!normalized) {
return ''
}
if (normalized.length <= 10) {
return `${normalized.slice(0, 2)}****${normalized.slice(-2)}`
}
return `${normalized.slice(0, 6)}****${normalized.slice(-6)}`
}
export function maskPhone(value: unknown, options: MaskPhoneOptions = {}): string {
const text = String(value || '').trim()
if (!text) {
return ''
}
if (text.length <= 7) {
if (options.maskShort === false) {
return text
}
return `${text.slice(0, 2)}${options.shortMask || '***'}${text.slice(-2)}`
}
return `${text.slice(0, 3)}****${text.slice(-4)}`
}
export function maskCode(value: unknown, options: MaskCodeOptions = {}): string {
const text = String(value || '').trim()
if (!text) {
return ''
}
if (text.length <= 8) {
return `${text.slice(0, 2)}${options.shortMask || '***'}${text.slice(-2)}`
}
return `${text.slice(0, 4)}****${text.slice(-4)}`
}