Skip to content

Commit

Permalink
Add isNullish and isNonNullish functions to typed module
Browse files Browse the repository at this point in the history
  • Loading branch information
shtse8 committed Mar 13, 2024
1 parent 069b26c commit 3034b47
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 16 deletions.
23 changes: 23 additions & 0 deletions docs/typed/is-non-nullish.mdx
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

```
22 changes: 22 additions & 0 deletions docs/typed/is-nullish.mdx
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
```
66 changes: 50 additions & 16 deletions src/tests/typed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('typed module', () => {
assert.isFalse(result)
})
test('returns false for class instance', () => {
class Data {}
class Data { }
const result = _.isArray(new Data())
assert.isFalse(result)
})
Expand Down Expand Up @@ -56,7 +56,7 @@ describe('typed module', () => {
assert.isFalse(result)
})
test('returns false for class instance', () => {
class Data {}
class Data { }
const result = _.isObject(new Data())
assert.isFalse(result)
})
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('typed module', () => {
assert.isFalse(result)
})
test('returns false for class instance', () => {
class Data {}
class Data { }
const result = _.isFunction(new Data())
assert.isFalse(result)
})
Expand Down Expand Up @@ -172,7 +172,7 @@ describe('typed module', () => {
assert.isFalse(result)
})
test('returns false for class instance', () => {
class Data {}
class Data { }
const result = _.isString(new Data())
assert.isFalse(result)
})
Expand Down Expand Up @@ -212,7 +212,7 @@ describe('typed module', () => {
assert.isFalse(result)
})
test('returns false for class instance', () => {
class Data {}
class Data { }
const result = _.isNumber(new Data())
assert.isFalse(result)
})
Expand Down Expand Up @@ -247,7 +247,7 @@ describe('typed module', () => {
})

describe('isInt function', () => {
class Data {}
class Data { }
test('returns false for non-number values', () => {
assert.isFalse(_.isInt(undefined))
assert.isFalse(_.isInt(null))
Expand All @@ -270,7 +270,7 @@ describe('typed module', () => {
})

describe('isFloat function', () => {
class Data {}
class Data { }
test('returns false for non-number values', () => {
assert.isFalse(_.isFloat(undefined))
assert.isFalse(_.isFloat(null))
Expand All @@ -293,7 +293,7 @@ describe('typed module', () => {
})

describe('isEmpty function', () => {
class Data {}
class Data { }
class Person {
name: string = 'ray'
}
Expand All @@ -320,8 +320,8 @@ describe('typed module', () => {
assert.isFalse(_.isEmpty('abc'))
assert.isFalse(_.isEmpty(String('abc')))
assert.isFalse(_.isEmpty([1, 2, 3]))
assert.isFalse(_.isEmpty(function work() {}))
assert.isFalse(_.isEmpty(() => {}))
assert.isFalse(_.isEmpty(function work() { }))
assert.isFalse(_.isEmpty(() => { }))
assert.isFalse(_.isEmpty(Symbol('')))
assert.isFalse(_.isEmpty(Symbol('hello')))
const map = new Map()
Expand All @@ -342,8 +342,8 @@ describe('typed module', () => {
assert.isFalse(_.isDate('abc'))
assert.isFalse(_.isDate(String('abc')))
assert.isFalse(_.isDate([1, 2, 3]))
assert.isFalse(_.isDate(function work() {}))
assert.isFalse(_.isDate(() => {}))
assert.isFalse(_.isDate(function work() { }))
assert.isFalse(_.isDate(() => { }))
assert.isFalse(_.isDate(Symbol('')))
assert.isFalse(_.isDate(Symbol('hello')))
})
Expand All @@ -353,16 +353,16 @@ describe('typed module', () => {
test('return true for Promise values', () => {
assert.isTrue(_.isPromise(new Promise(res => res(0))))
assert.isTrue(_.isPromise(new Promise(res => res(''))))
assert.isTrue(_.isPromise((async () => {})()))
assert.isTrue(_.isPromise((async () => { })()))
})
test('return false for non-Date values', () => {
assert.isFalse(_.isPromise(22))
assert.isFalse(_.isPromise({ name: 'x' }))
assert.isFalse(_.isPromise('abc'))
assert.isFalse(_.isPromise(String('abc')))
assert.isFalse(_.isPromise([1, 2, 3]))
assert.isFalse(_.isPromise(function work() {}))
assert.isFalse(_.isPromise(() => {}))
assert.isFalse(_.isPromise(function work() { }))
assert.isFalse(_.isPromise(() => { }))
assert.isFalse(_.isPromise(Symbol('')))
assert.isFalse(_.isPromise(Symbol('hello')))
assert.isFalse(_.isPromise({ then: 2 }))
Expand All @@ -381,7 +381,7 @@ describe('typed module', () => {
assert.isFalse(result)
})
test('returns false for empty class instance', () => {
class Data {}
class Data { }
const input = new Data()
const result = _.isSymbol(input)
assert.isFalse(result)
Expand Down Expand Up @@ -541,4 +541,38 @@ describe('typed module', () => {
assert.isFalse(_.isEqual([complex], [{ ...complex, num: 222 }]))
})
})

describe('isNullish function', () => {
test('returns true for null and undefined', () => {
assert.isTrue(_.isNullish(null))

Check failure on line 547 in src/tests/typed.test.ts

View workflow job for this annotation

GitHub Actions / verify-cdn-build-matches-src

Property 'isNullish' does not exist on type 'typeof import("/home/runner/work/radash/radash/src/index")'.
assert.isTrue(_.isNullish(undefined))

Check failure on line 548 in src/tests/typed.test.ts

View workflow job for this annotation

GitHub Actions / verify-cdn-build-matches-src

Property 'isNullish' does not exist on type 'typeof import("/home/runner/work/radash/radash/src/index")'.
})
test('returns false for non-nullish values', () => {
assert.isFalse(_.isNullish(0))

Check failure on line 551 in src/tests/typed.test.ts

View workflow job for this annotation

GitHub Actions / verify-cdn-build-matches-src

Property 'isNullish' does not exist on type 'typeof import("/home/runner/work/radash/radash/src/index")'.
assert.isFalse(_.isNullish(''))

Check failure on line 552 in src/tests/typed.test.ts

View workflow job for this annotation

GitHub Actions / verify-cdn-build-matches-src

Property 'isNullish' does not exist on type 'typeof import("/home/runner/work/radash/radash/src/index")'.
assert.isFalse(_.isNullish(false))

Check failure on line 553 in src/tests/typed.test.ts

View workflow job for this annotation

GitHub Actions / verify-cdn-build-matches-src

Property 'isNullish' does not exist on type 'typeof import("/home/runner/work/radash/radash/src/index")'.
assert.isFalse(_.isNullish([]))

Check failure on line 554 in src/tests/typed.test.ts

View workflow job for this annotation

GitHub Actions / verify-cdn-build-matches-src

Property 'isNullish' does not exist on type 'typeof import("/home/runner/work/radash/radash/src/index")'.
assert.isFalse(_.isNullish({}))

Check failure on line 555 in src/tests/typed.test.ts

View workflow job for this annotation

GitHub Actions / verify-cdn-build-matches-src

Property 'isNullish' does not exist on type 'typeof import("/home/runner/work/radash/radash/src/index")'.
assert.isFalse(_.isNullish(() => { }))

Check failure on line 556 in src/tests/typed.test.ts

View workflow job for this annotation

GitHub Actions / verify-cdn-build-matches-src

Property 'isNullish' does not exist on type 'typeof import("/home/runner/work/radash/radash/src/index")'.
assert.isFalse(_.isNullish(Symbol('')))

Check failure on line 557 in src/tests/typed.test.ts

View workflow job for this annotation

GitHub Actions / verify-cdn-build-matches-src

Property 'isNullish' does not exist on type 'typeof import("/home/runner/work/radash/radash/src/index")'.
assert.isFalse(_.isNullish(new Date()))

Check failure on line 558 in src/tests/typed.test.ts

View workflow job for this annotation

GitHub Actions / verify-cdn-build-matches-src

Property 'isNullish' does not exist on type 'typeof import("/home/runner/work/radash/radash/src/index")'.
})
})

describe('isNonNullish function', () => {
test('returns false for null and undefined', () => {
assert.isFalse(_.isNonNullish(null))
assert.isFalse(_.isNonNullish(undefined))
})
test('returns true for non-nullish values', () => {
assert.isTrue(_.isNonNullish(0))
assert.isTrue(_.isNonNullish(''))
assert.isTrue(_.isNonNullish(false))
assert.isTrue(_.isNonNullish([]))
assert.isTrue(_.isNonNullish({}))
assert.isTrue(_.isNonNullish(() => { }))
assert.isTrue(_.isNonNullish(Symbol('')))
assert.isTrue(_.isNonNullish(new Date()))
})
})
})
16 changes: 16 additions & 0 deletions src/typed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,19 @@ 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
}

0 comments on commit 3034b47

Please sign in to comment.