iterize
Use JavaScript Iterator, Easily
iterize is a minimalistic creator for the iterator. A great feature called Iterator was added into JavaScript. However, it's a strange concept for most of the front-end developers. iterize helps you create your code more easily and efficiently using the various attributes of the Iterable Protocol.
Why Powerful?
Lazy Evaluation
An array does not need to be allocated to memory on compile-time, nor does it need to be declared explicitly. You can let it be calculated on run-time, or any time you want it to be used.
Expressing Infinity
It was not easy to express the concept of infinity using the conventional syntax of JavaScript. iterize can help you express this concept effectively.
Reuse
Most functions of iterize are implemented as Higher-Order Functions. You can improve your productivity by reusing the functions you have already implemented before.
Install
$ npm install iterize --save
You can import iterize using ESModule style.
;
;
If you want to use UMD, please import dist/iterize.umd.js
on your html file.
API
Range
Returns a transmitter that increases with some steps within a certain range.
Interface
rangestart: number, end?: number, step?: number | Function: Iterator
Example
You can use with for-of
syntax.
; for let number of console; // 0, 1, 2, 3, 4 for let number of console; // 0, 1, 2, 3, 4 for let number of console; // 0, 2, 4
With the spread operator.
;...; // [0, 1, 2, 3, 4]...; // [0, 1, 2, 3, 4]...; // [5, 4, 3, 2, 1]...; // [1, 2, 3 ... 9]...; // [1, 2, 3 ... 9]...; // [2, 4, 16]
Cycle
Interface
Receives a string, an array or an iterator and returns an emitter that repeats infinitely.
cycleitem: string | Array<any> | Iterator: Iterator
Example
; const iter = ;iternext; // { value: 'A', done: false }iternext; // { value: 'B', done: false }iternext; // { value: 'C', done: false }iternext; // { value: 'D', done: false }iternext; // { value: 'A', done: false }iternext; // { value: 'B', done: false }...
; const iter = ;iternext; // { value: 0, done: false }iternext; // { value: 1, done: false }iternext; // { value: 2, done: false }iternext; // { value: 0, done: false }iternext; // { value: 1, done: false }iternext; // { value: 2, done: false }...
Cycle
can also receive a range iterator.
; const rangeIterator = ;const iter = ;iternext; // { value: 0, done: false }iternext; // { value: 2, done: false }iternext; // { value: 4, done: false }iternext; // { value: 0, done: false }iternext; // { value: 2, done: false }iternext; // { value: 4, done: false }iternext; // { value: 0, done: false }...
Repeat
Returns the N copies of the input(number, string, or iterator).
Interface
repeatitem: number | string | Function | Iterator, count: number: Iterator
Example
; for let number of console; // [1, 1, 1]
With the spread operator.
; ...; // [0, 0, 0, 0, 0]...; // ['a', 'a', 'a', 'a', 'a']
; const rangeIterator = ;...; // [1, 2, 3, 4, 1, 2, 3, 4]
Take
Returns the first N items of the iterator sequentially.
Interface
takepredicate: number, iter: Iterator: Iterator
Example
; const cycleIterator = ;for let number of console; // 1, 2, 3, 1, 2
Interface
takeWhilepredicate: Function, iter: Iterator: Iterator
Example
; for let number of console; // 0, 1, 2
CONTRIBUTING
Contributing Guide
Read contributing guide to learn about our development process, how to propose bugfixes and improvements, and how to build and test your changes to iterize.
LICENSE
iterize is MIT licensed.