bignumber.js
bignumber.js copied to clipboard
eslint-plugin-big-number-rules
I made an eslint plugin some time ago that's reached v2 and I thought it was now useful enough to share more broadly.
Essentially it offers replacements like:
from | to | plugin will also suggest |
---|---|---|
0.1 + 0.2 |
BigNumber.sum(0.1, 0.2) |
('').concat(0.1, 0.2) and `${0.1}${0.2}` when you want string-concatenation instead of FP arithmetic |
result === 0.3 |
BigNumber(result).isEqualTo(0.3) |
Object.is(result, 0.3) when you want to strictly compare things that are not FP calculations |
19.99 * 0.1 |
BigNumber(19.99).multipliedBy(0.1) |
|
1 < 2 |
BigNumber(1).isLessThan(2) |
|
2 >>> 4 |
BigNumber(2).shiftedBy(4) |
|
4 << 2 |
BigNumber(4).shiftedBy(-2) |
|
Math.min(1, 2) |
BigNumber.minimum(1, 2) |
|
Math.sign(-6) |
BigNumber(-6).comparedTo(0) |
|
(1).toFixed(2) |
BigNumber(1).toFixed(2) |
|
parseFloat('1.2') |
BigNumber('1.2') |
|
Number.parseFloat('2.1') |
BigNumber('2.1') |
There's configuration to enforce the rules only in files that import 'bignumber.js'
, along with other ways and means to mitigate the number of warnings if you introduce the plugin into a large code-base.
It's proved useful to me and I hope for others too.