@bemoje/iter
v2.0.0
Published
Utility functions for working with iterables, especially map-like key-value pair structures.
Maintainers
Readme
@bemoje/iter
Utility functions for working with iterables, especially map-like key-value pair structures.
Exports
- filterArray: Filter an array based on a predicate function.
- filterIterable: Filter an iterable based on a predicate function.
- filterIterableEntries: Filter an iterable of key-value entries based on a predicate function.
- filterIterableKeys: Filter an iterable of key-value entries based on a predicate function that tests keys.
- filterIterableValues: Filter an iterable of key-value entries based on a predicate function that tests values.
- filterMap: Filter a Map based on a predicate function.
- filterObject: Filter an object's properties based on a predicate function.
- filterSet: Filter a Set based on a predicate function.
- forEachArray: Execute a callback function for each element in an array.
- forEachIterable: Execute a callback function for each element in an iterable.
- forEachIterableEntries: Execute a callback function for each entry in a map-like iterable.
- forEachIterableKeys: Execute a callback function for each key in an iterable of key-value pairs.
- forEachIterableValues: Execute a callback function for each value in an iterable of key-value pairs.
- forEachMap: Execute a callback function for each entry in a Map.
- forEachObject: Execute a callback function for each property in an object.
- forEachSet: Execute a callback function for each value in a Set.
- mapArray: Transform each element in an array using a mapper function.
- mapIterable: Transform each element in an iterable using a mapper function.
- mapIterableEntries: Transform each entry in an iterable of key-value pairs using a mapper function.
- mapIterableKeys: Transform the keys in an iterable of key-value pairs using a mapper function.
- mapIterableValues: Transform the values in an iterable of key-value pairs using a mapper function.
- mapMap: Transform the values in a Map using a mapper function.
- mapObject: Transform the values in an object using a mapper function.
- mapSet: Transform each value in a Set using a mapper function.
- reduceArray: Reduce an array to a single value using a reducer function.
- reduceIterable: Reduce an iterable to a single value using a reducer function.
- reduceIterableEntries: Reduce an iterable of key-value entries to a single value using a reducer function.
- reduceIterableKeys: Reduce an iterable of key-value entries based on keys using a reducer function.
- reduceIterableValues: Reduce an iterable of key-value entries based on values using a reducer function.
- reduceMap: Reduce a Map to a single value using a reducer function.
- reduceObject: Reduce an object to a single value using a reducer function.
- reduceSet: Reduce a Set to a single value using a reducer function.
- toObjectIterable: Convert a map-like iterable to a regular object.
Installation
npm install @bemoje/iterUsage
Transform Iterables
import { mapIterable, mapIterableKeys, mapIterableValues } from '@bemoje/iter'
const entries: Iterable<[string, number]> = new Map([
['a', 1],
['b', 2],
])
// Transform both keys and values
const mapped = mapIterable(entries, (value, key) => [key.toUpperCase(), value * 10])
// [['A', 10], ['B', 20]]
// Transform only keys
const newKeys = mapIterableKeys(entries, (key) => key.toUpperCase())
// [['A', 1], ['B', 2]]
// Transform only values
const doubled = mapIterableValues(entries, (value) => value * 2)
// [['a', 2], ['b', 4]]Filter and Reduce
import { filterIterable, reduceIterable, forEachIterable } from '@bemoje/iter'
const entries: Iterable<[string, number]> = new Map([
['a', 1],
['b', 2],
['c', 3],
])
// Filter entries by value
const filtered = filterIterable(entries, (value) => value > 1)
// [['b', 2], ['c', 3]]
// Reduce to a single value
const sum = reduceIterable(entries, (acc, value) => acc + value, 0)
// 6
// Side effects
forEachIterable(entries, (value, key) => console.log(`${key}: ${value}`))Count Unique Values
import { countUniques } from '@bemoje/iter'
const words = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple']
const counts = countUniques(words)
// ExtMap { 'apple' => 3, 'banana' => 2, 'cherry' => 1 } (sorted by count desc)Convert to Object
import { toObjectIterable } from '@bemoje/iter'
const entries: Iterable<[string, number]> = [
['a', 1],
['b', 2],
]
const obj = toObjectIterable(entries)
// { a: 1, b: 2 }