babel-plugin-transform-bigdecimal
NOTICE: This will not work in many cases, so please use bignumber.js or decimal.js directly only if you know, that the code works only with BigDecimals. Since proposal decimal is still in stage-1, it might changed a lot and this transformer many take this breaking changes in the feature.
A plugin for babel to transform x * y
into something like JSBD.multiply(x, y)
to support bigdecimals.
Example
Input using native BigDecimal
s:
const a = BigDecimal(0.1);
const b = 0.2m;
a + b;
a - b;
a * b;
a / b;
a % b;
a ** b;
a === b;
a < b;
a <= b;
a > b;
a >= b;
a.toString();
Compiled output using JSBD
:
const a = JSBD.BigDecimal(0.1);
const b = JSBD.BigDecimal(0.2);
JSBD.add(a, b);
JSBD.subtract(a, b);
JSBD.multiply(a, b);
JSBD.divide(a, b);
JSBD.remainder(a, b);
JSBD.pow(a, b);
JSBD.equal(a, b);
JSBD.lessThan(a, b);
JSBD.lessThanOrEqual(a, b);
JSBD.greaterThan(a, b);
JSBD.greaterThanOrEqual(a, b);
a.toString();
Playground
- Open https://babeljs.io/en/repl
- Turn off all presets.
- "Add plugin" @babel/babel-plugin-syntax-decimal
- "Add plugin" babel-plugin-transform-bigdecimal
¡ It is buggy !
TODO(s)
- [x] support
BigDecimal.round(1m)
; - [ ] support
Number(0.1m)
; - [ ] support shift operators
<<
and>>
; - [ ] support bitwise operators like
&
/|
/^
;
License
Published under ISC License.