1.1.1 • Published 2 years ago
rrjx v1.1.1
Javascript range utility
Features
- Use iterator internally.
- Have optional
stepandinclusiveparameter.
Install
yarn add rrjximport range from 'rrjx'
// or
const range = require('rrjx')Usage
Exported type
declare function range(startOrCount: number, end?: number, step?: number, inclusive?: boolean): Generator<number, void, unknown>;
export default range;Supported call patterns:
range(count) // [0, count - 1)
range(start, end) // [start, end)
range(start, end, step, false) // [start, start + step, start + 2 * step, ..., end)
range(start, end, step, true) // [start, start + step, start + 2 * step, ..., end]Usage Samples
- For all elements of an array:
for (const idx of range(arr.length)) console.log(arr[idx]). - Same as above but in the reversed order:
for (const idx of range(arr.length -1, 0, -1, true)) console.log(arr[idx]). - Generate an array
[0, 1, 2, 3]:[...range(4)]. - Forever loop:
for (const _ of range(Infinity)) if (cond()) break. - If
end > start(step >= 0), do nothing:for (const i of range(2, 1)) console.log(i).