Skip to content

Commit

Permalink
feat(guards): isNIL
Browse files Browse the repository at this point in the history
  • Loading branch information
unicornware committed Nov 12, 2021
1 parent f27cf72 commit 011f516
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/guards/__tests__/is-nil.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { Testcase } from '@tests/utils/types'
import testSubject from '../is-nil.guard'

/**
* @file Unit Tests - isNIL
* @module tutils/guards/tests/unit/isNIL
*/

describe('unit:guards/isNIL', () => {
type Case = Testcase<boolean> & { state: string; value: any }

const cases: Case[] = [
{ expected: false, state: 'boolean', value: true },
{ expected: false, state: 'number', value: 13 },
{ expected: false, state: 'object', value: { data: 26 } },
{ expected: false, state: 'string', value: 'string' },
{ expected: true, state: 'null', value: null },
{ expected: true, state: 'undefined', value: undefined }
]

it.each<Case>(cases)('should return $expected given $state', testcase => {
// Arrange
const { expected, value } = testcase

// Act + Expect
expect(testSubject(value)).toBe(expected)
})
})
1 change: 1 addition & 0 deletions src/guards/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
* @module tutils/guards
*/

export { default as isNIL } from './is-nil.guard'
export { default as isNodeEnv } from './is-node-env.guard'
18 changes: 18 additions & 0 deletions src/guards/is-nil.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { NIL } from '@tutils/types'

/**
* @file Type Guards - isNIL
* @module tutils/guards/isNIL
*/

/**
* Checks if `value` is a {@link NIL}.
*
* @param {any} [value] - Value to check
* @return {boolean} `true` if `value` is `null` or `undefined`
*/
const isNIL = (value?: any): value is NIL => {
return value === null || value === undefined
}

export default isNIL

0 comments on commit 011f516

Please sign in to comment.