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

ho-iter

v0.3.0

Published

The iterator which takes iterators as arguments and returns an iterator

Downloads

168

Readme

Higher-Order Iterator

NPM Status Travis Status Coverage Status Dependency Status

The iterator which takes iterators as arguments and returns an iterator.

Iterators and Generators Guide

Install

$ npm install --save ho-iter

Usage

const hoi = require('ho-iter');

const entries1 = hoi({ foo: 'bar' });
const entries2 = hoi({ baz: 42 });

const keys = [];
const values = [];

for (let [key, value] of hoi.series(entries1, entries2)) {
    keys.push(key);
    values.push(value);
}

console.log(`keys: ${keys}`);
console.log(`values: ${values}`);

// ➜ keys: [foo, baz]
// ➜ values: [bar, 42]

API

Create iterator:

Helpers:

hoi(iterable)

Creates iterator for iterable object.

Uses Iterable protocol to create iterator.

Iterable data structures:

Example 1. Create iterator with array items.

const hoi = require('ho-iter');

const iter = hoi([1, 2, 3, 4]);

for (let item of iter) {
    console.log(item);
}

// ➜ 1
// ➜ 2
// ➜ 3
// ➜ 4

Example 2. Create iterator with set values.

const hoi = require('ho-iter');

const set = new Set([1, 2, 3, 4]);
const iter = hoi(set);

for (let item of iter) {
    console.log(item);
}

// ➜ 1
// ➜ 2
// ➜ 3
// ➜ 4

Example 3. Create iterator with map entries.

const hoi = require('ho-iter');

const map = new Map();

map.set('key1', 'val1');
map.set('key2', 'val2');

const iter = hoi(map);

for (let [key, val] of iter) {
    console.log(`key: ${key}, val: ${val}`);
}

// ➜ key: key1, val: val1
// ➜ key: key2, val: val2

Example 3. Create iterator with string characters.

const hoi = require('ho-iter');

const iter = hoi('Iterate me!');

for (let char of iter) {
    console.log(char);
}

// ➜ I
// ➜ t
// ➜ e
// ➜ r
// ➜ a
// ➜ t
// ➜ e
// ➜
// ➜ m
// ➜ e
// ➜ !

Important: If you want to create iterator with integral string use hoi.value(string) helper.

const hoi = require('ho-iter');

const iter = hoi.value("Don’t touch me!");

for (let item of iter) {
    console.log(item);
}

// ➜ Don’t touch me!

Example 4. Creates iterator with arguments.

const hoi = require('ho-iter');

const iter = hoi.value("Don't touch me!");

function foo(arg1, arg2) {
    const iter = hoi(arguments);

    for (let arg of iter) {
        console.log(arg);
    }
}

foo('bar', 'baz');

// ➜ bar
// ➜ baz

Example 5. Create empty iterator.

If value is not specified, returns empty iterator.

The empty iterator just returns done for each call.

const hoi = require('ho-iter');

const iter = hoi();

iter.next();

// ➜ { done: true, value: null }

hoi(iterator)

Provides specified iterator or creates iterator by Iterable protocol.

Important: If iterator does not support Iterable protocol then hoi(iterator) does not clone specified iterator.

const hoi = require('ho-iter');

function makeIterator(array) {
    let nextIndex = 0;

    return {
        next() {
            return nextIndex < array.length ?
                { value: array[nextIndex++], done: false } :
                { done: true };
        }
    }
}

const iter = makeIterator([1, 2, 3]);

iter.next(); // { value: 1, done: false }
iter.next(); // { value: 1, done: false }
iter.next(); // { value: 1, done: false }
iter.next(); // { done: true }

const hoIter = hoi(iter); // iterator will not be clonned

// ➜ { done: true }

hoi(object)

Creates an iterator whose elements are arrays corresponding to the enumerable property [key, value] pairs found directly upon object.

The ordering of the properties is the same as that given by looping over the property values of the object manually.

This is reminiscent of the Object.entries method or Map[iterator].

Important: Object does not Iterable protocol in ES2015.

const hoi = require('ho-iter');

const iter = hoi({
    foo: 'bar',
    baz: 42
});

for (let [key, val] of iter) {
    console.log(`key: ${key}, val: ${val}`);
}

// ➜ key: foo, val: bar
// ➜ key: baz, val: 42

Important: If you want to create iterator with integral object use hoi.value(object) helper.

const hoi = require('ho-iter');

const iter = hoi.value({ foo: 'bar' });

for (let item of iter) {
    console.log(item);
}

// ➜ { foo: 'bar' }

hoi(number|boolean|symbol|regexp|function)

If value is not iterable then throws error:

It is impossible to create iterator: `value` is not iterable object.

Not iterable data structures:

const hoi = require('ho-iter');

const iter = hoi(null);

// It is impossible to create iterator: `null` is not iterable object.

Use hoi.value(value) to create iterator with one value.

Helpers

value(value)

Creates iterator with specified value.

If value is not specified, returns empty iterator.

Example:

const hoi = require('ho-iter');

const iter = hoi.value(123);

iter.next(); // { value: 123, done: false }
iter.next(); // { value: null, done: true }

isIterable(iterable)

Returns true if the specified object implements the Iterator protocol via implementing a Symbol.iterator.

Example:

const isIterable = require('ho-iter').isIterable;

isIterable([1, 2, 3]); // true
isIterable(123); // false

isIterator(iterator)

Returns true if the specified object is iterator.

const isIterator = require('ho-iter').isIterator;

const generator = function *() {};
const iterator = generator();

isIterator(generator) // false
isIterator(iterator) // true

series(...iterables)

Returns an Iterator, that traverses iterators in series.

This is reminiscent of the concatenation of arrays.

Example:

const series = require('ho-iter').series;

const arr1 = [1, 2];
const arr2 = [3, 4];

const set1 = new Set([1, 2]);
const set2 = new Set([3, 4]);

[].concat(arr1, arr2);

// ➜ [1, 2, 3, 4]

for (let item of series(set1, set2)) {
    console.log(item);
}

// ➜ 1 2 3 4

evenly(...iterables)

Returns an Iterator, that traverses iterators evenly.

This is reminiscent of the traversing of several arrays.

Example:

const evenly = require('ho-iter').evenly;

const arr1 = [1, 2];
const arr2 = [3, 4];

const set1 = new Set([1, 2]);
const set2 = new Set([3, 4]);

for (let i = 0; i < arr1.length; i++) {
    console.log(arr1[i]);
    console.log(arr2[i]);
}

// ➜ 1 3 2 4

for (let item of evenly(set1, set2)) {
    console.log(item);
}

// ➜ 1 3 2 4

reverse(iterator)

Returns an reversed Iterator.

Important: incompatible with infinite iterator. Don't use infinite iterator, otherwise reverse method will fall into endless loop loop.

This is reminiscent of the reversing of an array.

Example:

const reverse = require('ho-iter').reverse;

const arr = [1, 2, 3, 4];
const set = new Set([1, 2, 3, 4]);

for (let i = arr.length; i >= 0; i--) {
    console.log(arr[i]);
}

// ➜ 4 3 2 1

for (let item of reverse(set)) {
    console.log(item);
}

// ➜ 4 3 2 1

License

MIT © Andrew Abramov