npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-transform-bigint

v1.0.34

Published

A plugin for babel to transform `x * y` into something like `JSBI.multiply(x, y)` to support bigints.

Downloads

4,306

Readme

babel-plugin-transform-bigint

Update: Now it can convert a code using BigInt into a code using JSBI (https://github.com/GoogleChromeLabs/jsbi). It will try to detect when an operator is used for bigints, not numbers. This will not work in many cases, so please use JSBI directly only if you know, that the code works only with bigints.

An example from https://github.com/GoogleChromeLabs/babel-plugin-transform-jsbi-to-bigint:

Input using native BigInts:

const a = BigInt(Number.MAX_SAFE_INTEGER);
const b = 2n;

a + b;
a - b;
a * b;
a / b;
a % b;
a ** b;
a << b;
a >> b;
a & b;
a | b;
a ^ b;

-a;
~a;

a === b;
a < b;
a <= b;
a > b;
a >= b;

a.toString();
Number(a);

Compiled output using JSBI:

const a = JSBI.BigInt(Number.MAX_SAFE_INTEGER);
const b = JSBI.BigInt(2);
JSBI.add(a, b);
JSBI.subtract(a, b);
JSBI.multiply(a, b);
JSBI.divide(a, b);
JSBI.remainder(a, b);
JSBI.exponentiate(a, b);
JSBI.leftShift(a, b);
JSBI.signedRightShift(a, b);
JSBI.bitwiseAnd(a, b);
JSBI.bitwiseOr(a, b);
JSBI.bitwiseXor(a, b);
JSBI.unaryMinus(a);
JSBI.bitwiseNot(a);
JSBI.equal(a, b);
JSBI.lessThan(a, b);
JSBI.lessThanOrEqual(a, b);
JSBI.greaterThan(a, b);
JSBI.greaterThanOrEqual(a, b);
a.toString();
JSBI.toNumber(a);

Playground:

  1. Open https://babeljs.io/en/repl
  2. Turn off all presets.
  3. "Add plugin" @babel/babel-plugin-syntax-bigint
  4. "Add plugin" babel-plugin-transform-bigint

¡ It is buggy !

Example:

  1. Create a folder "example".
  2. Create a file test.js:
// floor(log2(n)), n >= 1
function ilog2(n) {
  let i = 0n;
  while (n >= 2n**(2n**i)) {
    i += 1n;
  }
  let e = 0n;
  let t = 1n;
  while (i >= 0n) {
    let b = 2n**i;
    if (n >= t * 2n**b) {
      t *= 2n**b;
      e += b;
    }
    i -= 1n;
  }
  return e;
}

// floor(sqrt(S)), S >= 0, https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
function sqrt(S) {
  let e = ilog2(S);
  if (e < 2n) {
    return 1n;
  }
  let f = e / 4n + 1n;
  let x = (sqrt(S / 2n**(f * 2n)) + 1n) * 2n**f;
  let xprev = x + 1n;
  while (xprev > x) {
    xprev = x;
    x = (x + S / x) / 2n;
  }
  return xprev;
}

function squareRoot(value, decimalDigits) {
  return (sqrt(BigInt(value) * 10n**(BigInt(decimalDigits) * 2n + 2n)) + 5n).toString();
}

  1. Use babel:
npm init
npm install --save https://github.com/Yaffle/babel-plugin-transform-bigint
npm install --save-dev @babel/core @babel/cli
npm install jsbi --save
npm install --global http-server
npx babel --plugins=babel-plugin-transform-bigint test.js > test-transformed.js
http-server -p 8081
  1. Comment out the next line in test-transformed.js:
import JSBI from "./jsbi.mjs";
  1. Create a file test.html.
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <script src="./node_modules/jsbi/dist/jsbi-umd.js"></script>
  <script src="test-transformed.js"></script>
  <script>
    document.addEventListener("DOMContentLoaded", function (event) {
      const form = document.querySelector("form");
      form.oninput = function () {
        const value = form.value.value;
        const digits = Number(form.digits.value);
        const s = squareRoot(value, digits);
        // IE 11 does not support <output> element, so `form.output.value = "value";` is not working.
        form.querySelector("output").textContent = '√' + value +  ' ≈ ' + s.slice(0, 0 - digits - 1) + '.' + s.slice(0 - digits - 1, -1) + '…';
      };
      form.oninput();
    }, false);
  </script>
  <style>
    output {
      overflow-wrap: break-word;
    }
  </style>
</head>
<body>
  <form>
    <div>
      <label for="value">Value:</label>
      <input id="value" name="value" type="number" min="2" step="1" value="2" />
    </div>
    <div>
      <label for="digits">Number of decimal digits:</label>
      <input id="digits" name="digits" type="number" min="1" step="1" value="100" />
    </div>
    <div>
      <output id="output" name="output" for="value digits" tabindex="0"></output>
    </div>
  </form>
</body>
</html>
  1. Open http://127.0.0.1:8081/test.html in a web browser.