From 4048a7fc7732e50101e02d706ebbfc620c73d45e Mon Sep 17 00:00:00 2001 From: Nordth Date: Tue, 20 Nov 2018 21:07:13 +0400 Subject: [PATCH 1/3] Fix moby.search() with reserved words Now `moby.search('constructor')` gives an error because `constructor` is standart object property. Instead of using `{}` for `words` i suggest to use `Map` --- index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index f159622..596e67b 100755 --- a/index.js +++ b/index.js @@ -4,7 +4,7 @@ var fs = require('fs') var path = require('path') var thesaurus = require('thesaurus') var union = require('lodash.union') -var words = {} +var words = new Map() var firstWordRegex = new RegExp(/^([\w\-]+),/) var moby = module.exports = {} @@ -13,14 +13,14 @@ fs.readFileSync(path.join(__dirname, 'words.txt')) .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) words.get(term.toLowerCase()) if (!result) return [] result = result.split(',') result = union(result, thesaurus.find(term)) @@ -29,7 +29,7 @@ moby.search = function (term) { moby.reverseSearch = function (term) { if (!term) return [] - return Object.keys(words).filter(function (w) { + return Array.from(words.keys()).filter(function (w) { return words[w].match(new RegExp(',' + term + ',', 'i')) }) } From 11af5e2cf83388c3fee71690d0f5dcda3c42d659 Mon Sep 17 00:00:00 2001 From: Nordth Date: Tue, 20 Nov 2018 21:23:11 +0400 Subject: [PATCH 2/3] Fix moby.search() and moby.reverseSearch() --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 596e67b..d3ecd4f 100755 --- a/index.js +++ b/index.js @@ -20,7 +20,7 @@ fs.readFileSync(path.join(__dirname, 'words.txt')) moby.search = function (term) { if (!term) return [] var result = words.get(term) - if (!result) words.get(term.toLowerCase()) + if (!result) result = words.get(term.toLowerCase()) if (!result) return [] result = result.split(',') result = union(result, thesaurus.find(term)) @@ -30,7 +30,7 @@ moby.search = function (term) { moby.reverseSearch = function (term) { if (!term) return [] return Array.from(words.keys()).filter(function (w) { - return words[w].match(new RegExp(',' + term + ',', 'i')) + return words.get(w).match(new RegExp(',' + term + ',', 'i')) }) } From d4f89623121de5a8276f2502ff9fdb07cb408ead Mon Sep 17 00:00:00 2001 From: Nordth Date: Tue, 20 Nov 2018 21:29:57 +0400 Subject: [PATCH 3/3] Fix: Unnecessary escape character: \-. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index d3ecd4f..3dbff2d 100755 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ var path = require('path') var thesaurus = require('thesaurus') var union = require('lodash.union') var words = new Map() -var firstWordRegex = new RegExp(/^([\w\-]+),/) +var firstWordRegex = new RegExp(/^([\w-]+),/) var moby = module.exports = {} fs.readFileSync(path.join(__dirname, 'words.txt'))