isBetween method

bool isBetween(
  1. T min,
  2. T max, {
  3. bool? inclusiveMin,
  4. bool? inclusiveMax,
})

compares if this is between min and max

additional named params allow making full/open-ended inclusive range comparison

an assertion error is thrown if min and max are in the wrong order. on production code you always get false

Implementation

bool isBetween(
  T min,
  T max, {
  bool? inclusiveMin,
  bool? inclusiveMax,
}) {
  if ((max as Comparable).isSmallerThan(min as Comparable)) {
    assert(false, 'min and max values passed in wrong order');
    return false;
  }

  final iMin = inclusiveMin ?? false;
  final iMax = inclusiveMax ?? false;

  if (iMin) {
    if (!isGreaterThanOrEqualTo(min)) return false;
  } else {
    if (!isGreaterThan(min)) return false;
  }

  if (iMax) {
    if (!isSmallerThanOrEqualTo(max)) return false;
  } else {
    if (!isSmallerThan(max)) return false;
  }

  return true;
}