-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.js
executable file
·56 lines (49 loc) · 1.45 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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const thesaurus = require('thesaurus')
const union = require('lodash.union')
const words = {}
const firstWordRegex = /^([\w-]+),/
const moby = module.exports = {}
fs.readFileSync(path.join(__dirname, 'words.txt'))
.toString()
.split('\n')
.forEach(function (line) {
if (line.match(firstWordRegex)) {
words[line.match(firstWordRegex)[1]] = line.replace(firstWordRegex, '')
}
})
moby.search = function (term) {
if (!term) return []
let result = words[term]
if (!result) result = words[term.toLowerCase()]
if (!result) return []
result = result.split(',')
result = union(result, thesaurus.find(term))
return result
}
moby.reverseSearch = function (term) {
if (!term) return []
return Object.keys(words).filter(function (w) {
return words[w].match(new RegExp(',' + term + ',', 'i'))
})
}
if (!module.parent) {
if (process.argv.length < 3) {
console.log('\nUsage: moby <term>\n')
} else if (process.argv.length >= 3) {
const word = process.argv.slice(2).join(' ')
const searchResults = moby.search(word)
const reverseSearchResults = moby.reverseSearch(word)
if (searchResults.length > 0) {
console.log('\n' + searchResults.join(', '))
} else {
console.log('\nNo match found')
}
if (reverseSearchResults.length > 0) {
console.log('\nSee also:')
console.log(reverseSearchResults.join(', '))
}
}
}