What is this ?
Small package for handling nested ternary statements, allowing you to read your code better
npm i nested-ternary
const nt = ;// or import nt from 'nested-ternary'// lets model a simple number guessing gameconst num = Math; // 1 - 100const guess = someUserinput;// checks condition using key:cond in the object passed in// returns key:t if true, key:f if false// t/f can be objects themselves with keys cond,t & f;
Advanced Usage (Lazy Loading)
sometimes you need to lazy load your values so that the complier does not compile them and return an error because of some missing attribute or function
const nt = ;//lets model an array of values coming from a stock exchange api//lets assume that there will always be a name but value, prefix, suffix and format will be dynamicconst data =name: 'icorp'value: 4prefix: '$'suffix: 'per share'nname: 'eu inc'value: 7prefix: '€'suffix: 'per share'name: 'dead corp'data
we have to make sure that the value of t and f is a function whenever we want to return a final value (as in not nesting another object). we need this so that the compiler does not try to evaluate the value, in our example here format is missing from the last 2 stocks so if we removed the function set up: '() =>' then we run into an error since stock.format(...) is not a function.
Please dont forget to evalute the return from nt using '()' because a function is being returned and you wont see anything unless you evaluate it :)