diff --git a/src/internal/__tests__/validate-string.spec-d.ts b/src/internal/__tests__/validate-string.spec-d.ts new file mode 100644 index 00000000..1050ec09 --- /dev/null +++ b/src/internal/__tests__/validate-string.spec-d.ts @@ -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() + }) +}) diff --git a/src/internal/__tests__/validate-string.spec.ts b/src/internal/__tests__/validate-string.spec.ts new file mode 100644 index 00000000..ff04ddcc --- /dev/null +++ b/src/internal/__tests__/validate-string.spec.ts @@ -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) + }) +}) diff --git a/src/internal/validate-string.ts b/src/internal/validate-string.ts new file mode 100644 index 00000000..191baadb --- /dev/null +++ b/src/internal/validate-string.ts @@ -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