UNPKG

util-helpers

Version:

一个基于业务场景的工具方法库

57 lines (56 loc) 2.15 kB
type Options = { start?: number; end?: number; char?: string; repeat?: number; exclude?: string; }; /** * 替换字符,应用场景如:脱敏 * * @alias module:Processor.replaceChar * @since 1.1.0 * @param {string} str 要处理的字符串 * @param {Object} [options] 配置项 * @param {number} [options.start=3] 开始位置,默认`3` * @param {number} [options.end=-4] 结束位置,默认`-4` * @param {string} [options.char="*"] 替换字符,默认`*` * @param {number} [options.repeat] 替换字符的重复次数,默认为替换内容长度,可设置为固定值 * @param {string} [options.exclude] 排除字符,如果指定排除项,repeat设置无效 * @returns {string} 处理后的字符 * @example * * // 手机号 前34 * replaceChar('13000000000'); // 130****0000 * * // 手机号 前24 * replaceChar('13000000000', { start: 2 }); // 13*****0000 * * // 身份证 前64 * replaceChar('130701199310302288', { start: 6, end: -4 }); // 130701********2288 * * // 邮箱@前两位 * const email = '12345@qq.com'; * const emailAtIndex = email.indexOf('@'); * replaceChar('email', { start: emailAtIndex - 2, end: emailAtIndex }); // 123**@qq.com * * // 邮箱 前2和@后面内容,固定替换字符4位 * replaceChar(email, {start: 2, end: email.indexOf('@'), repeat: 4}); // 12****@qq.com * * // 银行卡号 只展示后4位,固定替换字符4位 * replaceChar('6228480402564890018', {start: 0, end: -4, repeat: 4}); // ****0018 * * // 银行卡号 前64 * replaceChar('6228480402564890018', { start: 6, end: -4 }); // 622848*********0018 * * // 银行卡号 前43 忽略格式的空格 * replaceChar('6228 4804 0256 4890 018', {start: 4, end: -4, exclude: ' '}); // 6228 **** **** **** 018 * * // 用户名 * replaceChar('林某某', {start: 1, end: Infinity, repeat: 2}); // 林** * replaceChar('林某', {start: 1, end: Infinity, repeat: 2}); // 林** * replaceChar('林某某某', { start: 1, end: -1, repeat: 1 }); // 林*某 * */ declare function replaceChar(str?: string, options?: Options): string; export default replaceChar;