20 lines
489 B
TypeScript
20 lines
489 B
TypeScript
function sortJson(value: unknown): unknown {
|
|
if (Array.isArray(value)) {
|
|
return value.map(sortJson);
|
|
}
|
|
|
|
if (value && typeof value === 'object') {
|
|
return Object.fromEntries(
|
|
Object.entries(value)
|
|
.sort(([left], [right]) => left.localeCompare(right))
|
|
.map(([key, child]) => [key, sortJson(child)]),
|
|
);
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
export function serializeOpenApiDocument(document: unknown) {
|
|
return `${JSON.stringify(sortJson(document), null, 2)}\n`;
|
|
}
|