Skip to main content

Types

NumericRange

A numeric range consisting of individual numbers and range bounds.
NumericRange
Array<NumericRangePart>
An array of numbers and/or range bounds

NumericRangePart

NumericRangePart
number | NumericRangeBound
Either a single number or a range bound

NumericRangeBound

Represents a numeric range bound with start and end values.
from
number
required
The starting value of the range
to
number
required
The ending value of the range (inclusive)

parseNumericRange

Parses a string representation of a numeric range into a NumericRange array.
s
string
required
The string to parse (e.g., “1,3,5..10,15..=20”)
return
NumericRange
A parsed numeric range
throws
NumericRangeError
Throws if the input string contains invalid integers or ranges

Example

import { parseNumericRange } from "@temelj/iterator";

const range = parseNumericRange("1,3,5..8,10..=12");
console.log(range);
// [1, 3, { from: 5, to: 7 }, { from: 10, to: 12 }]

// Decreasing ranges
const decRange = parseNumericRange("10..5");
console.log(decRange);
// [{ from: 10, to: 6 }]

numericRangeToString

Converts a numeric range to a string representation.
range
NumericRange
required
The range to convert
return
string
The string representation of the numeric range

Example

import { numericRangeToString } from "@temelj/iterator";

const range = [1, { from: 5, to: 8 }, 10];
const str = numericRangeToString(range);
console.log(str); // "1,5..=8,10"

flattenNumericRange

Flattens a numeric range to a number array.
range
NumericRange
required
The range to flatten
return
number[]
A flattened array of all numbers in the range

Example

import { parseNumericRange, flattenNumericRange } from "@temelj/iterator";

const range = parseNumericRange("1,3,5..8");
const flattened = flattenNumericRange(range);
console.log(flattened); // [1, 3, 5, 6, 7]

reverseNumericRange

Reverses a numeric range.
range
NumericRange
required
The range to reverse
return
NumericRange
A reversed range

Example

import { reverseNumericRange } from "@temelj/iterator";

const range = [1, { from: 5, to: 8 }];
const reversed = reverseNumericRange(range);
console.log(reversed); // [{ from: 8, to: 5 }, 1]

numericRangeContains

Checks if a numeric range contains a specified value.
range
NumericRange
required
The numeric range to check
value
number
required
The value to check for in the range
return
boolean
True if the value is contained in the range, false otherwise

Example

import { parseNumericRange, numericRangeContains } from "@temelj/iterator";

const range = parseNumericRange("1,5..10");
console.log(numericRangeContains(range, 7));  // true
console.log(numericRangeContains(range, 3));  // false
console.log(numericRangeContains(range, 1));  // true

isNumericRangeIncreasing

Checks whether a range is strictly increasing.
range
NumericRange
required
The range to check
return
boolean
True if the range is increasing, false otherwise

Example

import { isNumericRangeIncreasing } from "@temelj/iterator";

const inc = [1, 3, { from: 5, to: 10 }];
console.log(isNumericRangeIncreasing(inc)); // true

const dec = [10, { from: 5, to: 1 }];
console.log(isNumericRangeIncreasing(dec)); // false

isNumericRangeDecreasing

Checks whether a range is strictly decreasing.
range
NumericRange
required
The range to check
return
boolean
True if the range is decreasing, false otherwise

NumericRangeIterator

An iterator that iterates over a numeric range.

Constructor

range
NumericRange
required
The numeric range to iterate over

Example

import { parseNumericRange, NumericRangeIterator } from "@temelj/iterator";

const range = parseNumericRange("1,3,5..8");
const iterator = new NumericRangeIterator(range);

for (const num of iterator) {
  console.log(num);
}
// Output: 1, 3, 5, 6, 7

range

A simple programmatic generator for numeric ranges.
start
number
required
The starting number (inclusive)
end
number
required
The ending number (exclusive)
step
number
default:"1"
The step size
return
Generator<number>
A generator that yields numbers from start to end
throws
Error
Throws an error if step is zero

Example

import { range } from "@temelj/iterator";

// Basic range
for (const n of range(0, 5)) {
  console.log(n);
}
// Output: 0, 1, 2, 3, 4

// Range with step
for (const n of range(0, 10, 2)) {
  console.log(n);
}
// Output: 0, 2, 4, 6, 8

// Decreasing range
for (const n of range(5, 0, -1)) {
  console.log(n);
}
// Output: 5, 4, 3, 2, 1

Build docs developers (and LLMs) love