Eric Foster 35b96bc934 initial commit | 6 anos atrás | |
---|---|---|
.. | ||
# | 6 anos atrás | |
test | 6 anos atrás | |
.editorconfig | 6 anos atrás | |
.npmignore | 6 anos atrás | |
CHANGELOG.md | 6 anos atrás | |
CHANGES | 6 anos atrás | |
LICENSE | 6 anos atrás | |
README.md | 6 anos atrás | |
appveyor.yml | 6 anos atrás | |
array.js | 6 anos atrás | |
for-of.js | 6 anos atrás | |
get.js | 6 anos atrás | |
index.js | 6 anos atrás | |
is-iterable.js | 6 anos atrás | |
package.json | 6 anos atrás | |
string.js | 6 anos atrás | |
valid-iterable.js | 6 anos atrás |
$ 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, Webmake or Webpack
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
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 }
Dedicated for arrays and array-likes. Supports three iteration kinds:
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:
(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));
Assures proper iteration over unicode symbols.
See: http://mathiasbynens.be/notes/javascript-unicode
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 }
Polyfill for ECMAScript 6 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:
var result = [];
forOf([1,2,3,4]', function (val, doBreak) {
result.push(monkey);
if (val >= 3) doBreak();
});
console.log(result); // [1, 2, 3];
Return iterator for any iterable object.
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 }
Whether obj is iterable
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
If obj is an iterable it is returned. Otherwise TypeError is thrown.
Chain multiple iterators into one.
$ npm test