npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

iterize

v1.5.0

Published

Use JavaScript Iterator, Easily

Downloads

37

Readme

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

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.