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 🙏

© 2025 – Pkg Stats / Ryan Hefner

loop-controls

v1.1.0

Published

break/continue controls for loops and higher-order functions (sync, async, concurrent). Designed to compose with ts-pattern.

Readme

loop-controls

break/continue for loops used together with higher-order functions - sync, async, and bounded-concurrent - designed to compose cleanly with ts-pattern.

Motivation

Problem

Loop controls are not available in the presence of callbacks:

import { match } from "ts-pattern";

type Item = { type: "this" } | { type: "that" }

for (const item of items) {
  match(item)
    .with({ type: "this"}, () => {
      // no way to continue or break the loop from here
    })
  ...
}

Solution

With loop-controls, you can break/continue from within callbacks by using the control object $:

import { forEach } from "loop-controls";
import { match } from "ts-pattern";

type Item = { type: "this" } | { type: "that" };

forEach(items, (item, $, i) => {
  match(item)
    .with({ type: "this"}, () => {
      $.continue(); // skip to next iteration
    })
    .with({ type: "that"}, () => {
      $.break(); // exit the loop entirely
    })
    .exhaustive();
});

API

All handlers receive a control object $ with methods:

  • $.continue(): never — skip to the next iteration.
  • $.break(value?: any): never — stop the loop. In reducers, an optional value replaces the accumulator; in find/findAsync, an optional value becomes the function's return value.
  • In concurrent functions (forEachConcurrent), the control also includes signal: AbortSignal to cancel in-flight I/O that respects AbortSignal (e.g., fetch).

Sync

  • forEach(iter, (item, $, i) => void): { broken: false } | { broken: true }
  • reduce(iter, seed, (acc, item, $, i) => acc): acc
  • find(iter, (item, $, i) => boolean): item | undefined - calling $.break(value) returns value instead.
    • find(iter, (item, $, i) => item is S): S | undefined - supports type-guard predicates.

Async (sequential)

  • forEachAsync(iter, async (item, $, i) => void): Promise<{ broken: false } | { broken: true }>
  • reduceAsync(iter, seed, async (acc, item, $, i) => acc): Promise<acc>
  • findAsync(iter, async (item, $, i) => boolean): Promise<item | undefined>

Async (bounded concurrent over arrays)

  • forEachConcurrent(items: T[], async (item, $, i) => void, { concurrency: number; } = {}): Promise<{ broken: boolean }>

$.break() cancels the remaining queue and provides $.signal to cancel in-flight I/O that respects AbortSignal (e.g., fetch).

Iterable utilities

  • range(end) / range(start, end, step?) - numeric ranges
  • repeat(value, count?) - repeat a value
  • count(start?, step?) - counting sequence
import { forEach, range } from "loop-controls";

// Traditional: for (let i = 0; i < 10; i++)
forEach(range(10), (i, $) => {
  if (i === 5) $.break();
  console.log(i); // 0, 1, 2, 3, 4
});

// Traditional: for (let i = 2; i < 20; i += 3)
forEach(range(2, 20, 3), (i, $) => {
  console.log(i); // 2, 5, 8, 11, 14, 17
});

Design notes

  • Implemented with sentinel exceptions (_Break, _Continue) caught by loop wrappers - cheap on the non-throw path.
  • break/continue are typed as never, so TypeScript understands control flow.
  • No dependencies.

License

MIT