mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-08 15:15:02 -04:00
validating if yaml is correct and collecting format options
This commit is contained in:
parent
7a70dbbe0c
commit
3a99642fd6
7 changed files with 132 additions and 1 deletions
37
src/tools/yaml-viewer/yaml-models.ts
Normal file
37
src/tools/yaml-viewer/yaml-models.ts
Normal file
|
@ -0,0 +1,37 @@
|
|||
import { type MaybeRef, get } from '@vueuse/core';
|
||||
import JSON5 from 'json5';
|
||||
|
||||
export { sortObjectKeys, formatYaml };
|
||||
|
||||
function sortObjectKeys<T>(obj: T): T {
|
||||
if (typeof obj !== 'object' || obj === null) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
if (Array.isArray(obj)) {
|
||||
return obj.map(sortObjectKeys) as unknown as T;
|
||||
}
|
||||
|
||||
return Object.keys(obj)
|
||||
.sort((a, b) => a.localeCompare(b))
|
||||
.reduce((sortedObj, key) => {
|
||||
sortedObj[key] = sortObjectKeys((obj as Record<string, unknown>)[key]);
|
||||
return sortedObj;
|
||||
}, {} as Record<string, unknown>) as T;
|
||||
}
|
||||
|
||||
function formatYaml({
|
||||
rawYaml,
|
||||
sortKeys = false,
|
||||
stripComments = false,
|
||||
indentSize = 3,
|
||||
}: {
|
||||
rawYaml: MaybeRef<string>
|
||||
sortKeys?: MaybeRef<boolean>
|
||||
stripComments?: MaybeRef<boolean>
|
||||
indentSize?: MaybeRef<number>
|
||||
}) {
|
||||
const parsedObject = JSON5.parse(get(rawYaml));
|
||||
|
||||
return JSON.stringify(get(sortKeys) ? sortObjectKeys(parsedObject) : parsedObject, null, get(indentSize));
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue