Skip to content

Instantly share code, notes, and snippets.

@wjvander
Last active April 4, 2018 10:37
Show Gist options
  • Save wjvander/cffd450bfe6fa7dc4b89a9c1ddc2048c to your computer and use it in GitHub Desktop.
Save wjvander/cffd450bfe6fa7dc4b89a9c1ddc2048c to your computer and use it in GitHub Desktop.

Revisions

  1. wjvander revised this gist Apr 4, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ Waits a certain amount of time, then returns with a promise

    #### Code
    ```javascript
    let waitAWhile = (timeToWait) => new Promise((resolve, reject) => setTimeout(resolve, timeToWait));
    let waitAWhile = (timeToWait) => new Promise(resolve => setTimeout(resolve, timeToWait));
    ```

    ## Arrays
  2. wjvander revised this gist Apr 4, 2018. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,13 @@
    # Javascript Cheat Sheet
    ## Snippets
    ### Promise Delay
    Waits a certain amount of time, then returns with a promise

    #### Code
    ```javascript
    let waitAWhile = (timeToWait) => new Promise((resolve, reject) => setTimeout(resolve, timeToWait));
    ```

    ## Arrays
    ### Split Array into chunks
    Does not mutate the original array
  3. wjvander revised this gist Mar 8, 2018. 1 changed file with 7 additions and 6 deletions.
    13 changes: 7 additions & 6 deletions javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -2,12 +2,7 @@
    ## Arrays
    ### Split Array into chunks
    Does not mutate the original array
    #### Usage
    ```javascript
    let someArray = [1, 2]
    let chunkSize = 10
    let chunks = chunk(someArray, chunkSize)
    ```

    #### Code
    ```javascript
    const chunk = (collection, length) => collection.length === 0
    @@ -18,6 +13,12 @@ or a more compact one-liner
    ```javascript
    const chunk = (a, l) => a.length === 0 ? [] : [a.slice(0, l)].concat(chunk(a.slice(l), l));
    ```
    #### Usage
    ```javascript
    let someArray = [1, 2]
    let chunkSize = 10
    let chunks = chunk(someArray, chunkSize)
    ```
    ### Async/Await batching of Array
    #### Description
    It runs batches synchronously, but async for each item in a batch.
  4. wjvander revised this gist Mar 8, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -95,8 +95,8 @@ const mySwitchCases = switchCase({
    })('defaultValue');

    // you can then use it as such
    let text = mySwitchCases(101); // '101'
    let text = mySwitchCases(100000); // 'defaultValue'
    let text = mySwitchCases('101'); // 'one-o-one'
    let text = mySwitchCases('100000'); // 'defaultValue'
    ```
    ## Design Patterns
    ### Chain of Responsibility
  5. wjvander revised this gist Mar 8, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -35,7 +35,7 @@ for(let batch of batches){
    ## Refactorings
    ### Switch Statements
    #### Description
    You have a switch statement, but it is ugly and not easy to maintain.
    You have a `switch` statement, but it is ugly and not easy to maintain.
    #### Original
    ```javascript
    let mySwitchCase = (code) => {
  6. wjvander revised this gist Mar 8, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ const chunk = (a, l) => a.length === 0 ? [] : [a.slice(0, l)].concat(chunk(a.sli
    ### Async/Await batching of Array
    #### Description
    It runs batches synchronously, but async for each item in a batch.
    Might be a good idea to just do a `try catch` inside the `map`.
    Might be a good idea to just do a `try...catch` inside the `map`.

    #### Code
    ```javascript
  7. wjvander revised this gist Mar 8, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,8 @@ const chunk = (a, l) => a.length === 0 ? [] : [a.slice(0, l)].concat(chunk(a.sli
    ```
    ### Async/Await batching of Array
    #### Description
    It runs batches synchronously, but async for each item in a batch
    It runs batches synchronously, but async for each item in a batch.
    Might be a good idea to just do a `try catch` inside the `map`.

    #### Code
    ```javascript
  8. wjvander revised this gist Mar 8, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -80,6 +80,8 @@ let text = myCases('10000'); // defaultValue
    #### Attempt 3
    So I like the idea of an object lookup and wrapping it in a function, which is fine for one case, but if I have multiple of these, I have to keep writing the function, the cases and the logic check...this smells a bit.
    So I can extract it even more, so that the logic is only written once for each switch case.

    This also means that the cases no longer have to be hard coded, we could load them from a JSON file, dynamically create them etc. This gives us a lot more flexible code.
    ```javascript
    // You declare a function that can return a specified key or a default value and curry the hell out of it
    const switchCase = cases => defaultCase => key =>
  9. wjvander revised this gist Mar 8, 2018. 1 changed file with 47 additions and 3 deletions.
    50 changes: 47 additions & 3 deletions javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -35,16 +35,60 @@ for(let batch of batches){
    ### Switch Statements
    #### Description
    You have a switch statement, but it is ugly and not easy to maintain.
    #### Code
    #### Original
    ```javascript
    let mySwitchCase = (code) => {
    switch (code) {
    case '101': {
    return 'one-o-one';
    }
    case '102': {
    return 'one-o-two';
    }
    default: {
    return 'defaultValue';
    }
    }
    }
    ```
    #### Attempt 1
    You could turn it into an object lookup, but then it a default value would be more complicated
    ```javascript
    let myCases = {
    '101': 'one-o-one',
    '102': 'one-o-two'
    };

    let text = myCases['101']; // one-o-one
    let text = myCases['10000'] || 'defaultValue'; // defaultValue
    ```
    ### Attempt 2
    I like the object lookup idea, but having to check if it returned a value each time just to declare a default is not nice. So I could wrap it in a function
    ```javascript
    let myCases = (code) => {
    let cases = {
    '101': 'one-o-one',
    '102': 'one-o-two'
    };

    return cases[code] || 'defaultValue';
    };

    let text = myCases('101'); // one-o-one
    let text = myCases('10000'); // defaultValue
    ```
    #### Attempt 3
    So I like the idea of an object lookup and wrapping it in a function, which is fine for one case, but if I have multiple of these, I have to keep writing the function, the cases and the logic check...this smells a bit.
    So I can extract it even more, so that the logic is only written once for each switch case.
    ```javascript
    // You declare a function that can return a specified key or a default value and curry the hell out of it
    const switchCase = cases => defaultCase => key =>
    cases.hasOwnProperty(key) ? cases[key] : defaultCase;

    // you can then declare your cases with a default like so
    const mySwitchCases = switchCase({
    101: 'one-o-one',
    102: 'one-o-two'
    '101': 'one-o-one',
    '102': 'one-o-two'
    })('defaultValue');

    // you can then use it as such
  10. wjvander revised this gist Mar 7, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    # Javascript Cheat Sheet
    ## Arrays
    ### Split Array into chunks
    Does not mutate the original array
    #### Usage
    ```javascript
    let someArray = [1, 2]
  11. wjvander revised this gist Mar 7, 2018. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,10 @@ const chunk = (collection, length) => collection.length === 0
    ? []
    : [collection.slice(0, length)].concat(chunk(collection.slice(length), length));
    ```
    or a more compact one-liner
    ```javascript
    const chunk = (a, l) => a.length === 0 ? [] : [a.slice(0, l)].concat(chunk(a.slice(l), l));
    ```
    ### Async/Await batching of Array
    #### Description
    It runs batches synchronously, but async for each item in a batch
  12. wjvander revised this gist Mar 7, 2018. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,9 @@ let chunks = chunk(someArray, chunkSize)
    ```
    #### Code
    ```javascript
    const chunk = (collection, length) => collection.length === 0 ? [] : [collection.slice(0, length)].concat(chunk(collection.slice(length), length));
    const chunk = (collection, length) => collection.length === 0
    ? []
    : [collection.slice(0, length)].concat(chunk(collection.slice(length), length));
    ```
    ### Async/Await batching of Array
    #### Description
  13. wjvander revised this gist Mar 7, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ let chunks = chunk(someArray, chunkSize)
    ```
    #### Code
    ```javascript
    const chunk = (a, l) => a.length === 0 ? [] : [a.slice(0, l)].concat(chunk(a.slice(l), l));
    const chunk = (collection, length) => collection.length === 0 ? [] : [collection.slice(0, length)].concat(chunk(collection.slice(length), length));
    ```
    ### Async/Await batching of Array
    #### Description
  14. wjvander revised this gist Mar 7, 2018. 1 changed file with 19 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,26 @@ for(let batch of batches){
    }));
    })
    ```
    ## Refactorings
    ### Switch Statements
    #### Description
    You have a switch statement, but it is ugly and not easy to maintain.
    #### Code
    ```javascript
    // You declare a function that can return a specified key or a default value and curry the hell out of it
    const switchCase = cases => defaultCase => key =>
    cases.hasOwnProperty(key) ? cases[key] : defaultCase;

    // you can then declare your cases with a default like so
    const mySwitchCases = switchCase({
    101: 'one-o-one',
    102: 'one-o-two'
    })('defaultValue');

    // you can then use it as such
    let text = mySwitchCases(101); // '101'
    let text = mySwitchCases(100000); // 'defaultValue'
    ```
    ## Design Patterns
    ### Chain of Responsibility
    #### Use cases
  15. wjvander revised this gist Mar 7, 2018. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -72,13 +72,16 @@ class MyHandler extends RequestHandler{
    }

    // How to use
    // Declare all your handlers, they can be different handlers, as long as they extend RequestHandler
    let myHandler = new MyHandler(someDependency);
    let mySecondHandler = new MyHandler(someDependecy);
    let myThirdHandler = new MyOtherHandler(someOtherDependency);
    let myThirdHandler = new MyOtherHandler();

    // Define the order in which the handlers will execute ie. pipeline
    myHandler
    .setNext(mySecondHandler)
    .setNext(myThirdHandler);

    // Start the pipeline
    myHandler.handleRequest(someRequest);
    ```
  16. wjvander revised this gist Mar 7, 2018. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -74,8 +74,11 @@ class MyHandler extends RequestHandler{
    // How to use
    let myHandler = new MyHandler(someDependency);
    let mySecondHandler = new MyHandler(someDependecy);
    let myThirdHandler = new MyOtherHandler(someOtherDependency);

    myHandler.setNext(mySecondHandler);
    myHandler
    .setNext(mySecondHandler)
    .setNext(myThirdHandler);

    myHandler.handleRequest(someRequest);
    ```
  17. wjvander revised this gist Mar 7, 2018. 1 changed file with 57 additions and 1 deletion.
    58 changes: 57 additions & 1 deletion javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -22,4 +22,60 @@ for(let batch of batches){
    await Promise.all(batch.map(async(item) => {
    await doSomeAsyncStuffForEachItemInBatch(item);
    }));
    })
    })
    ```

    ## Design Patterns
    ### Chain of Responsibility
    #### Use cases
    * When there are multiple handlers for a request but you are unsure of which one will handle the request
    * When there is a list of handlers that should handle a request
    * Handlers are dynamically defined

    #### Code
    ```javascript
    // Handler Super Class
    class RequestHandler {
    constructor(){
    this.next = {
    console.log('End of pipeline');
    return Promise.resolve();
    }
    }

    setNext(next){
    this.next = next;
    return next;
    }

    async handleRequest(request){}
    }

    // Some handler
    class MyHandler extends RequestHandler{
    constructor(someDependency){
    super();
    this.something = someDependency;
    }

    // Super class override
    async handleRequest(request){
    // do something here
    // if you want to stop the pipeline, return, otherwise pass request to next
    // This is not needed if you want all handlers to receive the request

    if (shouldStopHere)
    return;

    await this.next.handleRequest(request);
    }
    }

    // How to use
    let myHandler = new MyHandler(someDependency);
    let mySecondHandler = new MyHandler(someDependecy);

    myHandler.setNext(mySecondHandler);

    myHandler.handleRequest(someRequest);
    ```
  18. wjvander revised this gist Mar 7, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ const chunk = (a, l) => a.length === 0 ? [] : [a.slice(0, l)].concat(chunk(a.sli
    It runs batches synchronously, but async for each item in a batch

    #### Code
    ```javascrip
    ```javascript
    let batches = chunk(myArray, 10)
    for(let batch of batches){
    await Promise.all(batch.map(async(item) => {
  19. wjvander revised this gist Mar 7, 2018. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -11,3 +11,15 @@ let chunks = chunk(someArray, chunkSize)
    ```javascript
    const chunk = (a, l) => a.length === 0 ? [] : [a.slice(0, l)].concat(chunk(a.slice(l), l));
    ```
    ### Async/Await batching of Array
    #### Description
    It runs batches synchronously, but async for each item in a batch

    #### Code
    ```javascrip
    let batches = chunk(myArray, 10)
    for(let batch of batches){
    await Promise.all(batch.map(async(item) => {
    await doSomeAsyncStuffForEachItemInBatch(item);
    }));
    })
  20. wjvander revised this gist Mar 6, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -2,12 +2,12 @@
    ## Arrays
    ### Split Array into chunks
    #### Usage
    ```
    ```javascript
    let someArray = [1, 2]
    let chunkSize = 10
    let chunks = chunk(someArray, chunkSize)
    ```
    #### Code
    ```
    ```javascript
    const chunk = (a, l) => a.length === 0 ? [] : [a.slice(0, l)].concat(chunk(a.slice(l), l));
    ```
  21. wjvander revised this gist Mar 6, 2018. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,12 @@
    ## Arrays
    ### Split Array into chunks
    #### Usage
    ```let someArray = [1, 2]
    let chunkSize = 10
    let chunks = chunk(someArray, chunkSize)
    ```
    let someArray = [1, 2]
    let chunkSize = 10
    let chunks = chunk(someArray, chunkSize)
    ```
    #### Code
    `const chunk = (a, l) => a.length === 0 ? [] : [a.slice(0, l)].concat(chunk(a.slice(l), l));
    ```
    const chunk = (a, l) => a.length === 0 ? [] : [a.slice(0, l)].concat(chunk(a.slice(l), l));
    ```
  22. wjvander revised this gist Mar 6, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,6 @@
    ```let someArray = [1, 2]
    let chunkSize = 10
    let chunks = chunk(someArray, chunkSize)
    ```
    #### Code
    const chunk = (a, l) => a.length === 0 ? [] : [a.slice(0, l)].concat(chunk(a.slice(l), l));
    `const chunk = (a, l) => a.length === 0 ? [] : [a.slice(0, l)].concat(chunk(a.slice(l), l));
  23. wjvander revised this gist Mar 6, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,9 @@
    ## Arrays
    ### Split Array into chunks
    #### Usage
    let someArray = [1, 2]
    let chunkSize = 10
    let chunks = chunk(someArray, chunkSize)
    ```let someArray = [1, 2]
    let chunkSize = 10
    let chunks = chunk(someArray, chunkSize)
    #### Code
    const chunk = (a, l) => a.length === 0 ? [] : [a.slice(0, l)].concat(chunk(a.slice(l), l));
  24. wjvander created this gist Mar 6, 2018.
    10 changes: 10 additions & 0 deletions javascriptCheatSheet.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    # Javascript Cheat Sheet
    ## Arrays
    ### Split Array into chunks
    #### Usage
    let someArray = [1, 2]
    let chunkSize = 10
    let chunks = chunk(someArray, chunkSize)

    #### Code
    const chunk = (a, l) => a.length === 0 ? [] : [a.slice(0, l)].concat(chunk(a.slice(l), l));