Skip to content

Instantly share code, notes, and snippets.

@jondcallahan
Created September 21, 2020 21:52
Show Gist options
  • Save jondcallahan/d92b62d4a96a7158a5a701f6921dfa14 to your computer and use it in GitHub Desktop.
Save jondcallahan/d92b62d4a96a7158a5a701f6921dfa14 to your computer and use it in GitHub Desktop.
Turns JavaScript using optional chaining back to duck typed `&&` syntax
function optToDuck(string) {
const parts = string.split("?.");
let duck = "";
for (let i = 1; i <= parts.length;i++) {
for (let j = 0; j < i; j++) {
duck += `${parts[j]}`
if (j !== i -1) {
duck += '.'
}
}
if (i !== parts.length) {
duck += " && "
}
}
return duck;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment