You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-06-15 23:00:36 +02:00
* Go to anything by body * Made limit parameter required * Made parameter required Co-authored-by: Laurent Cozic <laurent22@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
d54e52b1a8
commit
a45128807e
@ -58,4 +58,26 @@ ArrayUtils.contentEquals = function(array1, array2) {
|
||||
return true;
|
||||
};
|
||||
|
||||
// Merges multiple overlapping intervals into a single interval
|
||||
// e.g. [0, 25], [20, 50], [75, 100] --> [0, 50], [75, 100]
|
||||
ArrayUtils.mergeOverlappingIntervals = function(intervals, limit) {
|
||||
intervals.sort((a, b) => a[0] - b[0]);
|
||||
|
||||
const stack = [];
|
||||
if (intervals.length) {
|
||||
stack.push(intervals[0]);
|
||||
for (let i = 1; i < intervals.length && stack.length < limit; i++) {
|
||||
const top = stack[stack.length - 1];
|
||||
if (top[1] < intervals[i][0]) {
|
||||
stack.push(intervals[i]);
|
||||
} else if (top[1] < intervals[i][1]) {
|
||||
top[1] = intervals[i][1];
|
||||
stack.pop();
|
||||
stack.push(top);
|
||||
}
|
||||
}
|
||||
}
|
||||
return stack;
|
||||
};
|
||||
|
||||
module.exports = ArrayUtils;
|
||||
|
Reference in New Issue
Block a user