Skip to content

Instantly share code, notes, and snippets.

@wesbos
Created January 11, 2022 15:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesbos/166c2dd6be4952bb894780b7067d3003 to your computer and use it in GitHub Desktop.
Save wesbos/166c2dd6be4952bb894780b7067d3003 to your computer and use it in GitHub Desktop.
interface Without {
without(withoutWord: string): boolean;
}
// 1. Easiest / the way I'd do it
function youCantSpell(word: string): Without {
return {
without(withoutWord: string) {
return word.includes(withoutWord);
}
}
}
// 2. Using bind
function youCantSpell(word: string): Without {
return {
without: String.prototype.includes.bind(word)
}
}
// 3. Hot Shot Arrow function
const youCantSpell = (word: string): Without => ({ without: (withoutWord: string) => word.includes(withoutWord) });
console.log(youCantSpell('awesome').without('wes')); // true
console.log(youCantSpell('functions').without('fun')); // true
console.log(youCantSpell('families').without('lies')); // true
console.log(youCantSpell('pneumonoultramicroscopicsilicovolcanoconiosis').without('volcano')); // true
console.log(youCantSpell('hottie').without('scottie')); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment