Skip to content

Commit e675a12

Browse files
committed
feat: JSON格式化增加格式化JS能力
1 parent d40ead7 commit e675a12

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

Diff for: src/app/json/stringify-parse/page.tsx

+12-7
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ import { Space, Button, ButtonProps } from 'antd';
99
import { useMonacoEditor } from '../../../utils/config/editor';
1010
import * as json from '../../../utils/tools/json';
1111

12-
import s from './index.module.scss';
1312
import { copyToClipboard } from '../../../utils/tools/copy';
1413
import { storageStringifyParseValue } from '../../../utils/storage/json';
1514

15+
import s from './index.module.scss';
16+
1617
const demoText = `{
17-
"name": "John Doe",
18-
"text": "Line1\\nLine2\\tTabbed",
19-
"info": {
20-
"age": 30,
18+
"Name": "Rai120",
19+
"Github": "https://github.com/rain120",
20+
"Info": {
21+
"age": 20,
2122
"active": true,
22-
"description": "他说:“你好,世界!”"
23+
"description": "😊他说:“你好,世界!”"
2324
}
2425
}`;
2526

@@ -39,7 +40,7 @@ enum OperateType {
3940
export default function Page() {
4041
const { editor } = useMonacoEditor();
4142
// console.log('editorRef', editorRef.current, editor);
42-
const cache = storageStringifyParseValue()
43+
const cache = storageStringifyParseValue();
4344

4445
useEffect(() => {
4546
const value = editor?.getValue();
@@ -61,6 +62,10 @@ export default function Page() {
6162
(type: OperateType) => {
6263
const value = editor?.getValue();
6364

65+
if (!json.isJSON(value)) {
66+
editor.setValue(json.convert2JSON(value));
67+
}
68+
6469
switch (type) {
6570
case OperateType.format:
6671
editor.handleFormatDocument();

Diff for: src/utils/tools/json.ts

+39-8
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,14 @@ export function chinesePunctuationToEnglish(
126126
'?': '?',
127127
':': ':',
128128
';': ';',
129-
'“': escape ? '\\\"' : '"',
130-
'”': escape ? '\\\"' : '"',
131-
'‘': escape ? '\\\'' : "'",
132-
'’': escape ? '\\\'' : "'",
133-
'(': escape ? '\\\(' : '(',
134-
')': escape ? '\\\)' : ')',
135-
'【': escape ? '\\\[' : '[',
136-
'】': escape ? '\\\]' : ']',
129+
'“': escape ? '\\"' : '"',
130+
'”': escape ? '\\"' : '"',
131+
'‘': escape ? "\\'" : "'",
132+
'’': escape ? "\\'" : "'",
133+
'(': escape ? '\\(' : '(',
134+
')': escape ? '\\)' : ')',
135+
'【': escape ? '\\[' : '[',
136+
'】': escape ? '\\]' : ']',
137137
'—': '-',
138138
'…': '...',
139139
};
@@ -143,3 +143,34 @@ export function chinesePunctuationToEnglish(
143143
.map((char) => punctuationMap[char] || char)
144144
.join('');
145145
}
146+
147+
/**
148+
* 将给定的字符串转换为 JSON 格式
149+
* @param str 待转换的字符串
150+
* @returns 转换后的 JSON 字符串
151+
*/
152+
export function convert2JSON(str: string) {
153+
let jsonValue = str;
154+
155+
try {
156+
eval(`window.omnibox_convert_json= ${str}`);
157+
158+
jsonValue = JSON.stringify((window as any).omnibox_convert_json);
159+
} catch (error) {}
160+
161+
return jsonValue;
162+
}
163+
164+
/**
165+
* 判断给定的字符串是否为有效的 JSON 格式
166+
* @param str 待判断的字符串
167+
* @returns 如果字符串是有效的 JSON 格式,返回 true;否则返回 false
168+
*/
169+
export function isJSON(str: string) {
170+
try {
171+
JSON.parse(str);
172+
return true;
173+
} catch (error) {
174+
return false;
175+
}
176+
}

0 commit comments

Comments
 (0)