File tree Expand file tree Collapse file tree 5 files changed +46
-17
lines changed
Expand file tree Collapse file tree 5 files changed +46
-17
lines changed Original file line number Diff line number Diff line change @@ -16,23 +16,7 @@ import {
1616import { AlertCircle , Send } from "lucide-react" ;
1717import { useEffect , useState } from "react" ;
1818import 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
3721const ToolsTab = ( {
3822 tools,
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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"
You can’t perform that action at this time.
0 commit comments