Skip to content

Commit d84d88a

Browse files
committed
feat(internal): validateString
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent c7dfaab commit d84d88a

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @file Type Tests - validateString
3+
* @module ext-regex/internal/tests/unit-d/validateString
4+
*/
5+
6+
import type testSubject from '../validate-string'
7+
8+
describe('unit-d:internal/validateString', () => {
9+
it('should guard string', () => {
10+
expectTypeOf<typeof testSubject>().guards.toBeString()
11+
})
12+
})
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @file Unit Tests - validateString
3+
* @module ext-regex/internal/tests/unit/validateString
4+
*/
5+
6+
import { ErrorCode, type NodeError } from '@flex-development/errnode'
7+
import testSubject from '../validate-string'
8+
9+
describe('unit:internal/validateString', () => {
10+
let name: string
11+
12+
beforeEach(() => {
13+
name = 'ext'
14+
})
15+
16+
it('should return true if value is typeof string', () => {
17+
expect(testSubject('.mjs', name)).to.be.true
18+
})
19+
20+
it('should throw if value is not typeof string', () => {
21+
// Arrange
22+
const code: ErrorCode = ErrorCode.ERR_INVALID_ARG_TYPE
23+
let error: NodeError<TypeError>
24+
25+
// Act
26+
try {
27+
testSubject(null, name)
28+
} catch (e: unknown) {
29+
error = e as typeof error
30+
}
31+
32+
// Expect
33+
expect(error!).to.be.instanceof(TypeError)
34+
expect(error!).to.have.property('code').equal(code)
35+
})
36+
})

src/internal/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* @file Entry Point - Internal
3+
* @module ext-regex/internal
4+
*/
5+
6+
export { default as validateString } from './validate-string'

src/internal/validate-string.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @file Internal - validateString
3+
* @module ext-regex/internal/validateString
4+
*/
5+
6+
import * as errnode from '@flex-development/errnode'
7+
8+
/**
9+
* Checks if given `value` is a string.
10+
*
11+
* Throws [`ERR_INVALID_ARG_TYPE`][1] if the `value` is not a string.
12+
*
13+
* [1]: https://nodejs.org/api/errors.html#err_invalid_arg_value
14+
*
15+
* @see {@linkcode errnode.ERR_INVALID_ARG_TYPE}
16+
* @see {@linkcode errnode.NodeError}
17+
*
18+
* @param {unknown} value - Value supplied by user
19+
* @param {string} name - Name of invalid argument or property
20+
* @return {value is string} `true` if `value` is a string
21+
* @throws {errnode.NodeError<TypeError>} If `value` is not a string
22+
*/
23+
const validateString = (value: unknown, name: string): value is string => {
24+
if (typeof value === 'string') return true
25+
throw new errnode.ERR_INVALID_ARG_TYPE(name, 'string', value)
26+
}
27+
28+
export default validateString

0 commit comments

Comments
 (0)