-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
55 lines (33 loc) · 1.03 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var fs = require('fs');
var ac = {};
ac.import = function(callback){ // assigning a fuction to a property > so that it can be used later on
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){ // utf8 -> gives the data in a string so you dont need to convert it to a string
ac.words = data.split('\n');
//console.log(data);
return callback(err, 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= [];
for(var i = 0; i < ac.words.length; i++){
if(ac.words[i].search(word) > -1){ // if want to search the beginning should be 0 --> -1 anywhere
found.push(ac.words[i]);
}
}
return callback(null, found);
}
module.exports = ac;