As an input, I have a string and an array of intervals with colors:
// 0123456789.123456789.123456789.123456789.123
const str = 'The quick brown fox jumps over the lazy dog.';
const colors = [
{from: 0, to: 8, color: 'red'},
{from: 10, to: 14, color: 'brown'},
{from: 16, to: 24, color: 'blue'},
{from: 20, to: 29, color: 'yellow'}
];
Note that the input intervals can intersect!
As output, I want to calculate an array of non-intersecting intervals, with colors and substrings from the given string. The call should look like this:
result = colorizedStrings(str, colors, 'defaultColor');
The result should look like this:
[
{ from: 0, to: 8, sub: 'The quick', colors: ['red' ] },
{ from: 9, to: 9, sub: ' ', colors: ['defaultColor' ] },
{ from: 10, to: 14, sub: 'brown', colors: ['brown' ] },
{ from: 15, to: 15, sub: ' ', colors: ['defaultColor' ] },
{ from: 16, to: 19, sub: 'fox ', colors: ['blue' ] },
{ from: 20, to: 24, sub: 'jumps', colors: ['blue', 'yellow'] },
{ from: 25, to: 29, sub: ' over', colors: ['yellow' ] },
{ from: 30, to: 43, sub: ' the lazy dog.', colors: ['defaultColor' ] },
]
The output intervals contain more than one color when necessary.
I have tried to find an open source Javascript library that calculates the interval intersections. However, I could not find one that is simple and small – the libraries always included fancy stuff (e.g. graphics, charts, etc.). I am looking for "pure math", here.
Can you help me find a suitable solution, please?