Skip to content

Commit fe89b6d

Browse files
committed
Add jasmine custom matcher toBeCloseToArray
1 parent 0c3b14b commit fe89b6d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module.exports = {
2+
3+
// toBeCloseTo... but for arrays
4+
toBeCloseToArray: function(){
5+
return {
6+
compare: function(actual, expected, precision) {
7+
if(precision !== 0){
8+
precision = Math.pow(10, -precision) / 2 || 0.005;
9+
}
10+
11+
var tested = actual.map(function(element, i) {
12+
return Math.abs(expected[i] - element) < precision;
13+
});
14+
15+
var passed = tested.indexOf(false) < 0;
16+
17+
return {
18+
pass: passed,
19+
message: 'Expected ' + actual + ' to be close to ' + expected + '.'
20+
};
21+
}
22+
};
23+
}
24+
};

0 commit comments

Comments
 (0)