Miss any of our Open RFC calls?Watch the recordings here! »

iterize

1.5.0 • Public • Published

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(startnumber, 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(itemstring | 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(itemnumber | string | Function | Iterator, countnumber)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(predicatenumber, iterIterator)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(predicateFunction, iterIterator)Iterator

Example

import {takeWhile, range} from 'iterize';
 
for (let number of takeWhile(x => x < 3, range(5))) {
    console.log(number);  // 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.

Install

npm i iterize

DownloadsWeekly Downloads

3

Version

1.5.0

License

MIT

Unpacked Size

69.9 kB

Total Files

40

Last publish

Collaborators

  • avatar