Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BigInt ranges work in for/of, but not as values #1605

Open
bbrk24 opened this issue Nov 16, 2024 · 1 comment
Open

BigInt ranges work in for/of, but not as values #1605

bbrk24 opened this issue Nov 16, 2024 · 1 comment

Comments

@bbrk24
Copy link
Contributor

bbrk24 commented Nov 16, 2024

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 + 1 Math.abs.

@bbrk24
Copy link
Contributor Author

bbrk24 commented Nov 16, 2024

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));
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant