My task is to develop a method that can convert a dynamic Json with a specific structure
export interface Rules {
ruleDescription: string;
legalNature: string;
rulesForConnection: RuleForConnection[];
or?: LogicalOperator;
and?: LogicalOperator;
not?: LogicalOperator;
}
export interface LogicalOperator {
rulesForConnection?: RuleForConnection[];
or?: LogicalOperator;
and?: LogicalOperator;
not?: LogicalOperator;
}
export interface RuleForConnection {
existingLinkType?: number;
linkType?: number;
quantity?: string;
minimum?: number;
maximum?: number;
exactly?: number;
}
In this Json, the keys "or", "and", "not" are flexible.
For instance, given the following input Json:
let json: LogicalOperator = {
"or": {
"and": {
"rulesForConnection": [
{
"existingLinkType": 115,
"quantity": "NONE"
},
{
"existingLinkType": 118,
"quantity": "NONE"
}
]
},
"or": {
"rulesForConnection": [
{
"linkType": 115,
"minimum": 1
},
{
"linkType": 118,
"minimum": 1
}
]
}
}
}
The expected output would be:
({existingLinkType: 115, quantity: "NONE"} && {existingLinkType: 118, quantity: "NONE"}) || ({linkType: 115, minimum: 1} || {linkType: 115, minimum: 1})
In the example above, the "and" operator should combine the two objects in the array, the inner 'or' should apply to the two objects in the array, and the outer 'or' should concatenate the results of the inner 'and' and 'or'.
I have attempted to create a recursive method but it doesn't seem to be functioning as intended
export function convertToRule(logicalOperator: LogicalOperator, initial?: LogicalOperator, operator?: string, result?: string): string {
const initialJson = { ...logicalOperator };
if (logicalOperator.rulesForConnection) {
// Implementation logic...
} else if (logicalOperator.and) {
// Implementation logic...
} else if (logicalOperator.or) {
// Implementation logic...
} else {
return '';
}
}