-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy patharrayDestructuringInSwitch1.js
47 lines (45 loc) · 1.37 KB
/
arrayDestructuringInSwitch1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//// [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]
"use strict";
exports.__esModule = true;
exports.evaluate = void 0;
function evaluate(expression) {
if (Array.isArray(expression)) {
var operator = expression[0], operands = expression.slice(1);
switch (operator) {
case 'and': {
return operands.every(function (child) { return evaluate(child); });
}
case 'not': {
return !evaluate(operands[0]);
}
default: {
throw new Error("".concat(operator, " is not a supported operator"));
}
}
}
else {
return expression === 'true';
}
}
exports.evaluate = evaluate;