-
-
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 <unicornware@flexdevelopment.llc>
- Loading branch information
1 parent
3245972
commit 992bfb0
Showing
6 changed files
with
100 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
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,13 @@ | ||
/** | ||
* @file Type Tests - validateURLString | ||
* @module tsconfig-utils/internal/tests/unit-d/validateURLString | ||
*/ | ||
|
||
import type { URL } from 'node:url' | ||
import type testSubject from '../validate-url-string' | ||
|
||
describe('unit-d:internal/validateURLString', () => { | ||
it('should guard URL | string', () => { | ||
expectTypeOf<typeof testSubject>().guards.toEqualTypeOf<URL | string>() | ||
}) | ||
}) |
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,41 @@ | ||
/** | ||
* @file Unit Tests - validateURLString | ||
* @module tsconfig-utils/internal/tests/unit/validateURLString | ||
*/ | ||
|
||
import { ErrorCode, type NodeError } from '@flex-development/errnode' | ||
import { URL } from 'node:url' | ||
import testSubject from '../validate-url-string' | ||
|
||
describe('unit:internal/validateURLString', () => { | ||
let name: string | ||
|
||
beforeEach(() => { | ||
name = 'id' | ||
}) | ||
|
||
it('should return true if value is instance of URL', () => { | ||
expect(testSubject(new URL(import.meta.url), name)).to.be.true | ||
}) | ||
|
||
it('should return true if value is typeof string', () => { | ||
expect(testSubject(import.meta.url, name)).to.be.true | ||
}) | ||
|
||
it('should throw if value is not instance of URL or typeof string', () => { | ||
// Arrange | ||
const code: ErrorCode = ErrorCode.ERR_INVALID_ARG_TYPE | ||
let error: NodeError<TypeError> | ||
|
||
// Act | ||
try { | ||
testSubject(faker.datatype.boolean(), name) | ||
} catch (e: unknown) { | ||
error = e as typeof error | ||
} | ||
|
||
// Expect | ||
expect(error!).to.be.instanceof(TypeError) | ||
expect(error!).to.have.property('code').equal(code) | ||
}) | ||
}) |
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
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,32 @@ | ||
/** | ||
* @file Internal - validateURLString | ||
* @module tsconfig-utils/internal/validateURLString | ||
*/ | ||
|
||
import { ERR_INVALID_ARG_TYPE, type NodeError } from '@flex-development/errnode' | ||
import { isString } from '@flex-development/tutils' | ||
import { URL } from 'node:url' | ||
|
||
/** | ||
* Checks if given `value` is an instance of {@linkcode URL} or a string. | ||
* | ||
* Throws [`ERR_INVALID_ARG_TYPE`][1] if the `value` is of neither type. | ||
* | ||
* [1]: https://nodejs.org/api/errors.html#err_invalid_arg_value | ||
* | ||
* @see {@linkcode ERR_INVALID_ARG_TYPE} | ||
* | ||
* @param {unknown} value - Value supplied by user | ||
* @param {string} name - Name of invalid argument or property | ||
* @return {value is URL | string} `true` if `value` is `URL` instance or string | ||
* @throws {NodeError<TypeError>} If `value` is not `URL` instance or string | ||
*/ | ||
const validateURLString = ( | ||
value: unknown, | ||
name: string | ||
): value is URL | string => { | ||
if (value instanceof URL || isString(value)) return true | ||
throw new ERR_INVALID_ARG_TYPE(name, ['URL', 'string'], value) | ||
} | ||
|
||
export default validateURLString |
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