Skip to content

Commit

Permalink
Add support for if-then-else condition.
Browse files Browse the repository at this point in the history
  • Loading branch information
derekpierre committed Oct 8, 2024
1 parent a193c8b commit 3eb7c4b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
22 changes: 22 additions & 0 deletions packages/taco/src/conditions/if-then-else-condition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Condition } from './condition';
import {
IfThenElseConditionProps,
ifThenElseConditionSchema,
IfThenElseConditionType,
} from './schemas/if-then-else';
import { OmitConditionType } from './shared';

export {
IfThenElseConditionProps,
ifThenElseConditionSchema,
IfThenElseConditionType,
} from './schemas/if-then-else';

export class IfThenElseCondition extends Condition {
constructor(value: OmitConditionType<IfThenElseConditionProps>) {
super(ifThenElseConditionSchema, {
conditionType: IfThenElseConditionType,
...value,
});
}
}
1 change: 1 addition & 0 deletions packages/taco/src/conditions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export * as condition from './condition';
export * as conditionExpr from './condition-expr';
export { ConditionFactory } from './condition-factory';
export * as context from './context';
export * as ifThenElse from './if-then-else-condition';
export * as sequential from './sequential';
export { base, predefined };
19 changes: 16 additions & 3 deletions packages/taco/src/conditions/multi-condition.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { CompoundConditionType } from './compound-condition';
import { ConditionProps } from './condition';
import { IfThenElseConditionType } from './if-then-else-condition';
import { ConditionVariableProps, SequentialConditionType } from './sequential';

export const maxNestedDepth =
(maxDepth: number) =>
(condition: ConditionProps, currentDepth = 1) => {
(condition: ConditionProps, currentDepth = 1): boolean => {
if (
condition.conditionType === CompoundConditionType ||
condition.conditionType === SequentialConditionType
condition.conditionType === SequentialConditionType ||
condition.conditionType === IfThenElseConditionType
) {
if (currentDepth > maxDepth) {
// no more multi-condition types allowed at this level
Expand All @@ -18,11 +20,22 @@ export const maxNestedDepth =
return condition.operands.every((child: ConditionProps) =>
maxNestedDepth(maxDepth)(child, currentDepth + 1),
);
} else {
} else if (condition.conditionType === SequentialConditionType) {
return condition.conditionVariables.every(
(child: ConditionVariableProps) =>
maxNestedDepth(maxDepth)(child.condition, currentDepth + 1),
);
} else {
// if-then-else condition
const ifThenElseConditions = [];
ifThenElseConditions.push(condition.ifCondition);
ifThenElseConditions.push(condition.thenCondition);
if (typeof condition.elseCondition !== 'boolean') {
ifThenElseConditions.push(condition.elseCondition);
}
return ifThenElseConditions.every((child: ConditionProps) =>
maxNestedDepth(maxDepth)(child, currentDepth + 1),
);
}
}

Expand Down
30 changes: 30 additions & 0 deletions packages/taco/src/conditions/schemas/if-then-else.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { z } from 'zod';

import { maxNestedDepth } from '../multi-condition';

import { baseConditionSchema } from './common';
import { anyConditionSchema } from './utils';

export const IfThenElseConditionType = 'if-then-else';

export const ifThenElseConditionSchema: z.ZodSchema = z.lazy(() =>
baseConditionSchema
.extend({
conditionType: z
.literal(IfThenElseConditionType)
.default(IfThenElseConditionType),
ifCondition: anyConditionSchema,
thenCondition: anyConditionSchema,
elseCondition: z.union([anyConditionSchema, z.boolean()]),
})
.refine(
(condition) => maxNestedDepth(2)(condition),
{
message: 'Exceeded max nested depth of 2 for multi-condition type',
}, // Max nested depth of 2
),
);

export type IfThenElseConditionProps = z.infer<
typeof ifThenElseConditionSchema
>;
2 changes: 2 additions & 0 deletions packages/taco/src/conditions/schemas/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { z } from 'zod';
import { compoundConditionSchema } from '../compound-condition';

import { contractConditionSchema } from './contract';
import { ifThenElseConditionSchema } from './if-then-else';
import { jsonApiConditionSchema } from './json-api';
import { rpcConditionSchema } from './rpc';
import { sequentialConditionSchema } from './sequential';
Expand All @@ -16,5 +17,6 @@ export const anyConditionSchema: z.ZodSchema = z.lazy(() =>
compoundConditionSchema,
jsonApiConditionSchema,
sequentialConditionSchema,
ifThenElseConditionSchema,
]),
);

0 comments on commit 3eb7c4b

Please sign in to comment.