UNPKG

1.2 kBJavaScriptView Raw
1'use strict';
2
3const fs = require('fs');
4
5const { uniq } = require('../helpers/array');
6const { jsonStringify } = require('../helpers/string');
7const { consoleError, consoleInfo } = require('../helpers/console');
8
9const filename = 'yaspeller_error_dictionary.json';
10
11module.exports = {
12 name: 'error_dictionary',
13 onComplete(data) {
14 let buffer = [];
15
16 data.forEach(function(el) {
17 const error = el[0];
18 const typos = el[1];
19
20 if (!error) {
21 typos.data.forEach(function(typo) {
22 if (typo.word) {
23 buffer.push(typo.word);
24 }
25 });
26 }
27 });
28
29 buffer = uniq(buffer).sort(function(a, b) {
30 a = a.toLowerCase();
31 b = b.toLowerCase();
32
33 if (a > b) {
34 return 1;
35 }
36 if (a < b) {
37 return -1;
38 }
39
40 return 0;
41 });
42
43 try {
44 fs.writeFileSync(filename, jsonStringify(buffer));
45 consoleInfo(`JSON dictionary with typos: ./${filename}`);
46 } catch (e) {
47 consoleError(e);
48 }
49 }
50};