diff --git a/package-lock.json b/package-lock.json index 858cfa3..a2e17a0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3724,8 +3724,7 @@ "lodash": { "version": "4.17.21", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", - "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", - "dev": true + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" }, "lru-cache": { "version": "6.0.0", diff --git a/package.json b/package.json index b0a344f..233a1b7 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,8 @@ }, "homepage": "https://github.com/MoveInc/coding_exercise#readme", "dependencies": { - "express": "^4.17.1" + "express": "^4.17.1", + "lodash": "^4.17.21" }, "devDependencies": { "eslint": "^7.22.0", diff --git a/src/anagram-service.js b/src/anagram-service.js index 962a3a4..4d231e1 100644 --- a/src/anagram-service.js +++ b/src/anagram-service.js @@ -1,18 +1,17 @@ -const fs = require('fs'); - +const fs = require("fs"); +const lodash = require("lodash"); /** - * Looks up anagrams of a given word based on the + * Looks up anagrams of a given word based on the * word dictionary provided in the constructor. */ class AnagramService { - /** * Creates an AnagramService instance * @param {string} dictionaryFilePath Path to the dictionary file */ constructor(dictionaryFilePath) { this.dictionaryFilePath = dictionaryFilePath; - this.wordsMap = new Map(); + this.wordsArray = []; } /** @@ -26,10 +25,10 @@ class AnagramService { return reject(err); } - const lines = data.toString().split('\n'); + const lines = data.toString().split("\n"); lines.forEach((line) => { - this.wordsMap.set(line.toLowerCase(), [line]); + this.wordsArray.push(line.toLowerCase().trim()); }); return resolve(this); }); @@ -38,16 +37,38 @@ class AnagramService { /** * Returns all anagrams for the given term - * @param {string} term The term to find anagrams for + * @param {string} term The term to find anagrams for * @returns A string[] of anagram matches */ async getAnagrams(term) { - if (!this.wordsMap || this.wordsMap.size === 0) { - throw Error('Error: Dictionary not initialized'); + if (!this.wordsArray || this.wordsArray.length === 0) { + throw Error("Error: Dictionary not initialized"); } + const termCharacterMap = this.getCharacterMap(term); + + const filteredWordsMapKeys = this.wordsArray.filter((word) => { + const hasSameLength = word.length === term.length; + if (!hasSameLength) return false; + const wordCharacterMap = this.getCharacterMap(word); + const MapsAreEqual = lodash.isEqual(wordCharacterMap, termCharacterMap); + + return hasSameLength && MapsAreEqual; + }); + + return filteredWordsMapKeys; + } - // TODO: The anagram lookup 🤦‍♂️ - return this.wordsMap.get(term); + getCharacterMap(word) { + const map = {}; + for (const index in Array.from(word).sort()) { + const character = word[index]; + if (map[character]) { + map[character]++; + } else { + map[character] = 1; + } + } + return map; } }