npm.io
3.0.1 • Published 3d ago

@hugoalh/range-iterator

Licence
MIT
Version
3.0.1
Deps
0
Size
15 kB
Vulns
0
Weekly
0
Stars
2

Range Iterator (ES)

MIT

DistBoard @hugoalhGitHubJSRNPM

An ECMAScript module to iterate between range.

Runtime Targets

Any runtime which support ECMAScript should able to use this; These runtimes are officially supported:

Runtime Permissions

This does not request any runtime permission.

Sources & Entrypoints

  • GitHub Raw
    https://raw.githubusercontent.com/hugoalh/range-iterator-es/{Tag}/mod.ts
  • JSR
    jsr:@hugoalh/range-iterator[@{Tag}]
  • NPM
    npm:@hugoalh/range-iterator[@{Tag}]
Name Path Description
. ./mod.ts Default.

  • Different runtimes have vary support for the sources and entrypoints, visit the runtime documentation for more information.
  • It is recommended to include tag for immutability.
  • These are not part of the public APIs hence should not be used:
    • Benchmark/Test file (e.g.: example.bench.ts, example.test.ts).
    • Entrypoint name or path include any underscore prefix (e.g.: _example.ts, foo/_example.ts).
    • Identifier/Namespace/Symbol include any underscore prefix (e.g.: _example, Foo._example).

APIs

  • function rangeIterator(start: bigint, end: bigint, options?: RangeIteratorOptions<bigint>): Generator<bigint>;
    function rangeIterator(start: bigint, end: bigint, step: bigint): Generator<bigint>;
    function rangeIterator(start: number, end: number, options?: RangeIteratorOptions<number>): Generator<number>;
    function rangeIterator(start: number, end: number, step: number): Generator<number>;
    function rangeIterator(start: string, end: string, options?: RangeIteratorOptions<string>): Generator<string>;
    function rangeIterator(start: string, end: string, step: number): Generator<string>;
  • interface RangeIteratorOptions<T extends bigint | number | string> {
      excludeEnd?: boolean;
      excludes?: readonly T[] | Set<T>;
      excludeStart?: boolean;
      step?: T extends string ? number : T;
    }

Examples

  • Array.from(rangeIterator(1, 9));
    //=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
  • Array.from(rangeIterator(1n, 9n, { excludeEnd: true }));
    //=> [1n, 2n, 3n, 4n, 5n, 6n, 7n, 8n]
  • Array.from(rangeIterator(1, 9, { step: 0.5 }));
    //=> [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9]
  • Array.from(rangeIterator("a", "z"));
    //=> ["a", "b", "c", ... +20 ..., "x", "y", "z"]
  • Array.from(rangeIterator(9, 1));
    //=> [9, 8, 7, 6, 5, 4, 3, 2, 1]
  • Array.from(rangeIterator(9n, 1n, { excludeEnd: true }));
    //=> [9n, 8n, 7n, 6n, 5n, 4n, 3n, 2n]
  • Array.from(rangeIterator(9, 1, { step: 0.5 }));
    //=> [9, 8.5, 8, 7.5, 7, 6.5, 6, 5.5, 5, 4.5, 4, 3.5, 3, 2.5, 2, 1.5, 1]
  • Array.from(rangeIterator("z", "a"));
    //=> ["z", "y", "x", ... +20 ..., "c", "b", "a"]

Keywords