-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·56 lines (49 loc) · 1.47 KB
/
index.js
File metadata and controls
executable file
·56 lines (49 loc) · 1.47 KB
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 = new Map()
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.set(line.match(firstWordRegex)[1], line.replace(firstWordRegex, ''))
}
})
moby.search = function (term) {
if (!term) return []
var result = words.get(term)
if (!result) result = words.get(term.toLowerCase())
if (!result) return []
result = result.split(',')
result = union(result, thesaurus.find(term))
return result
}
moby.reverseSearch = function (term) {
if (!term) return []
return Array.from(words.keys()).filter(function (w) {
return words.get(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(', '))
}
}
}