Skip to content

Commit

Permalink
feat: add isNullish function (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilxqx authored and radashi-bot committed Nov 11, 2024
1 parent d3b6cf4 commit a8f1763
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 0 deletions.
23 changes: 23 additions & 0 deletions benchmarks/typed/isNullish.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as _ from 'radashi'

describe('isNullish', () => {
bench('with null', () => {
_.isNullish(null)
})

bench('with undefined', () => {
_.isNullish(undefined)
})

bench('with number', () => {
_.isNullish(0)
})

bench('with string', () => {
_.isNullish('')
})

bench('with array', () => {
_.isNullish([])
})
})
19 changes: 19 additions & 0 deletions docs/typed/isNullish.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: isNullish
description: Determine if a value is null or undefined
since: 12.2.0
---

### Usage

Pass in a value and get a boolean telling you if the value is null or undefined.

```ts
import * as _ from 'radashi'

_.isNullish(null) // => true
_.isNullish(undefined) // => true
_.isNullish('') // => false
_.isNullish([]) // => false
_.isNullish(0) // => false
```
1 change: 1 addition & 0 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export * from './typed/isInt.ts'
export * from './typed/isIntString.ts'
export * from './typed/isIterable.ts'
export * from './typed/isMap.ts'
export * from './typed/isNullish.ts'
export * from './typed/isNumber.ts'
export * from './typed/isObject.ts'
export * from './typed/isPlainObject.ts'
Expand Down
16 changes: 16 additions & 0 deletions src/typed/isNullish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Return true if the given value is null or undefined.
*
* @see https://radashi.js.org/reference/typed/isNullish
* @example
* ```ts
* isNullish(null) // => true
* isNullish(undefined) // => true
* isNullish('') // => false
* isNullish(0) // => false
* ```
* @version 12.2.0
*/
export function isNullish(value: unknown): value is null | undefined {
return value === null || value === undefined
}
52 changes: 52 additions & 0 deletions tests/typed/isNullish.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as _ from 'radashi'

describe('isNullish return type', () => {
test('value is any', () => {
const value = {} as any
if (_.isNullish(value)) {
expectTypeOf(value).toEqualTypeOf<null | undefined>()
} else {
expectTypeOf(value).toEqualTypeOf<any>()
}
})
test('value is unknown', () => {
const value = {} as unknown
if (_.isNullish(value)) {
expectTypeOf(value).toEqualTypeOf<null | undefined>()
} else {
expectTypeOf(value).toEqualTypeOf<unknown>()
}
})
test('value is never', () => {
const value = {} as never
if (_.isNullish(value)) {
expectTypeOf(value).toEqualTypeOf<never>()
} else {
expectTypeOf(value).toEqualTypeOf<never>()
}
})
test('value is string', () => {
const value = {} as string
if (_.isNullish(value)) {
expectTypeOf(value).toEqualTypeOf<never>()
} else {
expectTypeOf(value).toEqualTypeOf<string>()
}
})
test('value is number', () => {
const value = {} as number
if (_.isNullish(value)) {
expectTypeOf(value).toEqualTypeOf<never>()
} else {
expectTypeOf(value).toEqualTypeOf<number>()
}
})
test('value is any[]', () => {
const value = {} as any[]
if (_.isNullish(value)) {
expectTypeOf(value).toEqualTypeOf<never>()
} else {
expectTypeOf(value).toEqualTypeOf<any[]>()
}
})
})
36 changes: 36 additions & 0 deletions tests/typed/isNullish.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as _ from 'radashi'

describe('isNullish', () => {
test('returns true for null', () => {
const result = _.isNullish(null)
expect(result).toBeTruthy()
})
test('returns true for undefined', () => {
const result = _.isNullish(undefined)
expect(result).toBeTruthy()
})
test('returns false for boolean', () => {
const result = _.isNullish(false)
expect(result).toBeFalsy()
})
test('returns false for number', () => {
const result = _.isNullish(0)
expect(result).toBeFalsy()
})
test('returns false for NaN', () => {
const result = _.isNullish(Number.NaN)
expect(result).toBeFalsy()
})
test('returns false for array', () => {
const result = _.isNullish([])
expect(result).toBeFalsy()
})
test('returns false for object', () => {
const result = _.isNullish({})
expect(result).toBeFalsy()
})
test('returns false for string', () => {
const result = _.isNullish('')
expect(result).toBeFalsy()
})
})

0 comments on commit a8f1763

Please sign in to comment.