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

kld-array-iterators

v0.0.17

Published

A collection of classes used to iterate over arrays

Downloads

7

Readme

kld-array-iterators

This module is a collection of classes used to iterate over arrays. Iterators include:

  • Array Iterator - a simple in-order walk of the provided args or array
  • Reverse Iterator - a reverse walk of the provided args or array
  • Random Iterator - a random walk of the provided args or array. All items are visited once and only once
  • Combination Iterator - a walk of all of the subsets of the provided args or array
  • Permutation Iterator - a walk of all permutations of the provided args or array
  • Sequence Iterator - walk the list of iterators in the provided args or array. Each iterator is visited in order and run to exhaustion
  • Group Iterator - iterator over a list of iterators, treating the entire group much like a counter. This is the cross-product of all iterators in the provided args or array

Installation

npm install kld-array-iterators

Examples

The following are some examples of each iterator in use. You can find the full source for these under the "examples" folder.

Array Iterator

var Iterator = require('kld-array-iterators').ArrayIterator;

var iter = new Iterator(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

while (iter.hasNext()) {
    console.log(iter.next());
}

Output:

1
2
3
4
5
6
7
8
9
10

Reverse Iterator

var Iterator = require('kld-array-iterators').ReverseIterator;

var iter = new Iterator(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

while (iter.hasNext()) {
    console.log(iter.next());
}

Output:

10
9
8
7
6
5
4
3
2
1

Random Iterator

var Iterator = require('kld-array-iterators').RandomIterator;

var iter = new Iterator(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

while (iter.hasNext()) {
    console.log(iter.next());
}

Output:

10
6
7
5
3
8
1
9
2
4

Combination Iterator

var Iterator = require('kld-array-iterators').CombinationIterator;

var iter = new Iterator(1, 2, 3, 4);

while (iter.hasNext()) {
    console.log(iter.next());
}

Output:

[ 1 ]
[ 2 ]
[ 1, 2 ]
[ 3 ]
[ 1, 3 ]
[ 2, 3 ]
[ 1, 2, 3 ]
[ 4 ]
[ 1, 4 ]
[ 2, 4 ]
[ 1, 2, 4 ]
[ 3, 4 ]
[ 1, 3, 4 ]
[ 2, 3, 4 ]
[ 1, 2, 3, 4 ]

Permutation Iterator

var Iterator = require('kld-array-iterators').PermutationIterator;

var iter = new Iterator(1, 2, 3, 4);

while (iter.hasNext()) {
    console.log(iter.next());
}

Output:

[ 1, 2, 3, 4 ]
[ 1, 2, 4, 3 ]
[ 1, 3, 2, 4 ]
[ 1, 4, 2, 3 ]
[ 1, 3, 4, 2 ]
[ 1, 4, 3, 2 ]
[ 2, 1, 3, 4 ]
[ 2, 1, 4, 3 ]
[ 3, 1, 2, 4 ]
[ 4, 1, 2, 3 ]
[ 3, 1, 4, 2 ]
[ 4, 1, 3, 2 ]
[ 2, 3, 1, 4 ]
[ 2, 4, 1, 3 ]
[ 3, 2, 1, 4 ]
[ 4, 2, 1, 3 ]
[ 3, 4, 1, 2 ]
[ 4, 3, 1, 2 ]
[ 2, 3, 4, 1 ]
[ 2, 4, 3, 1 ]
[ 3, 2, 4, 1 ]
[ 4, 2, 3, 1 ]
[ 3, 4, 2, 1 ]
[ 4, 3, 2, 1 ]

Sequence Iterator

var ArrayIterator = require('kld-array-iterators').ArrayIterator,
    SequenceIterator = require('kld-array-iterators').SequenceIterator;

var iter = new SequenceIterator(
    new ArrayIterator(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    new ArrayIterator('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i')
);

while (iter.hasNext()) {
    console.log(iter.next());
}

Output:

1
2
3
4
5
6
7
8
9
10
a
b
c
d
e
f
g
h
i

Group Iterator

var ArrayIterator = require('kld-array-iterators').ArrayIterator,
    CombinationIterator = require('kld-array-iterators').CombinationIterator,
    PermutationIterator = require('kld-array-iterators').PermutationIterator,
    RandomIterator = require('kld-array-iterators').RandomIterator,
    ReverseIterator = require('kld-array-iterators').ReverseIterator,
    GroupIterator = require('kld-array-iterators').GroupIterator,
    flatten = require('./flatten.js');

var iter = new GroupIterator(
    new ArrayIterator(1, 2),
    new CombinationIterator('a','b', 'c'),
    new PermutationIterator('X', 'Y','Z'),
    new RandomIterator(4, 5, 6),
    new ReverseIterator('d','e')
);
var count = 0;

while (iter.hasNext()) {
    var value = iter.next();
    var flattened = flatten(value).join("");
    count++;

    console.log("%d: %s", count, flattened);
}

Output:

1: 1aXYZ5e
2: 2aXYZ5e
3: 1bXYZ5e
4: 2bXYZ5e
5: 1abXYZ5e
6: 2abXYZ5e
7: 1cXYZ5e
8: 2cXYZ5e
9: 1acXYZ5e
...
495: 1abZYX4d
496: 2abZYX4d
497: 1cZYX4d
498: 2cZYX4d
499: 1acZYX4d
500: 2acZYX4d
501: 1bcZYX4d
502: 2bcZYX4d
503: 1abcZYX4d
504: 2abcZYX4d

Generate Floats

var ArrayIterator = require('kld-array-iterators').ArrayIterator,
    GroupIterator = require('kld-array-iterators').GroupIterator,
    SequenceIterator = require('kld-array-iterators').SequenceIterator,
    flatten = require('./flatten');

var iter = new GroupIterator(
    new ArrayIterator('', '-', '+'),
    new ArrayIterator('1', '2'),
    new ArrayIterator('', '.0', '.1', '.02', '.003'),
    new SequenceIterator(
        new ArrayIterator(''),
        new GroupIterator(
            new ArrayIterator('e', 'E'),
            new ArrayIterator('', '-', '+'),
            new ArrayIterator('1', '2', '300')
        )
    )
);
var count = 0;

while (iter.hasNext()) {
    count++;

    var value = iter.next();
    var flattened = flatten(value).join('');

    console.log("%d: %s", count, flattened);
}

Output:

1: 1
2: -1
3: +1
4: 2
5: -2
6: +2
7: 1.0
8: -1.0
9: +1.0
...
561: +1.02E+300
562: 2.02E+300
563: -2.02E+300
564: +2.02E+300
565: 1.003E+300
566: -1.003E+300
567: +1.003E+300
568: 2.003E+300
569: -2.003E+300
570: +2.003E+300