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

calc-js

v3.0.0

Published

Handle JavaScript operations, avoiding the native problems of the language

Downloads

905

Readme

Calc JS

Handle JavaScript operations, avoiding the native problems of the language.

javescripeto le calco

npm version Npm Total Downloads Npm Monthly Downloads Open Source Love

The problem

The problem of representing decimal numbers encoded in IEEE 754 binary format is a problem that JavaScript suffers. The bug consists of the lack of ability for the format to represent some numerical values in the binary format, you can read an article talking about it here: Double precision floating-point.

This is a well-known problem, in Java they have developed a class called BigDecimal to solve this.

Error example

Error example

The problem is generated when you perform a simple calculation using some decimal values, example:

0.2 + 0.1 = 0.30000000000000004

But the same problem does not occur when the same result is reached using powers of 10, example:

3 / 10 = 0.3

Instalation

npm install calc-js --save
//  node
var Calc = require('calc-js').Calc;
<!-- browser -->
<script src="calc-js/calc.latest.min.js"></script>
//  typescript
import { Calc } from 'calc-js';

How to use

The use of the library is very simple:

import { Calc } from 'calc-js';

// 0.2 + 0.1
const numericResult = new Calc(0.2).sum(0.1).finish();

// 0.2 - 0.1
const numericResult = new Calc(0.2).minus(0.1).finish();

// 2199 / 100 * 5
const numericResult = new Calc(2199).divide(100).multiply(5).finish();

This is an example with TypeScript, but you can also use this in JavaScript application as you wish.

Error Handling

There are some EcmaScript errors that are not thrown by the language and it cannot be treated, these erros are: zero division and infinity numbers 0 / 10 = Infinity, non numberic math operations 'Some text' * 10 = NaN and insecure numbers (too large number that language cannot represent, but still allow you to use then).

The library will generate an error object when these situations are found, this is the CalcError and it extends native JavaScript Error. Use this will give you a stack trace to found the problem cause.

The CalcError

You can choose four ways to deal with errors:

1. Ignore then

Ignore all identified errors

Calc.configure({
  throwNaN: false,
  throwInfinite: false,
  throwUnsafeNumber: false
});

2. Listen an event

Register a function on library to listen all thrown errors. This will cover all Calc in application so, is a centralized treatment

Calc.configure({
  // you don't need really configure this, 'emit-event' is the default value
  thrownStrategy: 'emit-event'
});

Calc.onError(function(error) {
  //  do something
});

Note: if you choose emit-event and don't associate any function to listen the errors, the application will thrown then

3. Throw

This is a non centralized solution. You'll use this when the calc is critical and should not generate an invalid result.

Calc.configure({
  thrownStrategy: 'thrown'
});

try {
  const result = new Calc(10).sum(11).finish();
} catch (e) {
  //  do something
}

4. Show in console

This will write the error in the browser console

Calc.configure({
  thrownStrategy: 'console'
});

Config

All library settings are related to how errors will be thrown. The following exemple show how to change these configs and show the default values set in the library:

Calc.configure({
  thrownStrategy: 'emit-event',
  throwNaN: true,
  throwInfinite: true,
  throwUnsafeNumber: true
});

This configure function will override the default values to each Calc execution in your application, but these default values can be overriden in each execution (configure localy will never required, but optional):

const localConfig = { thrownStrategy: 'thrown' };
new Calc(10, localConfig).divide(0).finish();
const localConfig = { thrownStrategy: 'thrown' };
//  the old way to calc in the library
Calc.divide(10, 0, localConfig);
const localConfig = { thrownStrategy: 'thrown' };
//  it will throw if this isn't a valid number
Calc.checkNumber(Infinity, 'Validating data from query param', localConfig);

Contributing

1. Create an issue

No one feature will be implemented without it having an open issue and without which the proposed has been accepted by the team responsible for the project. After the issue is approved, the applicant, a team member or anyone else can open a pull request associated with that issue (just paste the issue link in the pull request).

2. Did you find a bug?

When logging a bug, please be sure to include the following:

  • The library version;
  • If at all possible, an isolated way to reproduce the behavior;
  • The behavior you expect to see, and the actual behavior.

You can try to update the library to the last version to see if the bug has already been fixed.

3. Do not create a duplicate issue

Search the existing issues before logging a new one.

Some search tips:

  • Don't restrict your search to only open issues. An issue with a title similar to yours may have been closed as a duplicate of one with a less-findable title.
  • Check for synonyms. For example, if your bug involves an interface, it likely also occurs with type aliases or classes.

4. Create a Pull Request

Follow the steps:

  • Create a fork from our repository by clicking here, install node, do a git clone of your forked repository and run npm install in the application folder;
  • Create a branch in your forked repository, then code the feature or fix the bug;
  • Run npm run lint, npm run test and npm run build in the repository;
  • Create a Pull Request from your repository to this one, with the issue in the body and some information you think could be usefull to the reviewer (print or a gif of it working will be appreciated);
  • The reviewer can ask some changes, don't be mad, this is the process;
  • You get approved and your branch with the feature / fix