@opensig/opendesign
Version:
28 lines (27 loc) • 924 B
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const ESCAPE_CHARACTER_REG = /[.*+?^${}()|[\]\\]/g;
function escapeRegExp(str) {
ESCAPE_CHARACTER_REG.lastIndex = 0;
return str.replace(ESCAPE_CHARACTER_REG, "\\$&");
}
function splitByMatch(dataSource, keyword) {
if (!dataSource || !keyword) {
return [];
}
const regexp = typeof keyword === "string" ? new RegExp(escapeRegExp(keyword), "ig") : keyword;
const matchedList = dataSource.matchAll(regexp);
const result = [];
let preMatchIndex = 0;
for (const match of matchedList) {
result.push(dataSource.slice(preMatchIndex, match.index));
result.push(match[0]);
preMatchIndex = match.index + match[0].length;
}
if (preMatchIndex < dataSource.length) {
result.push(dataSource.slice(preMatchIndex));
}
return result;
}
exports.escapeRegExp = escapeRegExp;
exports.splitByMatch = splitByMatch;