Skip to content

Instantly share code, notes, and snippets.

@rauschma
Created December 8, 2019 21:25
Show Gist options
  • Save rauschma/9bcbc3b93727119aff65cc724a9749b9 to your computer and use it in GitHub Desktop.
Save rauschma/9bcbc3b93727119aff65cc724a9749b9 to your computer and use it in GitHub Desktop.
function createStringBuilder() {
let str = '';
return function self(key) {
switch (key) {
case 'append':
return (s) => {
str += s;
return self;
};
case 'toString':
return () => str;
default:
throw new TypeError('Unknown key: ' + key);
}
};
}
const sb = createStringBuilder();
assert.equal(
sb('append')('Hello')('append')(' world!')('toString')(),
'Hello world!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment