mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-10 08:05:00 -04:00
add unescape unicode in JSON Prettify
This commit is contained in:
parent
08d977b8cd
commit
fa7c89cb44
3 changed files with 20 additions and 2 deletions
|
@ -20,16 +20,25 @@ function sortObjectKeys<T>(obj: T): T {
|
|||
}, {} as Record<string, unknown>) as T;
|
||||
}
|
||||
|
||||
function unescapeUnicodeJSON(str: string) {
|
||||
return str.replace(/\\u([\dA-Fa-f]{4})/g, (match, grp) =>
|
||||
String.fromCharCode(Number.parseInt(grp, 16)),
|
||||
);
|
||||
}
|
||||
|
||||
function formatJson({
|
||||
rawJson,
|
||||
sortKeys = true,
|
||||
indentSize = 3,
|
||||
unescapeUnicode = false,
|
||||
}: {
|
||||
rawJson: MaybeRef<string>
|
||||
sortKeys?: MaybeRef<boolean>
|
||||
indentSize?: MaybeRef<number>
|
||||
unescapeUnicode?: MaybeRef<boolean>
|
||||
}) {
|
||||
const parsedObject = JSON5.parse(get(rawJson));
|
||||
const raw = get(rawJson);
|
||||
const parsedObject = JSON5.parse(get(unescapeUnicode) ? unescapeUnicodeJSON(raw) : raw);
|
||||
|
||||
return JSON.stringify(get(sortKeys) ? sortObjectKeys(parsedObject) : parsedObject, null, get(indentSize));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue