Counting
Helper function for consuming iterables with a running count, for those cases
where you want a counter variable but don't feel like using a for-loop or
callback function.
import { counting } from "counting";
API
counting
function* counting<T>(iter: Iterable<T>): IterableIterator<[number, T]>;
Generates a new iterable from the input that yields an index along with each
value from the iterable.
Examples
const array = ["a", "b", "c"];
for (const [i, value] of counting(array)) {
console.log(`${i}: ${value}`);
}
new Map(counting(array));
const map = new Map([["a", "A"], ["b", "B"], ["c", "C"]]);
for (const [i, [key, value]] of counting(map)) {
console.log(`${i}: ${key} -> ${value}`);
}
for (const [i, key] of counting(map.keys())) {
console.log(`${i}: ${key}`);
}
for (const [i, value] of counting(map.values())) {
console.log(`${i}: ${value}`);
}
LICENSE
The MIT license.