1 | const numberToBN = require('../index.js');
|
2 | const BigNumber = require('bignumber.js');
|
3 | const BN = require('bn.js');
|
4 | const assert = require('chai').assert;
|
5 |
|
6 | describe("numberToBN", () => {
|
7 | describe("constructor", () => {
|
8 | it("should be function export", () => {
|
9 | assert.equal(typeof numberToBN, 'function');
|
10 | });
|
11 | });
|
12 |
|
13 | const testCases = [
|
14 | { value: 0.1, expected: '0', shouldThrow: true },
|
15 | { value: '0.1', expected: '0', shouldThrow: true },
|
16 | { value: '-0.1', expected: '0', shouldThrow: true },
|
17 | { value: '.', expected: '0', shouldThrow: true },
|
18 | { value: [], expected: '0', shouldThrow: true },
|
19 | { value: undefined, expected: '0', shouldThrow: true },
|
20 | { value: null, expected: '0', shouldThrow: true },
|
21 | { value: {}, expected: '0', shouldThrow: true },
|
22 | { value: true, expected: '0', shouldThrow: true },
|
23 | { value: false, expected: '0', shouldThrow: true },
|
24 | { value: 'Z', expected: '0', shouldThrow: true },
|
25 | { value: 'z', expected: '0', shouldThrow: true },
|
26 | { value: 'zZZ', expected: '0', shouldThrow: true },
|
27 | { value: 'a.a', expected: '0', shouldThrow: true },
|
28 | { value: 'aA.af', expected: '0', shouldThrow: true },
|
29 | { value: '0xaA.af', expected: '0', shouldThrow: true },
|
30 | { value: '-0xaA.af', expected: '0', shouldThrow: true },
|
31 | { value: '-0xa.0a', expected: '0', shouldThrow: true },
|
32 | { value: '-0X0A.0a', expected: '0', shouldThrow: true },
|
33 | { value: '0x10', expected: '16', shouldThrow: false },
|
34 | { value: '-0x10', expected: '-16', shouldThrow: false },
|
35 | { value: '0X10', expected: '16', shouldThrow: false },
|
36 | { value: '0x', expected: '0', shouldThrow: false },
|
37 | { value: '0x10', expected: '16', shouldThrow: false },
|
38 | { value: '-0X10', expected: '-16', shouldThrow: false },
|
39 | { value: ' 0X10', expected: '16', shouldThrow: false },
|
40 | { value: ' -0X10', expected: '-16', shouldThrow: false },
|
41 | { value: "20938490284092380dfsjkZ", expected: '0', shouldThrow: true },
|
42 | { value: ":", expected: '0', shouldThrow: true },
|
43 | { value: "%", expected: '0', shouldThrow: true },
|
44 | { value: new BigNumber('100.001'), expected: '0', shouldThrow: true },
|
45 | { value: new BigNumber('-100.001'), expected: '0', shouldThrow: true },
|
46 | { value: new BigNumber('1.20'), expected: '0', shouldThrow: true },
|
47 | { value: 'Someone', expected: '0', shouldThrow: true },
|
48 | { value: '100.002fsdfdss', expected: '0', shouldThrow: true },
|
49 | { value: new Array(), expected: '-1', shouldThrow: true },
|
50 | { value: '0x9184e72a000', expected: new BN('9184e72a000', 16).toString(10) },
|
51 | { value: '0x15df', expected: new BN('15df', 16).toString(10) },
|
52 | { value: '0x57840CC2C', expected: new BN('57840CC2C', 16).toString(10) },
|
53 | { value: '9184E72a000', expected: new BN('9184e72a000', 16).toString(10) },
|
54 | { value: '15Df', expected: new BN('15df', 16).toString(10) },
|
55 | { value: '57840cC2C', expected: new BN('57840CC2C', 16).toString(10) },
|
56 | { value: '9184e72a000', expected: new BN('9184e72a000', 16).toString(10) },
|
57 | { value: '15df', expected: new BN('15df', 16).toString(10) },
|
58 | { value: '57840CC2C', expected: new BN('57840CC2C', 16).toString(10) },
|
59 | { value: ' -9184e72a000 ', expected: new BN('-9184e72a000', 16).toString(10) },
|
60 | { value: ' -15df ', expected: new BN('-15df', 16).toString(10) },
|
61 | { value: ' -57840CC2C ', expected: new BN('-57840CC2C', 16).toString(10) },
|
62 | { value: '-9184e72a000', expected: new BN('-9184e72a000', 16).toString(10) },
|
63 | { value: '-15df', expected: new BN('-15df', 16).toString(10) },
|
64 | { value: '-57840CC2C', expected: new BN('-57840CC2C', 16).toString(10) },
|
65 | { value: ' 0x9184e72a000', expected: new BN('9184e72a000', 16).toString(10) },
|
66 | { value: ' 0x15df', expected: new BN('15df', 16).toString(10) },
|
67 | { value: ' 0x57840CC2C', expected: new BN('57840CC2C', 16).toString(10) },
|
68 | { value: '-0x9184e72a000', expected: new BN('-9184e72a000', 16).toString(10) },
|
69 | { value: '-0x15df', expected: new BN('-15df', 16).toString(10) },
|
70 | { value: '-0x57840CC2C', expected: new BN('-57840CC2C', 16).toString(10) },
|
71 | { value: '', expected: '0', shouldThrow: false },
|
72 | { value: '-', expected: '0', shouldThrow: false },
|
73 | { value: 0, expected: '0', shouldThrow: false },
|
74 | { value: 1, expected: '1', shouldThrow: false },
|
75 | { value: -0, expected: '0', shouldThrow: false },
|
76 | { value: ' 0', expected: '0', shouldThrow: false },
|
77 | { value: ' 1', expected: '1', shouldThrow: false },
|
78 | { value: ' -0', expected: '0', shouldThrow: false },
|
79 | { value: ' -1 ', expected: '-1', shouldThrow: false },
|
80 | { value: ' 0 ', expected: '0', shouldThrow: false },
|
81 | { value: ' 1 ', expected: '1', shouldThrow: false },
|
82 | { value: ' -0 ', expected: '0', shouldThrow: false },
|
83 | { value: ' -1 ', expected: '-1', shouldThrow: false },
|
84 | { value: '0 ', expected: '0', shouldThrow: false },
|
85 | { value: '1 ', expected: '1', shouldThrow: false },
|
86 | { value: '-0 ', expected: '0', shouldThrow: false },
|
87 | { value: '-1 ', expected: '-1', shouldThrow: false },
|
88 | { value: '0', expected: '0', shouldThrow: false },
|
89 | { value: '1', expected: '1', shouldThrow: false },
|
90 | { value: '-0', expected: '0', shouldThrow: false },
|
91 | { value: '-1', expected: '-1', shouldThrow: false },
|
92 | { value: new BN(0), expected: '0', shouldThrow: false },
|
93 | { value: new BN(1), expected: '1', shouldThrow: false },
|
94 | { value: new BN(-1), expected: '-1', shouldThrow: false },
|
95 | { value: new BN(-0), expected: '0', shouldThrow: false },
|
96 | { value: new BigNumber(0), expected: '0', shouldThrow: false },
|
97 | { value: new BigNumber(1), expected: '1', shouldThrow: false },
|
98 | { value: new BigNumber('-1'), expected: '-1', shouldThrow: false },
|
99 | { value: new BigNumber('-0'), expected: '0', shouldThrow: false },
|
100 | { value: -92348723897, expected: '-92348723897', shouldThrow: false },
|
101 | { value: 24387298347, expected: '24387298347', shouldThrow: false },
|
102 | { value: 'a', expected: new BN('a', 16).toString(10), shouldThrow: false },
|
103 | { value: '0a', expected: new BN('a', 16).toString(10), shouldThrow: false },
|
104 | { value: '0xa', expected: new BN('a', 16).toString(10), shouldThrow: false },
|
105 | { value: '0x0a', expected: new BN('a', 16).toString(10), shouldThrow: false },
|
106 | { value: 'A', expected: new BN('a', 16).toString(10), shouldThrow: false },
|
107 | { value: '0A', expected: new BN('a', 16).toString(10), shouldThrow: false },
|
108 | { value: '0xA', expected: new BN('a', 16).toString(10), shouldThrow: false },
|
109 | { value: '0x0A', expected: new BN('a', 16).toString(10), shouldThrow: false },
|
110 | { value: '0X0A', expected: new BN('a', 16).toString(10), shouldThrow: false },
|
111 | { value: ' af', expected: new BN('af', 16).toString(10), shouldThrow: false },
|
112 | { value: ' a', expected: new BN('a', 16).toString(10), shouldThrow: false },
|
113 | { value: ' 0a', expected: new BN('a', 16).toString(10), shouldThrow: false },
|
114 | { value: ' 0xa', expected: new BN('a', 16).toString(10), shouldThrow: false },
|
115 | { value: ' 0x0a', expected: new BN('a', 16).toString(10), shouldThrow: false },
|
116 | { value: ' A', expected: new BN('a', 16).toString(10), shouldThrow: false },
|
117 | { value: ' 0A', expected: new BN('a', 16).toString(10), shouldThrow: false },
|
118 | { value: ' 0xA', expected: new BN('a', 16).toString(10), shouldThrow: false },
|
119 | { value: ' 0x0A', expected: new BN('a', 16).toString(10), shouldThrow: false },
|
120 | { value: ' 0X0A', expected: new BN('a', 16).toString(10), shouldThrow: false },
|
121 | { value: ' 0aF', expected: new BN('af', 16).toString(10), shouldThrow: false },
|
122 | { value: ' 0xaf', expected: new BN('af', 16).toString(10), shouldThrow: false },
|
123 | { value: ' 0x0AF', expected: new BN('af', 16).toString(10), shouldThrow: false },
|
124 | { value: ' Af', expected: new BN('af', 16).toString(10), shouldThrow: false },
|
125 | { value: ' 0Af', expected: new BN('af', 16).toString(10), shouldThrow: false },
|
126 | { value: ' 0xAf', expected: new BN('af', 16).toString(10), shouldThrow: false },
|
127 | { value: ' 0x0AF', expected: new BN('af', 16).toString(10), shouldThrow: false },
|
128 | { value: ' 0X0Af', expected: new BN('af', 16).toString(10), shouldThrow: false },
|
129 | { value: 'af', expected: new BN('af', 16).toString(10), shouldThrow: false },
|
130 | { value: '0af', expected: new BN('af', 16).toString(10), shouldThrow: false },
|
131 | { value: '0xaf', expected: new BN('af', 16).toString(10), shouldThrow: false },
|
132 | { value: '0x0af', expected: new BN('af', 16).toString(10), shouldThrow: false },
|
133 | { value: 'Af', expected: new BN('af', 16).toString(10), shouldThrow: false },
|
134 | { value: '0Af', expected: new BN('af', 16).toString(10), shouldThrow: false },
|
135 | { value: '0xAf', expected: new BN('af', 16).toString(10), shouldThrow: false },
|
136 | { value: '0x0Af', expected: new BN('af', 16).toString(10), shouldThrow: false },
|
137 | { value: '0X0Af', expected: new BN('af', 16).toString(10), shouldThrow: false },
|
138 | { value: new BigNumber('423879248942387943287923489724387987923'), expected: '423879248942387943287923489724387987923', shouldThrow: false },
|
139 | { value: new BN('423879248942387943287923489724387987923'), expected: '423879248942387943287923489724387987923', shouldThrow: false },
|
140 | { value: new BigNumber('24897234987'), expected: '24897234987', shouldThrow: false },
|
141 | { value: new BN('24897234987'), expected: '24897234987', shouldThrow: false },
|
142 | { value: new BigNumber('-423879248942387943287923489724387987923'), expected: '-423879248942387943287923489724387987923', shouldThrow: false },
|
143 | { value: new BN('-423879248942387943287923489724387987923'), expected: '-423879248942387943287923489724387987923', shouldThrow: false },
|
144 | { value: new BigNumber('-24897234987'), expected: '-24897234987', shouldThrow: false },
|
145 | { value: new BN('-24897234987'), expected: '-24897234987', shouldThrow: false },
|
146 |
|
147 |
|
148 | { value: 1, expected: '1' },
|
149 | { value: '1', expected: '1' },
|
150 | { value: '0x1', expected: '1'},
|
151 | { value: '0x01', expected: '1'},
|
152 | { value: 15, expected: '15'},
|
153 | { value: '15', expected: '15'},
|
154 | { value: '0xf', expected: '15'},
|
155 | { value: '0x0f', expected: '15'},
|
156 | { value: new BN('f', 16), expected: '15'},
|
157 | { value: new BigNumber('f', 16), expected: '15'},
|
158 | { value: -1, expected: '-1'},
|
159 | { value: '-1', expected: '-1'},
|
160 | { value: '-0x1', expected: '-1'},
|
161 | { value: '-0x01', expected: '-1'},
|
162 | { value: -15, expected: '-15'},
|
163 | { value: '-15', expected: '-15'},
|
164 | { value: '-0xf', expected: '-15'},
|
165 | { value: '-0x0f', expected: '-15'},
|
166 | { value: '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: '115792089237316195423570985008687907853269984665640564039457584007913129639935'},
|
167 | { value: '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', expected: '115792089237316195423570985008687907853269984665640564039457584007913129639933'},
|
168 | { value: '-0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: '-115792089237316195423570985008687907853269984665640564039457584007913129639935'},
|
169 | { value: '-0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', expected: '-115792089237316195423570985008687907853269984665640564039457584007913129639933'},
|
170 | { value: 0, expected: '0'},
|
171 | { value: '0', expected: '0'},
|
172 | { value: '0x0', expected: '0'},
|
173 | { value: -0, expected: '0'},
|
174 | { value: '-0', expected: '0'},
|
175 | { value: '-0x0', expected: '0'},
|
176 | { value: new BN(0), expected: '0'},
|
177 | { value: new BigNumber(0), expected: '0'},
|
178 |
|
179 | { value: new BN('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16), expected: '115792089237316195423570985008687907853269984665640564039457584007913129639935'},
|
180 | { value: new BN('fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', 16), expected: '115792089237316195423570985008687907853269984665640564039457584007913129639933'},
|
181 | { value: new BN('-ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16), expected: '-115792089237316195423570985008687907853269984665640564039457584007913129639935'},
|
182 | { value: new BN('-fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', 16), expected: '-115792089237316195423570985008687907853269984665640564039457584007913129639933'},
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | { value: ' 87234987239872349872489724897248972348972389472498728723897234', expected: '87234987239872349872489724897248972348972389472498728723897234', shouldThrow: false },
|
191 | { value: ' 0x87234987239872349872489724897248972348972389472498728723897234', expected: '238767897175128767816754103632273854819223082003987283874830606953481859636', shouldThrow: false },
|
192 | { value: ' -0x87234987239872349872489724897248972348972389472498728723897234 ', expected: '-238767897175128767816754103632273854819223082003987283874830606953481859636', shouldThrow: false },
|
193 | { value: ' 0xfffffffffFffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: '115792089237316195423570985008687907853269984665640564039457584007913129639935'},
|
194 | { value: ' 0xfffffffffffffffffffffffffffffffffffffffFfffffffffffffffffffffffD ', expected: '115792089237316195423570985008687907853269984665640564039457584007913129639933'},
|
195 | { value: ' -0xfffffffffffffffffffFffffffffffffffffffffffffffffffffffffffffffff', expected: '-115792089237316195423570985008687907853269984665640564039457584007913129639935'},
|
196 | { value: ' -0xfffffffffffffffffffffffffffffffffffffffffffffffffFfffffffffffffd ', expected: '-115792089237316195423570985008687907853269984665640564039457584007913129639933'},
|
197 | { value: ' fffffffffFffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: '115792089237316195423570985008687907853269984665640564039457584007913129639935'},
|
198 | { value: ' fffffffffffffffffffffffffffffffffffffffFfffffffffffffffffffffffD ', expected: '115792089237316195423570985008687907853269984665640564039457584007913129639933'},
|
199 | { value: ' -fffffffffffffffffffFffffffffffffffffffffffffffffffffffffffffffff', expected: '-115792089237316195423570985008687907853269984665640564039457584007913129639935'},
|
200 | { value: ' -fffffffffffffffffffffffffffffffffffffffffffffffffFfffffffffffffd ', expected: '-115792089237316195423570985008687907853269984665640564039457584007913129639933'},
|
201 |
|
202 | { value: '87234987239872349872489724897248972348972389472498728723897234', expected: '87234987239872349872489724897248972348972389472498728723897234', shouldThrow: false },
|
203 | { value: '0x87234987239872349872489724897248972348972389472498728723897234', expected: '238767897175128767816754103632273854819223082003987283874830606953481859636', shouldThrow: false },
|
204 | { value: '-0x87234987239872349872489724897248972348972389472498728723897234', expected: '-238767897175128767816754103632273854819223082003987283874830606953481859636', shouldThrow: false },
|
205 | { value: '0xfffffffffFffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: '115792089237316195423570985008687907853269984665640564039457584007913129639935'},
|
206 | { value: '0xfffffffffffffffffffffffffffffffffffffffFfffffffffffffffffffffffD', expected: '115792089237316195423570985008687907853269984665640564039457584007913129639933'},
|
207 | { value: '-0xfffffffffffffffffffFffffffffffffffffffffffffffffffffffffffffffff', expected: '-115792089237316195423570985008687907853269984665640564039457584007913129639935'},
|
208 | { value: '-0xfffffffffffffffffffffffffffffffffffffffffffffffffFfffffffffffffd', expected: '-115792089237316195423570985008687907853269984665640564039457584007913129639933'},
|
209 | { value: 'fffffffffFffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: '115792089237316195423570985008687907853269984665640564039457584007913129639935'},
|
210 | { value: 'fffffffffffffffffffffffffffffffffffffffFfffffffffffffffffffffffD', expected: '115792089237316195423570985008687907853269984665640564039457584007913129639933'},
|
211 | { value: '-fffffffffffffffffffFffffffffffffffffffffffffffffffffffffffffffff', expected: '-115792089237316195423570985008687907853269984665640564039457584007913129639935'},
|
212 | { value: '-fffffffffffffffffffffffffffffffffffffffffffffffffFfffffffffffffd', expected: '-115792089237316195423570985008687907853269984665640564039457584007913129639933'},
|
213 | ];
|
214 |
|
215 | testCases.forEach(function (test) {
|
216 | if (test.shouldThrow === true) {
|
217 | it('should turn ' + test.value + ' to throw Error.. ', function () {
|
218 | assert.throws(() => numberToBN(test.value), Error);
|
219 | });
|
220 | } else {
|
221 | it('should turn ' + test.value + ' to ' + test.expected, function () {
|
222 | assert.equal(numberToBN(test.value).toString(10), test.expected);
|
223 | });
|
224 | }
|
225 | });
|
226 | });
|