1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# es6-iterator
## ECMAScript 6 Iterator interface
### Installation
$ npm install es6-iterator
To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: [Browserify](http://browserify.org/), [Webmake](https://github.com/medikoo/modules-webmake) or [Webpack](http://webpack.github.io/)
## API
### Constructors
#### Iterator(list) _(es6-iterator)_
Abstract Iterator interface. Meant for extensions and not to be used on its own.
Accepts any _list_ object (technically object with numeric _length_ property).
_Mind it doesn't iterate strings properly, for that use dedicated [StringIterator](#string-iterator)_
```javascript
var Iterator = require('es6-iterator')
var iterator = new Iterator([1, 2, 3]);
iterator.next(); // { value: 1, done: false }
iterator.next(); // { value: 2, done: false }
iterator.next(); // { value: 3, done: false }
iterator.next(); // { value: undefined, done: true }
```
#### ArrayIterator(arrayLike[, kind]) _(es6-iterator/array)_
Dedicated for arrays and array-likes. Supports three iteration kinds:
* __value__ _(default)_ - Iterates values
* __key__ - Iterates indexes
* __key+value__ - Iterates keys and indexes, each iteration value is in _[key, value]_ form.
```javascript
var ArrayIterator = require('es6-iterator/array')
var iterator = new ArrayIterator([1, 2, 3], 'key+value');
iterator.next(); // { value: [0, 1], done: false }
iterator.next(); // { value: [1, 2], done: false }
iterator.next(); // { value: [2, 3], done: false }
iterator.next(); // { value: undefined, done: true }
```
May also be used for _arguments_ objects:
```javascript
(function () {
var iterator = new ArrayIterator(arguments);
iterator.next(); // { value: 1, done: false }
iterator.next(); // { value: 2, done: false }
iterator.next(); // { value: 3, done: false }
iterator.next(); // { value: undefined, done: true }
}(1, 2, 3));
```
#### StringIterator(str) _(es6-iterator/string)_
Assures proper iteration over unicode symbols.
See: http://mathiasbynens.be/notes/javascript-unicode
```javascript
var StringIterator = require('es6-iterator/string');
var iterator = new StringIterator('fπoπoπ');
iterator.next(); // { value: 'f', done: false }
iterator.next(); // { value: 'π', done: false }
iterator.next(); // { value: 'o', done: false }
iterator.next(); // { value: 'π', done: false }
iterator.next(); // { value: 'o', done: false }
iterator.next(); // { value: 'π', done: false }
iterator.next(); // { value: undefined, done: true }
```
### Function utilities
#### forOf(iterable, callback[, thisArg]) _(es6-iterator/for-of)_
Polyfill for ECMAScript 6 [`for...of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) statement.
```
var forOf = require('es6-iterator/for-of');
var result = [];
forOf('πππ', function (monkey) { result.push(monkey); });
console.log(result); // ['π', 'π', 'π'];
```
Optionally you can break iteration at any point:
```javascript
var result = [];
forOf([1,2,3,4]', function (val, doBreak) {
result.push(monkey);
if (val >= 3) doBreak();
});
console.log(result); // [1, 2, 3];
```
#### get(obj) _(es6-iterator/get)_
Return iterator for any iterable object.
```javascript
var getIterator = require('es6-iterator/get');
var iterator = get([1,2,3]);
iterator.next(); // { value: 1, done: false }
iterator.next(); // { value: 2, done: false }
iterator.next(); // { value: 3, done: false }
iterator.next(); // { value: undefined, done: true }
```
#### isIterable(obj) _(es6-iterator/is-iterable)_
Whether _obj_ is iterable
```javascript
var isIterable = require('es6-iterator/is-iterable');
isIterable(null); // false
isIterable(true); // false
isIterable('str'); // true
isIterable(['a', 'r', 'r']); // true
isIterable(new ArrayIterator([])); // true
```
#### validIterable(obj) _(es6-iterator/valid-iterable)_
If _obj_ is an iterable it is returned. Otherwise _TypeError_ is thrown.
### Method extensions
#### iterator.chain(iterator1[, β¦iteratorn]) _(es6-iterator/#/chain)_
Chain multiple iterators into one.
### Tests [![Build Status](https://travis-ci.org/medikoo/es6-iterator.png)](https://travis-ci.org/medikoo/es6-iterator)
$ npm test
|