/
math.js
305 lines (291 loc) · 9.15 KB
/
math.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
'use strict';
require('mocha');
var assert = require('assert');
var hbs = require('handlebars').create();
var helpers = require('../helpers.js');
helpers.math({handlebars: hbs});
describe('math', function() {
describe('add', function() {
it('should return the sum of two numbers.', function() {
var fn = hbs.compile('{{add value 5}}');
assert.equal(fn({value: 5}), '10');
});
it('should return the sum of two variable numbers as strings.', function() {
var fn = hbs.compile('{{add a b}}');
assert.equal(fn({a: '5', b: '5'}), '10');
});
it('should return blank with undefined', function() {
var fn = hbs.compile('{{add a b}}');
assert.equal(fn({a: undefined, b: 5}), '');
});
});
describe('average', function() {
it('should return the average of a list of numbers:', function() {
var fn = hbs.compile('{{avg 1 2 3 4}}');
assert.equal(fn(), '2.5');
});
it('should return the average of an array of numbers:', function() {
var fn = hbs.compile('{{avg array}}');
assert.equal(fn({array: [1, 3, 6, 9]}), '4.75');
});
});
describe('ceil', function() {
it('should return the value rounded up to the nearest integer.', function() {
var fn = hbs.compile('{{ceil value}}');
assert.equal(fn({value: 5.6}), '6');
});
});
describe('divide', function() {
it('should return the division of two numbers.', function() {
var fn = hbs.compile('{{divide value 5}}');
assert.equal(fn({value: 5}), '1');
});
});
describe('floor', function() {
it('should return the value rounded down to the nearest integer.', function() {
var fn = hbs.compile('{{floor value}}');
assert.equal(fn({value: 5.6}), '5');
});
});
describe('multiply', function() {
it('should return the multiplication of two numbers.', function() {
var fn = hbs.compile('{{multiply value 5}}');
assert.equal(fn({value: 5}), '25');
});
});
describe('remainder', function() {
it('should return the remainder of two numbers.', function() {
var fn = hbs.compile('{{remainder value 5}}');
assert.equal(fn({value: 7}), '2');
});
it('should take the sign of the dividend.', function() {
var fn = hbs.compile('{{remainder 5 -3}}');
assert.equal(fn(), '2');
});
});
describe('round', function() {
it('should return the value rounded to the nearest integer.', function() {
var fn = hbs.compile('{{round value}}');
assert.equal(fn({value: 5.69}), '6');
});
});
describe('subtract', function() {
it('should return the difference of two numbers.', function() {
var fn = hbs.compile('{{subtract value 5}}');
assert.equal(fn({value: 5}), '0');
});
});
describe('sum', function() {
it('should return the sum of multiple numbers.', function() {
var fn = hbs.compile('{{sum value 67 80}}');
assert.equal(fn({value: 20}), '167');
});
it('should return the sum of multiple numbers.', function() {
var fn = hbs.compile('{{sum 1 2 3}}');
assert.equal(fn(), '6');
});
it('should return the total sum of array.', function() {
var fn = hbs.compile('{{sum value}}');
assert.equal(fn({value: [1, 2, 3]}), '6');
});
it('should return the total sum of array and numbers.', function() {
var fn = hbs.compile('{{sum value 5}}');
assert.equal(fn({value: [1, 2, 3]}), '11');
});
});
describe('random', function() {
it('should return a random number between two values.', function() {
var fn = hbs.compile('{{random 5 10}}');
assert(fn() >= 5);
assert(fn() <= 10);
});
});
describe('divide', function() {
it('should throw an error when A is not valid.', function() {
try {
var fn = hbs.compile('{{divide a b}}');
assert.equal(fn({a: undefined, b: 5}), '1');
} catch (error) {
assert(error);
}
});
it('should throw an error when B is not valid.', function() {
try {
var fn = hbs.compile('{{divide a b}}');
assert.equal(fn({a: 5, b: undefined}), '1');
} catch (error) {
assert(error);
}
});
});
describe('ceil', function() {
it('should an error when the value is not a number', function() {
try {
var fn = hbs.compile('{{ceil value}}');
assert.equal(fn({value: undefined}), '6');
} catch (error) {
assert(error);
}
});
});
describe('abs', function() {
it('should an error when the value is not a number', function() {
try {
var fn = hbs.compile('{{abs value}}');
assert.equal(fn({value: undefined}), '6');
} catch (error) {
assert(error);
}
});
it('should an error when the value is not a number', function() {
var fn = hbs.compile('{{abs value}}');
assert.equal(fn({value: 1}), '1');
});
});
describe('floor', function() {
it('should return error with not a number.', function() {
try {
var fn = hbs.compile('{{floor value}}');
assert.equal(fn({value: undefined}), '5');
} catch (error) {
assert(error);
}
});
});
describe('minus', function() {
it('should do a simple minus', function() {
var fn = hbs.compile('{{minus a b}}');
assert.equal(fn({a: 10, b: 5}), '5');
});
it('should throw an error when A is not valid.', function() {
try {
var fn = hbs.compile('{{minus a b}}');
assert.equal(fn({a: undefined, b: 5}), '1');
} catch (error) {
assert(error);
}
});
it('should throw an error when A is not valid.', function() {
try {
var fn = hbs.compile('{{minus a b}}');
assert.equal(fn({a: 5, b: undefined}), '1');
} catch (error) {
assert(error);
}
});
});
describe('modulo', function() {
it('should return valid response', function() {
var fn = hbs.compile('{{modulo a b}}');
assert.equal(fn({a: 100, b: 5}), '0');
});
it('should return error with not a number.', function() {
try {
var fn = hbs.compile('{{modulo a b}}');
assert.equal(fn({a: undefined, b: 5}), '5');
} catch (error) {
assert(error);
}
});
it('should return error with not a number.', function() {
try {
var fn = hbs.compile('{{modulo a b}}');
assert.equal(fn({a: 5, b: undefined}), '5');
} catch (error) {
assert(error);
}
});
});
describe('multiply', function() {
it('should return error with undifined value on a', function() {
try {
var fn = hbs.compile('{{multiply a b}}');
assert.equal(fn({a: undefined, b: 5}), '25');
} catch (error) {
assert(error);
}
});
it('should return error with undifined value on b', function() {
try {
var fn = hbs.compile('{{multiply a b}}');
assert.equal(fn({a: 5, b: undefined}), '25');
} catch (error) {
assert(error);
}
});
});
describe('plus', function() {
it('should return error with undifined value on a', function() {
try {
var fn = hbs.compile('{{plus a b}}');
assert.equal(fn({a: undefined, b: 5}), '25');
} catch (error) {
assert(error);
}
});
it('should return error with undifined value on b', function() {
try {
var fn = hbs.compile('{{plus a b}}');
assert.equal(fn({a: 5, b: undefined}), '25');
} catch (error) {
assert(error);
}
});
it('add the values together', function() {
var fn = hbs.compile('{{plus a b}}');
assert.equal(fn({a: 5, b: 5}), '10');
});
});
describe('random', function() {
it('should return an error with an undefined value on min', function() {
try {
var fn = hbs.compile('{{random a b}}');
assert(fn({a: undefined, b: 5}), '1');
} catch (error) {
assert(error);
}
});
it('should return an error with an undefined value on max', function() {
try {
var fn = hbs.compile('{{random a b}}');
assert(fn({a: 5, b: undefined}), '1');
} catch (error) {
assert(error);
}
});
});
describe('round', function() {
it('should return an error when the value is undefined', function() {
try {
var fn = hbs.compile('{{round value}}');
assert.equal(fn({value: undefined}), '6');
} catch (error) {
assert(error);
}
});
});
describe('subtract', function() {
it('should return an error with an undefined value on min', function() {
try {
var fn = hbs.compile('{{subtract a b}}');
assert(fn({a: undefined, b: 5}), '1');
} catch (error) {
assert(error);
}
});
it('should return an error with an undefined value on max', function() {
try {
var fn = hbs.compile('{{subtract a b}}');
assert(fn({a: 5, b: undefined}), '1');
} catch (error) {
assert(error);
}
});
});
describe('times', function() {
it('should return a valid multiplication', function() {
var fn = hbs.compile('{{times a b}}');
assert(fn({a: 5, b: 5}), '25');
});
});
});