Skip to content

Instantly share code, notes, and snippets.

@matutter
Created September 13, 2017 02:59
Show Gist options
  • Select an option

  • Save matutter/1a5b3aa8b444bac7b0e7dd7bf56b7950 to your computer and use it in GitHub Desktop.

Select an option

Save matutter/1a5b3aa8b444bac7b0e7dd7bf56b7950 to your computer and use it in GitHub Desktop.
Random acronym generator
var english_words = require('an-array-of-english-words');
function lookup(word) {
return english_words
.filter(x => { return x.startsWith(word); })
.sort((a, b) => { return 0.5 - Math.random(); });
}
function suggest(word, count, cb, constants) {
let splitter = new RegExp(`.{1,${count}}`,'g');
let parts = word.match(splitter);
let display = parts.join('-')
console.log('Generating suggestions for', display);
let lists = parts.map(lookup);
let limit = lists
.map(x => x.length)
.reduce((a, b) => {
return Math.min(a,b);
});
for(let i = 0; i < limit; i++) {
let selection = lists.map(x => x[i]);
if(constants && constants.length) {
let location = selection.length - constants.length;
selection = selection.splice(constants.length, location);
selection = constants.concat(selection);
}
cb(selection);
}
console.log(`Displaying ${limit} suggestions for ${display}`);
}
function on_result(words) {
console.log(words);
}
let word = "test";
suggest(word, 2, on_result, []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment