Skip to content

Instantly share code, notes, and snippets.

@karlbright
Created November 26, 2015 05:31
Show Gist options
  • Save karlbright/b602015a9c47a1b17454 to your computer and use it in GitHub Desktop.
Save karlbright/b602015a9c47a1b17454 to your computer and use it in GitHub Desktop.
var _ = require('lodash');
var fs = require('fs');
var path = require('path');
var byline = require('byline');
var anagrams = {}, matchers = [];
function findAnagrams (filePath, cb) {
byline(fs.createReadStream(filePath, { encoding: 'utf8' }))
.on('data', process)
.on('end', function() {
cb(removeEmptyAnagrams())
})
}
function process (word) {
var sortedWord = sortLettersAlphabetically(word)
if (anagrams[sortedWord]) {
anagrams[sortedWord].push(word)
} else {
anagrams[sortedWord] = [word]
}
}
function sortLettersAlphabetically (word) {
return word.toLowerCase().split("").sort().join("")
}
module.exports = findAnagrams;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment