-
Notifications
You must be signed in to change notification settings - Fork 1
/
userdb.js
49 lines (38 loc) · 1.14 KB
/
userdb.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
/**
* Super simple hard-coded user database
*/
var childProcess = require('child_process');
module.exports = function () {
var phraseMap = {};
var phoneMap = {};
var phraseSet = [];
var matchTolerance = 0.5;
return {
add: function (name, phone, phrase) {
if (phraseMap[phrase]) {
throw new Error("Phrase already exists");
}
var user = {
name: name,
phone: phone,
phrase: phrase
};
phraseMap[phrase] = user;
phoneMap[phone] = user;
phraseSet.push(phrase);
},
getByPhrase: function (phrase) {
var args = ["match.py", phrase].concat(phraseSet);
var proc = childProcess.spawnSync("python", args);
var results = JSON.parse(proc.stdout);
if (!results || results.length === 0) {
return;
}
var matchedPhrase = results[0].search;
return phraseMap[matchedPhrase];
},
getByPhone: function (phone) {
return phoneMap[phone];
}
};
};