Skip to content

Commit

Permalink
feat(internal): validateString
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Dec 7, 2022
1 parent 27696dc commit 1eae687
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/internal/__tests__/validate-string.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @file Type Tests - validateString
* @module pathe/internal/tests/unit-d/validateString
*/

import testSubject from '../validate-string'

describe('unit-d:internal/validateString', () => {
it('should extract string guard value', () => {
expectTypeOf(testSubject).guards.toBeString()
})
})
34 changes: 34 additions & 0 deletions src/internal/__tests__/validate-string.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @file Unit Tests - validateString
* @module pathe/internal/tests/unit/validateString
*/

import ERR_INVALID_ARG_TYPE from '../err-invalid-arg-type'
import testSubject from '../validate-string'

describe('unit:internal/validateString', () => {
let name: string

beforeEach(() => {
name = 'path'
})

it('should return true if value is typeof string', () => {
expect(testSubject(faker.datatype.string(13), name)).to.be.true
})

it('should throw if value is not typeof string', () => {
// Arrange
let error: ERR_INVALID_ARG_TYPE

// Act
try {
testSubject(null, name)
} catch (e: unknown) {
error = e as typeof error
}

// Expect
expect(error!).to.be.instanceOf(ERR_INVALID_ARG_TYPE)
})
})
25 changes: 25 additions & 0 deletions src/internal/validate-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @file Internal - validateString
* @module pathe/internal/validateString
*/

import ERR_INVALID_ARG_TYPE from './err-invalid-arg-type'

/**
* Checks if `value` is a string.
*
* Throws {@linkcode ERR_INVALID_ARG_TYPE} if `value` is not a string.
*
* [1]: https://nodejs.org/api/errors.html#err_invalid_arg_type
*
* @param {unknown} value - Possible string value
* @param {string} name - `value` label
* @return {boolean} `true` if `value` is a string
* @throws {ERR_INVALID_ARG_TYPE}
*/
const validateString = (value: unknown, name: string): value is string => {
if (typeof value === 'string') return true
throw new ERR_INVALID_ARG_TYPE(name, 'string', value)
}

export default validateString

0 comments on commit 1eae687

Please sign in to comment.