@zendesk/retrace
Version:
define and capture Product Operation Traces along with computed metrics with an optional friendly React beacon API
42 lines • 1.29 kB
JavaScript
;
/**
* Copyright Zendesk, Inc.
*
* Use of this source code is governed under the Apache License, Version 2.0
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.switchFn = switchFn;
/**
* Helper to replace your nested 'ifs' with a single, readable expression.
* Note that both the return and case are eagerly evaluated. If you wish to avoid that, use getters (2nd example).
* @example
* switchFn(
* {case: A === B, return: 'equal'},
* {case: B > C, return: 'different'},
* {return: 'default_value'}
* );
* @example
* switchFn(
* {
* get case() {
* return deepEqual(A, B);
* },
* get return() {
* return computationallyExpensive(A, B);
* },
* },
* // ...
* {return: 'default_value'}
* );
* @param caseDefinitions
*/
function switchFn(...caseDefinitions) {
const defaultCase = caseDefinitions[caseDefinitions.length - 1];
if (!defaultCase) {
throw new Error('Expected switchFn to receive a default case, but it did not.');
}
const match = caseDefinitions.find((definition) => 'case' in definition && Boolean(definition.case)) ?? defaultCase;
return match.return;
}
//# sourceMappingURL=switchFn.js.map