forked from bitpay/bitcore-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit.js
197 lines (155 loc) · 5.24 KB
/
unit.js
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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict';
var should = require('chai').should();
var expect = require('chai').expect;
var bitcore = require('..');
var errors = bitcore.errors;
var Unit = bitcore.Unit;
describe('Unit', function() {
it('can be created from a number and unit', function() {
expect(function() {
return new Unit(1.2, 'BTC');
}).to.not.throw();
});
it('can be created from a number and exchange rate', function() {
expect(function() {
return new Unit(1.2, 350);
}).to.not.throw();
});
it('no "new" is required for creating an instance', function() {
expect(function() {
return Unit(1.2, 'BTC');
}).to.not.throw();
expect(function() {
return Unit(1.2, 350);
}).to.not.throw();
});
it('has property accesors "BTC", "mBTC", "uBTC", "bits", and "satoshis"', function() {
var unit = new Unit(1.2, 'BTC');
unit.BTC.should.equal(1.2);
unit.mBTC.should.equal(1200);
unit.uBTC.should.equal(1200000);
unit.bits.should.equal(1200000);
unit.satoshis.should.equal(120000000);
});
it('a string amount is allowed', function() {
var unit;
unit = Unit.fromBTC('1.00001');
unit.BTC.should.equal(1.00001);
unit = Unit.fromMilis('1.00001');
unit.mBTC.should.equal(1.00001);
unit = Unit.fromMillis('1.00001');
unit.mBTC.should.equal(1.00001);
unit = Unit.fromBits('100');
unit.bits.should.equal(100);
unit = Unit.fromSatoshis('8999');
unit.satoshis.should.equal(8999);
unit = Unit.fromFiat('43', 350);
unit.BTC.should.equal(0.12285714);
});
it('should have constructor helpers', function() {
var unit;
unit = Unit.fromBTC(1.00001);
unit.BTC.should.equal(1.00001);
unit = Unit.fromMilis(1.00001);
unit.mBTC.should.equal(1.00001);
unit = Unit.fromBits(100);
unit.bits.should.equal(100);
unit = Unit.fromSatoshis(8999);
unit.satoshis.should.equal(8999);
unit = Unit.fromFiat(43, 350);
unit.BTC.should.equal(0.12285714);
});
it('converts to satoshis correctly', function() {
/* jshint maxstatements: 25 */
var unit;
unit = Unit.fromBTC(1.3);
unit.mBTC.should.equal(1300);
unit.bits.should.equal(1300000);
unit.satoshis.should.equal(130000000);
unit = Unit.fromMilis(1.3);
unit.BTC.should.equal(0.0013);
unit.bits.should.equal(1300);
unit.satoshis.should.equal(130000);
unit = Unit.fromBits(1.3);
unit.BTC.should.equal(0.0000013);
unit.mBTC.should.equal(0.0013);
unit.satoshis.should.equal(130);
unit = Unit.fromSatoshis(3);
unit.BTC.should.equal(0.00000003);
unit.mBTC.should.equal(0.00003);
unit.bits.should.equal(0.03);
});
it('takes into account floating point problems', function() {
var unit = Unit.fromBTC(0.00000003);
unit.mBTC.should.equal(0.00003);
unit.bits.should.equal(0.03);
unit.satoshis.should.equal(3);
});
it('exposes unit codes', function() {
should.exist(Unit.BTC);
Unit.BTC.should.equal('BTC');
should.exist(Unit.mBTC);
Unit.mBTC.should.equal('mBTC');
should.exist(Unit.bits);
Unit.bits.should.equal('bits');
should.exist(Unit.satoshis);
Unit.satoshis.should.equal('satoshis');
});
it('exposes a method that converts to different units', function() {
var unit = new Unit(1.3, 'BTC');
unit.to(Unit.BTC).should.equal(unit.BTC);
unit.to(Unit.mBTC).should.equal(unit.mBTC);
unit.to(Unit.bits).should.equal(unit.bits);
unit.to(Unit.satoshis).should.equal(unit.satoshis);
});
it('exposes shorthand conversion methods', function() {
var unit = new Unit(1.3, 'BTC');
unit.toBTC().should.equal(unit.BTC);
unit.toMilis().should.equal(unit.mBTC);
unit.toMillis().should.equal(unit.mBTC);
unit.toBits().should.equal(unit.bits);
unit.toSatoshis().should.equal(unit.satoshis);
});
it('can convert to fiat', function() {
var unit = new Unit(1.3, 350);
unit.atRate(350).should.equal(1.3);
unit.to(350).should.equal(1.3);
unit = Unit.fromBTC(0.0123);
unit.atRate(10).should.equal(0.12);
});
it('toString works as expected', function() {
var unit = new Unit(1.3, 'BTC');
should.exist(unit.toString);
unit.toString().should.be.a('string');
});
it('can be imported and exported from/to JSON', function() {
var json = JSON.stringify({amount:1.3, code:'BTC'});
var unit = Unit.fromObject(JSON.parse(json));
JSON.stringify(unit).should.deep.equal(json);
});
it('importing from invalid JSON fails quickly', function() {
expect(function() {
return Unit.fromJSON('¹');
}).to.throw();
});
it('inspect method displays nicely', function() {
var unit = new Unit(1.3, 'BTC');
unit.inspect().should.equal('<Unit: 130000000 satoshis>');
});
it('fails when the unit is not recognized', function() {
expect(function() {
return new Unit(100, 'USD');
}).to.throw(errors.Unit.UnknownCode);
expect(function() {
return new Unit(100, 'BTC').to('USD');
}).to.throw(errors.Unit.UnknownCode);
});
it('fails when the exchange rate is invalid', function() {
expect(function() {
return new Unit(100, -123);
}).to.throw(errors.Unit.InvalidRate);
expect(function() {
return new Unit(100, 'BTC').atRate(-123);
}).to.throw(errors.Unit.InvalidRate);
});
});