-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvoikko.js
82 lines (64 loc) · 2.09 KB
/
voikko.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'use strict';
let native = require('bindings')('voikko');
function ensureString(param) {
if (typeof param !== 'string') {
throw new TypeError(`Expected a string parameter, but 1st parameter is of type ${typeof param}.`);
}
}
class Voikko {
constructor(language, path) {
if (language instanceof native.Dictionary) {
this.native = new native.Voikko(language.language, language.path);
} else {
this.native = new native.Voikko(language, path);
}
}
static listDictionaries(paths) {
paths = paths ? paths : [''];
if (typeof paths === 'string') {
paths = [paths];
} else if (!Array.isArray(paths)) {
throw new TypeError(
`Expected a string or an array of strings as parameter, but 1st parameter is of type ${typeof paths}.`
);
}
return constructDictionaries([].concat(...initDictionarySearches(paths)));
}
static nativeInterfaces() { return native; }
check(word) {
ensureString(word);
return this.native.CheckUTF16(word);
}
suggest(word) {
ensureString(word);
return this.native.SuggestionsForUTF16(word);
}
hyphenate(word) {
ensureString(word);
return this.native.HyphenateUTF16(word);
}
analyze(word) {
ensureString(word);
return this.native.AnalyseUTF16(word);
}
}
function constructDictionaries(paramLists) {
let dictionaries = new Map();
for (let params of paramLists) {
if (!dictionaries.has(params[0])) {
dictionaries.set(params[0], new native.Dictionary(...params));
}
}
return Array.from(dictionaries.values());
}
function initDictionarySearches(paths) {
let paramLists = [];
for (let path of paths) {
if (typeof path !== 'string') {
throw new TypeError(`Expected an array of strings but the array contained a non-string object ${path}.`);
}
paramLists.push(native.Dictionary.FindDictionaries(path));
}
return paramLists;
}
module.exports = Voikko;