Skip to content

hg-pyun/iterize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

iterize

npm npm GitHub license Build Status

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.

import * as iterize from 'iterize';
import {range} from 'iterize';

If you want to use UMD, please import dist/iterize.umd.js on your html file.

<script src="iterize.umd.js" />

API

Range

Returns a transmitter that increases with some steps within a certain range.

Interface

range(start: number, end?: number, step?: number | Function): Iterator

Example

You can use with for-of syntax.

import {range} from 'iterize';

for (let number of range(5)) {
    console.log(number);  // 0, 1, 2, 3, 4
}

for (let number of range(0, 5)) {
    console.log(number);  // 0, 1, 2, 3, 4
}

for (let number of range(0, 5, 2)) {
    console.log(number);  // 0, 2, 4
}

With the spread operator.

import {range} from 'iterize';
[...range(5)];  // [0, 1, 2, 3, 4]
[...range(0, 5)]; // [0, 1, 2, 3, 4]
[...range(5, 0)]; // [5, 4, 3, 2, 1]
[...range(1, 10, 1)]; // [1, 2, 3 ... 9]
[...range(1, 10, x => x + 1)]; // [1, 2, 3 ... 9]
[...range(2, 64, x => x * x)]; // [2, 4, 16]

Cycle

Interface

Receives a string, an array or an iterator and returns an emitter that repeats infinitely.

cycle(item: string | Array<any> | Iterator): Iterator

Example

import {cycle} from 'iterize';

const iter = cycle('ABCD');
iter.next(); // { value: 'A', done: false }
iter.next(); // { value: 'B', done: false }
iter.next(); // { value: 'C', done: false }
iter.next(); // { value: 'D', done: false }
iter.next(); // { value: 'A', done: false }
iter.next(); // { value: 'B', done: false }
...
import {cycle} from 'iterize';

const iter = cycle([0, 1, 2]);
iter.next(); // { value: 0, done: false }
iter.next(); // { value: 1, done: false }
iter.next(); // { value: 2, done: false }
iter.next(); // { value: 0, done: false }
iter.next(); // { value: 1, done: false }
iter.next(); // { value: 2, done: false }
...

Cycle can also receive a range iterator.

import {cycle, range} from 'iterize';

const rangeIterator = range(0, 5, 2);
const iter = cycle(rangeIterator);
iter.next(); // { value: 0, done: false }
iter.next(); // { value: 2, done: false }
iter.next(); // { value: 4, done: false }
iter.next(); // { value: 0, done: false }
iter.next(); // { value: 2, done: false }
iter.next(); // { value: 4, done: false }
iter.next(); // { value: 0, done: false }
...

Repeat

Returns the N copies of the input(number, string, or iterator).

Interface

repeat(item: number | string | Function | Iterator, count: number): Iterator

Example

import {repeat} from 'iterize';

for (let number of repeat(1, 3)) {
    console.log(number);  // [1, 1, 1]
}

With the spread operator.

import {repeat} from 'iterize';

[...repeat(0, 5)];   // [0, 0, 0, 0, 0]
[...repeat('a', 5)]; // ['a', 'a', 'a', 'a', 'a']
import {repeat, range} from 'iterize';

const rangeIterator = range(1, 5, 1);
[...repeat(rangeIterator, 2)]; // [1, 2, 3, 4, 1, 2, 3, 4]

Take

Returns the first N items of the iterator sequentially.

Interface

take(predicate: number, iter: Iterator): Iterator

Example

import {take, cycle} from 'iterize';

const cycleIterator = cycle([1, 2, 3]);
for (let number of take(5, cycleIterator)) {
    console.log(number);  // 1, 2, 3, 1, 2
}

Interface

takeWhile(predicate: Function, iter: Iterator): Iterator

Example

import {takeWhile, range} from 'iterize';

for (let number of takeWhile(x => x < 3, range(5))) {
    console.log(number);  // 0, 1, 2
}

CONTRIBUTING

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.