Skip to content

Commit

Permalink
feat(internal): validateURLString
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Feb 3, 2023
1 parent 3245972 commit 992bfb0
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 0 deletions.
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@
"vitest-github-actions-reporter": "0.9.0",
"yaml-eslint-parser": "1.1.0"
},
"peerDependencies": {
"@types/node": ">=14.18.36"
},
"peerDependenciesMeta": {
"@types/node": {
"optional": true
}
},
"resolutions": {
"@ardatan/sync-fetch": "larsgw/sync-fetch#head=worker_threads",
"@flex-development/tutils": "6.0.0-alpha.10"
Expand Down
13 changes: 13 additions & 0 deletions src/internal/__tests__/validate-url-string.spec-d.ts
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>()
})
})
41 changes: 41 additions & 0 deletions src/internal/__tests__/validate-url-string.spec.ts
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)
})
})
1 change: 1 addition & 0 deletions src/internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

export { default as validateFunction } from './validate-function'
export { default as validateString } from './validate-string'
export { default as validateURLString } from './validate-url-string'
32 changes: 32 additions & 0 deletions src/internal/validate-url-string.ts
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
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,11 @@ __metadata:
vitest: "npm:0.28.3"
vitest-github-actions-reporter: "npm:0.9.0"
yaml-eslint-parser: "npm:1.1.0"
peerDependencies:
"@types/node": ">=14.18.36"
peerDependenciesMeta:
"@types/node":
optional: true
languageName: unknown
linkType: soft

Expand Down

0 comments on commit 992bfb0

Please sign in to comment.