1.0.0 • Published 6 years ago

unique-iterable-by v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

unique-iterable-by

Filters yielded values by testing uniqueness with an index, key, or callback.

Optionally lets you set a numeric limit on total unique values yielded.

Installation

npm install unique-iterable-by --save

The module exports a single function.

Usage Example

Let’s say you have an iterable collection of person objects and you only want one person with any given name.

const uniqueIterableBy = require('unique-iterable-by')

const people = [
  {name: 'John'},
  {name: 'John'},
  {name: 'Stephen'},
]

const u = uniqueIterableBy(people, 'name')
u.next().value // {name: 'John'}
u.next().value // {name: 'Stephen'}
u.next().done // true

Or you can use a callback that retrieves the significant value:

uniqueIterableBy(people, person => person.name)

You can also use the limit argument to cap the number of total unique values yielded:

const u = uniqueIterableBy(people, 'name', {limit: 1})
u.next().value // {name: 'John'}
u.next().done // true

Related Projects