inRangeOf method
Clamps the current integer to be within the range of min and max.
The min parameter specifies the minimum value of the range.
The max parameter specifies the maximum value of the range.
Throws an ArgumentError if min is greater than max.
Returns the current integer if it is within the range. If the current integer is
less than min, returns min. If the current integer is greater than max, returns max.
Example:
int value = 5;
int clampedValue = value.inRangeOf(1, 10);
print(clampedValue); // Output: 5
int clampedValueBelowMin = value.inRangeOf(6, 10);
print(clampedValueBelowMin); // Output: 6
int clampedValueAboveMax = value.inRangeOf(1, 4);
print(clampedValueAboveMax); // Output: 4
Implementation
int inRangeOf(int min, int max) {
if (min > max) throw ArgumentError('min must be smaller the max');
if (this < min) return min;
if (this > max) return max;
return this;
}