partition-iterable
v1.0.0
Published
Divides iterated values into those that match a filter and those that don’t.
Downloads
2
Maintainers
Readme
partition-iterable
Divides iterated values into those that match a filter and those that don’t.
Installation
Requires Node.js 6.0.0 or above.
npm i partition-iterable
API
The module exports a single function.
Parameters
- Bindable:
iter
(iterable): The iterable whose values should be partitioned. - Optional:
test
(any): A Function, an Array, or another value. Each iterated value fromiter
is tested in a manner that depends ontest
’s type:- If a Function: Does
test
return true when the value is passed to it? - If an Array: Is the value included in
test
? - If omitted or
undefined
: Is the value truthy? - Otherwise: Does the value equal
test
?
- If a Function: Does
Return Value
A two-element Array:
- An Array of iterated values that passed the test.
- An Array of those that did not.
Example
const partition = require('partition-iterable')
const isEven = n => n % 2 === 0
const [even, odd] = partition([1, 2, 3, 4, 5], isEven)
even // [2, 4]
odd // [1, 3, 5]
// Supports the bind operator
[1, 2, 3, 4, 5]::partition(isEven) // [[2, 4], [1, 3, 5]]
Related
- filter-iter: Filters an iterable object so that it only yields values which pass a test function.
- reduce-iterable: Applies a function to iterated values to reduce them to a single value.
- unique-iterable: Filters an iterable object so it doesn’t yield the same value more than once.
- unique-iterable-by: Filters yielded values by testing uniqueness with an index, key, or callback.