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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@tomi20v/iterators

v0.14.0

Published

TypeScript library of 2D and recursive array iterators with reverse and rotation.

Downloads

3

Readme

iterators

TypeScript library of various array iterators with reverse and rotation.

why?

Originally a proof of concept that matrix rotation can be done by transforming the matrix iterators, leaving the original data intact. It is implemented for 2D arrays ("bitmaps") but there is no general n-dimension array solution yet.

These iterators support the "for (const x in iterator) {...}" syntax. This is great since now these can be used in reactive template for loops (eg. svelte).

contents

ArrayAxisIterator

Iterates an array over one axis (dimension), eg. iterate a column in a 2D array. Not recursive, emits T items.

const anyArray3X3 = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
];
const iterator = new ArrayDimensionIterator(anyArray3X3, [null, 1]);
for (const i of iterator) console.log(i); // 2, 5, 8

ArrayIterator

Recursively iterates arrays (ReadonlyArray types) of any number of dimensions. Needs one loop per dimension when iterating. Compatible with frontend template loops.

const arr = [1, 2, 3, 4, 5];
const iterator = new ArrayIterator(arr);
for (const item of iterator) console.log(item); // 1, 2, 3, 4, 5
for (const item of iterator.reverse()) console.log(item); // 5, 4, 3, 2, 1

For >2D arrays, it yields partial iterators recursively. Eg, when the yielded item is an array too, then another partial ArrayIterator is returned which iterates over that array.

const arr = [[1, 2, 3], [4, 5,6]];
const iterator = new ArrayIterator(arr);
for (const row of iterator) { 
  for (const item of iterator) {
    console.log(item); // 1, 2, 3, 4, 5, 6
}} 
for (const row of iterator.reverse()) { 
  for (const item of iterator) {
    console.log(item); // 4, 5, 6, 1, 2, 3
}} 
for (const row of iterator) { 
  for (const item of iterator.reverse()) {
    console.log(item); // 3, 2, 1, 6, 5, 4
}} 

ArrayIterator2D

Can handle (at least) 2D data. Provides rotating (by 90 degrees). Useful for displaying some grid or table (eg. rotating blocks in tetris). Current version still extracts columns with 90 and 270 degree rotations.

const arr = [[1, 2, 3], [4, 5, 6]];
const iterator = new ArrayIterator2D(arr).rotate();
for (const row of iterator) {
  for (const item of iterator) {
    console.log(item); // 4, 1, 5, 2, 6, 3
  }}

FlatteningIterator

Recursively iterates arrays (ReadonlyArray types) of any number of dimensions, but can be iterated in a single loop. Supports mappers for transformations etc.

Yields IterationItem items which contain the indices ("coordinates") per dimension along with the value. Looping this way might result in more readable code, and, can reduce cyclomatic complexity.

const arr = [[4, 5], [14, 15], [24, 25]];
const iterator = new FlatteningIterator(arr);
console.log(...iterator);

// { x: 0, y: 0, value: 4 }
// { x: 0, y: 1, value: 5 }
// { x: 1, y: 0, value: 14 }
// { x: 1, y: 1, value: 15 } 
// { x: 2, y: 0, value: 24 } 
// { x: 2, y: 1, value: 25 }

Mappers for FlatteningIterator

IteratorMappers object holds various mappers, which can be used with the FlatteningIterator.

import {onlyTheValue, rotateCoords} from "./IteratorMappers";

const arr = [[4, 5], [14, 15], [24, 25]];
const iterator = new FlatteningIterator(arr);
const iterator2 = iterator.with(scale(2));
iterator.use(onlyTheValue);
console.log(...iterator); // 4, 5, 14, 15, 24, 25
console.log(...iterator2); // 8, 10, 28, 30, 48, 50

Custom mappers

Functions or arrow functions can be used for custom mappers. Functions will be bound to the iterator object when executing.

import {IterationItem} from "./IterationItem";
import FlatteningIterator from "./FlatteningIterator";

const arr = [[4, 5], [14, 15], [24, 25]];
const iterator = new FlatteningIterator<number>(arr).with(
  (this: FlatteningIterator<number>, each: IterationItem<number>) => {
    // expect(this).toBeInstanceOf(FlatteningIterator);
    return each;
  }
);
console.log(...iterator);
// { x: 0, y: 0, value: 4 }
// { x: 0, y: 1, value: 5 }
// { x: 1, y: 0, value: 14 }
// { x: 1, y: 1, value: 15 } 
// { x: 2, y: 0, value: 24 } 
// { x: 2, y: 1, value: 25 }