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

native-to-big

v0.2.4

Published

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

Downloads

2

Readme

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