Skip to content

Commit

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

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

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

const cases: Case[] = [
{ expected: false, state: 'array', value: [] },
{ expected: false, state: 'boolean', value: false },
{ expected: false, state: 'non-empty string', value: 'string value' },
{ expected: false, state: 'number', value: 13 },
{ expected: false, state: 'object', value: { data: 26 } },
{ expected: true, state: 'empty string (trimmed)', value: '' },
{ expected: true, state: 'empty string (untrimmed)', value: ' ' }
]

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 @@ -4,5 +4,6 @@
*/

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

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

/**
* Checks if `value` is an empty string.
*
* @param {any} [value] - Value to check
* @return {boolean} `true` if `value` is an empty string
*/
const isEmptyString = (value?: any): value is EmptyString => {
if (typeof value !== 'string') return false
return value.trim() === ''
}

export default isEmptyString

0 comments on commit 141902a

Please sign in to comment.