-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest-tools.js
40 lines (33 loc) · 1.06 KB
/
test-tools.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
var difflib = require('difflib');
module.exports.deepUnorderedEquals = deepUnorderedEquals;
module.exports.editDistancePercentage = editDistancePercentage;
// Deep list equality test, Ignores ordering.
function deepUnorderedEquals(list1, list2) {
// recursion base: not lists
if (!Array.isArray(list1) ||
!Array.isArray(list2))
return list1 == list2;
// Unequal length, trivially unequal
if (list1.length != list2.length)
return false;
// Try to match up every element in list1 with a unique element in list2
var found1 = [];
var found2 = [];
list1.forEach(function(e1, i1) {
list2.forEach(function(e2, i2) {
if (!found2[i2] && deepUnorderedEquals(e1, e2)) {
found1[i1] = true;
found2[i2] = true;
}
});
});
// Check that all elements in both lists are matched
for (var i=0; i < list1.length; i++)
if (!found1[i] || !found2[i])
return false;
return true;
}
// Edit distance as 0-100 percentage
function editDistancePercentage(x, y) {
return (new difflib.SequenceMatcher(null, x, y)).ratio();
}