mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
Merge branch 'up/fix/json-sort' into chore/all-my-stuffs
This commit is contained in:
commit
1b69560e6a
11 files changed
+197
No files matched your search
@@ -345,6 +345,10 @@ tools:
|
||||
title: PDF signature checker
|
||||
description: Verify the signatures of a PDF file. A signed PDF file contains one or more signatures that may be used to determine whether the contents of the file have been altered since the file was signed.
|
||||
|
||||
json-sort-master:
|
||||
title: JSON sort master
|
||||
description: Sort your JSON by keys and values.
|
||||
|
||||
json-minify:
|
||||
title: JSON minify
|
||||
description: Minify and compress your JSON by removing unnecessary whitespace.
|
||||
|
||||
@@ -71,6 +71,10 @@ tools:
|
||||
text: Text
|
||||
data: Data
|
||||
|
||||
json-sort-master:
|
||||
title: Maestro de clasificación JSON
|
||||
description: Ordena tu JSON por claves y valores.
|
||||
|
||||
csv-to-json:
|
||||
title: CSV a JSON
|
||||
description: Convierte CSV a JSON con detección automática de cabeceras.
|
||||
@@ -84,3 +84,7 @@ tools:
|
||||
csv-to-json:
|
||||
title: CSV vers JSON
|
||||
description: Convertit les fichiers CSV en JSON avec détection automatique des en-têtes.
|
||||
|
||||
json-sort-master:
|
||||
title: Maître de tri JSON
|
||||
description: Triez votre JSON par clés et valeurs.
|
||||
@@ -74,3 +74,7 @@ tools:
|
||||
csv-to-json:
|
||||
title: CSV para JSON
|
||||
description: Converte CSV para JSON com detecção automática de cabeçalhos.
|
||||
|
||||
json-sort-master:
|
||||
title: 'Mestre de classificação JSON'
|
||||
description: 'Ordene seu JSON por chaves e valores.'
|
||||
@@ -74,3 +74,7 @@ tools:
|
||||
csv-to-json:
|
||||
title: 'CSV в JSON'
|
||||
description: 'Конвертуйте CSV в JSON з автоматичним визначенням заголовків.'
|
||||
|
||||
json-sort-master:
|
||||
title: JSON сортувальник
|
||||
description: Сортуйте ваш JSON за ключами та значеннями.
|
||||
@@ -222,6 +222,10 @@ tools:
|
||||
title: Định dạng và làm đẹp JSON
|
||||
description: Định dạng chuỗi JSON của bạn thành một định dạng dễ đọc và thân thiện với con người.
|
||||
|
||||
json-sort-master:
|
||||
title: Sắp xếp JSON
|
||||
description: Sắp xếp JSON của bạn theo các khóa và giá trị.
|
||||
|
||||
docker-run-to-docker-compose-converter:
|
||||
title: Chuyển đổi lệnh docker run thành tệp docker-compose
|
||||
description: Chuyển đổi các lệnh docker run thành tệp docker-compose!
|
||||
|
||||
@@ -234,6 +234,10 @@ tools:
|
||||
title: JSON美化和格式化
|
||||
description: 将JSON字符串修饰为友好的可读格式。
|
||||
|
||||
json-sort-master:
|
||||
title: JSON排序大师
|
||||
description: 按键和值对您的JSON进行排序。
|
||||
|
||||
docker-run-to-docker-compose-converter:
|
||||
title: Docker Run 到 docker-compose 转换器
|
||||
description: 将 docker run 命令行转换为 docker-compose 文件!
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { tool as base64FileConverter } from './base64-file-converter';
|
||||
import { tool as base64StringConverter } from './base64-string-converter';
|
||||
import { tool as basicAuthGenerator } from './basic-auth-generator';
|
||||
import { tool as jsonSortMaster } from './json-sort-master';
|
||||
import { tool as jsonStringConverter } from './json-string-converter';
|
||||
import { tool as dnsQueries } from './dns-queries';
|
||||
import { tool as icoConverter } from './ico-converter';
|
||||
@@ -298,6 +299,7 @@ export const toolsByCategory: ToolCategory[] = [
|
||||
jsonToJavascript,
|
||||
jsonToYaml,
|
||||
jsonToToml,
|
||||
jsonSortMaster,
|
||||
jsonToTS,
|
||||
listConverter,
|
||||
listComparer,
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import { ArrowsSort } from '@vicons/tabler';
|
||||
import { defineTool } from '../tool';
|
||||
import { translate } from '@/plugins/i18n.plugin';
|
||||
|
||||
export const tool = defineTool({
|
||||
name: translate('tools.json-sort-master.title'),
|
||||
path: '/json-sort-master',
|
||||
description: translate('tools.json-sort-master.description'),
|
||||
keywords: ['json', 'sort'],
|
||||
component: () => import('./json-sort-master.vue'),
|
||||
icon: ArrowsSort,
|
||||
createdAt: new Date('2024-03-27'),
|
||||
});
|
||||
@@ -0,0 +1,80 @@
|
||||
<script setup lang="ts">
|
||||
import JSON5 from 'json5';
|
||||
import { useStorage } from '@vueuse/core';
|
||||
import { formatJson } from './json.models';
|
||||
import { withDefaultOnError } from '@/utils/defaults';
|
||||
import { useValidation } from '@/composable/validation';
|
||||
import TextareaCopyable from '@/components/TextareaCopyable.vue';
|
||||
|
||||
const inputElement = ref<HTMLElement>();
|
||||
|
||||
const rawJson = useStorage('json-prettify:raw-json', '{"hello": "world", "foo": "bar"}');
|
||||
const sortMethod = useStorage('json-prettify:sort-method', 'key_name');
|
||||
const indentSize = useStorage('json-prettify:indent-size', 3);
|
||||
const keyName = ref('');
|
||||
const cleanJson = computed(() => withDefaultOnError(() => formatJson({ rawJson, sortMethod, keyName, indentSize }), ''));
|
||||
|
||||
const rawJsonValidation = useValidation({
|
||||
source: rawJson,
|
||||
rules: [
|
||||
{
|
||||
validator: v => v === '' || JSON5.parse(v),
|
||||
message: 'Provided JSON is not valid.',
|
||||
},
|
||||
],
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div style="flex: 0 0 100%">
|
||||
<div style="margin: 0 auto; max-width: 400px" flex justify-center gap-3>
|
||||
<c-select
|
||||
v-model:value="sortMethod" mb-4 style="width: 200px" label="Sort Method" :options="[
|
||||
{
|
||||
label: 'Key Name',
|
||||
value: 'key_name',
|
||||
},
|
||||
{
|
||||
label: 'Key Value',
|
||||
value: 'key_val',
|
||||
},
|
||||
{
|
||||
label: 'Key Name (Descending)',
|
||||
value: 'key_name_desc',
|
||||
},
|
||||
{
|
||||
label: 'Key Value (Descending)',
|
||||
value: 'key_val_desc',
|
||||
},
|
||||
]"
|
||||
/>
|
||||
|
||||
<c-input-text v-if="!['key_name', 'key_name_desc'].includes(sortMethod)" v-model:value="keyName" label="Key Name:" style="width: 200px" clearable raw-text />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<n-form-item
|
||||
label="Your raw JSON" :feedback="rawJsonValidation.message"
|
||||
:validation-status="rawJsonValidation.status"
|
||||
>
|
||||
<c-input-text
|
||||
ref="inputElement" v-model:value="rawJson" placeholder="Paste your raw JSON here..." rows="20"
|
||||
multiline autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" monospace
|
||||
/>
|
||||
</n-form-item>
|
||||
<n-form-item label="Sorted version of your JSON">
|
||||
<TextareaCopyable :value="cleanJson" language="json" :follow-height-of="inputElement" />
|
||||
</n-form-item>
|
||||
</template>
|
||||
|
||||
<style lang="less" scoped>
|
||||
.result-card {
|
||||
position: relative;
|
||||
|
||||
.copy-button {
|
||||
position: absolute;
|
||||
top: 10px;
|
||||
right: 10px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,74 @@
|
||||
import { type MaybeRef, get } from '@vueuse/core';
|
||||
import JSON5 from 'json5';
|
||||
|
||||
export { sortObjectKeys, sortObjectValues, formatJson };
|
||||
|
||||
function sortObjectKeys<T>(obj: T, sortMethod: string): T {
|
||||
if (typeof obj !== 'object' || obj === null) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
if (Array.isArray(obj)) {
|
||||
return obj.map(value => sortObjectKeys(value, sortMethod)) as unknown as T;
|
||||
}
|
||||
|
||||
return Object.keys(obj)
|
||||
.sort((a, b) => sortMethod === 'key_name' ? a.localeCompare(b) : b.localeCompare(a))
|
||||
.reduce((sortedObj, key) => {
|
||||
sortedObj[key] = sortObjectKeys((obj as Record<string, unknown>)[key], sortMethod);
|
||||
return sortedObj;
|
||||
}, {} as Record<string, unknown>) as T;
|
||||
}
|
||||
|
||||
function sortObjectValues<T>(obj: T, sortMethod: string, keyName: string): T {
|
||||
if (Array.isArray(obj)) {
|
||||
return obj.sort((a, b) => {
|
||||
const valueA = a[keyName];
|
||||
const valueB = b[keyName];
|
||||
|
||||
if (typeof valueA === 'number' && typeof valueB === 'number') {
|
||||
return sortMethod === 'key_val' ? valueA - valueB : valueB - valueA;
|
||||
}
|
||||
if (typeof valueA === 'string' && typeof valueB === 'string') {
|
||||
return sortMethod === 'key_val' ? valueA.localeCompare(valueB) : valueB.localeCompare(valueA);
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof obj === 'object' && obj !== null) {
|
||||
const sortedKeys = Object.keys(obj).sort((a, b) => {
|
||||
if (sortMethod === 'key_val') {
|
||||
return a.localeCompare(b);
|
||||
}
|
||||
return b.localeCompare(a);
|
||||
});
|
||||
|
||||
return sortedKeys.reduce((sortedObj, key) => {
|
||||
sortedObj[key] = sortObjectValues((obj as Record<string, unknown>)[key], sortMethod, keyName);
|
||||
return sortedObj;
|
||||
}, {} as Record<string, unknown>) as T;
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
function formatJson({
|
||||
rawJson,
|
||||
sortMethod = 'key_name',
|
||||
keyName = '',
|
||||
indentSize = 3,
|
||||
}: {
|
||||
rawJson: MaybeRef<string>
|
||||
sortMethod: MaybeRef<string>
|
||||
keyName: MaybeRef<string>
|
||||
indentSize?: MaybeRef<number>
|
||||
}) {
|
||||
const parsedObject = JSON5.parse(get(rawJson));
|
||||
|
||||
if (['key_name', 'key_name_desc'].includes(get(sortMethod))) {
|
||||
return JSON.stringify(sortObjectKeys(parsedObject, get(sortMethod)), null, get(indentSize));
|
||||
}
|
||||
|
||||
return JSON.stringify(sortObjectValues(parsedObject, get(sortMethod), get(keyName)), null, get(indentSize));
|
||||
}
|
||||
Reference in new issue
Block a user