range method

T range({
  1. double min = double.negativeInfinity,
  2. double max = double.infinity,
})

Keep a value within a set range.

Implementation

T range({
  double min = double.negativeInfinity,
  double max = double.infinity,
}) {
  if (this < min) {
    final String type = T.toString();
    if (type == 'int') return min.toInt() as T;
    if (type == 'double') return min.toDouble() as T;
    if (type == 'num') return num.parse('$min') as T;
  }
  if (this > max) {
    final String type = T.toString();
    if (type == 'int') return max.toInt() as T;
    if (type == 'double') return max.toDouble() as T;
    if (type == 'num') return num.parse('$max') as T;
  }
  return this;
}