lib: add defaultTo (#357681)

This commit is contained in:
Silvan Mosberger 2024-12-08 10:24:42 +01:00 committed by GitHub
commit 944f47455e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 1 deletions

View File

@ -71,7 +71,7 @@ let
# these are the only ones that are currently not
inherit (builtins) addErrorContext isPath trace typeOf unsafeGetAttrPos;
inherit (self.trivial) id const pipe concat or and xor bitAnd bitOr bitXor
bitNot boolToString mergeAttrs flip mapNullable inNixShell isFloat min max
bitNot boolToString mergeAttrs flip defaultTo mapNullable inNixShell isFloat min max
importJSON importTOML warn warnIf warnIfNot throwIf throwIfNot checkListOfEnum
info showWarnings nixpkgsVersion version isInOldestRelease oldestSupportedReleaseIsAtLeast
mod compare splitByAndCompare seq deepSeq lessThan add sub

View File

@ -316,6 +316,40 @@ in {
*/
flip = f: a: b: f b a;
/**
Return `maybeValue` if not null, otherwise return `default`.
# Inputs
`default`
: 1\. Function argument
`maybeValue`
: 2\. Function argument
# Examples
:::{.example}
## `lib.trivial.defaultTo` usage example
```nix
defaultTo "default" null
=> "default"
defaultTo "default" "foo"
=> "foo"
defaultTo "default" false
=> false
```
:::
*/
defaultTo = default: maybeValue:
if maybeValue != null then maybeValue
else default;
/**
Apply function if the supplied argument is non-null.