-
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 #166 from codeforpakistan/handle-in-or-and
Handle in or and
- Loading branch information
Showing
2 changed files
with
130 additions
and
25 deletions.
There are no files selected for viewing
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
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,91 @@ | ||
class Validation { | ||
static bool handleCriteria( | ||
value1, value2, String comparisionType, String criteria) { | ||
if (criteria == '>') { | ||
return isLessThan(value2, value1, comparisionType); | ||
} else if (criteria == '<') { | ||
return isLessThan(value1, value2, comparisionType); | ||
} else if (criteria == '==') { | ||
return isEqual(value1, value2, comparisionType); | ||
} else if (criteria == 'in' || criteria == 'and') { | ||
//now all values of array 2 which is in value2 must be in 1 otherwise false | ||
return hasValue1ContainsValue2(value1, value2, criteria); | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
static hasValue1ContainsValue2(value1, value2, criteria) { | ||
if (!isNullOrEmpty(value1) && | ||
!isNullOrEmpty(value2) && | ||
value1?.length > 0 && | ||
value2?.length > 0) { | ||
value2 = value2.where((element) => element != null).cast<int>().toList(); | ||
if (criteria == 'and') { | ||
return value2?.every((value) => | ||
!isNullOrEmpty(value) && | ||
value1.any( | ||
(map) => map.containsKey("index") && map["index"] == value)); | ||
} else if (criteria == 'in') { | ||
return value2?.any((value) => | ||
!isNullOrEmpty(value) && | ||
value1.any( | ||
(map) => map.containsKey("index") && map["index"] == value)); | ||
} | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
static isLessThan(val, val1, comparisionType) { | ||
if (isNullOrEmpty(val ?? '') && isNullOrEmpty(val1 ?? '')) { | ||
return comparisionType == 'int' ? -1 : false; | ||
} | ||
if (comparisionType == 'int') { | ||
return int.parse(val) < int.parse(val1); | ||
} else { | ||
return val < val1; | ||
} | ||
} | ||
|
||
static isEqual(val, val1, comparisionType) { | ||
if (isNullOrEmpty(val) && isNullOrEmpty(val1)) { | ||
return comparisionType == 'int' ? -1 : false; | ||
} | ||
if (comparisionType == 'int') { | ||
return int.parse(val) == int.parse(val1); | ||
} else { | ||
return val == val1; | ||
} | ||
} | ||
|
||
static bool isNullOrEmpty(value) { | ||
return value == null || value == ''; | ||
} | ||
|
||
static handleAndConditions(_currentValue, conditions, String returnType) { | ||
try { | ||
bool isValid = false; | ||
for (var eachOR in conditions) { | ||
isValid = handleCriteria( | ||
_currentValue[eachOR?['key']], | ||
(eachOR?['type'] == 'mutual' | ||
? _currentValue[eachOR?[eachOR?['key']]] | ||
: eachOR?['selecteds'] ?? -1), | ||
eachOR?['comparisionType'] ?? '', | ||
eachOR?['criteria'] ?? ''); | ||
if (!isValid) { | ||
break; | ||
} | ||
print(isValid); | ||
print(eachOR?['type']); | ||
print(eachOR?['selecteds']); | ||
} | ||
; | ||
return returnType == 'bool' ? isValid : (isValid ? 'Yes' : 'No'); | ||
} catch (err) { | ||
print(err); | ||
return returnType == 'bool' ? false : ''; | ||
} | ||
} | ||
} |