super-iterable
super-iterable is a JavaScript library providing ES6 Iterable utilities.
Features
- 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.
Examples
Fizzbuzz
; { if n % 15 === 0 return "fizzbuzz"; else if n % 3 === 0 return "fizz"; else if n % 5 === 0 return "buzz"; else return n; } for const out of _ console;
Fibonacci sequence
const _ = ; const fibs = ;for const fib of fibs console;
Install
npm install --save super-iterable
How to use
// example.js const _ = ;// or you can do this in Babel; // your code...
Babel + Node
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
TypeScript + ES6
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; // your code...
API
_(iterable) => SuperIterable
Wraps an Iterable into a SuperIterable
.
// arrays //=> SuperIterable(1,2,3) // setsconst set = 123;; //=> SuperIterable(1,2,3) // mapsconst map = 1 "one" 2 "two";; //=> SuperIterable([1, "one"], [2, "two"])
_(generatorFunc) => SuperIterable
Creates a SuperIterable
from a generator function.
; //=> SuperIterable(1,2,3)
_.times(count)
Creates a SuperIterable
that enumerates 0 .. count
.
_ ; //=> [0,1,2]
_.count(begin, step = 1)
Creates a SuperIterable
that enumerates begin
, begin + step
, begin + 2 *step
... .
_ ; //=> [1,3,5]
SuperIterable
SuperIterable
is a convenient wrapper for Iterable objects.
SuperIterable
itself is also an Iterable.
SuperIterable#map(func)
Transforms values with func
function.
; //=> [2,4,6]
SuperIterable#filter(pred)
Filters values with pred
function.
; //=> [2,4]
SuperIterable#flatMap(func)
Transforms values into Iterables with func
function and concatenates them.
; //=> [1,2,2,4,3,6]
SuperIterable#entries(func)
Make pairs from the indices and the values.
; //=> [[1, "foo"], [2, "bar"], [3, "baz"]]
SuperIterable#reduce(func, initValue)
Accumulates each value.
; //=> 24
SuperIterable#take(count)
Takes the first count
values.
; //=> ["foo", "bar"]
SuperIterable#takeWhile(pred)
Takes the elements while pred(element)
is true.
; //=> [2,4,6]
SuperIterable#drop(count)
Drops the first count
values.
; //=> [3,1,2]
SuperIterable#dropWhile(pred)
Drops the elements while pred(element)
is true.
; //=> [5,4]
SuperIterable#count()
Counts the elements.
; //=> 4
SuperIterable#concat(...otherIterables)
Concatenates Iterables.
; //=> [1,2,3,4,5,6,7,8,9]
SuperIterable#every(pred)
Returns true if every elements fulfills pred
condition, otherwise false.
; //=> true
; //=> false
SuperIterable#some(pred)
Returns true if at least one element fulfills pred
condition, otherwise false.
; //=> true
; //=> false
SuperIterable#find(pred)
Returns the element that fulfills pred
condition, otherwise undefined.
; //=> 4
; //=> undefined
SuperIterable#findIndex(predicate)
Returns the index of the element that fulfills pred
condition, otherwise -1.
; //=> 1
; //=> -1
SuperIterable#indexOf(elem)
Returns the index of the element, or returns -1 if it is not found.
; // => 1
; // => -1
SuperIterable#includes(elem)
Returns whether the SuperIterable
includes the element or not.
; // => true
; // => false
SuperIterable#keys()
Returns the "keys" (the first values of element pairs).
; //=> [1,3,5]
SuperIterable#values()
Returns the "values" (the second values of element pairs).
; //=> [2,4,6]
SuperIterable#toArray()
Converts the SuperIterable
into an Array.
; //=> [1,2,3,4]