sorted-iterators-find-matching
Given an iterator/list of sorted items, find matching items in another (sorted) iterator/list.
Installation
npm install sorted-iterators-find-matching
Usage
Let's say you have a sorted list of people
const people = id: 'ada' name: 'Ada' id: 'bob' name: 'Bob' id: 'dora' name: 'Dora'
and a list of books, sorted by who owns them:
const books = // Ada has no books. nr: 5 belongsTo: 'bob' nr: 1 belongsTo: 'bob' nr: 4 belongsTo: 'bob' // We don't know who Carlos is. nr: 6 belongsTo: 'carlos' nr: 2 belongsTo: 'carlos' nr: 0 belongsTo: 'dora' nr: 3 belongsTo: 'elisa'
Because both lists are sorted by the "person ID", all books owned by a single person occur consecutively. Therefore, we can tell which books a person owns, only by reading the books list until it mentions a different person. This is what sorted-iterators-find-matching
does:
const iterateMatching = // We define a function that, given a person, tells if the book belongs to// - that person, or// - a person with an alphabetically ealier/lower person ID, or// - a person with an alphabetically later/higher person ID.const matchBookWithPerson = { if personid === bookbelongsTo return 0 return personid < bookbelongsTo ? -1 : 1}const ownedBooks = for const person of people console for const book of console
async iterators
If you have two async iterators, use sorted-iterators-find-matching/async
:
const iterateMatching = const match = { if itemOfListAfield === itemOfListBfield return 0 return itemOfListAfield < itemOfListBfield ? -1 : 1}const matching = // Note the `for await`!for { for await const itemB of console }
Contributing
If you have a question or need support using sorted-iterators-find-matching
, please double-check your code and setup first. If you think you have found a bug or want to propose a feature, use the issues page.