-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
66 lines (61 loc) · 1.89 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
55
56
57
58
59
60
61
62
63
64
65
66
/**
* https://github.com/jeka-kiselyov/mdict
* Very rude refactoring of https://github.com/fengdh/mdict-js to make it work with node.js by Jeka Kiselyov ( https://github.com/jeka-kiselyov ).
* Done enough to make it work for my project(with predefined dictionaries). Though tested with few .mdx files only.
* There may be some bugs for other dictionaries. Please check.
* Please feel free to post pull requests with optimizations, unit tests etc.
* Released under terms of the MIT License, as original library
*/
/**
* Usage:
* var mdict = require('mdict');
*
* mdict.dictionary('dehkhoda.mdx').then(function(dictionary){
* //// dictionary is loaded
* dictionary.search({
* phrase: 'دهخدا*',
* max: 10
* }).then(function(foundWords){
* console.log('Found words:');
* console.log(foundWords);
*
* var word = ''+foundWords[0];
* console.log('Loading definitions for: '+word);
* return dictionary.lookup(word); /// typeof word === string
* }).then(function(definitions){
* console.log('definitions:');
* console.log(definitions);
* });
*
* });
*/
var mdictParser = require(__dirname+'/mdict-parser.js');
var Promise = require('bluebird');
exports.dictionary = function(filenames) {
if (!Array.isArray(filenames)) {
filenames = [filenames];
}
return new Promise(function(resolve, reject){
mdictParser.load(filenames).then(function(resources){
return resources.mdx;
}).then(function(mdx){
resolve({
lookup: function(string) {
return mdx(string);
},
search: function(params) {
if (typeof params === 'string' || params instanceof String) {
params = {
phrase: params
};
}
params = params || {};
params.phrase = params.phrase || '';
params.max = params.max || 10;
params.follow = params.follow || false;
return mdx(params);
}
});
});
});
};