mapNumber static method
Return value of the satisfied key. Default is false
.
defaultValue
is the default value if there is no version in range.
defaultCondition
is the default condition if there is no leading condition
in this versionMap
.
const versions = {'<100' : true, '>=102' : false};
print(SatisfiedVersion.map(100, versions)); // => false
print(SatisfiedVersion.map(103, versions)); // => false
print(SatisfiedVersion.map(99, versions)); // => true
Implementation
static bool mapNumber(
/// This is normally your current app version.
int version,
/// The version that has conditions to compare with `version`.
Map<String, bool> versionMap, {
/// [defaultValue] is the default result for `Map` when `version` is not in any range.
bool defaultValue = false,
/// [defaultCondition] is the default condition if the compared version is provided without condition.
SatisfiedCondition defaultCondition = SatisfiedCondition.equal,
}) {
final versionMapCopy = {...versionMap};
// Sort the versionList by it's version.
versionMapCopy.entries.toList().sort((a, b) {
final aComparator = int.parse(SatisfiedCondition.removeCondition(a.key));
final bComparator = int.parse(SatisfiedCondition.removeCondition(b.key));
return aComparator.compareTo(bComparator);
});
bool? result;
// Compare from the biggest version, the first matched will be returned.
for (int i = versionMapCopy.length - 1; i >= 0; i--) {
final compareWith = versionMapCopy.keys.elementAt(i);
if (number(version, compareWith, defaultCondition: defaultCondition)) {
result = versionMapCopy.values.elementAt(i);
} else {
if (result != null) return result;
}
}
return result ?? defaultValue;
}