Skip to content

Commit

Permalink
Improve logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inokawa committed Mar 18, 2023
1 parent 0ee19ad commit 56ac064
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/renderers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const createRegexRenderer = (
});

let prevEnd = 0;
const activeStyles = new Set<StyleOrRender>();
const activeStyles: StyleOrRender[] = [];
const res: React.ReactNode[] = [];
for (let i = 0; i < indexes.length; i++) {
const start = indexes[i]!;
Expand All @@ -73,20 +73,19 @@ export const createRegexRenderer = (
const endStyles = endToStyleMap.get(end);
if (startStyles) {
startStyles.forEach((s) => {
activeStyles.add(s);
activeStyles.push(s);
});
activeStyles.sort((a, b) => {
return (
matchers.findIndex(([, s]) => s === b) -
matchers.findIndex(([, s]) => s === a)
);
});
}

const v = value.slice(start, end);
const sortedStyles = Array.from(activeStyles).sort((a, b) => {
return (
matchers.findIndex(([, s]) => s === b) -
matchers.findIndex(([, s]) => s === a)
);
});

res.push(
sortedStyles.reduceRight((acc, styleOrRender, j) => {
activeStyles.reduceRight((acc, styleOrRender, j) => {
const key = j === 0 ? String(start) : undefined;
if (typeof styleOrRender === "function") {
return styleOrRender({ children: acc, value: v, key });
Expand All @@ -102,7 +101,7 @@ export const createRegexRenderer = (

if (endStyles) {
endStyles.forEach((s) => {
activeStyles.delete(s);
activeStyles.splice(activeStyles.indexOf(s), 1);
});
}

Expand Down

0 comments on commit 56ac064

Please sign in to comment.