ortense
6/2/2017 - 12:12 AM

A simple experiment with fluent interface in javascript

A simple experiment with fluent interface in javascript

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_