Generate Range Array
Generates array of numbers in specified range (like Python's range function)
Implementation
Copy the complete example or adapt its typed interface to your project. Comments are kept inside the snippet so the intent travels with the code.
/**
* Generate array of numbers in specified range
* @param start Start number (default: 0)
* @param end End number (exclusive)
* @param step Step size (default: 1)
* @returns Array of numbers in range
*/
function range(start = 0, end?: number, step = 1): number[] {
if (end === undefined) {
end = start;
start = 0;
}
const length = Math.max(Math.ceil((end - start) / step), 0);
const rangeArray = Array(length);
for (let i = 0; i < length; i++) {
rangeArray[i] = start + (i * step);
}
return rangeArray;
}
// Usage Example
console.log(range(5)); // [0, 1, 2, 3, 4]
console.log(range(1, 6)); // [1, 2, 3, 4, 5]
console.log(range(0, 10, 2)); // [0, 2, 4, 6, 8]When to use it
Great for creating pagination arrays, loop counters, or generating test data sets
Review the input assumptions and browser or runtime support before adopting a utility unchanged. Small helpers are easiest to maintain when their contract stays narrow.
Reasoning and trade-offs
How it works
The function calculates how many steps fit between start and end, then maps indexes to values using the requested step. It provides a compact numeric sequence similar to range helpers in other languages.
What to watch for
A zero step cannot progress, direction must agree with the boundaries, and floating-point increments accumulate precision errors. Huge ranges can allocate excessive memory before a caller processes the first value.
Production use
Use arrays for small pagination, labels, or test inputs. For large or open-ended sequences, prefer a generator that yields values lazily and validate a maximum length before allocating output.
Before you ship
- Confirm the input contract with tests that cover empty, invalid, boundary, and representative real-world values before sharing the helper across a codebase.
- Keep the function focused on one responsibility and name any project-specific policy explicitly instead of hiding it inside a generic-looking utility.
- Measure behavior in the browsers or runtime versions your product supports, especially when timing, storage, DOM state, locale, or network access is involved.
- Document the return type, mutation behavior, failure mode, and performance characteristics beside the call site so future maintainers can evaluate changes safely.
- Compare the example with current official platform documentation when it relies on a browser API, language feature, or runtime behavior that may change over time.
- If the helper crosses a trust boundary, validate again on the server and avoid treating browser-side checks, TypeScript annotations, or formatted output as a security guarantee.
- Add a small regression test whenever a real edge case is discovered; concrete examples preserve the reason behind a guard more reliably than a general comment.