-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathautocomplete.js
More file actions
40 lines (30 loc) · 767 Bytes
/
Copy pathautocomplete.js
File metadata and controls
40 lines (30 loc) · 767 Bytes
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
'use strict';
var fs = require('fs');
var byline = require('byline');
var debug = require('debug')('autocomplete');
var Trie = require('./lib/trie');
var DEFAULT_DICTIONARY = '/usr/share/dict/words';
function autoComplete(opts, cb) {
var stream;
if (!cb) {
cb = opts;
opts = {};
}
var dictionary = opts.dictionary || DEFAULT_DICTIONARY;
try {
debug('Starting to read dictionary');
stream = byline(fs.createReadStream(dictionary, { encoding: 'utf8' }));
} catch (err) {
return cb(err);
}
var trie = new Trie();
debug('Starting to add words in trie');
stream.on('data', function(line) {
trie.add(line);
});
stream.on('end', function(line) {
debug('Dictionary read completed');
cb(null, trie);
});
}
module.exports = autoComplete;