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..639e6ff --- /dev/null +++ b/package/validationUtil/checkBase64/index.test.ts @@ -0,0 +1,19 @@ +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); + }); + + it("만약 빈 문자열로 함수를 호출할 경우 false를 반환해야 합니다.", () => { + expect(checkBase64("")).toBe(false); + expect(checkBase64(" ")).toBe(false); + }); +}); diff --git a/package/validationUtil/checkBase64/index.ts b/package/validationUtil/checkBase64/index.ts new file mode 100644 index 0000000..dfa2976 --- /dev/null +++ b/package/validationUtil/checkBase64/index.ts @@ -0,0 +1,6 @@ +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); +} 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";