Open this example in the interactive library
Source code
import { Readable } from "node:stream";
import { createInterface } from "node:readline";
const input = Readable.from([
'{"doc":"guide","rev":1,"author":"Ada"}\n',
'{"doc":"api","rev":1,"author":"Leo"}\n',
'{"doc":"guide","rev":3,"author":"Kim"}\n',
'{"doc":"guide","rev":4,"author":"Ada"}\n',
]);
const rows = [];
for await (const line of createInterface({ input, crlfDelay: Infinity })) {
rows.push(JSON.parse(line));
}
const result = [...new Set(rows.map((r) => r.doc))].flatMap((doc) => {
const seen = rows.filter((r) => r.doc === doc).map((r) => r.rev);
return Array.from({ length: Math.max(...seen) }, (_, i) => i + 1)
.filter((n) => !seen.includes(n))
.map((rev) => ({ doc, rev }));
});
console.log(JSON.stringify(result, null, 2));
Sample output
[
{
"doc": "guide",
"rev": 2
}
]