Open this example in the interactive library
Source code
import { Readable } from "node:stream";
import { createInterface } from "node:readline";
const input = Readable.from([
'{"line":"Red","start":8,"end":10}\n',
'{"line":"Blue","start":9,"end":11}\n',
'{"line":"Red","start":12,"end":15}\n',
'{"line":"Gold","start":15,"end":16}\n',
]);
const rows = [];
for await (const line of createInterface({ input, crlfDelay: Infinity })) {
rows.push(JSON.parse(line));
}
const result = rows.flatMap((a, i) =>
rows
.slice(i + 1)
.filter((b) => a.start < b.end && b.start < a.end)
.map((b) => [a.line, b.line]),
);
console.log(JSON.stringify(result, null, 2));
Sample output
[
[
"Red",
"Blue"
]
]