diff --git a/.gitignore b/.gitignore index e47c70b..9fd42ff 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,5 @@ dist *.njsproj *.sln *.sw? + +vitest-report.xml diff --git a/package/stringUtil/escapeHtml/index.test.ts b/package/stringUtil/escapeHtml/index.test.ts index 9fec021..787bc38 100644 --- a/package/stringUtil/escapeHtml/index.test.ts +++ b/package/stringUtil/escapeHtml/index.test.ts @@ -1,8 +1,37 @@ -import { expect, test } from "vitest"; +import { describe, expect, test } from "vitest"; import escapeHtml from "."; -test("HTML 특수 문자를 이스케이프한다.", () => { - const input = " 안녕하세요 "; - const output = escapeHtml(input); - expect(output).toBe("<span> 안녕하세요 </span>"); +describe("escapeHtml 유틸 함수 테스트", () => { + test("HTML 특수 문자를 올바르게 이스케이프한다", () => { + const input = " 안녕하세요 "; + const output = escapeHtml(input); + expect(output).toBe("<span> 안녕하세요 </span>"); + }); + + test("여러 HTML 특수 문자를 포함한 문자열을 올바르게 이스케이프한다", () => { + const input = "Tom & Jerry <3 \"Best Friends\" 'Forever' / Fun"; + const output = escapeHtml(input); + expect(output).toBe( + "Tom & Jerry <3 "Best Friends" 'Forever' / Fun" + ); + }); + + test("HTML 특수 문자가 없는 문자열은 그대로 반환한다", () => { + const input = "Hello World!"; + const output = escapeHtml(input); + expect(output).toBe("Hello World!"); + }); + + test("빈 문자열을 입력하면 빈 문자열을 반환한다", () => { + const input = ""; + const output = escapeHtml(input); + expect(output).toBe(""); + }); + + test("null 또는 undefined를 입력하면 그대로 반환한다", () => { + // @ts-ignore + expect(escapeHtml(null)).toBe(null); + // @ts-ignore + expect(escapeHtml(undefined)).toBe(undefined); + }); }); diff --git a/package/stringUtil/escapeHtml/index.ts b/package/stringUtil/escapeHtml/index.ts index 2c43151..9fc1d7b 100644 --- a/package/stringUtil/escapeHtml/index.ts +++ b/package/stringUtil/escapeHtml/index.ts @@ -1,4 +1,5 @@ -export default function escapeHtml(str: string): string { +export default function escapeHtml(str: string | null | undefined): string | null | undefined { + if (str == null) return str; return str .replace(/&/g, "&") .replace(/ { @@ -6,3 +6,31 @@ test("HTML 특수 문자를 언이스케이프한다.", () => { const output = unescapeHtml(input); expect(output).toBe(" 안녕하세요 "); }); + +describe("unescapeHtml 유틸 함수 테스트", () => { + test("여러 HTML 특수 문자를 포함한 문자열을 올바르게 언이스케이프한다", () => { + const input = + "Tom & Jerry <3 "Best Friends" 'Forever' / Fun"; + const output = unescapeHtml(input); + expect(output).toBe("Tom & Jerry <3 \"Best Friends\" 'Forever' / Fun"); + }); + + test("HTML 특수 문자가 없는 문자열은 그대로 반환한다", () => { + const input = "Hello World!"; + const output = unescapeHtml(input); + expect(output).toBe("Hello World!"); + }); + + test("빈 문자열을 입력하면 빈 문자열을 반환한다", () => { + const input = ""; + const output = unescapeHtml(input); + expect(output).toBe(""); + }); + + test("null 또는 undefined를 입력하면 그대로 반환한다", () => { + // @ts-ignore + expect(unescapeHtml(null)).toBe(null); + // @ts-ignore + expect(unescapeHtml(undefined)).toBe(undefined); + }); +}); diff --git a/package/stringUtil/unescapeHtml/index.ts b/package/stringUtil/unescapeHtml/index.ts index 1d85ac6..d126b7d 100644 --- a/package/stringUtil/unescapeHtml/index.ts +++ b/package/stringUtil/unescapeHtml/index.ts @@ -1,8 +1,10 @@ -export default function unescapeHtml(str: string): string { - return str.replace(/&/g, "&") - .replace(/</g, "<") - .replace(/>/g, ">") - .replace(/"/g, "\"") - .replace(/'/g, "'") - .replace(///g, "/"); -} \ No newline at end of file +export default function unescapeHtml(str: string | null | undefined): string | null | undefined { + if (str == null) return str; + return str + .replace(/&/g, "&") + .replace(/</g, "<") + .replace(/>/g, ">") + .replace(/"/g, '"') + .replace(/'/g, "'") + .replace(///g, "/"); +}