native-to-big
TypeScript icon, indicating that this package has built-in type declarations

0.2.4 • Public • Published

n2b

Did you know that 0.1 + 0.2 == 0.3 equals false in JavaScript (see this useful article)?

Did you also know that packages like Big.js exist to deal with exact number issues like this?

Do you feel like an idiot for not noticing this issue sooner and now realize that your whole codebase is fucked (totally didn't happen to me btw)?

Then maybe this simple CLI tool (and library) may come to the rescue to convert your existing code to Bigs!

This utility uses the TypeScript compiler API under the hood, so it should be fast enough for tooling.

Installation

Install the CLI application globally:

$ npm install --global native-to-big

Install the package locally, for programmatic purposes:

$ npm install --save-dev native-to-big

Usage

Library

import { convert } from 'native-to-big';

// convert raw code & log the result to console
convert({
  sourceCode: 'const nr = 1 + 2 - 3;', 
  prependNew: true,      // write 'new Big()' instead of 'Big()'
  appendToNumber: true,  // append '.toNumber()' at the end of each Big
  variables: ['total'],  // transforms 'let total = 0' to 'let total = Big(0)' whenever a variabe named 'total' is found
  onConverted: (file) => {
    console.log(file.getFullText());
    // output: const nr = new Big(1).plus(2).minus(3).toNumber();
  },
});

// convert multiple files & write them to disk
convert({
  source: ['./src/**/*{.js,.ts}', './bin/main.ts'], 
  onConverted: (file) => file.saveSync()
});

// convert multiple files from a TypeScript project & asynchronously write them to disk
convert({
  sourceTsConfig: '/path/to/project/tsconfig.json',
  onConverted: async (file) => file.save()
})
  .then(() => console.log('All files written successfully!'))
  .catch(console.error);

CLI

Usage:

$ n2b --help

Examples:

$ n2b -sc 0.1 + 0.2
$ n2b -s ./src/**/*.ts --dryRun
$ n2b --prependNew --appendToNumber --variables total,sum --sourceTsConfig ./tsconfig.json

Missing features

These features are currently missing. Feel free to contribute!

  • Doesn't support conversion of Math.pow. Math.sqrt and Math.abs are supported though. A warning will be logged to console whenever it's found and it will be ignored.

References

Package Sidebar

Install

npm i native-to-big

Weekly Downloads

1

Version

0.2.4

License

MIT

Unpacked Size

20.2 kB

Total Files

12

Last publish

Collaborators

  • sleeyax