diff --git a/subprojects/bahaghari/lib/math.nix b/subprojects/bahaghari/lib/math.nix index 8c7b5d1b..15dd7c6b 100644 --- a/subprojects/bahaghari/lib/math.nix +++ b/subprojects/bahaghari/lib/math.nix @@ -137,6 +137,20 @@ rec { isWithinRange = min: max: number: (lib.max number min) <= (lib.min number max); + /* Returns a boolean whether the given number is within the given (exclusive) range. + + Type: isWithinRange :: Number -> Number -> Number -> Bool + + Example: + isWithinRange 30 50 6 + => false + + isWithinRange 0 100 75 + => true + */ + isWithinRange' = min: max: number: + (lib.max number min) < (lib.min number max); + /* Given a number, make it grow by given amount of percentage. A value of 100 should make the number doubled. diff --git a/subprojects/bahaghari/tests/lib/math.nix b/subprojects/bahaghari/tests/lib/math.nix index f3710deb..204ef06e 100644 --- a/subprojects/bahaghari/tests/lib/math.nix +++ b/subprojects/bahaghari/tests/lib/math.nix @@ -157,6 +157,21 @@ lib.runTests { expected = false; }; + testMathWithinRangeExclusive = { + expr = self.math.isWithinRange' 5 10 (-5); + expected = false; + }; + + testMathWithinRangeExclusive2 = { + expr = self.math.isWithinRange' 5 10 10; + expected = false; + }; + + testMathWithinRangeExclusive3 = { + expr = self.math.isWithinRange' (-100) 100 750; + expected = false; + }; + testMathFactorial = { expr = self.math.factorial 3; expected = 6;