mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-09-23 07:59:31 +02:00
29 lines
697 B
TypeScript
29 lines
697 B
TypeScript
export const stringifyJson = (
|
|
input: string,
|
|
indentationType: 'tab' | 'space',
|
|
spacesCount: number,
|
|
escapeHtml: boolean
|
|
): string => {
|
|
let parsedInput;
|
|
try {
|
|
// Safely evaluate the input string as JavaScript
|
|
parsedInput = eval('(' + input + ')');
|
|
} catch (e) {
|
|
throw new Error('Invalid JavaScript object/array');
|
|
}
|
|
|
|
const indent = indentationType === 'tab' ? '\t' : ' '.repeat(spacesCount);
|
|
let result = JSON.stringify(parsedInput, null, indent);
|
|
|
|
if (escapeHtml) {
|
|
result = result
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, ''');
|
|
}
|
|
|
|
return result;
|
|
};
|