1- export const condition = ( ) :string => {
2- // if(value = "") {
3- // var i, ifVal, j, ref, trIf, trRest;
4- // switch (value) {
5- // case "else":
6- // this.chain();
7- // return this.param(value, "if", "if");
8- // case "end":
9- // this.chain();
10- // for (i = j = ref = this.chained.length - 1; j >= 0; i = j += -1) {
11- // ifVal = this.chained[i].getValue("if");
12- // if (ifVal === "end") {
13- // break;
14- // } else if (ifVal != null) {
15- // trIf = Transformation.new().if(ifVal);
16- // this.chained[i].remove("if");
17- // trRest = this.chained[i];
18- // this.chained[i] = Transformation.new().transformation([trIf, trRest]);
19- // if (ifVal !== "else") {
20- // break;
21- // }
22- // }
23- // }
24- // return this.param(value, "if", "if");
25- // case "":
26- // return Condition.new().setParent(this);
27- // default:
28- // return this.param(value, "if", "if", function (value) {
29- // return Condition.new(value).toString();
30- // });
31- // }
32- return ''
1+ import transform , { toTransformationStr } from "." ;
2+ import { ConditionalParams , ConditionOperators } from "../constants/condition" ;
3+ import { Condition , ConditionExpression , Expression } from "../types/transformation/Condition" ;
4+ import { toString } from "../utils" ;
5+
6+ export const computeCondition = ( conditionObj : ConditionExpression ) : { expression : string , transformations : string } => {
7+ const expression = conditionObj . expression ? toString ( conditionObj . expression . map ( exp => computeConditionExpression ( exp ) ) , '_and_' ) : ''
8+
9+ const transformations = conditionObj . transformations . map ( transformation => toTransformationStr ( transform ( transformation ) ) )
10+
11+ return {
12+ expression,
13+ transformations : toString ( transformations , '/' )
14+ }
15+ }
16+
17+ export const mapCharacteristic = ( expression : string ) => ConditionalParams [ expression ] || expression
18+
19+ export const computeConditionExpression = ( expression : Expression ) => {
20+ const characteristic = Array . isArray ( expression . characteristic ) ? toString ( ( expression . characteristic as string [ ] ) . map ( mapCharacteristic ) ) : mapCharacteristic ( expression . characteristic as string )
21+ const operator = ConditionOperators [ expression . operator ]
22+ const value = isNaN ( expression . value as number ) ? `!${ expression . value } !` : expression . value
23+
24+ return toString ( [ characteristic , operator , value ] , '_' )
25+ }
26+
27+ export const condition = ( conditionObj ?: Condition ) :string | string [ ] => {
28+ if ( ! conditionObj || ! conditionObj . if || ! conditionObj . if . expression ) return ''
29+
30+ const ifCondition = computeCondition ( conditionObj . if )
31+ const elseCondition = conditionObj . else ? computeCondition ( conditionObj . else ) : null
32+
33+ const formattedIf = toString ( [ `if_${ ifCondition . expression } ` , ifCondition . transformations ] , '/' )
34+ const formattedElse = elseCondition ? toString ( [ 'if_else' , elseCondition . transformations ] , '/' ) : ''
35+
36+ return [ toString ( [ formattedIf , formattedElse , 'if_end' ] , '/' ) ]
3337}
0 commit comments