1 |
|
2 | if (typeof module !== 'undefined' && module.exports) {
|
3 | var numeral = require('../numeral');
|
4 | var expect = require('chai').expect;
|
5 | }
|
6 |
|
7 | describe('Numeral', function() {
|
8 | afterEach(function() {
|
9 | numeral.reset();
|
10 | });
|
11 |
|
12 | describe('Default', function() {
|
13 | it('should set a default format', function() {
|
14 | numeral.defaultFormat('0,0');
|
15 |
|
16 | expect(numeral(10000).format()).to.equal('10,000');
|
17 | });
|
18 | });
|
19 |
|
20 | describe('Types', function() {
|
21 | it('should return a value as correct type', function() {
|
22 | var tests = [
|
23 | [1234.56,'number'],
|
24 | ['1234.56','number'],
|
25 | [0,'number'],
|
26 | [NaN,'object'],
|
27 | [null,'object']
|
28 | ],
|
29 | i;
|
30 |
|
31 | for (i = 0; i < tests.length; i++) {
|
32 | expect(typeof numeral(tests[i][0]).value()).to.equal(tests[i][1]);
|
33 | }
|
34 | });
|
35 | });
|
36 |
|
37 | describe('Value', function() {
|
38 | it('should return a value', function() {
|
39 | var tests = [
|
40 | [1000, 1000],
|
41 | [0.5, 0.5],
|
42 | [null, null],
|
43 | ['1,000', 1000],
|
44 | ['not a number', null]
|
45 | ],
|
46 | num;
|
47 |
|
48 | for (var i = 0; i < tests.length; i++) {
|
49 | num = numeral(tests[i][0]);
|
50 |
|
51 | expect(num.value()).to.equal(tests[i][1]);
|
52 | }
|
53 | });
|
54 | });
|
55 |
|
56 | describe('Set', function() {
|
57 | it('should set a value', function() {
|
58 | var tests = [
|
59 | [1000,1000],
|
60 | [-0.25,-0.25]
|
61 | ],
|
62 | num;
|
63 |
|
64 | for (var i = 0; i < tests.length; i++) {
|
65 | num = numeral().set(tests[i][0]);
|
66 |
|
67 | expect(num.value()).to.equal(tests[i][1]);
|
68 | }
|
69 | });
|
70 | });
|
71 |
|
72 | describe('Custom Zero', function() {
|
73 | it('should change zero value', function() {
|
74 | var tests = [
|
75 | [0,null,'0','0'],
|
76 | [0,'N/A','0','N/A'],
|
77 | [0,'','','']
|
78 | ];
|
79 |
|
80 | for (var i = 0; i < tests.length; i++) {
|
81 | numeral.zeroFormat(tests[i][1]);
|
82 |
|
83 | expect(numeral(tests[i][0]).format(tests[i][2])).to.equal(tests[i][3]);
|
84 | }
|
85 | });
|
86 | });
|
87 |
|
88 | describe('Custom Null', function() {
|
89 | it('should change null value', function() {
|
90 | var tests = [
|
91 | [null,null,'0','0'],
|
92 | [null,'N/A','0','N/A'],
|
93 | [null,'','','']
|
94 | ];
|
95 |
|
96 | for (var i = 0; i < tests.length; i++) {
|
97 | numeral.nullFormat(tests[i][1]);
|
98 |
|
99 | expect(numeral(tests[i][0]).format(tests[i][2])).to.equal(tests[i][3]);
|
100 | }
|
101 | });
|
102 | });
|
103 |
|
104 | describe('Clone', function() {
|
105 | it('should clone', function() {
|
106 | var a = numeral(1000),
|
107 | b = numeral(a),
|
108 | c = a.clone(),
|
109 | aVal = a.value(),
|
110 | aSet = a.set(2000).value(),
|
111 | bVal = b.value(),
|
112 | cVal = c.add(10).value();
|
113 |
|
114 | expect(aVal).to.equal(1000);
|
115 | expect(aSet).to.equal(2000);
|
116 | expect(bVal).to.equal(1000);
|
117 | expect(cVal).to.equal(1010);
|
118 | });
|
119 | });
|
120 |
|
121 | describe('isNumeral', function() {
|
122 | it('should return boolean', function() {
|
123 | var tests = [
|
124 | [numeral(),true],
|
125 | [1,false]
|
126 | ];
|
127 |
|
128 | for (var i = 0; i < tests.length; i++) {
|
129 | expect(numeral.isNumeral(tests[i][0])).to.equal(tests[i][1]);
|
130 | }
|
131 | });
|
132 | });
|
133 |
|
134 | describe('Format', function() {
|
135 | it('should format to a number', function() {
|
136 | var tests = [
|
137 | [0, null, '0'],
|
138 | [0, '0.00', '0.00'],
|
139 | [null, null, '0'],
|
140 | [NaN, '0.0', '0.0'],
|
141 | [10000,'0,0.0000','10,000.0000'],
|
142 | [10000.23,'0,0','10,000'],
|
143 | [-10000,'0,0.0','-10,000.0'],
|
144 | [10000.1234,'0.000','10000.123'],
|
145 | [10000,'0[.]00','10000'],
|
146 | [10000.1,'0[.]00','10000.10'],
|
147 | [10000.123,'0[.]00','10000.12'],
|
148 | [10000.456,'0[.]00','10000.46'],
|
149 | [10000.001,'0[.]00','10000'],
|
150 | [10000.45,'0[.]00[0]','10000.45'],
|
151 | [10000.456,'0[.]00[0]','10000.456'],
|
152 | [10000,'(0,0.0000)','10,000.0000'],
|
153 | [-10000,'(0,0.0000)','(10,000.0000)'],
|
154 | [-12300,'+0,0.0000','-12,300.0000'],
|
155 | [1230,'+0,0','+1,230'],
|
156 | [1230,'-0,0','1,230'],
|
157 | [-1230,'-0,0','-1,230'],
|
158 | [-1230.4,'0,0.0+','1,230.4-'],
|
159 | [-1230.4,'0,0.0-','1,230.4-'],
|
160 | [1230.4,'0,0.0-','1,230.4'],
|
161 | [100.78, '0', '101'],
|
162 | [100.28, '0', '100'],
|
163 | [1.932,'0.0','1.9'],
|
164 | [1.9687,'0','2'],
|
165 | [1.9687,'0.0','2.0'],
|
166 | [-0.23,'.00','-.23'],
|
167 | [-0.23,'(.00)','(.23)'],
|
168 | [0.23,'0.00000','0.23000'],
|
169 | [0.67,'0.0[0000]','0.67'],
|
170 | [3162.63,'0.0[00000000000000]','3162.63'],
|
171 | [1.99,'0.[0]','2'],
|
172 | [1.0501,'0.00[0]','1.05'],
|
173 |
|
174 | [2000000000,'0.0a','2.0b'],
|
175 | [1230974,'0.0a','1.2m'],
|
176 | [1460,'0a','1k'],
|
177 | [-104000,'0 a','-104 k'],
|
178 | [999950,'0.0a','1.0m'],
|
179 | [999999999,'0a','1b'],
|
180 |
|
181 | [-5444333222111, '0,0 ak', '-5,444,333,222 k'],
|
182 | [5444333222111, '0,0 am', '5,444,333 m'],
|
183 | [-5444333222111, '0,0 ab', '-5,444 b'],
|
184 | [-5444333222111, '0,0 at', '-5 t'],
|
185 | [123456, '0.0[0] ak', '123.46 k'],
|
186 | [150,'0.0 ak','0.2 k']
|
187 | ],
|
188 | i,
|
189 | n,
|
190 | output;
|
191 |
|
192 | for (i = 0; i < tests.length; i++) {
|
193 | n = numeral(tests[i][0]);
|
194 | output = n.format(tests[i][1]);
|
195 |
|
196 | expect(output).to.equal(tests[i][2]);
|
197 |
|
198 | expect(typeof output).to.equal('string');
|
199 | }
|
200 | });
|
201 | });
|
202 |
|
203 | describe('Unformat', function() {
|
204 | before(function() {
|
205 | numeral.zeroFormat('N/A');
|
206 | numeral.nullFormat('N/A');
|
207 | });
|
208 |
|
209 | after(function() {
|
210 | numeral.reset();
|
211 | });
|
212 |
|
213 | it('should unformat a number', function() {
|
214 | var tests = [
|
215 | ['10,000.123', 10000.123],
|
216 | ['(0.12345)', -0.12345],
|
217 | ['((--0.12345))', 0.12345],
|
218 | ['1.23t', 1230000000000],
|
219 | ['N/A', 0],
|
220 | ['', null],
|
221 |
|
222 | [0, 0],
|
223 | [1, 1],
|
224 | [1.1, 1.1],
|
225 | [-0, 0],
|
226 | [-1, -1],
|
227 | [-1.1, -1.1]
|
228 | ];
|
229 |
|
230 | for (var i = 0; i < tests.length; i++) {
|
231 | expect(numeral(tests[i][0]).value()).to.equal(tests[i][1]);
|
232 | }
|
233 | });
|
234 | });
|
235 |
|
236 | describe('Validate', function() {
|
237 | var locale = 'en';
|
238 |
|
239 | describe('Numbers', function() {
|
240 | it('should validate numbers', function() {
|
241 | var tests = [
|
242 | ['1000', true],
|
243 | ['1,000', true],
|
244 | ['10,0,0', true],
|
245 | ['10.123', true],
|
246 | ['1,000.123', true],
|
247 | ['1000,123.123', true],
|
248 | ['1000 ', true],
|
249 | [' 1000 ', true],
|
250 | [' 1000', true],
|
251 | [' 1000,100.123', true],
|
252 | ['1.0,00', false],
|
253 | ['1.0.00', false],
|
254 | ['1 000', false],
|
255 | ['1.000,123', false],
|
256 | ['1000.', false],
|
257 | ['1000,', false],
|
258 | ['10..00', false],
|
259 | ['10,,00', false],
|
260 | ['10, 00', false]
|
261 | ];
|
262 |
|
263 | for (var i = 0; i < tests.length; i++) {
|
264 | expect(numeral.validate(tests[i][0], locale)).to.equal(tests[i][1]);
|
265 | }
|
266 | });
|
267 | });
|
268 |
|
269 | describe('Currency', function() {
|
270 | it('should validate currency', function() {
|
271 | var tests = [
|
272 | ['$1000', true],
|
273 | ['$1,000', true],
|
274 | ['$10,0,0', true],
|
275 | ['$10.123', true],
|
276 | ['$1,000.123', true],
|
277 | ['$1000 ', true],
|
278 | [' $1000 ', true],
|
279 | [' $1000', true],
|
280 | [' $1000,100.123', true],
|
281 | ['$100.123k', true],
|
282 | ['$100.123m', true],
|
283 | ['$100.123b', true],
|
284 | ['$100.123t', true],
|
285 | ['100,456.123k', true],
|
286 | [' 100,456.123t ', true],
|
287 | ['$1,00.123k', true],
|
288 | ['%100', false],
|
289 | [' %1.0.00', false],
|
290 | [' ^1 000 ', false],
|
291 | ['^1.000 ', false],
|
292 | ['$ 1000.', false],
|
293 | ['%1000', false],
|
294 | ['100,456.123z', false],
|
295 | ['$100$', false],
|
296 | ['$100,213.456l', false],
|
297 | ['aa100,213.456l', false],
|
298 | ['$100,213.456kk', false]
|
299 | ];
|
300 |
|
301 | for (var i = 0; i < tests.length; i++) {
|
302 | expect(numeral.validate(tests[i][0], locale)).to.equal(tests[i][1]);
|
303 | }
|
304 | });
|
305 | });
|
306 | });
|
307 |
|
308 | describe('Manipulate', function() {
|
309 |
|
310 | describe('Add', function() {
|
311 | it('should add', function() {
|
312 | var tests = [
|
313 | [1000,10,1010],
|
314 | [0.5,3,3.5],
|
315 | [-100,200,100],
|
316 | [0.1,0.2,0.3],
|
317 | [0.28,0.01,0.29],
|
318 | [0.289999,0.000001,0.29],
|
319 | [0.29,0.01,0.3]
|
320 | ],
|
321 | num;
|
322 |
|
323 | for (var i = 0; i < tests.length; i++) {
|
324 | num = numeral(tests[i][0]);
|
325 |
|
326 | num.add(tests[i][1]);
|
327 |
|
328 | expect(num.value()).to.equal(tests[i][2]);
|
329 | }
|
330 | });
|
331 | });
|
332 |
|
333 | describe('Subtract', function() {
|
334 | it('should subtract', function() {
|
335 | var tests = [
|
336 | [1000,10,990],
|
337 | [0.5,3,-2.5],
|
338 | [-100,200,-300],
|
339 | [0.3,0.1,0.2],
|
340 | [0.28,0.01,0.27],
|
341 | [0.29,0.01,0.28]
|
342 | ],
|
343 | num;
|
344 |
|
345 | for (var i = 0; i < tests.length; i++) {
|
346 | num = numeral(tests[i][0]);
|
347 |
|
348 | num.subtract(tests[i][1]);
|
349 |
|
350 | expect(num.value()).to.equal(tests[i][2]);
|
351 | }
|
352 | });
|
353 | });
|
354 |
|
355 |
|
356 | describe('Add', function() {
|
357 | it('should add', function() {
|
358 | });
|
359 | });
|
360 |
|
361 |
|
362 | describe('Multiply', function() {
|
363 | it('should multiply', function() {
|
364 | var tests = [
|
365 | [1000,10,10000],
|
366 | [0.5,3,1.5],
|
367 | [-100,200,-20000],
|
368 | [0.1,0.2,0.02],
|
369 | [0.28,0.01,0.0028],
|
370 | [0.29,0.01,0.0029],
|
371 | [0.00000231,10000000,23.1]
|
372 | ],
|
373 | num;
|
374 |
|
375 | for (var i = 0; i < tests.length; i++) {
|
376 | num = numeral(tests[i][0]);
|
377 |
|
378 | num.multiply(tests[i][1]);
|
379 |
|
380 | expect(num.value()).to.equal(tests[i][2]);
|
381 | }
|
382 | });
|
383 | });
|
384 |
|
385 | describe('Divide', function() {
|
386 | it('should divide', function() {
|
387 | var tests = [
|
388 | [1000,10,100],
|
389 | [0.5,3,0.16666666666666666],
|
390 | [-100,200,-0.5],
|
391 | [5.3,0.1,53],
|
392 | [0.28,0.01,28],
|
393 | [0.29,0.01,29]
|
394 | ],
|
395 | num;
|
396 |
|
397 | for (var i = 0; i < tests.length; i++) {
|
398 | num = numeral(tests[i][0]);
|
399 |
|
400 | num.divide(tests[i][1]);
|
401 |
|
402 | expect(num.value()).to.equal(tests[i][2]);
|
403 | }
|
404 | });
|
405 | });
|
406 |
|
407 | describe('Difference', function() {
|
408 | it('should find a difference', function() {
|
409 | var tests = [
|
410 | [1000,10,990],
|
411 | [0.5,3,2.5],
|
412 | [-100,200,300],
|
413 | [0.3,0.2,0.1],
|
414 | [0.28,0.01,0.27],
|
415 | [0.29,0.01,0.28]
|
416 | ],
|
417 | num;
|
418 |
|
419 | for (var i = 0; i < tests.length; i++) {
|
420 | num = numeral(tests[i][0]);
|
421 |
|
422 | expect(num.difference(tests[i][1])).to.equal(tests[i][2]);
|
423 | }
|
424 | });
|
425 | });
|
426 |
|
427 | describe('Rounding', function() {
|
428 | it('should format with rounding', function() {
|
429 | var tests = [
|
430 |
|
431 | [2280002, '0.00a', '2.28m', '2.29m'],
|
432 | [10000.23,'0,0','10,000', '10,001'],
|
433 | [1000.234,'0,0.00','1,000.23', '1,000.24'],
|
434 | [0.97487823,'0.000','0.974','0.975'],
|
435 | [-0.433,'0.0','-0.5', '-0.4']
|
436 | ],
|
437 | i;
|
438 |
|
439 | for (i = 0; i < tests.length; i++) {
|
440 |
|
441 | expect(numeral(tests[i][0]).format(tests[i][1], Math.floor)).to.equal(tests[i][2]);
|
442 |
|
443 |
|
444 | expect(numeral(tests[i][0]).format(tests[i][1], Math.ceil)).to.equal(tests[i][3]);
|
445 | }
|
446 | });
|
447 | });
|
448 | });
|
449 |
|
450 | describe('Utilities', function() {
|
451 | describe('Insert', function() {
|
452 | it('should insert into string', function() {
|
453 | var tests = [
|
454 | ['1000', '+', 0, '+1000'],
|
455 | ['1000', '-', 4, '1000-']
|
456 | ],
|
457 | i;
|
458 |
|
459 | for (i = 0; i < tests.length; i++) {
|
460 | expect(numeral._.insert(tests[i][0], tests[i][1], tests[i][2])).to.equal(tests[i][3]);
|
461 | }
|
462 | });
|
463 | });
|
464 | });
|
465 | });
|