diff --git a/locales/en.yml b/locales/en.yml index 173fc743..37d5f330 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -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. diff --git a/locales/es.yml b/locales/es.yml index 88fa6745..df3e0a3b 100644 --- a/locales/es.yml +++ b/locales/es.yml @@ -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. diff --git a/locales/fr.yml b/locales/fr.yml index 95ae89de..18652b12 100644 --- a/locales/fr.yml +++ b/locales/fr.yml @@ -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. \ No newline at end of file diff --git a/locales/pt.yml b/locales/pt.yml index 16f8fd43..ea3538b7 100644 --- a/locales/pt.yml +++ b/locales/pt.yml @@ -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.' diff --git a/locales/uk.yml b/locales/uk.yml index 30a1bec4..62522c0f 100644 --- a/locales/uk.yml +++ b/locales/uk.yml @@ -74,3 +74,7 @@ tools: csv-to-json: title: 'CSV в JSON' description: 'Конвертуйте CSV в JSON з автоматичним визначенням заголовків.' + + json-sort-master: + title: JSON сортувальник + description: Сортуйте ваш JSON за ключами та значеннями. diff --git a/locales/vi.yml b/locales/vi.yml index 603b3926..06d73cd2 100644 --- a/locales/vi.yml +++ b/locales/vi.yml @@ -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! diff --git a/locales/zh.yml b/locales/zh.yml index 2089c231..77c4b0d0 100644 --- a/locales/zh.yml +++ b/locales/zh.yml @@ -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 文件! diff --git a/src/tools/index.ts b/src/tools/index.ts index 9390a587..debfe42b 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -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, diff --git a/src/tools/json-sort-master/index.ts b/src/tools/json-sort-master/index.ts new file mode 100644 index 00000000..5bb84d1c --- /dev/null +++ b/src/tools/json-sort-master/index.ts @@ -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'), +}); diff --git a/src/tools/json-sort-master/json-sort-master.vue b/src/tools/json-sort-master/json-sort-master.vue new file mode 100644 index 00000000..28d5f1c9 --- /dev/null +++ b/src/tools/json-sort-master/json-sort-master.vue @@ -0,0 +1,80 @@ + + + + + diff --git a/src/tools/json-sort-master/json.models.ts b/src/tools/json-sort-master/json.models.ts new file mode 100644 index 00000000..b8e24889 --- /dev/null +++ b/src/tools/json-sort-master/json.models.ts @@ -0,0 +1,74 @@ +import { type MaybeRef, get } from '@vueuse/core'; +import JSON5 from 'json5'; + +export { sortObjectKeys, sortObjectValues, formatJson }; + +function sortObjectKeys(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)[key], sortMethod); + return sortedObj; + }, {} as Record) as T; +} + +function sortObjectValues(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)[key], sortMethod, keyName); + return sortedObj; + }, {} as Record) as T; + } + + return obj; +} + +function formatJson({ + rawJson, + sortMethod = 'key_name', + keyName = '', + indentSize = 3, +}: { + rawJson: MaybeRef + sortMethod: MaybeRef + keyName: MaybeRef + indentSize?: MaybeRef +}) { + 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)); +}