@naturalcycles/js-lib
Version:
Standard library for universal (browser + Node.js) javascript
35 lines (34 loc) • 1.03 kB
JavaScript
import { AsyncIterable2 } from '../iter/asyncIterable2';
import { Iterable2 } from '../iter/iterable2';
export function _range(fromIncl, toExcl, step = 1) {
if (toExcl === undefined) {
return Array.from(new Array(fromIncl), (_, i) => i);
}
return Array.from({ length: Math.ceil((toExcl - fromIncl) / step) }, (_, i) => i * step + fromIncl);
}
export function _rangeIterable(fromIncl, toExcl, step = 1) {
if (toExcl === undefined) {
toExcl = fromIncl;
fromIncl = 0;
}
return Iterable2.of({
*[Symbol.iterator]() {
for (let i = fromIncl; i < toExcl; i += step) {
yield i;
}
},
});
}
export function _rangeAsyncIterable(fromIncl, toExcl, step = 1) {
if (toExcl === undefined) {
toExcl = fromIncl;
fromIncl = 0;
}
return AsyncIterable2.of({
async *[Symbol.asyncIterator]() {
for (let i = fromIncl; i < toExcl; i += step) {
yield i;
}
},
});
}