util-helpers
Version:
31 lines (28 loc) • 1.1 kB
JavaScript
import { toString } from '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 = 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);
}
export { replaceChar as default };