-
Notifications
You must be signed in to change notification settings - Fork 93
/
spellChecker.js
60 lines (54 loc) · 1.78 KB
/
spellChecker.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
const checker = require('spellchecker')
const fs = require('fs')
const yaml = require('js-yaml')
const strip = require('remark-plain-text')
const remark = require('remark')
// Extend the dictionary with our own accepted words
const acceptedWords = fs.readFileSync('./v2.0/dictionary/accepted_words.yml')
let words = yaml.load(acceptedWords)
words.forEach(checker.add)
/**
* Extracts all titles and descriptions from a specification
*/
const extract = (item, corpus=[], parents=[]) => {
if (!item) { return corpus }
Object.keys(item).forEach(key => {
// Loop every item
let value = item[key]
// Extract every description and title with String values and add them to the list
if (['description', 'title'].includes(key) && typeof(value) === 'string') {
value = value.replace(/\s+/g, ' ')
let path = [...parents, key].join('.')
corpus.push({ path, value })
// Otherwise, check if the item is an object and recursively continue searching
} else if (typeof(value) === 'object') {
extract(value, corpus, [...parents, key])
}
})
return corpus
}
/**
* Checks the spelling for an item
* @param {Object} item the item to check
*/
const check = async (item) => {
return new Promise((resolve, reject) => {
remark().use(strip).process(item.value, (error, result) => {
if (error) { reject(error) }
item.plain_value = String(result).replace(/\n/g, " ").trim()
item.checks = checker.checkSpelling(item.plain_value)
resolve(item)
})
})
}
/**
* Spell checks a file's titles and descriptions
*/
class SpellChecker {
async check(specification) {
let parts = extract(specification)
let checks = await Promise.all(parts.map(check))
return checks.filter(item => item.checks.length > 0)
}
}
module.exports = new SpellChecker()