incremental-alphabet
v1.0.0
Published
Generate short, incremental alphabetic strings such as a, b, ..., z, aa, ab.
Maintainers
Readme
incremental-alphabet
A tiny, dependency-free generator for incremental strings:
a, b, c, ... z, aa, ab, ac, ...Installation
npm install incremental-alphabetUsage
import { IncrementalAlphabet } from "incremental-alphabet";
const incrementalAlphabet = new IncrementalAlphabet();
incrementalAlphabet.next(); // "a"
incrementalAlphabet.next(); // "b"
incrementalAlphabet.next(); // "c"The generator continues after z with aa, ab, ac, and so on. Each instance keeps its own position in memory.
Custom alphabet
Pass a string to the constructor to control the characters and their order:
const binary = new IncrementalAlphabet("01");
binary.next(); // "0"
binary.next(); // "1"
binary.next(); // "00"
binary.next(); // "01"The alphabet must contain at least one character. Unicode characters, including emoji, are supported.
Iteration
IncrementalAlphabet implements JavaScript's iterable protocol, so it can be used anywhere an iterator is accepted:
const ids = new IncrementalAlphabet();
for (const id of ids) {
console.log(id);
if (id === "ac") break;
}The iterator is infinite, so make sure the consuming code has a stopping condition.
API
new IncrementalAlphabet(alphabet?)
Creates a new generator. alphabet defaults to the lowercase English alphabet (abcdefghijklmnopqrstuvwxyz).
incrementalAlphabet.next()
Returns the next string and advances the generator.
Development
pnpm install
pnpm test
pnpm run typecheck
pnpm run pack:dry-run