-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
27696dc
commit 1eae687
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |