-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from episerver/feature/AFORM-3701-refactor-con…
…dition-function Refactor conditions function in js sdk
- Loading branch information
Showing
4 changed files
with
148 additions
and
162 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/@optimizely/forms-sdk/src/form-depend-conditions/ConditionFunctions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
96 changes: 25 additions & 71 deletions
96
src/@optimizely/forms-sdk/src/form-depend-conditions/formDependConditions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
1 change: 0 additions & 1 deletion
1
src/@optimizely/forms-sdk/src/models/enums/ConditionFunctionType.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.