mirror of
https://github.com/hanzoai/tsgo.git
synced 2026-10-30 19:37:15 +00:00
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
//// [tests/cases/compiler/arrayDestructuringInSwitch1.ts] ////
|
|
|
|
//// [arrayDestructuringInSwitch1.ts]
|
|
export type Expression = BooleanLogicExpression | 'true' | 'false';
|
|
export type BooleanLogicExpression = ['and', ...Expression[]] | ['not', Expression];
|
|
|
|
export function evaluate(expression: Expression): boolean {
|
|
if (Array.isArray(expression)) {
|
|
const [operator, ...operands] = expression;
|
|
switch (operator) {
|
|
case 'and': {
|
|
return operands.every((child) => evaluate(child));
|
|
}
|
|
case 'not': {
|
|
return !evaluate(operands[0]);
|
|
}
|
|
default: {
|
|
throw new Error(`${operator} is not a supported operator`);
|
|
}
|
|
}
|
|
} else {
|
|
return expression === 'true';
|
|
}
|
|
}
|
|
|
|
//// [arrayDestructuringInSwitch1.js]
|
|
export function evaluate(expression) {
|
|
if (Array.isArray(expression)) {
|
|
const [operator, ...operands] = expression;
|
|
switch (operator) {
|
|
case 'and': {
|
|
return operands.every((child) => evaluate(child));
|
|
}
|
|
case 'not': {
|
|
return !evaluate(operands[0]);
|
|
}
|
|
default: {
|
|
throw new Error(`${operator} is not a supported operator`);
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
return expression === 'true';
|
|
}
|
|
}
|