Skip to content

Instantly share code, notes, and snippets.

@ellockie
Last active April 2, 2020 17:12
Show Gist options
  • Save ellockie/ecc4fe404df023c503ab00094cc1e46b to your computer and use it in GitHub Desktop.
Save ellockie/ecc4fe404df023c503ab00094cc1e46b to your computer and use it in GitHub Desktop.
// the generator singleton
const generatorSingleton = (() => {
function getUniqueInstance() {
if (!instance) {
instance = this.getLetterRepresenations();
}
return instance;
}
let instance = null;
return {getUniqueInstance};
})();
// art reverse-mapper
function getLetterFromArt(representation) {
const generatorInstance = generatorSingleton.getUniqueInstance.bind(this)();
return generatorInstance[representation] ? generatorInstance[representation] : '?';
}
// approximates the testing environment
function runTestingEnv() {
// hidden function
this.getLetterRepresenations = () => {
console.log('G E N E R A T I N G L E T T E R S...');
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const representations = {};
letters.split('')
// reverse-mapped 'art' representation
.map(letter => representations[letter + letter + letter] = letter);
return representations;
}
console.log(getLetterFromArt.call(this, 'AAA')); // G E N E R A T I N G L E T T E R S...
// A
console.log(getLetterFromArt.call(this, 'BBB')); // B
console.log(getLetterFromArt.call(this, 'XXX')); // X
console.log(getLetterFromArt.call(this, 'Who am I?')); // ?
}
runTestingEnv();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment