bahaghari/lib: add trivial.optionalNull

This commit is contained in:
Gabriel Arazas 2024-06-04 19:45:29 +08:00
parent 16df88b116
commit 3825c8568c
No known key found for this signature in database
GPG Key ID: 62104B43D00AA360
2 changed files with 28 additions and 0 deletions

View File

@ -195,4 +195,22 @@ rec {
*/ */
scale = { inMin, inMax, outMin, outMax }: v: scale = { inMin, inMax, outMin, outMax }: v:
((v - inMin) * (outMax - outMin)) / ((inMax - inMin) + outMin); ((v - inMin) * (outMax - outMin)) / ((inMax - inMin) + outMin);
/* Returns a null value if the condition fails. Otherwise, returns the given
value `as`.
Type: optionalNull :: Bool -> Any -> Any
Example:
optionalNull true "HELLO"
=> "HELLO"
optionalNull (null != null) "HELLO"
=> null
*/
optionalNull = cond: as:
if cond then
as
else
null;
} }

View File

@ -260,4 +260,14 @@ lib.runTests {
expr = self.trivial.isNumber true; expr = self.trivial.isNumber true;
expected = false; expected = false;
}; };
testOptionalNull = {
expr = self.trivial.optionalNull true "HELLO";
expected = "HELLO";
};
testOptionalNull2 = {
expr = self.trivial.optionalNull false "HELLO";
expected = null;
};
} }