@bitfinex/lib-js-util-math 0.7.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +9 -29
- package/src/bn.js +4 -0
- package/test/array.test.js +268 -269
- package/test/bn.test.js +169 -167
- package/test/ema.test.js +23 -27
- package/test/impact-mid-price.test.js +223 -214
- package/test/std-deviation.test.js +161 -166
- package/test/twap.test.js +209 -215
- package/test/vwap.test.js +115 -117
- package/test/index.test.js +0 -19
package/test/bn.test.js
CHANGED
|
@@ -1,172 +1,174 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { BigNumber } = require('bignumber.js')
|
|
4
|
-
const
|
|
5
|
-
.use(require('dirty-chai'))
|
|
6
|
-
const { expect } = chai
|
|
4
|
+
const test = require('brittle')
|
|
7
5
|
const { BN, nBN, validateBN } = require('../src/bn')
|
|
8
6
|
|
|
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
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
7
|
+
test('BN - should return BigNumber constructor', (t) => {
|
|
8
|
+
t.is(BN() === BigNumber, true)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
test('nBN - should fail on invalid string as number', (t) => {
|
|
12
|
+
t.exception.all(nBN.bind(null, '12a'))
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
test('nBN - should fail on invalid string as NaN', (t) => {
|
|
16
|
+
t.exception.all(nBN.bind(null, 'NaN'))
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
test('nBN - should fail on NaN type', (t) => {
|
|
20
|
+
t.exception.all(nBN.bind(null, NaN))
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
test('nBN - should fail on null type', (t) => {
|
|
24
|
+
t.exception.all(nBN.bind(null, null))
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('nBN - should fail on "null" value', (t) => {
|
|
28
|
+
t.exception.all(nBN.bind(null, 'null'))
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
test('nBN - should fail on bool value', (t) => {
|
|
32
|
+
t.exception.all(nBN.bind(null, true))
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
test('nBN - should fail on infinite value', (t) => {
|
|
36
|
+
t.exception(nBN.bind(null, Infinity), /ERR_NUM_INFINITE/)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test('nBN - should fail on -infinite value', (t) => {
|
|
40
|
+
t.exception(nBN.bind(null, -Infinity), /ERR_NUM_INFINITE/)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test('nBN - should fail on infinity string value', (t) => {
|
|
44
|
+
t.exception(nBN.bind(null, 'Infinity'), /ERR_NUM_INFINITE/)
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test('nBN - should work fail undefined input', (t) => {
|
|
48
|
+
t.exception.all(nBN.bind(null))
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('nBN - should return BigNumber instance on valid input', (t) => {
|
|
52
|
+
t.is(nBN(33) instanceof BigNumber, true)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('nBN - should return BigNumber instance on valid BigNumber input', (t) => {
|
|
56
|
+
t.is(nBN(new BigNumber(0)) instanceof BigNumber, true)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('nBN - should return BigNumber instance on 0 input', (t) => {
|
|
60
|
+
t.is(nBN(0) instanceof BigNumber, true)
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
test('nBN - should return BigNumber instance on string number input', (t) => {
|
|
64
|
+
t.is(nBN('3332') instanceof BigNumber, true)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test('nBN - should return BigNumber instance on exponential number input', (t) => {
|
|
68
|
+
t.is(nBN(10e-2) instanceof BigNumber, true)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
test('nBN - should return BigNumber instance on exponential string number input', (t) => {
|
|
72
|
+
t.is(nBN('32.33e+2') instanceof BigNumber, true)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
test('nBN - should return BigNumber instance on decimal input', (t) => {
|
|
76
|
+
t.is(nBN(0.333) instanceof BigNumber, true)
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
test('nBN - should return BigNumber instance on negative number input', (t) => {
|
|
80
|
+
t.is(nBN(-33) instanceof BigNumber, true)
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
test('nBN - should return BigNumber instance on negative string number input', (t) => {
|
|
84
|
+
t.is(nBN('-33.323') instanceof BigNumber, true)
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test('validateBN - should fail on invalid string as NaN', (t) => {
|
|
88
|
+
t.exception.all(
|
|
89
|
+
validateBN.bind(null, 'NaN')
|
|
90
|
+
)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
test('validateBN - should fail on NaN type', (t) => {
|
|
94
|
+
t.exception.all(
|
|
95
|
+
validateBN.bind(null, NaN)
|
|
96
|
+
)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
test('validateBN - should fail on null type', (t) => {
|
|
100
|
+
t.exception.all(
|
|
101
|
+
validateBN.bind(null, null)
|
|
102
|
+
)
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
test('validateBN - should fail on "null" value', (t) => {
|
|
106
|
+
t.exception.all(
|
|
107
|
+
validateBN.bind(null, 'null')
|
|
108
|
+
)
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
test('validateBN - should fail on bool value', (t) => {
|
|
112
|
+
t.exception.all(
|
|
113
|
+
validateBN.bind(null, true)
|
|
114
|
+
)
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
test('validateBN - should fail on infinite value', (t) => {
|
|
118
|
+
t.exception(
|
|
119
|
+
validateBN.bind(null, Infinity),
|
|
120
|
+
/ERR_NUM_INFINITE/
|
|
121
|
+
)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
test('validateBN - should fail on -infinite value', (t) => {
|
|
125
|
+
t.exception(
|
|
126
|
+
validateBN.bind(null, -Infinity),
|
|
127
|
+
/ERR_NUM_INFINITE/
|
|
128
|
+
)
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
test('validateBN - should fail on infinity string value', (t) => {
|
|
132
|
+
t.exception(
|
|
133
|
+
validateBN.bind(null, 'Infinity'),
|
|
134
|
+
/ERR_NUM_INFINITE/
|
|
135
|
+
)
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test('validateBN - should throw ERR_NUM_NAN on invalid number', (t) => {
|
|
139
|
+
t.exception(
|
|
140
|
+
validateBN.bind(null, 'aa'),
|
|
141
|
+
/ERR_NUM_NAN/
|
|
142
|
+
)
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
test('validateBN - should throw ERR_NUM_NOT_POS on negative number when restricted', (t) => {
|
|
146
|
+
t.exception(
|
|
147
|
+
validateBN.bind(null, '-33', { allowNegative: false, allowZero: true, allowDecimals: true }),
|
|
148
|
+
/ERR_NUM_NOT_POS/
|
|
149
|
+
)
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
test('validateBN - should throw ERR_NUM_ZERO on zero when restricted', (t) => {
|
|
153
|
+
t.exception(
|
|
154
|
+
validateBN.bind(null, 0, { allowNegative: true, allowZero: false, allowDecimals: true }),
|
|
155
|
+
/ERR_NUM_ZERO/
|
|
156
|
+
)
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
test('validateBN - should throw ERR_NUM_WITH_PRECISION on decimal number when restricted', (t) => {
|
|
160
|
+
t.exception(
|
|
161
|
+
validateBN.bind(null, '2e-7', { allowNegative: true, allowZero: true, allowDecimals: false }),
|
|
162
|
+
/ERR_NUM_WITH_PRECISION/
|
|
163
|
+
)
|
|
164
|
+
t.exception(
|
|
165
|
+
validateBN.bind(null, '0.33', { allowNegative: true, allowZero: true, allowDecimals: false }),
|
|
166
|
+
/ERR_NUM_WITH_PRECISION/
|
|
167
|
+
)
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
test('validateBN - should work on any valid number with no restriction (default)', (t) => {
|
|
171
|
+
t.execution(
|
|
172
|
+
validateBN.bind(null, '-0.33')
|
|
173
|
+
)
|
|
174
|
+
})
|
package/test/ema.test.js
CHANGED
|
@@ -1,34 +1,30 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
.use(require('dirty-chai'))
|
|
5
|
-
const { expect } = chai
|
|
3
|
+
const test = require('brittle')
|
|
6
4
|
const { EMA, multiplier } = require('../src/ema')
|
|
7
5
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
).to.throw()
|
|
14
|
-
})
|
|
6
|
+
test('multiplier - it should fail with non numeric values', (t) => {
|
|
7
|
+
t.exception.all(
|
|
8
|
+
multiplier.bind(null, 'a23')
|
|
9
|
+
)
|
|
10
|
+
})
|
|
15
11
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
12
|
+
test('multiplier - it should calc the expected value', (t) => {
|
|
13
|
+
t.is(
|
|
14
|
+
multiplier('10'),
|
|
15
|
+
'0.18181818181818181818'
|
|
16
|
+
)
|
|
17
|
+
})
|
|
21
18
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
19
|
+
test('ema - it should fail with nan value', (t) => {
|
|
20
|
+
t.exception.all(
|
|
21
|
+
EMA.bind(null, 'a7301.41', '7300.9807', 10)
|
|
22
|
+
)
|
|
23
|
+
})
|
|
27
24
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
25
|
+
test('ema - it should return the expected value', (t) => {
|
|
26
|
+
t.is(
|
|
27
|
+
EMA('7301.41', 7300.9807, '0.18181818181818181818'),
|
|
28
|
+
'7301.058754545454545454544674'
|
|
29
|
+
)
|
|
30
|
+
})
|