forked from words/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·56 lines (49 loc) · 1.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
#!/usr/bin/env node
var fs = require('fs')
var path = require('path')
var thesaurus = require('thesaurus')
var union = require('lodash.union')
var words = {}
var firstWordRegex = new RegExp(/^([\w\-]+),/)
var 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 []
var 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) {
var word = process.argv.slice(2).join(' ')
var searchResults = moby.search(word)
var 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(', '))
}
}
}