-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.hbs
71 lines (43 loc) · 2.06 KB
/
README.hbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
[![NPM](https://nodei.co/npm/ramda-decimal.png?compact=true)](https://nodei.co/npm/ramda-decimal/)
# ramda-decimal [![CircleCI](https://circleci.com/gh/WealthWizardsEngineering/ramda-decimal.svg?style=svg)](https://circleci.com/gh/WealthWizardsEngineering/ramda-decimal)
Ramda wrapper to Decimal.js
Use Decimal.js arbitrary precision maths in Ramda style. Immutable. Composable. Curry-able.
Drop-in replacements for most Ramda math functions.
const R = require('ramda);
const RD = require('ramda-decimal);
const annualTax = R.pipe(
R.pluck(income),
RD.sum,
RD.times(TAX_RATE),
);
const format = RD.fixed(2);
console.log(format(annualTax(months)));
# Liberal inputs, conservative outputs
Like Decimal.js, RD accepts Decimals, number primitives and strings that look like numbers. RD does this by calling `new Decimal()` on any input.
All RD functions return `Decimal`s.
# Constants
`ZERO` and `ONE` are defined as constants. Use them! They're immutable.
# Parameter order
When there is a corresponding Ramda function, RD uses the same parameter order. This does mean that curried relation functions
are unintuitive (in my opinion) in the same way as Ramda:
const f = RD.gt(5);
return f(6); // false: gt(5,6)
Where there is no corresponding Ramda function, we have flipped the parameters when it seems useful for point-free coding:
const formatToPennies = RD.toFixed(2);
# Currying and efficiency
For brevity the documentation suggests that (for example)
`RD.add(a)(b)` is equivalent to `new Decimal(a).plus(b)`. The reality is
a bit better than that. `RD.add(a)` stores any `Decimal` it creates
for reuse. Hence in:
const addTen = RD.add(10);
const n110 = addTen(100);
const n210 = addTen(200);
... `new Decimal(10)` is only run once.
# Mixed style
Returned values are just `Decimal` objects, so you are free to combine styles
if you want to:
const displayTotal = RD.sum(vals).fixed(2, Decimal.ROUND_HALF_UP);
# Docs
{{>main}}
------
Created by Wealth Wizards Software Engineering - http://wealthwizards.com