Types
NumericRange
A numeric range consisting of individual numbers and range bounds.
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.
The starting value of the range
The ending value of the range (inclusive)
parseNumericRange
Parses a string representation of a numeric range into a NumericRange array.
The string to parse (e.g., “1,3,5..10,15..=20”)
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.
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.
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.
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.
The numeric range to check
The value to check for in the range
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.
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.
True if the range is decreasing, false otherwise
NumericRangeIterator
An iterator that iterates over a numeric range.
Constructor
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.
The starting number (inclusive)
The ending number (exclusive)
A generator that yields numbers from start to end
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