Created
June 2, 2017 00:12
-
-
Save ortense/ed37c97a64c7ef7aeef1fb3ab48b17cc to your computer and use it in GitHub Desktop.
A simple experiment with fluent interface in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const lower = str => str.toLowerCase() | |
const upper = str => str.toUpperCase() | |
const trim = str => str.trim() | |
const exclaim = str => `${str}!!` | |
const string = str => ({ | |
get upper() { return string(upper(str)) }, | |
get lower() { return string(lower(str)) }, | |
get trim() { return string(trim(str)) }, | |
get exclaim() { return string(exclaim(str)) }, | |
prefix(token) { return string(`${token}${str}`) }, | |
sufix(token) { return string(`${str}${token}`) }, | |
result() { return str } | |
}) | |
console.log(string(' foo ').trim.upper.exclaim.result()) // FOO!! | |
console.log(string('BAR').lower.prefix('_').sufix('_').result()) // _bar_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment