Skip to content

Instantly share code, notes, and snippets.

@appsparkler
Last active June 8, 2021 23:29
Show Gist options
  • Select an option

  • Save appsparkler/f9bc8da266f6c6b62b40073504b42bf7 to your computer and use it in GitHub Desktop.

Select an option

Save appsparkler/f9bc8da266f6c6b62b40073504b42bf7 to your computer and use it in GitHub Desktop.

Revisions

  1. Akash revised this gist Jun 8, 2021. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion javascript-diary.markdown
    Original file line number Diff line number Diff line change
    @@ -92,4 +92,17 @@ const arr1 = reduceToDouble([1,2,3])
    const arr2 = reduceToDouble([3,4,5])
    ```
    In this approach we do not have to call the `reduce` method each time; nor the empty array `[]`.
    Instead we simply call a single method `reduceToDouble` on the `array`.
    Instead we simply call a single method `reduceToDouble` on the `array`.

    ## Tertiary operators before `if-else` statements:
    Always look for ways to simplify conditional statements with tertiary. For ex:
    ```js
    if(somethingIsTrue) {
    executeFunc8()
    } else {
    executeFunc9()
    }

    // can be replaced with:
    somethingIsTrue ? executeFunc8() : executeFunc9();
    ```
  2. Akash revised this gist Jun 7, 2021. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion javascript-diary.markdown
    Original file line number Diff line number Diff line change
    @@ -75,6 +75,7 @@ console.log(add1And2(3)) // 6
    ```

    ## Convert the reducers to functions

    ```js
    const reduceToDouble = (acc, num) => [
    ...acc,
    @@ -89,4 +90,6 @@ const arr2 = [3,4,5].reduce(reduceToDouble, []) // [6, 8, 10]
    const reduceToDouble = arr => arr.reduce((acc, num) => [...acc, num * 2])
    const arr1 = reduceToDouble([1,2,3])
    const arr2 = reduceToDouble([3,4,5])
    ```
    ```
    In this approach we do not have to call the `reduce` method each time; nor the empty array `[]`.
    Instead we simply call a single method `reduceToDouble` on the `array`.
  3. Akash revised this gist Jun 7, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions javascript-diary.markdown
    Original file line number Diff line number Diff line change
    @@ -82,6 +82,8 @@ const reduceToDouble = (acc, num) => [
    ]
    const arr1 = [1,2,3].reduce(reduceToDouble, []) // [2, 4, 6]
    const arr2 = [3,4,5].reduce(reduceToDouble, []) // [6, 8, 10]


    // Notice in the above example we have to keep pasing the empty array
    // A clearner way to do this:
    const reduceToDouble = arr => arr.reduce((acc, num) => [...acc, num * 2])
  4. Akash revised this gist Jun 7, 2021. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions javascript-diary.markdown
    Original file line number Diff line number Diff line change
    @@ -72,4 +72,19 @@ console.log(add1(2,3)) // 6
    // curry-in the first 2 arguments
    const add1And2 = add.bind(null, 1, 2);
    console.log(add1And2(3)) // 6
    ```

    ## Convert the reducers to functions
    ```js
    const reduceToDouble = (acc, num) => [
    ...acc,
    num * 2
    ]
    const arr1 = [1,2,3].reduce(reduceToDouble, []) // [2, 4, 6]
    const arr2 = [3,4,5].reduce(reduceToDouble, []) // [6, 8, 10]
    // Notice in the above example we have to keep pasing the empty array
    // A clearner way to do this:
    const reduceToDouble = arr => arr.reduce((acc, num) => [...acc, num * 2])
    const arr1 = reduceToDouble([1,2,3])
    const arr2 = reduceToDouble([3,4,5])
    ```
  5. Akash revised this gist Jun 7, 2021. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions javascript-diary.markdown
    Original file line number Diff line number Diff line change
    @@ -61,4 +61,15 @@ const newArr = arr.slice() // clones arr
    ```js
    const arr = [1,2,3,4,5];
    const reversedArray = arr.slice().reverse()
    ```

    ## `bind` for functional programs
    ```js
    const add = (x,y,z) => x + y + z
    // curry-in the first argument
    const add1 = add.bind(null, 1);
    console.log(add1(2,3)) // 6
    // curry-in the first 2 arguments
    const add1And2 = add.bind(null, 1, 2);
    console.log(add1And2(3)) // 6
    ```
  6. Akash revised this gist Jun 6, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion javascript-diary.markdown
    Original file line number Diff line number Diff line change
    @@ -51,7 +51,7 @@ After introducting `HOC`; even the `minLength` is dynamic. So now we have more
    flexibility.
    - This pattern can be very useful for new approach implementation over legacy code.

    ## Avoid mutating an array
    ## Cloning an array
    ```js
    const arr = [1,2,3,4,5]
    const newArr = arr.slice() // clones arr
  7. Akash revised this gist Jun 6, 2021. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions javascript-diary.markdown
    Original file line number Diff line number Diff line change
    @@ -51,6 +51,12 @@ After introducting `HOC`; even the `minLength` is dynamic. So now we have more
    flexibility.
    - This pattern can be very useful for new approach implementation over legacy code.

    ## Avoid mutating an array
    ```js
    const arr = [1,2,3,4,5]
    const newArr = arr.slice() // clones arr
    ```

    ## Reversing an array the functional way.
    ```js
    const arr = [1,2,3,4,5];
  8. Akash revised this gist Jun 6, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion javascript-diary.markdown
    Original file line number Diff line number Diff line change
    @@ -51,7 +51,7 @@ After introducting `HOC`; even the `minLength` is dynamic. So now we have more
    flexibility.
    - This pattern can be very useful for new approach implementation over legacy code.

    ## Copy of an array
    ## Reversing an array the functional way.
    ```js
    const arr = [1,2,3,4,5];
    const reversedArray = arr.slice().reverse()
  9. Akash revised this gist Jun 6, 2021. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion javascript-diary.markdown
    Original file line number Diff line number Diff line change
    @@ -49,4 +49,10 @@ const greaterThan20Words = createLengthTest(20) // etc.
    - Earlier only the `word` argument was dynamic; wheras the minLength was fixed.
    After introducting `HOC`; even the `minLength` is dynamic. So now we have more
    flexibility.
    - This pattern can be very useful for new approach implementation over legacy code.
    - This pattern can be very useful for new approach implementation over legacy code.

    ## Copy of an array
    ```js
    const arr = [1,2,3,4,5];
    const reversedArray = arr.slice().reverse()
    ```
  10. Akash revised this gist Jun 6, 2021. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions javascript-diary.markdown
    Original file line number Diff line number Diff line change
    @@ -35,15 +35,18 @@ submitForm({
    ```

    # Higher Order Functions

    ```js
    // without HOC
    const greaterThan5Words = string => string.length > 5;
    const greaterThan5Words = word => word.length > 5;
    // We may also want to have a dynamic length. To do that we can create an HOC:
    const createLengthTest = minLength => word => word.length > minLength
    // Now we can re-write our greaterThan5Words
    const greaterThan5WordsNew = createLengthTest(5)
    // The added advantage of this is that now we can create more variants:
    const greaterThan3Words = createLengthTest(6)
    const greaterThan20Words = createLengthTest(20) // etc.
    ```
    ```
    - Earlier only the `word` argument was dynamic; wheras the minLength was fixed.
    After introducting `HOC`; even the `minLength` is dynamic. So now we have more
    flexibility.
    - This pattern can be very useful for new approach implementation over legacy code.
  11. Akash revised this gist Jun 6, 2021. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions javascript-diary.markdown
    Original file line number Diff line number Diff line change
    @@ -40,10 +40,10 @@ submitForm({
    // without HOC
    const greaterThan5Words = string => string.length > 5;
    // We may also want to have a dynamic length. To do that we can create an HOC:
    const minLengthTest = minLength => word => word.length > minLength
    const createLengthTest = minLength => word => word.length > minLength
    // Now we can re-write our greaterThan5Words
    const greaterThan5WordsNew = minLengthTest(5)
    const greaterThan5WordsNew = createLengthTest(5)
    // The added advantage of this is that now we can create more variants:
    const greaterThan3Words = minLengthTest(6)

    const greaterThan3Words = createLengthTest(6)
    const greaterThan20Words = createLengthTest(20) // etc.
    ```
  12. Akash revised this gist Jun 6, 2021. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions javascript-diary.markdown
    Original file line number Diff line number Diff line change
    @@ -32,4 +32,18 @@ submitForm({
    onSuccess: () => notify("succes", "Form is submitted"),
    onFailure
    })
    ```

    # Higher Order Functions

    ```js
    // without HOC
    const greaterThan5Words = string => string.length > 5;
    // We may also want to have a dynamic length. To do that we can create an HOC:
    const minLengthTest = minLength => word => word.length > minLength
    // Now we can re-write our greaterThan5Words
    const greaterThan5WordsNew = minLengthTest(5)
    // The added advantage of this is that now we can create more variants:
    const greaterThan3Words = minLengthTest(6)

    ```
  13. Akash revised this gist Jun 5, 2021. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion javascript-diary.markdown
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,9 @@ const submitFormDigio = ({payload, onSuccess, onFailure}) => {
    onFailure
    })
    }
    // implementation
    // implementation - note that instead of handling notification inside
    // submitForm method; we are handling it outside of it. This is one way
    // how callbacks helps us in SRP
    const onFailure = (err) => setErrors([err])
    submitForm({
    payload: {},
  14. Akash revised this gist Jun 5, 2021. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion javascript-diary.markdown
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,10 @@
    # Javascript Diary

    ## Callbacks, FP
    Callbacks are a great tool to achieve SRP. For ex. the following function is responsbile for `submitting forms` and nothing else.

    ```js
    // SRP
    const submitForm = ({apiUrl, payload, onSuccess, onFailure}) => {
    fetch(apiUrl, payload)
    .then((res) => {
    @@ -11,7 +14,7 @@ const submitForm = ({apiUrl, payload, onSuccess, onFailure}) => {
    onFailure(err);
    })
    }
    // submit to digio-api
    // submit to digio-api - functional programming
    const submitFormDigio = ({payload, onSuccess, onFailure}) => {
    submitForm({
    apiUrl: "https://digio.com/api/submit",
    @@ -20,4 +23,11 @@ const submitFormDigio = ({payload, onSuccess, onFailure}) => {
    onFailure
    })
    }
    // implementation
    const onFailure = (err) => setErrors([err])
    submitForm({
    payload: {},
    onSuccess: () => notify("succes", "Form is submitted"),
    onFailure
    })
    ```
  15. Akash revised this gist Jun 5, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion javascript-diary.markdown
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Javascript Diary

    ## Callbacks
    ## Callbacks, FP
    ```js
    const submitForm = ({apiUrl, payload, onSuccess, onFailure}) => {
    fetch(apiUrl, payload)
  16. Akash revised this gist Jun 5, 2021. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions javascript-diary.markdown
    Original file line number Diff line number Diff line change
    @@ -11,4 +11,13 @@ const submitForm = ({apiUrl, payload, onSuccess, onFailure}) => {
    onFailure(err);
    })
    }
    // submit to digio-api
    const submitFormDigio = ({payload, onSuccess, onFailure}) => {
    submitForm({
    apiUrl: "https://digio.com/api/submit",
    payload,
    onSuccess,
    onFailure
    })
    }
    ```
  17. Akash revised this gist Jun 5, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions javascript-diary.markdown
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,8 @@

    ## Callbacks
    ```js
    const submitForm = () => {
    fetch('api-url')
    const submitForm = ({apiUrl, payload, onSuccess, onFailure}) => {
    fetch(apiUrl, payload)
    .then((res) => {
    onSucces(res)
    })
  18. Akash revised this gist Jun 5, 2021. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion javascript-diary.markdown
    Original file line number Diff line number Diff line change
    @@ -1 +1,14 @@
    # There are times
    # Javascript Diary

    ## Callbacks
    ```js
    const submitForm = () => {
    fetch('api-url')
    .then((res) => {
    onSucces(res)
    })
    .catch(err => {
    onFailure(err);
    })
    }
    ```
  19. Akash created this gist Jun 5, 2021.
    1 change: 1 addition & 0 deletions javascript-diary.markdown
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    # There are times