super-iterable is a JavaScript library providing ES6 Iterable utilities.
- Utility methods like
map
andfilter
for ES6 Iterables - Lazy evaluation (it's nature of Iterables)
- TypeScript
.d.ts
included (only for ES6 output mode)
super-iterable itself is written in ES6 and TypeScript.
import _ from "super-iterable";
function fizzbuzz(n) {
if (n % 15 === 0) {
return "fizzbuzz";
} else if (n % 3 === 0) {
return "fizz";
} else if (n % 5 === 0) {
return "buzz";
} else {
return n.toString();
}
}
for (const out of _.times(50).map(fizzbuzz)) {
console.log(out);
}
const _ = require("super-iterable");
const fibs = _(function* () {
let a = 0;
let b = 1;
while (true) {
yield a;
[a, b] = [b, a + b];
}
});
for (const fib of fibs.take(50)) {
console.log(fib);
}
npm install --save super-iterable
// example.js
const _ = require("super-iterable");
// or you can do this in Babel
import _ from "super-iterable";
// your code...
If you are using Babel + Node, you have to override ignore
option to import super-iterable
module as ES6 (Babel ignores JavaScript files inside node_modules
by default).
Example:
babel-node --ignore false example.js
Since super-iterable provides TypeScript type definitions by default, it is also nice to use it in TypeScript.
TypeScript >= 1.8 (nightly) is recommended, since it has started support CommonJS when targeting ES6.
// example.ts
import _ = require("super-iterable");
// your code...
Wraps an Iterable into a SuperIterable
.
// arrays
_([1,2,3]) //=> SuperIterable(1,2,3)
// sets
const set = new Set([1,2,3]);
_(set); //=> SuperIterable(1,2,3)
// maps
const map = new Map([1, "one"], [2, "two"]);
_(map); //=> SuperIterable([1, "one"], [2, "two"])
Creates a SuperIterable
from a generator function.
_(function *() {
yield 1;
yield 2;
yield 3;
}); //=> SuperIterable(1,2,3)
Creates a SuperIterable
that enumerates 0 .. count
.
_.times(3)
.toArray(); //=> [0,1,2]
Creates a SuperIterable
that enumerates begin
, begin + step
, begin + 2 *step
... .
_.count(1, 2)
.take(3)
.toArray(); //=> [1,3,5]
SuperIterable
is a convenient wrapper for Iterable objects.
SuperIterable
itself is also an Iterable.
Transforms values with func
function.
_([1,2,3])
.map(x => x * 2)
.toArray(); //=> [2,4,6]
Filters values with pred
function.
_([1,2,3,4])
.filter(x => x % 2 == 0)
.toArray(); //=> [2,4]
Transforms values into Iterables with func
function and concatenates them.
_([1,2,3])
.flatMap(x => [x, x * 2])
.toArray(); //=> [1,2,2,4,3,6]
Make pairs from the indices and the values.
_(["foo", "bar", "baz"])
.entries(); //=> [[1, "foo"], [2, "bar"], [3, "baz"]]
Accumulates each value.
_([1,2,3,4])
.reduce((acc, x) => acc * x, 1); //=> 24
Takes the first count
values.
_(["foo", "bar", "baz"])
.take(2); //=> ["foo", "bar"]
Takes the elements while pred(element)
is true.
_([2,4,6,5,4])
.takeWhile(x => x % 2 == 0); //=> [2,4,6]
Drops the first count
values.
_([1,2,3,1,2])
.drop(2); //=> [3,1,2]
Drops the elements while pred(element)
is true.
_([2,4,6,5,4])
.dropWhile(x => x % 2 == 0); //=> [5,4]
Counts the elements.
_([1,2,3,4])
.count(); //=> 4
Concatenates Iterables.
_([1,2,3,4])
.concat([5,6,7], new Set([8,9])); //=> [1,2,3,4,5,6,7,8,9]
Returns true if every elements fulfills pred
condition, otherwise false.
_([2,4,6])
.every(x => x % 2 == 0); //=> true
_([1,2,3])
.every(x => x % 2 == 0); //=> false
Returns true if at least one element fulfills pred
condition, otherwise false.
_([1,2,3])
.some(x => x % 2 == 0); //=> true
_([1,3,5])
.some(x => x % 2 == 0); //=> false
Returns the element that fulfills pred
condition, otherwise undefined.
_([1,4,3])
.find(x => x % 2 == 0); //=> 4
_([1,5,3])
.find(x => x % 2 == 0); //=> undefined
Returns the index of the element that fulfills pred
condition, otherwise -1.
_([1,4,3])
.find(x => x % 2 == 0); //=> 1
_([1,5,3])
.find(x => x % 2 == 0); //=> -1
Returns the index of the element, or returns -1 if it is not found.
_([1,4,3])
.indexOf(4); // => 1
_([1,5,3])
.indexOf(4); // => -1
Returns whether the SuperIterable
includes the element or not.
_([1,4,3])
.includes(4); // => true
_([1,5,3])
.includes(4); // => false
Returns the "keys" (the first values of element pairs).
_([[1,2],[3,4],[5,6]])
.keys(); //=> [1,3,5]
Returns the "values" (the second values of element pairs).
_([[1,2],[3,4],[5,6]])
.keys(); //=> [2,4,6]
Converts the SuperIterable
into an Array.
_([1,2,3,4])
.toArray(); //=> [1,2,3,4]