Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor conditions function in js sdk #39

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { isNull, isNullOrEmpty } from "../helpers";
import { getConcatString } from "../helpers/dependencyHelper";
import { ConditionFunctionType } from "../models";

export type ConditionFunctionRecord = Record<string, (actualValue: Object, patternOfExpected: string) => boolean>;

export const ConditionFunctions: ConditionFunctionRecord = {
[ConditionFunctionType.Contains]: Contains,
[ConditionFunctionType.NotContains]: NotContains,
[ConditionFunctionType.Equals]: Equals,
[ConditionFunctionType.NotEquals]: NotEquals,
[ConditionFunctionType.MatchRegularExpression]: MatchRegularExpression,
};
/**
* Compare whether user input data equals depend value or not.
* @param actualValue
* @param dependencyFieldValue
* @returns
*/
function Equals(actualValue: Object, dependencyFieldValue: string): boolean {
const _actualValue = !actualValue ? "" : getConcatString(actualValue, ",").toLocaleUpperCase();
dependencyFieldValue = !dependencyFieldValue ? "" : dependencyFieldValue.toLocaleUpperCase();
return _actualValue === dependencyFieldValue;
}
/**
* Compare whether user input data does NOT equal depend value or not.
* @param actualValue
* @param dependencyFieldValue
* @returns
*/
function NotEquals(actualValue: Object, dependencyFieldValue: string): boolean {
const _actualValue = !actualValue ? "" : getConcatString(actualValue, ",").toLocaleUpperCase();
dependencyFieldValue = !dependencyFieldValue ? "" : dependencyFieldValue.toLocaleUpperCase();
return _actualValue !== dependencyFieldValue;
}
/**
* Compare whether user input data contains depend value or not.
* @param actualValue
* @param dependencyFieldValue
* @returns
*/
function Contains(actualValue: Object, dependencyFieldValue: string): boolean {
const _actualValue = isNull(actualValue) ? "" : getConcatString(actualValue, ",").toLocaleUpperCase();
dependencyFieldValue = !dependencyFieldValue ? "" : dependencyFieldValue.toLocaleUpperCase();
return _actualValue.indexOf(dependencyFieldValue) >= 0;
}
/**
* Compare whether user input data does NOT contain depend value or not.
* @param actualValue
* @param dependencyFieldValue
* @returns
*/
function NotContains(actualValue: Object, dependencyFieldValue: string): boolean {
const _actualValue = !actualValue ? "" : getConcatString(actualValue, ",").toLocaleUpperCase();
const actualValueNull = isNullOrEmpty(_actualValue)
const dependencyFieldValueNull = isNullOrEmpty(dependencyFieldValue)
return (!actualValueNull && dependencyFieldValueNull) ||
(actualValueNull && !dependencyFieldValueNull) ||
(!actualValueNull && !dependencyFieldValueNull && _actualValue.indexOf(dependencyFieldValue.toLocaleUpperCase()) < 0);
}
/**
* Compare user input with a pattern. Return true if actualValue matchs patternOfExpected
* @param actualValue
* @param patternOfExpected
* @returns
*/
function MatchRegularExpression(actualValue: Object, patternOfExpected: string): boolean {
var regex = new RegExp(patternOfExpected, "igm");
const _actualValue = !actualValue ? "" : getConcatString(actualValue, ",");
return isNullOrEmpty(patternOfExpected) || (!isNullOrEmpty(patternOfExpected) && regex.test(_actualValue));
}
Original file line number Diff line number Diff line change
@@ -1,93 +1,47 @@
import { equals, isNull, isNullOrEmpty } from "../helpers";
import { getConcatString } from "../helpers/dependencyHelper";
import { ConditionCombinationType, ConditionFunctionType, ConditionProperties, FormElementBase, FormSubmission } from "../models";

import { equals, isNull } from "../helpers";
import { ConditionCombinationType, ConditionProperties, FormElementBase, FormSubmission } from "../models";
import { ConditionFunctions } from "./ConditionFunctions";
/**
* Class to check if a element conditions is met
*/
export class FormDependConditions {
readonly _element: FormElementBase;
constructor(element: FormElementBase) {
this._element = element;
}
/**
* Main function to check if a element conditions is met
* @param formSubmissions
* @returns
*/
checkConditions = (formSubmissions: FormSubmission[]): boolean => {
if (!isNull(formSubmissions)) {
const conditionProps = (this._element.properties as unknown) as ConditionProperties;
if (isNull(conditionProps?.conditions)) {
return false;
// no condition to check, return true
return true;
}
let conditionArr = conditionProps.conditions.map(condition => {
for (let i = 0; i < conditionProps.conditions.length; i++) {
const condition = conditionProps.conditions[i]
const fieldValue = formSubmissions.filter(s => equals(s.elementKey, condition.field))[0]?.value as string
if (!isNull(fieldValue)) {
switch (condition.operator) {
case ConditionFunctionType.Contains:
return this.Contains(fieldValue, condition.fieldValue)
case ConditionFunctionType.NotContains:
return this.NotContains(fieldValue, condition.fieldValue)
case ConditionFunctionType.Equals:
return this.Equals(fieldValue, condition.fieldValue)
case ConditionFunctionType.NotEquals:
return this.NotEquals(fieldValue, condition.fieldValue)
case ConditionFunctionType.MatchRegularExpression:
return this.MatchRegularExpression(fieldValue, condition.fieldValue)
const conditionFunction = ConditionFunctions[condition.operator];
if (!isNull(conditionFunction)){
var checkResult = conditionFunction(fieldValue, condition.fieldValue)
if (conditionProps.conditionCombination === ConditionCombinationType.Any && checkResult) {
return true
}
if (conditionProps.conditionCombination !== ConditionCombinationType.Any && !checkResult) {
return false
}
}
}
return false
});
for (let i = 0; i < conditionArr.length; i++) {
const result = conditionArr[i]
if (conditionProps.conditionCombination === ConditionCombinationType.Any && result) {
return true
}
if (conditionProps.conditionCombination === ConditionCombinationType.All && !result) {
return false
}
}
// When reach here, there are two cases
// 1 : All conditions are statisfied and ConditionCombination === ConditionCombinations.All
// 2 : No condition is statisfied and ConditionCombination === ConditionCombinations.Any
return conditionProps.conditionCombination === ConditionCombinationType.All;
return !(conditionProps.conditionCombination === ConditionCombinationType.Any);
}
return false
}
/**
* Compare whether user input data equals depend value or not.
*/
Equals(actualValue: Object, dependencyFieldValue: string): boolean {
const _actualValue = !actualValue ? "" : getConcatString(actualValue, ",");
dependencyFieldValue = !dependencyFieldValue ? "" : dependencyFieldValue.toUpperCase();
return _actualValue === dependencyFieldValue;
}
/**
* Compare whether user input data does NOT equal depend value or not.
*/
NotEquals(actualValue: Object, dependencyFieldValue: string): boolean {
const _actualValue = !actualValue ? "" : getConcatString(actualValue, ",");
dependencyFieldValue = !dependencyFieldValue ? "" : dependencyFieldValue.toUpperCase();
return _actualValue !== dependencyFieldValue;
}
/**
* Compare whether user input data contains depend value or not.
*/
Contains(actualValue: Object, dependencyFieldValue: string): boolean {
const _actualValue = isNull(actualValue) ? "" : getConcatString(actualValue, ",").toUpperCase();
dependencyFieldValue = !dependencyFieldValue ? "" : dependencyFieldValue.toUpperCase();
return _actualValue.indexOf(dependencyFieldValue) >= 0;
}
/**
* Compare whether user input data does NOT contain depend value or not.
*/
NotContains(actualValue: Object, dependencyFieldValue: string): boolean {
const _actualValue = !actualValue ? "" : getConcatString(actualValue, ",");
const actualValueNull = isNullOrEmpty(_actualValue)
const dependencyFieldValueNull = isNullOrEmpty(dependencyFieldValue)
return (!actualValueNull && dependencyFieldValueNull) ||
(actualValueNull && !dependencyFieldValueNull) ||
(!actualValueNull && !dependencyFieldValueNull && _actualValue.toUpperCase().indexOf(dependencyFieldValue.toUpperCase()) < 0);
}
/**
* Compare user input with a pattern. Return true if actualValue matchs patternOfExpected
*/
MatchRegularExpression(actualValue: Object, patternOfExpected: string): boolean {
var regex = new RegExp(patternOfExpected, "igm");
const _actualValue = !actualValue ? "" : getConcatString(actualValue, ",");
return isNullOrEmpty(patternOfExpected) || (!isNullOrEmpty(patternOfExpected) && regex.test(_actualValue));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export enum ConditionFunctionType {
MatchRegularExpression = "MatchRegularExpression",
NotApplicable = "NotApplicable",
Contains = "Contains",
NotContains = "NotContains",
Equals = "Equals",
Expand Down
Loading