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({
sourceCode: 'const nr = 1 + 2 - 3;',
prependNew: true,
appendToNumber: true,
variables: ['total'],
onConverted: (file) => {
console.log(file.getFullText());
},
});
convert({
source: ['./src/**/*{.js,.ts}', './bin/main.ts'],
onConverted: (file) => file.saveSync()
});
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