-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (34 loc) · 1.1 KB
/
index.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 fs = require('fs');
var ac = {};
ac.import = function(callback){ // saving import fct as a property of ac{}
if(!callback || typeof callback !== 'function'){
return new Error('callback argument MUST be a function');
}
var filename = __dirname + '/words.txt';
fs.readFile(filename, 'utf8', function(err, data){ // fs.readFile(myFile, ['utf8'], callback)
ac.words = data.split('\n'); // stores array of words in ac{}
return callback(err, ac.words); // call callback function on ac.words
});
}
ac.stats = function(word, callback){
if (!ac.searches){
ac.searches = {};
}
if (!ac.searches[word]){
ac.searches[word] = [];
}
ac.searches[word].push(new Date().getTime());
callback(null, ac.searches);
}
ac.findWord = function (word, callback){
//var found = ac.words[ac.words.indexOf(word)];
var found = [];
for (var i = 0; i < ac.words.length; i++){
if (ac.words[i].search(word) === 0){
found.push(ac.words[i]);
}
}
return callback(null,found);
}
// record timestamp at which a search was made
module.exports = ac;// exporting ac object to use elsewhere