From 1196d6c7bfd22548cd843eb7e70588ba5718e241 Mon Sep 17 00:00:00 2001 From: klmhyeonwoo Date: Mon, 1 Sep 2025 21:30:39 +0900 Subject: [PATCH 1/2] feat: Add checkBase64 util function --- README.md | 2 ++ package/validationUtil/checkBase64/index.test.ts | 14 ++++++++++++++ package/validationUtil/checkBase64/index.ts | 5 +++++ package/validationUtil/index.ts | 1 + 4 files changed, 22 insertions(+) create mode 100644 package/validationUtil/checkBase64/index.test.ts create mode 100644 package/validationUtil/checkBase64/index.ts diff --git a/README.md b/README.md index 4410022..9eff0bf 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ const difference = numberUtil.subtract(10, 3); // 7 const isValid = validationUtil.checkEmail("user@example.com"); // true const isHttpUrl = validationUtil.checkHttpUrl("https://example.com"); // true const isDomain = validationUtil.checkDomain("example.com"); // true +const isBase64 = validationUtil.checkBase64("U29tZSB2YWxpZCBiYXNlNjQgc3RyaW5n"); // true // Common utilities const empty = commonUtil.isEmpty(""); // true @@ -83,6 +84,7 @@ const theme = cookieUtil.getCookie("theme"); - `checkEmail(email: string): boolean` - Validates email format - `checkHttpUrl(url: string): boolean` - Validates HTTP/HTTPS URL format - `checkDomain(domain: string): boolean` - Validates domain name format +- `checkBase64(value: string): boolean` - Validates whether a string is a valid base64 encoded value ### CommonUtil diff --git a/package/validationUtil/checkBase64/index.test.ts b/package/validationUtil/checkBase64/index.test.ts new file mode 100644 index 0000000..11cbf40 --- /dev/null +++ b/package/validationUtil/checkBase64/index.test.ts @@ -0,0 +1,14 @@ +import { describe, expect, it } from "vitest"; +import checkBase64 from "."; + +describe("checkBase64 유틸 함수 테스트", () => { + it("유효한 base64 문자열에 대해 true를 반환해야 합니다.", () => { + expect(checkBase64("U29tZSB2YWxpZCBiYXNlNjQgc3RyaW5n")).toBe(true); + expect(checkBase64("SGVsbG8gd29ybGQ=")).toBe(true); + }); + + it("유효하지 않은 base64 문자열에 대해 false를 반환해야 합니다.", () => { + expect(checkBase64("bfdbfdbsba")).toBe(false); + expect(checkBase64("U29tZSB2YWxpZCBiYXNlNjQgc3RyaW5n===")).toBe(false); + }); +}); diff --git a/package/validationUtil/checkBase64/index.ts b/package/validationUtil/checkBase64/index.ts new file mode 100644 index 0000000..0967742 --- /dev/null +++ b/package/validationUtil/checkBase64/index.ts @@ -0,0 +1,5 @@ +export default function checkBase64(value: string): boolean { + const base64Regex = + /^(?:[A-Z0-9+\/]{4})*(?:[A-Z0-9+\/]{2}==|[A-Z0-9+\/]{3}=)?$/i; + return base64Regex.test(value); +} diff --git a/package/validationUtil/index.ts b/package/validationUtil/index.ts index f6f495d..e49429b 100644 --- a/package/validationUtil/index.ts +++ b/package/validationUtil/index.ts @@ -1,3 +1,4 @@ export { default as checkEmail } from "./checkEmail"; export { default as checkHttpUrl } from "./checkHttpUrl"; export { default as checkDomain } from "./checkDomain"; +export { default as checkBase64 } from "./checkBase64"; From adfb5abd233121b0ed7d007d9a635bc10dabb74f Mon Sep 17 00:00:00 2001 From: klmhyeonwoo Date: Mon, 1 Sep 2025 21:34:41 +0900 Subject: [PATCH 2/2] feat: Exception handling for empty strings --- package/validationUtil/checkBase64/index.test.ts | 5 +++++ package/validationUtil/checkBase64/index.ts | 1 + 2 files changed, 6 insertions(+) diff --git a/package/validationUtil/checkBase64/index.test.ts b/package/validationUtil/checkBase64/index.test.ts index 11cbf40..639e6ff 100644 --- a/package/validationUtil/checkBase64/index.test.ts +++ b/package/validationUtil/checkBase64/index.test.ts @@ -11,4 +11,9 @@ describe("checkBase64 유틸 함수 테스트", () => { expect(checkBase64("bfdbfdbsba")).toBe(false); expect(checkBase64("U29tZSB2YWxpZCBiYXNlNjQgc3RyaW5n===")).toBe(false); }); + + it("만약 빈 문자열로 함수를 호출할 경우 false를 반환해야 합니다.", () => { + expect(checkBase64("")).toBe(false); + expect(checkBase64(" ")).toBe(false); + }); }); diff --git a/package/validationUtil/checkBase64/index.ts b/package/validationUtil/checkBase64/index.ts index 0967742..dfa2976 100644 --- a/package/validationUtil/checkBase64/index.ts +++ b/package/validationUtil/checkBase64/index.ts @@ -1,4 +1,5 @@ export default function checkBase64(value: string): boolean { + if (value.trim() === "") return false; const base64Regex = /^(?:[A-Z0-9+\/]{4})*(?:[A-Z0-9+\/]{2}==|[A-Z0-9+\/]{3}=)?$/i; return base64Regex.test(value);