Skip to content

Instantly share code, notes, and snippets.

@DrBoolean
Last active October 27, 2020 09:57
Show Gist options
  • Save DrBoolean/66755c2f9572336686e07dad848d4e9a to your computer and use it in GitHub Desktop.
Save DrBoolean/66755c2f9572336686e07dad848d4e9a to your computer and use it in GitHub Desktop.

Revisions

  1. Brian Lonsdorf revised this gist Sep 20, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fp_arch.js
    Original file line number Diff line number Diff line change
    @@ -46,6 +46,6 @@ const sendSpam = tpl => tpl.send()
    const joinWithSpace = (x, y) => [x, y].join(' ')
    module.exports = {joinWithSpace}

    // User was literally just the json we got from the server
    // User was literally just the json we got from the server or db
    // Spam did nothing other than delegate to Emailer

  2. Brian Lonsdorf revised this gist Sep 20, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fp_arch.js
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,7 @@ const sendSpam = tpl => tpl.send()
    /* 3) Generalize names, reorganize */

    // format.js
    const joinWithSpace = (first, last) => [first, last].join(' ')
    const joinWithSpace = (x, y) => [x, y].join(' ')
    module.exports = {joinWithSpace}

    // User was literally just the json we got from the server
  3. Brian Lonsdorf created this gist Sep 20, 2018.
    51 changes: 51 additions & 0 deletions fp_arch.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    // Simple example, but the idea holds for more complex objects.


    /* 1) Start with OO */

    // user.js
    class User {
    constructor(firstName, lastName, email) {
    this.firstName = firstName
    this.lastName = lastName
    this.email = email
    }
    fullName() {
    return [this.firstName, this.lastName].join(' ')
    }
    serialize() {
    return {firstName: this.firstName, lastName: this.lastName, email: this.email}
    }
    spam() {
    Emailer.reminderTpl(this.serialize).send()
    }
    }

    export default User


    /* 2) Next we make constructors into factories, methods into fns, and separate actions from calcs */



    // user.js
    const User = (firstName, lastName, email) => ({firstName, lastName, email})

    const fullname = (firstName, lastName) => [firstName, lastName].join(' ')

    const spamTemplate = user => Emailer.reminderTpl(user)

    const sendSpam = tpl => tpl.send()




    /* 3) Generalize names, reorganize */

    // format.js
    const joinWithSpace = (first, last) => [first, last].join(' ')
    module.exports = {joinWithSpace}

    // User was literally just the json we got from the server
    // Spam did nothing other than delegate to Emailer