Skip to content

Commit

Permalink
chore: replace array.reduce with for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
bachbui committed Nov 13, 2024
1 parent 70afbe7 commit 95b081e
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions packages/@atjson/util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,22 +401,28 @@ export function extractSlices(value: {
}

// Normalize the ranges again, as we may have extended ranges into each other
// First sort the ranges
rangesToDelete = rangesToDelete.sort(compareRanges).reduce((acc, range) => {
// Since they are sorted, check if the current range can be merged into
// the previous one
const last = acc[acc.length - 1];
if (last) {
const [, lastEnd] = last;
if (range[0] <= lastEnd && lastEnd <= range[1]) {
last[1] = range[1];
return acc;
if (rangesToDelete.length > 1) {
const normalizedRanges = [rangesToDelete[0]];
// First sort the ranges
rangesToDelete.sort(compareRanges);
for (
let j = 1, lastRange = rangesToDelete[0];
j < rangesToDelete.length;
j++
) {
const currentRange = rangesToDelete[j];
// Since they are sorted, check if the current range can be merged into the
// previous one
if (currentRange[0] <= lastRange[1] && lastRange[1] <= currentRange[1]) {
lastRange[1] = currentRange[1];
} else {
// Otherwise we can add the range to the list
normalizedRanges.push(currentRange);
lastRange = currentRange;
}
}
// Otherwise add the range to the list
acc.push(range);
return acc;
}, [] as [number, number][]);
rangesToDelete = normalizedRanges;
}

let firstRange = rangesToDelete[0];
let text = firstRange ? value.text.slice(0, firstRange[0]) : "";
Expand Down

0 comments on commit 95b081e

Please sign in to comment.