skip to Main Content

Currently there is no bugs or any bad issues going on with this, but everytime I try to add new data to it then it does break.
It also looks very messy.
Is there any better way to do this?

(
                                                                    s==="FFP"?o.ffp:(
                                        s==='XP'? (await getXP(index,true))||o.e.xpLvl*o.e.xp:(
                                            s==="FSC"?o.fsc:(
                                                s==='BGP'?o.bgp:(
                                                    s=== "BTN"?btns[index]||0:(
                                                        s==='CIV'?await getCiv(index)||0:(
                                                            s==="TIQ"?await getTotalInv(index)||0:(
                                                                s==='IS1'?isold[index]||0:(
                                                                    s==="ITO"?acn[index].cache.itemsObtained||0:(
                                                                        s==='FLW'?flws[index]||0:(
                                                                            s==="PLY"?acn[index].playtime||0:(   
                                                                                s==="QSC"?(qusc[index]?qusc[index].completed:0):(
                                                                                    s==='QSR'?(qusc[index]?qusc[index].rankings:0):(
                                                                                        s==="DXP"?(dbs.xp[index]||0):(
                                                                                            s==='FPS'?etcscores.fps[index]||0:(
                                                                                                s==='SMN'?(index ==="admin_907"?1:0):
                                                                                                "N/A")
                                                                                      )
                                                                                  )
                                                                              )
                                                                          )
                                                                      )
                                                                  )
                                                              )
                                                          )
                                                      )
                                                  )
                                              )
                                          )
                                      )
                                    )
                              )

I’ve tried if/then/else statements but it’s literally the same messy thing although breaks a bit less often.

3

Answers


  1. If all the checks are testing the exact value of the string variable s, a solution with object-lookup should keep things cleaning, like this:

    const output = {
      'FFP': o.ffp,
      'XP': (await getXP(index, true)) || o.e.xpLvl * o.e.xp,
      'FSC': o.fsc,
      'BGP': o.bgp,
      // ...
    }[s] ?? 'N/A';
    

    But be aware, there’s no short-circuiting in this form, all the options are initiated even the value does not match, which means the result has to wait all the await conditions to be resolved first.

    If that’s an issue, using an IIFE with a switch condition would also be a good choice.

    const output = await (async () => {
      switch (s) {
        case 'FFP':
          return o.ffp;
        case 'XP':
          return (await getXP(index, true)) || o.e.xpLvl * o.e.xp;
        case 'FSC':
          return o.fsc;
        case 'BGP': 
          return o.bgp;
        // ...
        default:
          return 'N/A';
      }
    })();
    
    Login or Signup to reply.
  2. Doesn’t a switch statement work? I’m assuming you can define this in the context where all these vars like "o" are defined:

    async function lookup(s) {
        switch (s) {
            case "FFP":
                return o.ffp;
            case "XP":
                return (await getXP(index, true)) || o.e.xpLvl * o.e.xp;
            case "FSC":
                return o.fsc;
            case "BGP":
                return o.bgp;
    
            //...
    
            default:
                return "N/A";
        }
    

    I would also reorder all the cases to be alphabetical for readability, unless you know for sure some happen way more frequently and should be placed higher in the list.

    I would recommend that all the state variables like "o" and "btns" be stored in some global state management object so that you can just pass them in as well, and define this in a more global scope, like this:

     async function lookup(s, globalState):
          switch (s) {
               case "FFP":
                    return globalState.o.ffp;
               // ...  
    
    Login or Signup to reply.
  3. I think this would be a good use case for a switch statement. It would be like this.

    switch (s) {
        case "FFP": return o.ffp;
        case "XP": return (await getXP(index, true)) || o.e.xpLvl * o.e.xp
        case "FSC": return o.fsc
        case "BGP": return o.bgp
        case "BTN": return btns[index] || 0
        case "CIV": return await getCiv(index) || 0
        case "TIQ": return await getTotalInv(index) || 0
        case "IS1": return isold[index] || 0
        case "ITO": return acn[index].cache.itemsObtained || 0
        case "FLW": return flws[index] || 0
        case "PLY": return acn[index].playtime || 0
        case "QSC": return qusc[index] ? qusc[index].completed : 0
        case "QSR": return qusc[index] ? qusc[index].rankings : 0
        case "DXP": return dbs.xp[index] || 0
        case "FPS": return etcscores.fps[index] || 0
        case "SMN": return index === "admin_907" ? 1 : 0
        default: return "N/A"
    }
    

    Still, it is pretty cryptic this piece of code, so you should better extract the branch cases into properly named/documented functions.

    Another option would be to store each key -> expression pair in a dictionary (you would store the expressions inside functions, named or anonymous, so the execution is deferred.)

    It would be something like this:

    const lookup = {
        "FFP": () => o.ffp,
        "XP": async () => (await getXP(index, true)) || o.e.xpLvl * o.e.xp,
        "FSC": () => o.fsc,
        "BGP": () => o.bgp,
        "BTN": () => btns[index] || 0,
        "CIV": async () => await getCiv(index) || 0,
        "TIQ": async () => await getTotalInv(index) || 0,
        "IS1": () => isold[index] || 0,
        "ITO": () => acn[index].cache.itemsObtained || 0,
        "FLW": () => flws[index] || 0,
        "PLY": () => acn[index].playtime || 0,
        "QSC": () => qusc[index] ? qusc[index].completed : 0,
        "QSR": () => qusc[index] ? qusc[index].rankings : 0,
        "DXP": () => dbs.xp[index] || 0,
        "FPS": () => etcscores.fps[index] || 0,
        "SMN": () => index === "admin_907" ? 1 : 0,
    }
    
    await lookup[s]?.() ?? "N/A" 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search