range
util
Creates an array of numbers (positive and/or negative) progressing from start up to, but not including, end.
Installation
Import
import { range } from '@tulx/utils';Source Code
Implementation
/**
* Creates an array of numbers (positive and/or negative) progressing from start up to, but not including, end.
*
* @param start - The start of the range.
* @param end - The end of the range.
* @param step - The value to increment or decrement by.
* @returns Returns the range of numbers.
*
* @example
* ```ts
* range(4); // [0, 1, 2, 3]
* range(-4); // [0, -1, -2, -3]
* range(1, 5); // [1, 2, 3, 4]
* range(0, 20, 5); // [0, 5, 10, 15]
* range(0, -4, -1); // [0, -1, -2, -3]
* range(1, 4, 0); // [1, 1, 1]
* range(0); // []
* ```
*/
export function range(start: number, end?: number, step?: number): number[] {
if (end === undefined) {
end = start;
start = 0;
}
if (step === undefined) {
step = start < end ? 1 : -1;
}
if (step === 0) {
return Array(Math.abs(end - start)).fill(start);
}
const result: number[] = [];
let current = start;
if (step > 0) {
while (current < end) {
result.push(current);
current += step;
}
} else {
while (current > end) {
result.push(current);
current += step;
}
}
return result;
}
Example
import { range } from '@tulx/utils';
range(4); // [0, 1, 2, 3]
range(-4); // [0, -1, -2, -3]
range(1, 5); // [1, 2, 3, 4]
range(0, 20, 5); // [0, 5, 10, 15]
range(0, -4, -1); // [0, -1, -2, -3]
range(1, 4, 0); // [1, 1, 1]
range(0); // []Related Functions
attempt
Attempts to invoke func, returning either the result or the caught error object.
bindAll
Binds methods of an object to the object itself, overwriting the existing method.
cond
Creates a function that iterates over pairs and invokes the corresponding function of the first predicate to return truthy.
conforms
Creates a function that invokes the predicate properties of source with the corresponding property values of a given object.
constant
Creates a function that returns value.
defaultTo
Checks value to determine whether a default value should be returned in its place.