Civet
Civet copied to clipboard
BigInt ranges work in for/of, but not as values
trafficstars
for i of [1n..28n]
;
i is in [1n..28n]
// vvv
for (let i1 = 1n; i1 <= 28n; ++i1) {
const i = i1;
}
((s, e) => {
let step = e > s ? 1 : -1;
return Array.from({ length: Math.abs(e - s) + 1 }, (_, i) => s + i * step);
})(1n, 28n).includes(i);
The latter will throw a TypeError at runtime due to the + 1Math.abs.
Potential translation that works for both bigint and number:
((s, e) => {
let step = e > s ? 1 : -1;
const makeStep = typeof s == 'bigint'
? (i) => BigInt(step * i)
: (i) => step * i;
return Array.from({ length: Math.abs(Number(e - s)) + 1 }, (_, i) => s + makeStep(i));
})
We may also be able to convert is in [...] with range literals into comparison operations.