-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add isNullish
, isNonNullish
and isKeyOf
Utility Functions
#385
base: master
Are you sure you want to change the base?
Changes from all commits
3034b47
4817a80
71682ed
81adcc9
0cefc6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
title: isKeyOf | ||
description: 'Checks if the given value is a key of the given object. It is useful for narrowing down the type of a value.' | ||
group: Typed | ||
--- | ||
|
||
## Basic usage | ||
|
||
Pass the object and the value to check. It will return a boolean indicating if the value is a key of the object. | ||
|
||
```ts | ||
import { isKeyOf } from 'radash' | ||
|
||
const obj = { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
} | ||
isKeyOf(obj, 'a') // true | ||
isKeyOf(obj, 'd') // false | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
title: isNonNullish | ||
description: 'Checks if the given value is not null or undefined.' | ||
group: Typed | ||
--- | ||
|
||
## Basic usage | ||
|
||
Pass in a value and get a boolean telling you if the value is not a Nullish. | ||
|
||
```ts | ||
import { isNonNullish } from 'radash' | ||
|
||
isNonNullish(null) // false | ||
isNonNullish(undefined) // false | ||
isNonNullish('') // true | ||
isNonNullish(0) // true | ||
isNonNullish(false) // true | ||
isNonNullish(NaN) // true | ||
isNonNullish([]) // true | ||
isNonNullish({}) // true | ||
|
||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
title: isNullish | ||
description: 'Checks if the given value is null or undefined.' | ||
group: Typed | ||
--- | ||
|
||
## Basic usage | ||
|
||
Pass in a value and get a boolean telling you if the value is a Nullish. | ||
|
||
```ts | ||
import { isNullish } from 'radash' | ||
|
||
isNullish(null) // true | ||
isNullish(undefined) // true | ||
isNullish(0) // false | ||
isNullish('') // false | ||
isNullish(false) // false | ||
isNullish(NaN) // false | ||
isNullish([]) // false | ||
isNullish({}) // false | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,3 +104,32 @@ export const isEqual = <TType>(x: TType, y: TType): boolean => { | |
} | ||
return true | ||
} | ||
|
||
/** | ||
* Checks if the given value is null or undefined. | ||
*/ | ||
export const isNullish = (value: any): value is null | undefined => { | ||
return value === null || value === undefined | ||
} | ||
|
||
/** | ||
* Checks if the given value is not null or undefined. | ||
*/ | ||
export const isNonNullish = <TType>( | ||
value: TType | ||
): value is Exclude<TType, null | undefined> => { | ||
return value !== null && value !== undefined | ||
} | ||
|
||
/** | ||
* Checks if the given value is a key of the given object. It is useful for narrowing down the type of a value. | ||
* @param value key to check | ||
* @param obj object to check | ||
* @returns true if the value is a key of the object | ||
*/ | ||
export const isKeyOf = <TType extends Record<string | number | symbol, any>>( | ||
value: string | number | symbol, | ||
obj: TType | ||
): value is keyof TType => { | ||
return value in obj | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This feels like it goes against the library's philosophy. You can easily do Also, I half-expect this to match |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe not having this is by-design? Why not
value == null
orvalue != null
instead?