-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
29 lines (27 loc) · 849 Bytes
/
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
let assert = require('assert').strict;
let NearestNeighborChain = require('./index');
{
let array = [4, 90, 12, 61, 29];
let clusters = NearestNeighborChain(array, (a, b) => Math.abs(a - b));
assert.deepEqual(clusters, [[29, [4, 12]], [90, 61]]);
}
{
let intersection = function(a, b) {
a = new Set(a);
b = new Set(b);
return [...a].filter(v => b.has(v));
};
let array = ['ac', 'ab', 'baab', 'aba', 'bc'];
let clusters = NearestNeighborChain(array, (a, b) => -intersection(a, b).length);
assert.deepEqual(clusters, ['ac', 'bc', ['ab', 'baab', 'aba']]);
}
{
let array = [1, 1, 1, 1, 1];
let clusters = NearestNeighborChain(array, (a, b) => Math.abs(a - b));
assert.deepEqual(clusters, [1, 1, 1, 1, 1]);
}
{
let array = [];
let clusters = NearestNeighborChain(array, (a, b) => Math.abs(a - b));
assert.deepEqual(clusters, []);
}