From de20d7218a5e440f0688237757caf4f71274ce2a Mon Sep 17 00:00:00 2001 From: Veldhuis Date: Thu, 11 Apr 2024 22:30:08 +0200 Subject: [PATCH] feat: add util to check whether type is valid GitHub alert type --- src/is-github-alert-type.test.ts | 29 +++++++++++++++++++++++++++++ src/is-github-alert-type.ts | 5 +++++ 2 files changed, 34 insertions(+) create mode 100644 src/is-github-alert-type.test.ts create mode 100644 src/is-github-alert-type.ts diff --git a/src/is-github-alert-type.test.ts b/src/is-github-alert-type.test.ts new file mode 100644 index 0000000..dbb0860 --- /dev/null +++ b/src/is-github-alert-type.test.ts @@ -0,0 +1,29 @@ +import { describe, expect, it } from "vitest"; +import {isGithubAlertType} from "./is-github-alert-type.js"; +import {GithubAlertType} from "./github-alert.type.js"; + +describe('is-github-alert-type', () => { + it('should return false for an undefined value', () => { + expect(isGithubAlertType(undefined)).toEqual(false); + }); + + it('should return false for a null value', () => { + expect(isGithubAlertType(null)).toEqual(false); + }); + + it('should return false for a string value not equal to a GitHub alert type', () => { + expect(isGithubAlertType("test")).toEqual(false); + }); + + it('should return true for a value directly taken from the GitHub alert type enum', () => { + expect(isGithubAlertType(GithubAlertType.WARNING)).toEqual(true); + }); + + it('should return true for a string value that equals an alert type', () => { + expect(isGithubAlertType("IMPORTANT")).toEqual(true); + }); + + it('should return false for a string value with the wrong capitalization', () => { + expect(isGithubAlertType("important")).toEqual(false); + }); +}) \ No newline at end of file diff --git a/src/is-github-alert-type.ts b/src/is-github-alert-type.ts new file mode 100644 index 0000000..fb2f551 --- /dev/null +++ b/src/is-github-alert-type.ts @@ -0,0 +1,5 @@ +import {GITHUB_ALERT_TYPES, type GithubAlertType} from "./github-alert.type.js"; + +export function isGithubAlertType(type: unknown): type is GithubAlertType { + return GITHUB_ALERT_TYPES.includes(type as GithubAlertType); +} \ No newline at end of file