-
Notifications
You must be signed in to change notification settings - Fork 141
/
test.js
391 lines (310 loc) · 14.1 KB
/
test.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import test from 'ava';
import currency from '../src/currency';
test('should be immutable', t => {
var value = currency(1),
// eslint-disable-next-line no-unused-vars
newValue = value.add(0.25);
t.is(parseFloat(value), 1, 'original value not modified');
});
test('should be instance of currency', t => {
var value = currency(1);
t.true(value instanceof currency, 'value is instance of currency');
});
test('should allow numbers', t => {
var intVal = currency(1.23);
t.is(parseFloat(intVal), 1.23, 'currency("1.23") is 1.23');
});
test('should allow strings', t => {
var strVal = currency('1.23');
t.is(parseFloat(strVal), 1.23, 'currency("1.23") is 1.23');
});
test('should allow currency', t => {
var curVal = currency(currency('1.23'));
t.is(parseFloat(curVal), 1.23, 'currency(currency("1.23")) is 1.23');
});
test('should accept strings with symbols', t => {
var str1 = currency('$1234.56'),
str2 = currency('£78.90');
t.is(parseFloat(str1), 1234.56, 'currency("$1234.56") is 1234.56');
t.is(parseFloat(str2), 78.90, 'currency("£78.90") is 78.90');
});
test('should allow comma delimited strings', t => {
var str = currency('1,234,567.89');
t.is(parseFloat(str), 1234567.89, 'currency("1,234,567.89") is 1234567.89');
});
test('should strip invalid characters', t => {
var str = currency('a1b2c3');
t.is(parseFloat(str), 123, 'currency("a1b2c3") is 123');
});
test('should allow negative strings', t => {
var str1 = currency('(5.24)'),
str2 = currency('-12.34');
t.is(parseFloat(str1), -5.24, 'currency("(5.24)") is -5.24');
t.is(parseFloat(str2), -12.34, 'currency("-12.34") is -12.34');
});
test('should default to 0 with invalid strings', t => {
t.is(currency('abc').value, 0, 'currency("abc") is 0');
});
test('should return value', t => {
t.is(currency(1.23).value, 1.23, 'currency.value exists');
});
test('should return intValue', t => {
t.is(currency(1.23).intValue, 123, 'currency.intValue exists');
});
test('should add floating point value', t => {
var value = currency(2.51).add(.01);
t.is(parseFloat(value), 2.52, 'currency(2.51).add(.01) equals decimal value 2.52');
t.not(parseFloat(value), 2.51+.01, 'currency(2.51).add(.01) does not equal 2.5199999999999996');
});
test('should subtract floating point', t => {
var value = currency(2.52).subtract(.01);
t.is(parseFloat(value), 2.51, 'currency(2.52).subtract(.01) equals decimal value 2.51');
t.not(parseFloat(value), 2.52-.01, 'currency(2.52).subtract(.01) does not equal 2.5100000000000002');
});
test('should round half up', t => {
var value1 = currency(17.955)
, value2 = currency(17.855)
, value3 = currency(17.455);
t.is(value1.value, 17.96, 'currency(17.955) rounds half up to 17.96');
t.is(value2.value, 17.86, 'currency(17.855) rounds half up to 17.86');
t.is(value3.value, 17.46, 'currency(17.955) rounds half up to 17.46');
});
test('should round negative values half up', t => {
var value1 = currency(-17.955)
, value2 = currency(-17.855)
, value3 = currency(-17.455);
t.is(value1.value, -17.95, 'currency(-17.955) rounds half up to -17.95');
t.is(value2.value, -17.85, 'currency(-17.855) rounds half up to -17.85');
t.is(value3.value, -17.45, 'currency(-17.955) rounds half up to -17.45');
});
test('currency multiplication', t => {
var value = currency(1.23).multiply(2);
var floatingValue = currency(.1).multiply(.2);
t.is(parseFloat(value), 2.46, 'currency(1.23).multiply(2) is 2.46');
t.is(parseFloat(floatingValue), .02, 'currency(.1).multiply(.2) equals 0.02');
t.not(parseFloat(floatingValue), .1*.2, 'currency(.1).multiply(.2) does not equal 0.020000000000000004');
});
test('currency multiplication with precision', t => {
var value = currency(1.369, { precision: 3 }).multiply(3);
t.is(parseFloat(value), 4.107, 'currency(1.369).multiply(3) is 4.107');
});
test('currency division', t => {
var value = currency(9.87).divide(2);
t.is(parseFloat(value), 4.94, 'currency(9.87).divide(2) is 4.94');
});
test('currency division with precision', t => {
var value = currency(4.107, { precision: 3 }).divide(3);
t.is(parseFloat(value), 1.369, 'currency(4.107).divide(3) is 1.369');
});
test('should parse negative values', t => {
var pos = currency(1.23),
neg = currency(-1.23),
distribute = neg.distribute(4);
var total = 0;
for(var i = 0; i < distribute.length; i++) {
total += parseFloat(distribute[i]);
}
t.is(parseFloat(neg), -1.23, 'currency(-1.23) is -1.23');
t.is(parseFloat(pos.subtract(2.01)), -0.78, 'currency(1.23).subtract(2.01) is -0.78');
t.is(parseFloat(distribute[0]), -0.31, 'currency(-1.23).distribute(4), first distributed value is -0.31');
t.is(total, -1.23, 'currency(-1.23).distribute(4) sum is -1.23');
});
test('should create equal distribution', t => {
var values = currency(1.00).distribute(4);
var realValues = [];
for(var i = 0; i < values.length; i++) {
realValues.push(parseFloat(values[i]));
}
t.is(values.length, 4, 'currency(1.00).distribute(4) creates 4 distributed items');
t.deepEqual(realValues, [0.25, 0.25, 0.25, 0.25], 'all distributed items are equal');
});
test('should create non-equal distribution with pennies', t => {
var values = currency(1.01).distribute(4);
t.is(parseFloat(values[0]), 0.26, 'first value is 0.26');
t.is(parseFloat(values[1]), 0.25, 'next value is 0.25');
var total = 0;
for(var i = 0; i < values.length; i++) {
total += parseFloat(values[i]);
}
t.is(total, 1.01, 'sum of values matches our original amount');
});
test('should create non-equal distribution with a negative penny', t => {
var values = currency(-0.01).distribute(2);
t.is(parseFloat(values[0]), -0.01, 'first value is -0.01');
t.is(parseFloat(values[1]), 0, 'second value is 0');
var total = 0;
for(var i = 0; i < values.length; i++) {
total += parseFloat(values[i]);
}
t.is(total, -0.01, 'sum of values matches our original amount');
});
test('should get dollar value', t => {
var value = currency(1.23);
t.is(value.add(2).dollars(), 3, 'is dollar amount');
t.is(value.add(.8).dollars(), 2, 'is dollar amount');
t.is(value.subtract(3).dollars(), -1, 'is negative dollar amount');
});
test('should get cent value', t => {
var value = currency(1.23);
t.is(value.cents(), 23, 'is cent amount');
t.is(value.add(.31).cents(), 54, 'is cent amount');
});
test('should support different precision values', t => {
let c1 = currency(1.234, { precision: 3 });
let c2 = currency(1.234, { symbol: '¥', precision: 0 });
t.is(c1.value, 1.234);
t.is(c2.value, 1);
t.is(c1.intValue, 1234);
t.is(c2.intValue, 1);
t.is(c1.add(4.567).value, 5.801);
t.is(c2.add(4.567).value, 6);
t.is(c1.subtract(4.567).value, -3.333);
t.is(c2.subtract(4.567).value, -4);
t.is(c1.cents(), 234);
t.is(c2.cents(), 0);
t.is(c1.format(true), '$1.234');
t.is(c2.format(true), '¥1');
t.deepEqual(c1.distribute(4).map(x => x.value), [.309, .309, .308, .308]);
t.deepEqual(c2.distribute(4).map(x => x.value), [1, 0, 0, 0]);
});
test('should use source precision for arithmetic with different precisions', t => {
let c1 = currency(1.23);
let c2 = currency(1.239, { precision: 3 });
t.is(c1.add(c2).value, 2.47);
t.is(c2.add(c1).value, 2.469);
});
test('should use source formatting for mixed currency formats', t => {
let c1 = currency('1,234.56');
let c2 = currency('1 234,56', { separator: ' ', decimal: ',' });
t.is(c1.add(c2).format(), '2,469.12');
t.is(c2.add(c1).format(), '2 469,12');
});
test('should default rounding when parsing', t => {
var round1 = currency(1.234)
, round2 = currency(5.6789)
, multiply = currency(10.00)
, divide = currency(0.01);
t.is(round1.value, 1.23, 'value is rounded to nearest cent');
t.is(round2.value, 5.68, 'value is rounded to nearest cent');
t.is(multiply.multiply(.001).value, .01, 'multiply value is not rounded');
t.is(divide.divide(.001).value, 10, 'divide value is not rounded');
});
test('should have int value and real value', t => {
var value1 = currency(2.51).add(.01)
, value2 = currency(2.52).subtract(.01);
t.is(value1.value, 2.52, 'real value is 2.52');
t.is(value1.intValue, 252, 'int value is 252');
t.is(value2.value, 2.51, 'real value is 2.51');
t.is(value2.intValue, 251, 'int value is 251');
});
test('should stringify json', t => {
var value1 = currency(1.23)
, value2 = currency(2.51).add(.01)
, value3 = currency(2.52).subtract(.02);
t.is(JSON.stringify({ 'value': value1 }), '{"value":1.23}', 'value is 1.23');
t.is(JSON.stringify({ 'value': value2 }), '{"value":2.52}', 'value is 2.52');
t.is(JSON.stringify({ 'value': value3 }), '{"value":2.5}', 'value is 2.50');
});
test('should format value using defaults', t => {
var value1 = currency(1.23).format()
, value2 = currency(1234.56).format()
, value3 = currency(1234567.89).format();
t.is(typeof value1, 'string', 'value is string');
t.is(value1, '1.23', 'value is "1.23"');
t.is(value2, '1,234.56', 'value is "1,234.45"');
t.is(value3, '1,234,567.89', 'value is "1,234,567.89"');
});
test('should format value using international', t => {
let c = value => currency(value, { separator: '.', decimal: ',' });
t.is(c(1.23).format(), '1,23', 'value is "1,23"');
t.is(c(1000.00).format(), '1.000,00', 'value is "1.000,00"');
t.is(c(1000000.00).format(), '1.000.000,00', 'value is "1.000.000,00"');
});
test('should format vedic groupings', t => {
let c = value => currency(value, { useVedic: true });
t.is(c(1.23).format(), '1.23', 'value is "1.23"');
t.is(c(1000.00).format(), '1,000.00', 'value is "1,000"');
t.is(c(100000.00).format(), '1,00,000.00', 'value is "1,00,000,00"');
t.is(c(1000000.00).format(), '10,00,000.00', 'value is "10,00,000,00"');
});
test('should parse international values', t => {
let c = value => currency(value, { separator: '.', decimal: ',' });
t.is(c('1,23').value, 1.23, 'value is "1.23"');
t.is(c('1.000,00').value, 1000.00, 'value is "1,000.00"');
t.is(c('1.000.000,00').value, 1000000.00, 'value is "1,000,000.00"');
});
test('should format with symbol', t => {
t.is(currency(1.23).format(true), '$1.23', 'value is "$1.23"');
});
test('should format without symbol', t => {
t.is(currency(1.23).format(false), '1.23', 'value is "1.23"');
});
test('should format with international symbol', t => {
t.is(currency(1.23, { symbol: '£' }).format(true), '£1.23', 'value is "£1.23"');
t.is(currency(1.23, { symbol: '¥' }).format(true), '¥1.23', 'value is "¥1.23"');
});
test('should return 0.00 currency with invalid input', t => {
// eslint-disable-next-line no-undefined
var value = currency(undefined);
t.is(value.value, 0, 'value is "0.00"');
});
test('should round down to nearest value when using increments', t => {
let c = value => currency(value, { increment: .05 });
t.is(c(1.01).toString(), '1.00', 'value is rounded to 1.00');
t.is(c(1.02).toString(), '1.00', 'value is rounded to 1.00');
t.is(c(1.06).toString(), '1.05', 'value is rounded to 1.05');
t.is(c(1.07).toString(), '1.05', 'value is rounded to 1.05');
t.is(c(1000.01).format(), '1,000.00', 'value is rounded to 1000.00');
t.is(c(10000.01).format(), '10,000.00', 'value is rounded to 10000.00');
t.is(c(100000.01).format(), '100,000.00', 'value is rounded to 100000.00');
t.is(c(1000000.01).format(), '1,000,000.00', 'value is rounded to 1000000.00');
});
test('should round up to nearest value when using increments', t => {
let c = value => currency(value, { increment: .05 });
t.is(c(1.03).toString(), '1.05', 'value is rounded to 1.05');
t.is(c(1.04).toString(), '1.05', 'value is rounded to 1.05');
t.is(c(1.08).toString(), '1.10', 'value is rounded to 1.10');
t.is(c(1.09).toString(), '1.10', 'value is rounded to 1.10');
t.is(c(1000.09).format(), '1,000.10', 'value is rounded to 1000.10');
t.is(c(10000.09).format(), '10,000.10', 'value is rounded to 10000.10');
t.is(c(100000.09).format(), '100,000.10', 'value is rounded to 100000.10');
t.is(c(1000000.09).format(), '1,000,000.10', 'value is rounded to 1000000.10');
});
test('should handle negative rounding when using increments', t => {
let c = value => currency(value, { increment: .05 });
t.is(c(-1.01).toString(), '-1.00', 'value is rounded to -1.00');
t.is(c(-1.02).toString(), '-1.00', 'value is rounded to -1.00');
t.is(c(-1.03).toString(), '-1.05', 'value is rounded to -1.05');
t.is(c(-1.04).toString(), '-1.05', 'value is rounded to -1.05');
t.is(c(-1.06).toString(), '-1.05', 'value is rounded to -1.05');
t.is(c(-1.07).toString(), '-1.05', 'value is rounded to -1.05');
t.is(c(-1.08).toString(), '-1.10', 'value is rounded to -1.10');
t.is(c(-1.09).toString(), '-1.10', 'value is rounded to -1.10');
});
test('should round only the final value to nearest increment', t => {
let c = value => currency(value, { increment: .05 });
t.is(c(1.00).add(.01).add(.01).add(.01).toString(), '1.05', 'value is rounded to 1.05');
t.is(c(1.00).subtract(.01).subtract(.01).subtract(.01).toString(), '0.95', 'value is rounded to 0.95');
});
test('should not modify internal values when rounding', t => {
let c = value => currency(value, { increment: .05 });
t.is(c(1.00).add(.01).intValue, 101, 'intValue is to 101');
t.is(c(1.00).add(.01).value, 1.01, 'value is to 1.01');
t.is(c(1.00).add(.04).intValue, 104, 'intValue is to 104');
t.is(c(1.00).add(.04).value, 1.04, 'value is to 1.04');
});
test('should allow arbitrary rounding increments', t => {
let c1 = value => currency(value, { increment: .1 });
let c2 = value => currency(value, { increment: .25 });
let c3 = value => currency(value, { increment: 5, precision: 0 });
t.is(c1(1.06).toString(), '1.10', 'value is rounded to 1.10');
t.is(c1(-1.06).toString(), '-1.10', 'value is rounded to -1.10');
t.is(c2(1.17).toString(), '1.25', 'value is rounded to 1.25');
t.is(c2(-1.17).toString(), '-1.25', 'value is rounded to -1.25');
t.is(c3(117).toString(), '115', 'value is rounded to 120');
t.is(c3(-117).toString(), '-115', 'value is rounded to 120');
});
test('should throw exception with invalid input', t => {
// eslint-disable-next-line no-undefined
t.throws(function() { currency(undefined, { errorOnInvalid: true }); }, Error, 'Threw exception');
});