Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coding exercise completed by Karl Tiedt #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
114 changes: 71 additions & 43 deletions src/anagram-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,77 @@ const fs = require('fs');
*/
class AnagramService {

/**
* Creates an AnagramService instance
* @param {string} dictionaryFilePath Path to the dictionary file
*/
constructor(dictionaryFilePath) {
this.dictionaryFilePath = dictionaryFilePath;
this.wordsMap = new Map();
}

/**
* Parses and loads the dictionary
* @returns Promise containing the initialized AnagramService
*/
async loadDictionary() {
return new Promise((resolve, reject) => {
fs.readFile(this.dictionaryFilePath, (err, data) => {
if (err) {
return reject(err);
}

const lines = data.toString().split('\n');

lines.forEach((line) => {
this.wordsMap.set(line.toLowerCase(), [line]);
});
return resolve(this);
});
});
}

/**
* Returns all anagrams for the given term
* @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');
}

// TODO: The anagram lookup 🤦‍♂️
return this.wordsMap.get(term);
}
/**
* Creates an AnagramService instance
* @param {string} dictionaryFilePath Path to the dictionary file
*/
constructor(dictionaryFilePath) {
this.dictionaryFilePath = dictionaryFilePath;
this.wordsMap = new Map();
this.lines = [];
}

/**
* Parses and loads the dictionary
* @returns Promise containing the initialized AnagramService
*/
async loadDictionary() {
return new Promise((resolve, reject) => {
fs.readFile(this.dictionaryFilePath, (err, data) => {
if (err) {
return reject(err);
}

// const lines = this.lines = data.toString().split('\n');
this.lines = data.toString().split('\n');

// lines.forEach((line) => {
// this.wordsMap.set(line.toLowerCase(), this.processAnagrams(line));
// });

return resolve(this);
});
});
}

processAnagrams(word) {
const letters = word.split('');
// test is the word being checked from the original lines array.
// word is the current word we are finding anagrams for.
const results = this.lines.filter((test) => {
if (word.length === test.length) {
const tested = {};
return letters.every((letter) => {
if (typeof tested[letter] === 'undefined') {
const duplicatesRE = new RegExp(`${letter}`, 'ig');
const duplicatesTest = test.match(duplicatesRE) || [];
const duplicatesLine = word.match(duplicatesRE) || [];
tested[letter] = duplicatesTest.length === duplicatesLine.length;
}

return tested[letter];
});
}

return false;
});

// console.log(`Matches for ${word}: `, results.join(', '));
return results;
}
/**
* Returns all anagrams for the given term
* @param {string} term The term to find anagrams for
* @returns A string[] of anagram matches
*/
async getAnagrams(term) {
if (!this.lines || this.lines.length === 0) {
throw Error('Error: Dictionary not initialized');
}

// TODO: The anagram lookup 🤦‍♂️
return this.processAnagrams(term);
}
}

module.exports = AnagramService;