Skip to content

Commit af44efb

Browse files
committed
chore: extract utils escapeUnicode
1 parent 3488bdb commit af44efb

File tree

5 files changed

+46
-17
lines changed

5 files changed

+46
-17
lines changed

client/src/components/ToolsTab.tsx

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,7 @@ import {
1616
import { AlertCircle, Send } from "lucide-react";
1717
import { useEffect, useState } from "react";
1818
import ListPane from "./ListPane";
19-
20-
// Utility function to escape Unicode characters
21-
function escapeUnicode(obj: any): string {
22-
return JSON.stringify(
23-
obj,
24-
(_key: string, value) => {
25-
if (typeof value === "string") {
26-
// Replace non-ASCII characters with their Unicode escape sequences
27-
return value.replace(/[^\0-\x7F]/g, (char) => {
28-
return "\\u" + ("0000" + char.charCodeAt(0).toString(16)).slice(-4);
29-
});
30-
}
31-
return value;
32-
},
33-
2,
34-
);
35-
}
19+
import { escapeUnicode } from "@/utils/escapeUnicode";
3620

3721
const ToolsTab = ({
3822
tools,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { escapeUnicode } from "../escapeUnicode";
2+
3+
describe("escapeUnicode", () => {
4+
it("should escape Unicode characters in a string", () => {
5+
const input = { text: "你好世界" };
6+
const expected = '{\n "text": "\\\\u4f60\\\\u597d\\\\u4e16\\\\u754c"\n}';
7+
expect(escapeUnicode(input)).toBe(expected);
8+
});
9+
10+
it("should handle empty strings", () => {
11+
const input = { text: "" };
12+
const expected = '{\n "text": ""\n}';
13+
expect(escapeUnicode(input)).toBe(expected);
14+
});
15+
16+
it("should handle null and undefined values", () => {
17+
const input = { text: null, value: undefined };
18+
const expected = '{\n "text": null\n}';
19+
expect(escapeUnicode(input)).toBe(expected);
20+
});
21+
22+
it("should handle numbers and booleans", () => {
23+
const input = { number: 123, boolean: true };
24+
const expected = '{\n "number": 123,\n "boolean": true\n}';
25+
expect(escapeUnicode(input)).toBe(expected);
26+
});
27+
});

client/src/utils/escapeUnicode.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Utility function to escape Unicode characters
2+
export function escapeUnicode(obj: unknown): string {
3+
return JSON.stringify(
4+
obj,
5+
(_key: string, value) => {
6+
if (typeof value === "string") {
7+
// Replace non-ASCII characters with their Unicode escape sequences
8+
return value.replace(/[^\0-\x7F]/g, (char) => {
9+
return "\\u" + ("0000" + char.charCodeAt(0).toString(16)).slice(-4);
10+
});
11+
}
12+
return value;
13+
},
14+
2,
15+
);
16+
}

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"ts-node": "^10.9.2"
4343
},
4444
"devDependencies": {
45+
"@types/jest": "^29.5.14",
4546
"@types/node": "^22.7.5",
4647
"@types/shell-quote": "^1.7.5",
4748
"prettier": "3.3.3"

0 commit comments

Comments
 (0)