@dvcol/neo-svelte
Version:
Neomorphic ui library for svelte 5
22 lines (21 loc) • 769 B
JavaScript
export const markTokenizer = (str = '', pattern, flags = 'gi') => {
if (!pattern?.trim())
return [{ mark: false, token: str }];
// Case-insensitive with capturing
const regex = new RegExp(`(${pattern.trim()})`, flags);
const result = [];
let lastIndex = 0;
str.replace(regex, (match, group, offset) => {
// If no group, ignore result
if (!group)
return '';
// Push the text before + match
result.push({ mark: group, token: str.slice(lastIndex, offset) });
// Update lastIndex to move past the match
lastIndex = offset + group.length;
return '';
});
// Push the remaining text
result.push({ mark: false, token: str.slice(lastIndex) });
return result;
};