Skip to content

Commit 866bc88

Browse files
committed
feat(guards): isExceptionId
1 parent c7fac42 commit 866bc88

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import ExceptionCode from '@packages/exceptions/enums/exception-code.enum'
2+
import ExceptionId from '@packages/exceptions/enums/exception-id.enum'
3+
import type { Testcase } from '@tests/utils/types'
4+
import testSubject from '../is-exception-id.guard'
5+
6+
/**
7+
* @file Unit Tests - isExceptionId
8+
* @module exceptions/guards/tests/unit/isExceptionId
9+
*/
10+
11+
describe('unit:guards/isExceptionId', () => {
12+
type Case = Testcase<boolean> & { value: any }
13+
14+
const cases: Case[] = [
15+
{ expected: false, value: ExceptionCode.UNAUTHORIZED },
16+
{ expected: true, value: ExceptionId.I_AM_A_TEAPOT }
17+
]
18+
19+
it.each<Case>(cases)('should return $expected given $value', testcase => {
20+
// Arrange
21+
const { expected, value } = testcase
22+
23+
// Act + Expect
24+
expect(testSubject(value)).toBe(expected)
25+
})
26+
})

packages/exceptions/src/guards/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
*/
55

66
export { default as isExceptionCode } from './is-exception-code.guard'
7+
export { default as isExceptionId } from './is-exception-id.guard'
78
export { default as isExceptionJSON } from './is-exception-json.guard'
89
export { default as isException } from './is-exception.guard'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import ExceptionId from '@packages/exceptions/enums/exception-id.enum'
2+
3+
/**
4+
* @file Type Guards - isExceptionId
5+
* @module exceptions/guards/isExceptionId
6+
*/
7+
8+
/**
9+
* Checks if `value` is a valid `ExceptionId`.
10+
*
11+
* @param {any} [value] - Value to check
12+
* @return {boolean} `true` if `ExceptionId`, `false` otherwise
13+
*/
14+
const isExceptionId = (value: any = {}): value is ExceptionId => {
15+
return Object.keys(ExceptionId).includes(value)
16+
}
17+
18+
export default isExceptionId

0 commit comments

Comments
 (0)