chai-decimaljs
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

chai-decimaljs

NPM Package Build Status

Chai assertions for comparing arbitrary-precision decimals using the decimal.js library. Forked from chai-bn (originally forked from chai-bignumber), which uses the bn.js library (originally the bignumber.js library).

Installation

npm install --save-dev chai-decimaljs

Usage

const chai = require('chai');
const Decimal = require('decimal.js');

// Enable and inject Decimal dependency
chai.use(require('chai-decimaljs')(Decimal));

Assertions

The following assertion methods are provided and will override the existing builtin assertions if the bignumber property is set as part of the assertion chain:

  • equal/equals/eq
  • above/gt/greaterThan
  • least/gte
  • below/lt/lessThan
  • most/lte
  • closeTo

A set of additional assertion properties is also provided:

  • negative
  • zero

Both actual values (the values being asserted) and expected values (the values the actual value is expected to match) can be either instances of Decimal, or strings which can be converted into a valid number. This is a key difference with chai-bignumber, which automatically converts JavaScript numbers to BigNumber instances for both actual and expected values.

Only BDD style (expect or should) assertions are supported.

Examples

Methods:

const actual = new Decimal('100').plus(new Decimal('0.0000000000000000000000000000001'));
const expected = '100.0000000000000000000000000000001';

actual.should.be.a.decimal.that.equals(expected);
expect(actual).to.be.a.decimal.that.is.at.most(expected);
(new Decimal('1999.999999999999999999')).should.be.a.decimal.that.is.lessThan('2000');

Properties:

(new Decimal('-100.102')).should.be.a.decimal.that.is.negative;
expect(new Decimal('1.33').sub(new Decimal('1.33'))).to.be.a.decimal.that.is.zero;

Some Chai properties (e.g. the that.is chain) have no effect other than increasing readability, and can be dropped if less verbosity is desired.

License

MIT

Package Sidebar

Install

npm i chai-decimaljs

Weekly Downloads

12

Version

0.0.1

License

MIT

Unpacked Size

12.2 kB

Total Files

5

Last publish

Collaborators

  • siltros971
  • valqrtis
  • mihalex
  • andreihrs
  • peroxy
  • elliotkennedy
  • hubbleadmin