Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ dist
*.njsproj
*.sln
*.sw?

vitest-report.xml
39 changes: 34 additions & 5 deletions package/stringUtil/escapeHtml/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
import { expect, test } from "vitest";
import { describe, expect, test } from "vitest";
import escapeHtml from ".";

test("HTML 특수 문자를 이스케이프한다.", () => {
const input = "<span> 안녕하세요 </span>";
const output = escapeHtml(input);
expect(output).toBe("&lt;span&gt; 안녕하세요 &lt;/span&gt;");
describe("escapeHtml 유틸 함수 테스트", () => {
test("HTML 특수 문자를 올바르게 이스케이프한다", () => {
const input = "<span> 안녕하세요 </span>";
const output = escapeHtml(input);
expect(output).toBe("&lt;span&gt; 안녕하세요 &lt;/span&gt;");
});

test("여러 HTML 특수 문자를 포함한 문자열을 올바르게 이스케이프한다", () => {
const input = "Tom & Jerry <3 \"Best Friends\" 'Forever' / Fun";
const output = escapeHtml(input);
expect(output).toBe(
"Tom &amp; Jerry &lt;3 &quot;Best Friends&quot; &#39;Forever&#39; / 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);
});
});
3 changes: 2 additions & 1 deletion package/stringUtil/escapeHtml/index.ts
Original file line number Diff line number Diff line change
@@ -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, "&amp;")
.replace(/</g, "&lt;")
Expand Down
30 changes: 29 additions & 1 deletion package/stringUtil/unescapeHtml/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
import { expect, test } from "vitest";
import { describe, expect, test } from "vitest";
import unescapeHtml from ".";

test("HTML 특수 문자를 언이스케이프한다.", () => {
const input = "&lt;span&gt; 안녕하세요 &lt;/span&gt;";
const output = unescapeHtml(input);
expect(output).toBe("<span> 안녕하세요 </span>");
});

describe("unescapeHtml 유틸 함수 테스트", () => {
test("여러 HTML 특수 문자를 포함한 문자열을 올바르게 언이스케이프한다", () => {
const input =
"Tom &amp; Jerry &lt;3 &quot;Best Friends&quot; &#39;Forever&#39; &#x2F; 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);
});
});
18 changes: 10 additions & 8 deletions package/stringUtil/unescapeHtml/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export default function unescapeHtml(str: string): string {
return str.replace(/&amp;/g, "&")
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">")
.replace(/&quot;/g, "\"")
.replace(/&#39;/g, "'")
.replace(/&#x2F;/g, "/");
}
export default function unescapeHtml(str: string | null | undefined): string | null | undefined {
if (str == null) return str;
return str
.replace(/&amp;/g, "&")
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">")
.replace(/&quot;/g, '"')
.replace(/&#39;/g, "'")
.replace(/&#x2F;/g, "/");
}