You might still want to put guards around each case, want to avoid query failure for bad data, or otherwise do not want to run every case.
Let
can accept an array of bindings, a useful trick to reuse variables in subsequent expressions.
Here is a switch, put together with some helper methods.
const matchCase = (testValue, expr) =>
q.If(
q.IsNull(q.Var('_case')),
q.If(q.Equals(testValue, q.Var('_switch')), expr, null),
q.Var('_case')
);
const defaultCase = (expr) =>
q.If(q.IsNull(q.Var('_case')), expr, q.Var('_case'));
const body = q.Query(
q.Lambda(
['input'],
q.Let(
{
type: q.Select('type', q.Var('input')),
value: q.Select('value', q.Var('input')),
result: q.Let(
[
{ _switch: q.Var('type') },
{ _case: null },
{ _case: matchCase('string', q.Multiply(q.ToNumber(q.Var('value')), 10)) },
{ _case: matchCase('number', q.Multiply(q.Var('value'), 10)) },
{ _case: defaultCase(0) },
],
q.Var('_case')
),
},
q.Var('result')
)
)
);