-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💥 Return
undefined
when the value is nullish
It's more useful in most of case.
- Loading branch information
1 parent
9d44553
commit d26eb1c
Showing
3 changed files
with
38 additions
and
23 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
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,17 +1,17 @@ | ||
type Nullish = undefined | null; | ||
export type Nullish = undefined | null; | ||
|
||
/** | ||
* Returns the value as is if `value` is nullish, otherwise | ||
* Returns `undefined` if `value` is nullish, otherwise | ||
* it executes `callback` and returns the result. | ||
* | ||
* This function is the opposite of the Nullish coalescing operator (??). | ||
*/ | ||
export function unnullish<T, R>( | ||
value: T | Nullish, | ||
callback: (value: T) => R, | ||
): R | Nullish { | ||
callback: (v: T) => R, | ||
): R | undefined { | ||
if (value == null) { | ||
return value as Nullish; | ||
return undefined; | ||
} | ||
return callback(value); | ||
} |
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