Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

multiple cond() returning functions

node v14.20.1
version: 2.0.0
endpointsharetweet
const R = require("ramda") // set up the environment const isFalsy = R.anyPass([R.isNil, R.isEmpty, R.not]); const isTruthy = R.complement(isFalsy); const MeterType = Object.freeze({ STANDARD: 'STANDARD', SMART: 'SMART', DEACTIVATED: 'DEACTIVATED' }); let enableSmartReadsSOLR; let enableSmartReadsSERL; let isSOLRCustomer; // current render functions const renderStandard = () => console.error('RenderStandard'); const renderSmart = () => console.error('RenderSmart'); const renderDeactivated = () => console.error('RenderDeactivated'); /*******************************************************************************************/ // various conditional functions to simplify the cond() below - note the () => to create functions for cond() const smartEnabledAndisSOLR = R.both(() => isTruthy(enableSmartReadsSOLR), () => isTruthy(isSOLRCustomer)); const smartDisabledAndisSOLR = R.both(() => isFalsy(enableSmartReadsSOLR), () => isTruthy(isSOLRCustomer)); const smartEnabledAndisSERL = R.both(() => isTruthy(enableSmartReadsSERL), () => isFalsy(isSOLRCustomer)); const smartDisabledAndisSERL = R.both(() => isFalsy(enableSmartReadsSERL), () => isFalsy(isSOLRCustomer)); // determine which render function to use for Smart Meter Customers const getSmartRenderFn = R.cond([ [R.either(smartEnabledAndisSOLR, smartEnabledAndisSERL), R.always(renderSmart)], [R.either(smartDisabledAndisSOLR, smartDisabledAndisSERL), R.always(renderStandard)], [R.T, R.always(R.F)] ]); // determine meterType - note that SMART conditional "calls" getSmartRenderFn rather than just returning it const getRenderFn = (meterType) => R.cond([ [R.equals(MeterType.STANDARD), R.always(renderStandard)], [R.equals(MeterType.SMART), R.always(getSmartRenderFn())], [R.equals(MeterType.DEACTIVATED), R.always(renderDeactivated)], [R.T, R.always(R.F)] ])(meterType); /*******************************************************************************************/ // change the values here & re-run enableSmartReadsSOLR = false; enableSmartReadsSERL = true; isSOLRCustomer = true; let fn = getRenderFn('SMART') fn() console.assert(fn === renderStandard, "wrong render function");
Loading…

no comments

    sign in to comment