Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ 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 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[line.match(firstWordRegex)[1]] = line.replace(firstWordRegex, '')
words.set(line.match(firstWordRegex)[1], line.replace(firstWordRegex, ''))
}
})

moby.search = function (term) {
if (!term) return []
var result = words[term]
if (!result) result = words[term.toLowerCase()]
var result = words.get(term)
if (!result) result = words.get(term.toLowerCase())
if (!result) return []
result = result.split(',')
result = union(result, thesaurus.find(term))
Expand All @@ -29,8 +29,8 @@ moby.search = function (term) {

moby.reverseSearch = function (term) {
if (!term) return []
return Object.keys(words).filter(function (w) {
return words[w].match(new RegExp(',' + term + ',', 'i'))
return Array.from(words.keys()).filter(function (w) {
return words.get(w).match(new RegExp(',' + term + ',', 'i'))
})
}

Expand Down