Skip to content

Commit 71e8176

Browse files
authored
Merge pull request #44 from klmhyeonwoo/bugfix/42
bugfix: Fixed an error that occurred when undefined was entered in unescapeHtml and escapeHtml
2 parents 1e2ee87 + 014ab1c commit 71e8176

5 files changed

Lines changed: 77 additions & 15 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ dist
2121
*.njsproj
2222
*.sln
2323
*.sw?
24+
25+
vitest-report.xml
Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
1-
import { expect, test } from "vitest";
1+
import { describe, expect, test } from "vitest";
22
import escapeHtml from ".";
33

4-
test("HTML 특수 문자를 이스케이프한다.", () => {
5-
const input = "<span> 안녕하세요 </span>";
6-
const output = escapeHtml(input);
7-
expect(output).toBe("&lt;span&gt; 안녕하세요 &lt;/span&gt;");
4+
describe("escapeHtml 유틸 함수 테스트", () => {
5+
test("HTML 특수 문자를 올바르게 이스케이프한다", () => {
6+
const input = "<span> 안녕하세요 </span>";
7+
const output = escapeHtml(input);
8+
expect(output).toBe("&lt;span&gt; 안녕하세요 &lt;/span&gt;");
9+
});
10+
11+
test("여러 HTML 특수 문자를 포함한 문자열을 올바르게 이스케이프한다", () => {
12+
const input = "Tom & Jerry <3 \"Best Friends\" 'Forever' / Fun";
13+
const output = escapeHtml(input);
14+
expect(output).toBe(
15+
"Tom &amp; Jerry &lt;3 &quot;Best Friends&quot; &#39;Forever&#39; / Fun"
16+
);
17+
});
18+
19+
test("HTML 특수 문자가 없는 문자열은 그대로 반환한다", () => {
20+
const input = "Hello World!";
21+
const output = escapeHtml(input);
22+
expect(output).toBe("Hello World!");
23+
});
24+
25+
test("빈 문자열을 입력하면 빈 문자열을 반환한다", () => {
26+
const input = "";
27+
const output = escapeHtml(input);
28+
expect(output).toBe("");
29+
});
30+
31+
test("null 또는 undefined를 입력하면 그대로 반환한다", () => {
32+
// @ts-ignore
33+
expect(escapeHtml(null)).toBe(null);
34+
// @ts-ignore
35+
expect(escapeHtml(undefined)).toBe(undefined);
36+
});
837
});

package/stringUtil/escapeHtml/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
export default function escapeHtml(str: string): string {
1+
export default function escapeHtml(str: string | null | undefined): string | null | undefined {
2+
if (str == null) return str;
23
return str
34
.replace(/&/g, "&amp;")
45
.replace(/</g, "&lt;")
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,36 @@
1-
import { expect, test } from "vitest";
1+
import { describe, expect, test } from "vitest";
22
import unescapeHtml from ".";
33

44
test("HTML 특수 문자를 언이스케이프한다.", () => {
55
const input = "&lt;span&gt; 안녕하세요 &lt;/span&gt;";
66
const output = unescapeHtml(input);
77
expect(output).toBe("<span> 안녕하세요 </span>");
88
});
9+
10+
describe("unescapeHtml 유틸 함수 테스트", () => {
11+
test("여러 HTML 특수 문자를 포함한 문자열을 올바르게 언이스케이프한다", () => {
12+
const input =
13+
"Tom &amp; Jerry &lt;3 &quot;Best Friends&quot; &#39;Forever&#39; &#x2F; Fun";
14+
const output = unescapeHtml(input);
15+
expect(output).toBe("Tom & Jerry <3 \"Best Friends\" 'Forever' / Fun");
16+
});
17+
18+
test("HTML 특수 문자가 없는 문자열은 그대로 반환한다", () => {
19+
const input = "Hello World!";
20+
const output = unescapeHtml(input);
21+
expect(output).toBe("Hello World!");
22+
});
23+
24+
test("빈 문자열을 입력하면 빈 문자열을 반환한다", () => {
25+
const input = "";
26+
const output = unescapeHtml(input);
27+
expect(output).toBe("");
28+
});
29+
30+
test("null 또는 undefined를 입력하면 그대로 반환한다", () => {
31+
// @ts-ignore
32+
expect(unescapeHtml(null)).toBe(null);
33+
// @ts-ignore
34+
expect(unescapeHtml(undefined)).toBe(undefined);
35+
});
36+
});
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
export default function unescapeHtml(str: string): string {
2-
return str.replace(/&amp;/g, "&")
3-
.replace(/&lt;/g, "<")
4-
.replace(/&gt;/g, ">")
5-
.replace(/&quot;/g, "\"")
6-
.replace(/&#39;/g, "'")
7-
.replace(/&#x2F;/g, "/");
8-
}
1+
export default function unescapeHtml(str: string | null | undefined): string | null | undefined {
2+
if (str == null) return str;
3+
return str
4+
.replace(/&amp;/g, "&")
5+
.replace(/&lt;/g, "<")
6+
.replace(/&gt;/g, ">")
7+
.replace(/&quot;/g, '"')
8+
.replace(/&#39;/g, "'")
9+
.replace(/&#x2F;/g, "/");
10+
}

0 commit comments

Comments
 (0)