Skip to content

Commit

Permalink
Fixing the evaluation and interpolation through jsonlogic.
Browse files Browse the repository at this point in the history
  • Loading branch information
travist committed May 22, 2024
1 parent b08482a commit e4ddc85
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
35 changes: 25 additions & 10 deletions src/modules/jsonlogic/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { BaseEvaluator } from 'utils';
import { BaseEvaluator, EvaluatorOptions } from 'utils';
import { jsonLogic } from './jsonLogic';
class JSONLogicEvaluator extends BaseEvaluator {
public static evaluate(func: any, args: any = {}, ret: any = '', tokenize: boolean = false, context: any = {}) {
public static evaluate(
func: any,
args: any = {},
ret: any = '',
interpolate: boolean = false,
context: any = {},
options: EvaluatorOptions = {}
) {
let returnVal = null;
if (typeof func === 'object') {
try {
Expand All @@ -13,7 +20,7 @@ class JSONLogicEvaluator extends BaseEvaluator {
}
}
else {
returnVal = BaseEvaluator.evaluate(func, args, ret, tokenize, context);
returnVal = BaseEvaluator.evaluate(func, args, ret, interpolate, context, options);
}
return returnVal;
}
Expand All @@ -32,27 +39,35 @@ export function evaluate(
evaluation: string,
ret: string = 'result',
evalContextFn?: EvaluatorFn,
fnName?: string,
options: any = {}
options: EvaluatorOptions = {}
) {
const { evalContext, instance } = context;
const evalContextValue = evalContext ? evalContext(context) : context;
if (evalContextFn) {
evalContextFn(evalContextValue);
}
fnName = fnName || 'evaluate';
if (instance && (instance as any)[fnName]) {
return (instance as any)[fnName](evaluation, evalContextValue, ret, options);
if (instance && (instance as any).evaluate) {
return (instance as any).evaluate(evaluation, evalContextValue, ret, false, options);
}
return (JSONLogicEvaluator as any)[fnName](evaluation, evalContextValue, ret);
return (JSONLogicEvaluator as any).evaluate(evaluation, evalContextValue, ret, false, context, options);
}

export function interpolate(
context: EvaluatorContext,
evaluation: string,
evalContextFn?: EvaluatorFn
) : string {
return evaluate(context, evaluation, undefined, evalContextFn, 'interpolate', {
const { evalContext, instance } = context;
const evalContextValue = evalContext ? evalContext(context) : context;
if (evalContextFn) {
evalContextFn(evalContextValue);
}
if (instance && (instance as any).evaluate) {
return (instance as any).interpolate(evaluation, evalContextValue, {
noeval: true
});
}
return (JSONLogicEvaluator as any).interpolate(evaluation, evalContextValue, {
noeval: true
});
}
Expand Down
13 changes: 9 additions & 4 deletions src/utils/Evaluator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { noop, trim, keys, get, set, isObject, values } from 'lodash';

export interface EvaluatorOptions {
noeval?: boolean;
data?: any;
}

// BaseEvaluator is for extending.
export class BaseEvaluator {
static templateSettings = {
Expand All @@ -22,7 +27,7 @@ export class BaseEvaluator {
return new Function(...params, func);
};

public static interpolateString(rawTemplate: string, data: any, options: any = {}) {
public static interpolateString(rawTemplate: string, data: any, options: EvaluatorOptions = {}) {
if (!rawTemplate) {
return '';
}
Expand Down Expand Up @@ -74,7 +79,7 @@ export class BaseEvaluator {
});
}

public static interpolate(rawTemplate: any, data: any, options: any = {}) {
public static interpolate(rawTemplate: any, data: any, options: EvaluatorOptions = {}) {
if (typeof rawTemplate === 'function' && !(Evaluator.noeval || options.noeval)) {
try {
return rawTemplate(data);
Expand All @@ -101,7 +106,7 @@ export class BaseEvaluator {
ret: any = '',
interpolate: boolean = false,
context: any = {},
options: any = {}
options: EvaluatorOptions = {}
): any {
let returnVal = null;
options = isObject(options) ? options : { noeval: options };
Expand Down Expand Up @@ -158,7 +163,7 @@ export class BaseEvaluator {
* @param args
* @returns
*/
public static execute(func: string | any, args: any, context: any = {}, options: any = {}) {
public static execute(func: string | any, args: any, context: any = {}, options: EvaluatorOptions = {}) {
options = isObject(options) ? options : { noeval: options };
if (Evaluator.noeval || options.noeval) {
console.warn('No evaluations allowed for this renderer.');
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { Evaluator, BaseEvaluator } from './Evaluator';
export { Evaluator, EvaluatorOptions, BaseEvaluator } from './Evaluator';
export { sanitize } from './sanitize';
export { override } from './override';
export { unwind } from './unwind';
Expand Down
1 change: 0 additions & 1 deletion src/utils/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { checkCustomConditional, checkJsonConditional, checkLegacyConditional, c
import { LogicActionCustomAction, LogicActionMergeComponentSchema, LogicActionProperty, LogicActionPropertyBoolean, LogicActionPropertyString, LogicActionValue } from "types/AdvancedLogic";
import { get, set, clone, isEqual, assign } from 'lodash';
import { evaluate, interpolate } from 'modules/jsonlogic';
import { getComponentKey } from "./formUtil";

export const hasLogic = (context: LogicContext): boolean => {
const { component } = context;
Expand Down

0 comments on commit e4ddc85

Please sign in to comment.