-
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.
Showing
5 changed files
with
70 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/// Clamps number within the inclusive lower and upper bounds. | ||
/// | ||
/// Arguments | ||
/// | ||
/// - number (num): The number to clamp. | ||
/// - [lower] (num): The lower bound. | ||
/// - upper (num): The upper bound. | ||
/// | ||
/// Returns | ||
/// | ||
/// - (num): Returns the clamped number. | ||
dynamic clamp(num number, num? lower, [num? upper]) { | ||
if (upper == null) { | ||
upper = lower; | ||
if (number > upper!) { | ||
return upper; | ||
} | ||
return number; | ||
} else { | ||
if (number > upper) { | ||
return upper; | ||
} else if (number < lower!) { | ||
return lower; | ||
} | ||
return number; | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export 'clamp.dart'; | ||
export 'random.dart'; |
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
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,22 @@ | ||
import 'package:flodash/flodash.dart' as flodash; | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
main() { | ||
test('lower', () { | ||
expect(flodash.clamp(-10, -5, 5), equals(-5)); | ||
}); | ||
test('upper', () { | ||
expect(flodash.clamp(10, -5, 5), equals(5)); | ||
}); | ||
test('in range', () { | ||
expect(flodash.clamp(3, -5, 5), equals(3)); | ||
}); | ||
test('2 arguments - upper', () { | ||
expect(flodash.clamp(12, 6), equals(6)); | ||
}); | ||
test('2 arguments - in range', () { | ||
expect(flodash.clamp(4, 6), equals(4)); | ||
expect(flodash.clamp(-500, 5), equals(-500)); | ||
}); | ||
} |