Javascript [SOLVED]: What would be a good design pattern for passing in a type, going through some conditionals, and returning an object

Javascript [SOLVED]: What would be a good design pattern for passing in a type, going through some conditionals, and returning an object

Home Forums Scripting Javascript Tutorials Javascript [SOLVED]: What would be a good design pattern for passing in a type, going through some conditionals, and returning an object

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #245692

    Cloudy Point
    Keymaster

    QuestionQuestion

    const someFailedAction = (caseIds, growlMessages) => {
    
        if (caseIds.length > 1) {
            toastr.error(growlMessages[0], errorToastrOptions);
        } else if (isCaseDetailsDisplayed) {
            toastr.error(growlMessages[1], errorToastrOptions);
        } else if (errorParts.fieldIds.length === 0) {
            toastr.error(growlMessages[2], errorToastrOptions);
        } else {
            toastr.error(growlMessages[3], errorToastrOptions);
        }
    }
    

    I have a bunch of conditional statements like the one above which are executed on failures of different case actions. Most of the actions have the same if/elseIf structure, but some have an additional elseIf or a subtraction of one or more.

    const SomeOtherFailedAction = (caseIds, growlMessages) => {
    
        if (caseIds.length > 1) {
            toastr.error(growlMessages[0], errorToastrOptions);
        } else if (isCaseDetailsDisplayed) {
            toastr.error(growlMessages[1], errorToastrOptions);
        } else {
            toastr.error(growlMessages[2], errorToastrOptions);
        }
    
    }
    

    I was wondering if there was a good design pattern for which I could just pass in the type and array of messages without having a long, nested, repetitious switch statement.

    #245693

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    Just use an array of booleans and find the index that succeeeds:

    const i = [
     caseIds.length > 1,
     isCaseDetailsDisplayed,
     errorParts.fieldIds.length === 0,
     true
    ].indexOf(true);
    
    toastr.error(growlMessages[i], errorToastrOptions)
    

    Alternatively, you could store an reusable array of functions, execute one after another and return the first index where the function returns true:

    const tests = [
     () => caseIds.length > 1,
     () => isCaseDetailsDisplayed,
     () => errorParts.fieldIds.length === 0,
     () => true
    ]
    
    const i = tests.findIndex(f => f());
    
    toastr.error(growlMessages[i], errorToastrOptions)
    

    Source: https://stackoverflow.com/questions/48025318/what-would-be-a-good-design-pattern-for-passing-in-a-type-going-through-some-co
    Author: Jonas W.
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.