UNPKG

util-helpers

Version:

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

33 lines (29 loc) 1.11 kB
'use strict'; var ut2 = require('ut2'); function replaceChar(str, options) { if (str === void 0) { str = ''; } if (options === void 0) { options = {}; } var _a = options.char, char = _a === void 0 ? '*' : _a, exclude = options.exclude; var _b = options.start, start = _b === void 0 ? 3 : _b, _c = options.end, end = _c === void 0 ? -4 : _c, repeat = options.repeat; var realStr = ut2.toString(str); var strLen = realStr.length; if (Math.abs(start) >= strLen) { return realStr; } start = start >= 0 ? start : strLen + start; end = end >= 0 ? end : strLen + end; if (start >= end) { return realStr; } var middleStr = realStr.substring(start, end); if (exclude) { var reg = new RegExp("[^".concat(exclude, "]"), 'g'); middleStr = middleStr.replace(reg, char); } else { repeat = typeof repeat === 'number' && repeat >= 0 ? repeat : middleStr.length; middleStr = char.repeat(repeat); } return realStr.substring(0, start) + middleStr + realStr.substring(end); } module.exports = replaceChar;