@bitfinex/lib-js-util-math 0.7.1 → 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/test/bn.test.js CHANGED
@@ -1,172 +1,174 @@
1
1
  'use strict'
2
2
 
3
3
  const { BigNumber } = require('bignumber.js')
4
- const chai = require('chai')
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
- module.exports = () => {
10
- describe('# bn-tests', () => {
11
- it('BN - should return BigNumber constructor', () => {
12
- return expect(BN() === BigNumber).to.be.true
13
- })
14
-
15
- it('nBN - should fail on invalid string as number', () => {
16
- return expect(nBN.bind(null, '12a')).to.throw()
17
- })
18
-
19
- it('nBN - should fail on invalid string as NaN', () => {
20
- return expect(nBN.bind(null, 'NaN')).to.throw()
21
- })
22
-
23
- it('nBN - should fail on NaN type', () => {
24
- return expect(nBN.bind(null, NaN)).to.throw()
25
- })
26
-
27
- it('nBN - should fail on null type', () => {
28
- return expect(nBN.bind(null, null)).to.throw()
29
- })
30
-
31
- it('nBN - should fail on "null" value', () => {
32
- return expect(nBN.bind(null, 'null')).to.throw()
33
- })
34
-
35
- it('nBN - should fail on bool value', () => {
36
- return expect(nBN.bind(null, true)).to.throw()
37
- })
38
-
39
- it('nBN - should fail on infinite value', () => {
40
- return expect(nBN.bind(null, Infinity)).to.throw('ERR_NUM_INFINITE')
41
- })
42
-
43
- it('nBN - should fail on -infinite value', () => {
44
- return expect(nBN.bind(null, -Infinity)).to.throw('ERR_NUM_INFINITE')
45
- })
46
-
47
- it('nBN - should fail on infinity string value', () => {
48
- return expect(nBN.bind(null, 'Infinity')).to.throw('ERR_NUM_INFINITE')
49
- })
50
-
51
- it('nBN - should work fail undefined input', () => {
52
- return expect(nBN.bind(null)).to.throw()
53
- })
54
-
55
- it('nBN - should return BigNumber instance on valid input', () => {
56
- return expect(nBN(33) instanceof BigNumber).to.be.true
57
- })
58
-
59
- it('nBN - should return BigNumber instance on valid BigNumber input', () => {
60
- return expect(nBN(new BigNumber(0)) instanceof BigNumber).to.be.true
61
- })
62
-
63
- it('nBN - should return BigNumber instance on 0 input', () => {
64
- return expect(nBN(0) instanceof BigNumber).to.be.true
65
- })
66
-
67
- it('nBN - should return BigNumber instance on string number input', () => {
68
- return expect(nBN('3332') instanceof BigNumber).to.be.true
69
- })
70
-
71
- it('nBN - should return BigNumber instance on exponential number input', () => {
72
- return expect(nBN(10e-2) instanceof BigNumber).to.be.true
73
- })
74
-
75
- it('nBN - should return BigNumber instance on exponential string number input', () => {
76
- return expect(nBN('32.33e+2') instanceof BigNumber).to.be.true
77
- })
78
-
79
- it('nBN - should return BigNumber instance on decimal input', () => {
80
- return expect(nBN(0.333) instanceof BigNumber).to.be.true
81
- })
82
-
83
- it('nBN - should return BigNumber instance on negative number input', () => {
84
- return expect(nBN(-33) instanceof BigNumber).to.be.true
85
- })
86
-
87
- it('nBN - should return BigNumber instance on negative string number input', () => {
88
- return expect(nBN('-33.323') instanceof BigNumber).to.be.true
89
- })
90
-
91
- it('validateBN - should fail on invalid string as NaN', () => {
92
- return expect(
93
- validateBN.bind(null, 'NaN')
94
- ).to.throw()
95
- })
96
-
97
- it('validateBN - should fail on NaN type', () => {
98
- return expect(
99
- validateBN.bind(null, NaN)
100
- ).to.throw()
101
- })
102
-
103
- it('validateBN - should fail on null type', () => {
104
- return expect(
105
- validateBN.bind(null, null)
106
- ).to.throw()
107
- })
108
-
109
- it('validateBN - should fail on "null" value', () => {
110
- return expect(
111
- validateBN.bind(null, 'null')
112
- ).to.throw()
113
- })
114
-
115
- it('validateBN - should fail on bool value', () => {
116
- return expect(
117
- validateBN.bind(null, true)
118
- ).to.throw()
119
- })
120
-
121
- it('validateBN - should fail on infinite value', () => {
122
- return expect(
123
- validateBN.bind(null, Infinity)
124
- ).to.throw('ERR_NUM_INFINITE')
125
- })
126
-
127
- it('validateBN - should fail on -infinite value', () => {
128
- return expect(
129
- validateBN.bind(null, -Infinity)
130
- ).to.throw('ERR_NUM_INFINITE')
131
- })
132
-
133
- it('validateBN - should fail on infinity string value', () => {
134
- return expect(
135
- validateBN.bind(null, 'Infinity')
136
- ).to.throw('ERR_NUM_INFINITE')
137
- })
138
-
139
- it('validateBN - should throw ERR_NUM_NAN on invalid number', () => {
140
- return expect(
141
- validateBN.bind(null, 'aa')
142
- ).to.throw('ERR_NUM_NAN')
143
- })
144
-
145
- it('validateBN - should throw ERR_NUM_NOT_POS on negative number when restricted', () => {
146
- return expect(
147
- validateBN.bind(null, '-33', { allowNegative: false, allowZero: true, allowDecimals: true })
148
- ).to.throw('ERR_NUM_NOT_POS')
149
- })
150
-
151
- it('validateBN - should throw ERR_NUM_ZERO on zero when restricted', () => {
152
- return expect(
153
- validateBN.bind(null, 0, { allowNegative: true, allowZero: false, allowDecimals: true })
154
- ).to.throw('ERR_NUM_ZERO')
155
- })
156
-
157
- it('validateBN - should throw ERR_NUM_WITH_PRECISION on decimal number when restricted', () => {
158
- expect(
159
- validateBN.bind(null, '2e-7', { allowNegative: true, allowZero: true, allowDecimals: false })
160
- ).to.throw('ERR_NUM_WITH_PRECISION')
161
- return expect(
162
- validateBN.bind(null, '0.33', { allowNegative: true, allowZero: true, allowDecimals: false })
163
- ).to.throw('ERR_NUM_WITH_PRECISION')
164
- })
165
-
166
- it('validateBN - should work on any valid number with no restriction (default)', () => {
167
- return expect(
168
- validateBN.bind(null, '-0.33')
169
- ).not.to.throw()
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 chai = require('chai')
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
- module.exports = () => {
9
- describe('# ema-tests', () => {
10
- it('multiplier - it should fail with non numeric values', () => {
11
- return expect(
12
- multiplier.bind(null, 'a23')
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
- it('multiplier - it should calc the expected value', () => {
17
- return expect(
18
- multiplier('10')
19
- ).to.be.equal('0.18181818181818181818')
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
- it('ema - it should fail with nan value', () => {
23
- return expect(
24
- EMA.bind(null, 'a7301.41', '7300.9807', 10)
25
- ).to.throw()
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
- it('ema - it should return the expected value', () => {
29
- return expect(
30
- EMA('7301.41', 7300.9807, '0.18181818181818181818')
31
- ).to.be.equal('7301.058754545454545454544674')
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
+ })