Skip to content

Commit de8bb9b

Browse files
committed
Merge branch 'up/feat/js-to-json' into chore/all-my-stuffs
2 parents f7c2d89 + a385d18 commit de8bb9b

File tree

8 files changed

+119
-1
lines changed

8 files changed

+119
-1
lines changed

locales/en.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,15 @@ tools:
203203

204204
json-to-yaml-converter:
205205
title: JSON to YAML converter
206-
description: Simply convert JSON to YAML with this online live converter.
206+
description: Simply convert JSON to YAML with this live online converter.
207+
208+
json-to-javascript:
209+
title: JSON to JS converter
210+
description: Simply convert JSON to Javascript object with this live online converter.
211+
212+
javascript-to-json:
213+
title: JS to JSON converter
214+
description: Simply convert Javascript object to JSON with this live online converter.
207215

208216
url-parser:
209217
title: URL parser

locales/zh.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,14 @@ tools:
198198
title: JSON到YAML转换器
199199
description: 在线转换将JSON转换为YAML。
200200

201+
json-to-javascript:
202+
title: JSON 到 JS 转换器
203+
description: 使用此在线转换器将 JSON 转换为 Javascript 对象。
204+
205+
javascript-to-json:
206+
title: JS 到 JSON 转换器
207+
description: 使用此在线转换器将 Javascript 对象转换为 JSON。
208+
201209
url-parser:
202210
title: Url分析器
203211
description: 解析url字符串以获取所有不同的部分(协议、来源、参数、端口、用户名密码…)

src/components/TextareaCopyable.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { Copy } from '@vicons/tabler';
33
import { useElementSize } from '@vueuse/core';
44
import hljs from 'highlight.js/lib/core';
5+
import javascriptHljs from 'highlight.js/lib/languages/javascript';
56
import jsonHljs from 'highlight.js/lib/languages/json';
67
import sqlHljs from 'highlight.js/lib/languages/sql';
78
import xmlHljs from 'highlight.js/lib/languages/xml';
@@ -37,6 +38,7 @@ const props = withDefaults(
3738
downloadButtonText: 'Download',
3839
},
3940
);
41+
hljs.registerLanguage('javascript', javascriptHljs);
4042
hljs.registerLanguage('sql', sqlHljs);
4143
hljs.registerLanguage('json', jsonHljs);
4244
hljs.registerLanguage('html', xmlHljs);

src/tools/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ import { tool as jsonDiff } from './json-diff';
164164
import { tool as ipv4RangeExpander } from './ipv4-range-expander';
165165
import { tool as httpStatusCodes } from './http-status-codes';
166166
import { tool as yamlToJson } from './yaml-to-json-converter';
167+
import { tool as javascriptToJson } from './javascript-to-json';
168+
import { tool as jsonToJavascript } from './json-to-javascript';
167169
import { tool as jsonToYaml } from './json-to-yaml-converter';
168170
import { tool as ipv6UlaGenerator } from './ipv6-ula-generator';
169171
import { tool as ipv4AddressConverter } from './ipv4-address-converter';
@@ -289,6 +291,8 @@ export const toolsByCategory: ToolCategory[] = [
289291
textToUnicodeNames,
290292
yamlToJson,
291293
yamlToToml,
294+
javascriptToJson,
295+
jsonToJavascript,
292296
jsonToYaml,
293297
jsonToToml,
294298
listConverter,

src/tools/javascript-to-json/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Braces } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
import { translate } from '@/plugins/i18n.plugin';
4+
5+
export const tool = defineTool({
6+
name: translate('tools.javascript-to-json.title'),
7+
path: '/javascript-to-json',
8+
description: translate('tools.javascript-to-json.description'),
9+
keywords: ['javascript', 'to', 'json'],
10+
component: () => import('./javascript-to-json.vue'),
11+
icon: Braces,
12+
createdAt: new Date('2024-03-29'),
13+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<script setup lang="ts">
2+
import JSON5 from 'json5';
3+
import type { UseValidationRule } from '@/composable/validation';
4+
import { isNotThrowing } from '@/utils/boolean';
5+
import { withDefaultOnError } from '@/utils/defaults';
6+
7+
const indentSize = useStorage('json-prettify:indent-size', 3);
8+
const transformer = (value: string) => withDefaultOnError(() => JSON.stringify(JSON5.parse(value), null, indentSize.value), '');
9+
10+
const rules: UseValidationRule<string>[] = [
11+
{
12+
validator: (value: string) => value === '' || isNotThrowing(() => JSON.stringify(JSON5.parse(value))),
13+
message: 'Provided JS Object is not valid.',
14+
},
15+
];
16+
</script>
17+
18+
<template>
19+
<div style="flex: 0 0 100%">
20+
<div style="margin: 0 auto; max-width: 600px" flex justify-center gap-3>
21+
<n-form-item label="Indent size :" label-placement="left" label-width="100" :show-feedback="false">
22+
<n-input-number v-model:value="indentSize" min="0" max="10" style="width: 100px" />
23+
</n-form-item>
24+
</div>
25+
</div>
26+
27+
<format-transformer
28+
input-label="Your JavaScript Object"
29+
input-placeholder="Paste your JS Object here..."
30+
output-label="JSON from your JavaScript Object"
31+
output-language="json"
32+
:input-validation-rules="rules"
33+
:transformer="transformer"
34+
/>
35+
</template>

src/tools/json-to-javascript/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Braces } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
import { translate } from '@/plugins/i18n.plugin';
4+
5+
export const tool = defineTool({
6+
name: translate('tools.json-to-javascript.title'),
7+
path: '/json-to-javascript',
8+
description: translate('tools.json-to-javascript.description'),
9+
keywords: ['json', 'to', 'javascript'],
10+
component: () => import('./json-to-javascript.vue'),
11+
icon: Braces,
12+
createdAt: new Date('2024-03-29'),
13+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<script setup lang="ts">
2+
import JSON5 from 'json5';
3+
import type { UseValidationRule } from '@/composable/validation';
4+
import { isNotThrowing } from '@/utils/boolean';
5+
import { withDefaultOnError } from '@/utils/defaults';
6+
7+
const indentSize = useStorage('json-prettify:indent-size', 3);
8+
const transformer = (value: string) => withDefaultOnError(() => JSON5.stringify(JSON.parse(value), null, indentSize.value), '');
9+
10+
const rules: UseValidationRule<string>[] = [
11+
{
12+
validator: (value: string) => value === '' || isNotThrowing(() => JSON5.stringify(JSON.parse(value))),
13+
message: 'Provided JSON is not valid.',
14+
},
15+
];
16+
</script>
17+
18+
<template>
19+
<div style="flex: 0 0 100%">
20+
<div style="margin: 0 auto; max-width: 600px" flex justify-center gap-3>
21+
<n-form-item label="Indent size :" label-placement="left" label-width="100" :show-feedback="false">
22+
<n-input-number v-model:value="indentSize" min="0" max="10" style="width: 100px" />
23+
</n-form-item>
24+
</div>
25+
</div>
26+
27+
<format-transformer
28+
input-label="Your JSON"
29+
input-placeholder="Paste your JSON here..."
30+
output-label="JavaScript Object from your JSON"
31+
output-language="javascript"
32+
:input-validation-rules="rules"
33+
:transformer="transformer"
34+
/>
35+
</template>

0 commit comments

Comments
 (0)