-
Notifications
You must be signed in to change notification settings - Fork 0
/
left-join.js
53 lines (43 loc) · 1.39 KB
/
left-join.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
'use strict';
const hash = require('../hashtable/hashtable.js');
const synonyms = new hash.HashTable(1024);
const antonyms = new hash.HashTable(1024);
synonyms.add('fond', 'enamored');
synonyms.add('wrath', 'anger');
synonyms.add('diligent', 'employed');
synonyms.add('outfit', 'garb');
synonyms.add('guide', 'usher');
antonyms.add('fond', 'adverse');
antonyms.add('wrath', 'delight');
antonyms.add('diligent', 'idle');
antonyms.add('guide', 'follow');
antonyms.add('flow', 'jam');
const join = (leftTable, rightTable, direction) => {
let tableOne;
let tableTwo;
if (direction.toLowerCase() === 'left') {
tableOne = leftTable;
tableTwo = rightTable;
} else if (direction.toLowerCase() === 'right') {
tableOne = rightTable;
tableTwo = leftTable;
} else { throw 'error, invalid join direction'; }
const results = [];
for (let i = 0; i < tableOne.buckets.length; i++) {
const row = [];
if (tableOne.buckets[i]) {
const key = (Object.keys(tableOne.buckets[i].head.value)[0]);
const value = (tableOne.buckets[i].head.value[key]);
row[row.length] = key;
row[row.length] = value;
if (tableTwo.contains(key)) {
row[row.length] = tableTwo.get(key);
}
else { row[row.length] = null; }
results[results.length] = row;
}
}
return results;
};
// console.log(leftJoin(synonyms, antonyms, 'right'));
module.exports = join;