Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

native-to-big

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

native-to-big

Convert native math expressions to Big types from the big.js library

latest
Source
npmnpm
Version
0.2.4
Version published
Maintainers
1
Created
Source

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

Keywords

native to big.js

FAQs

Package last updated on 09 Mar 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts