-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
109 lines (91 loc) · 2.44 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var fetch = require('node-fetch')
var unified = require('unified')
var parse = require('rehype-parse')
var $ = require('hast-util-select')
var toString = require('hast-util-to-string')
module.exports = lookup
var agent =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
var own = {}.hasOwnProperty
var base = 'https://www.powerthesaurus.org'
var kinds = [
'synonyms',
'antonyms',
'related',
'narrower',
'broader',
'sound_like',
'similar',
'rhymes'
]
var shortPartToPart = {
'adj.': 'adjective',
'adv.': 'adverb',
'conj.': 'conjunction',
'exp.': 'expression',
'idi.': 'idiom',
'int.': 'interjection',
'n.': 'noun',
'phr.': 'phrase',
'phr. v.': 'phrasal verb',
'pr.': 'pronoun',
'prep.': 'preposition',
'v.': 'verb'
}
var processor = unified().use(parse)
function lookup(word, kind, callback) {
if (typeof kind !== 'string') {
callback = kind
kind = 'synonyms'
}
if (!callback) {
return new Promise(executor)
}
executor(null, callback)
function executor(resolve, reject) {
if (!kinds.includes(kind)) {
return done(new Error('Unexpected invalid kind `' + kind + '`'))
}
fetch(base + '/' + encodeURIComponent(word) + '/' + kind, {
headers: {'user-agent': agent}
})
.then(onresponse)
.then(onbody, done)
function onresponse(response) {
return response.text()
}
function onbody(body) {
var tree = processor.parse(body)
done(null, $.selectAll('main .k3_b', tree).map(each))
}
function done(err, results) {
/* istanbul ignore if - site never seems to fail */
if (err) {
reject(err)
} else if (resolve) {
resolve(results)
} else {
callback(null, results)
}
}
}
function each(node) {
var links = $.selectAll('a', node)
var serialized = links.map(serialize)
var word = serialized.shift()
var topics = serialized.filter((d) => !/\.$/.test(d))
var parts = serialized.filter((d) => /\.$/.test(d)).map(part)
return {word, parts, topics}
}
function part(value) {
/* istanbul ignore if - this may happen in the future if they add more values */
if (!own.call(shortPartToPart, value)) {
console.warn('powerthesaurus: could not map `%s` to part', value)
return
}
return shortPartToPart[value]
}
function serialize(node) {
return toString(node).trim()
}
}