Classes
Methods
(static) combine(collection, finishopt) → {itbl._Wrapper}
Combines the iterables in collection into a single iterable containing collections
of values from each iterable in collection.
collection can either be an Iterable or an object containing Iterables which
will be combined. The first value in the combined Iterable will be an Iterable or
an object containing the first values of the Iterables in collection, the second
value containing the second values of the Iterables in collection and so on.
The value of finish determines when the iteration ends.
If
finish === 'early' or 'e'(default) then the iteration ends as soon as the first iterator fromcollectionends.If
finish === 'late' or 'l'then the iteration continues untill all iterators fromcollectionare done. Values corresponding to iterators that have ended areundefined.If
finish === 'together' or 't'then all iterators fromcollectionmust finish on the same iteration or else anErroris thrown.
Note: The return value of the iterator is a collection of the of the return values the iterators
from collection. Return values corresponding to iterators that have not yet ended are undefined
combine is particularly powerful when used with
es6 destructuring assignment.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
collection |
Iterable | Object | Collection of iterators |
||
finish |
string |
<optional> |
'early' | Flag determining when iteration will finish. |
Returns:
Iterable containing collection of values
- Type
- itbl._Wrapper
Example
let mySet = new Set();
mySet.add(1);
mySet.add(Math);
[...itbl.combine([['a','b','c'], mySet, ['alpha','beta', 'gamma']])];
// -> [['a', 1, 'alpha'], ['b', Math, 'beta']]
// with `finish === late`
[...itbl.combine([['a','b','c'], ['alpha','beta', 'gamma'], mySet], 'late')];
// [['a', 1, 'alpha'], ['b', Math, 'beta'], ['c', undefined, 'gamma']]
let coordinates = itbl.combine({
x: [1,2,3,4,5],
y: [1,4,9,16,25],
});
for(let coor of coordinates) {
context.lineTo(coor.x, coor.y);
}
// more concise syntax using object destructuring
for(let {x, y} of coordinates) {
context.lineTo(x,y);
}
(static) filter(iterable, predicateopt) → {itbl._Wrapper}
Creates a new iterable containing values which the predicate returns truthy for.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
iterable |
Iterable | Iterable to filter the values of. |
||
predicate |
function |
<optional> |
_.identity | Function to run each value though. |
Throws:
-
Throws an error if iterators are not supported or the
iterableis not iterable. - Type
- Error
Returns:
iterable filtered using predicate, if iterable was an iterator then
an iterator is returned, otherwise an iterable is returned.
- Type
- itbl._Wrapper
Example
[...itbl.filter([0,1,2,3,4,5], val => val%2 === 0)];
// [0,2,4]
let mySet = new Set().add(1).add('a').add(NaN);
[...itbl.filter(mySet, value => _.isFinite)];
// [1]
var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false }
];
[...itbl.filter(users, function(o) { return !o.active; })];
// => objects for ['fred']
[...itbl.filter(users, _.matches({ 'age': 36, 'active': true }))];
// => objects for ['barney']
[...itbl.filter(users, _.matchesProperty('active', false))];
// => objects for ['fred']
[...itbl.filter(users, _.property('active'))];
// => objects for ['barney']
(static) isIterable(value) → {boolean}
Checks if value is an iterable object according to es6 iterator protocols.
In order to be iterable, an object must implement the @@iterator method,
meaning that the object (or one of the objects up its prototype chain)
must have a property with a [Symbol.iterator] key which defines a function.
(https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Iteration_protocols#iterable)
Parameters:
| Name | Type | Description |
|---|---|---|
value |
* | The value to check. |
Returns:
Returns true if value is correctly classified, else false.
- Type
- boolean
Example
function MyIterable() { }
MyIterable.prototype[Symbol.iterator] = function*(){
while(true) yield 1;
}
let iterableInstance = new MyIterable();
itbl.isIterable(iterableInstance);
// => true
// generator function that is then called
itbl.isIterable(iterableInstance[Symbol.iterator]());
// => true (this is both an iterator and also iterable)
itbl.isIterable([1, 2, 3]);
// => true
itbl.isIterable({1: 1, 2: 2, 3: 3});
// => false
(static) isIterator(value) → {boolean}
Checks if value is an iterator according to es6 iterator protocols.
An object is an iterator when it implements a next() method. See https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Iteration_protocols#iterator
for more information.
Parameters:
| Name | Type | Description |
|---|---|---|
value |
* | The value to check. |
Returns:
Returns true if value is correctly classified, else false.
- Type
- boolean
Example
function MyIterable() { }
MyIterable.prototype[Symbol.iterator] = function*() {
while(true) yield 1;
};
let iterableInstance = new MyIterable();
itbl.isIterator(iterableInstance);
// => false (this is an iterable but NOT an iterator)
// generator function that is then called
itbl.isIterator(iterableInstance[Symbol.iterator]());
// => true (this is both an iterator and also iterable)
itbl.isIterator([1, 2, 3][Symbol.iterator]());
// => true
for(let i of ['a'])
itbl.isIterator(i)
// => false (i is equal to 'a')
(static) map(iterable, iterateeopt) → {itbl._Wrapper}
Creates a new iterable whose iterators will have values corresponding to the value
of the Iterator of the original iterable run through iteratee.
The iteratee is invoked with only one argument (value).
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
iterable |
Iterable | Iterable to map values of. |
||
iteratee |
function |
<optional> |
_.identity | Function to run each value though. |
Throws:
-
Throws an error if
iterableis not iterable. - Type
- Error
Returns:
iterable mapped through iteratee, if iterable was an iterator then
an iterator is returned, otherwise an iterable is returned.
- Type
- itbl._Wrapper
Example
for(let coor of itbl.map([0, 1, 2, 3, 4, 5], x => ({ x, y: Math.exp(x) }))) {
context.lineTo(coor.x, coor.y);
}
let mySet = new Set().add(1).add('a').add(NaN);
[...itbl.map(mySet, value => value + 1)];
// [2, 'a1', NaN]
var users = [
{ 'user': 'barney' },
{ 'user': 'fred' },
];
[...itbl.map(users, _.property('user'))];
// => ['barney', 'fred']
(static) noConflict() → {itbl}
Reverts the itbl variable to its previous value and returns a reference to
the itbl function.
Returns:
Returns the itbl function.
- Type
- itbl
Example
var IterableUtil = itbl.noConflict();
Type Definitions
Step
Type:
- Object