mirror of
https://github.com/foo-dogsquared/nixos-config.git
synced 2025-01-30 22:57:55 +00:00
bahaghari/lib: add trivial.optionalNull
This commit is contained in:
parent
16df88b116
commit
3825c8568c
@ -195,4 +195,22 @@ rec {
|
||||
*/
|
||||
scale = { inMin, inMax, outMin, outMax }: v:
|
||||
((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;
|
||||
}
|
||||
|
@ -260,4 +260,14 @@ lib.runTests {
|
||||
expr = self.trivial.isNumber true;
|
||||
expected = false;
|
||||
};
|
||||
|
||||
testOptionalNull = {
|
||||
expr = self.trivial.optionalNull true "HELLO";
|
||||
expected = "HELLO";
|
||||
};
|
||||
|
||||
testOptionalNull2 = {
|
||||
expr = self.trivial.optionalNull false "HELLO";
|
||||
expected = null;
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user