File tree Expand file tree Collapse file tree 4 files changed +82
-0
lines changed Expand file tree Collapse file tree 4 files changed +82
-0
lines changed Original file line number Diff line number Diff line change
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
+ } )
Original file line number Diff line number Diff line change
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
+ } )
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @file Entry Point - Internal
3
+ * @module ext-regex/internal
4
+ */
5
+
6
+ export { default as validateString } from './validate-string'
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments