traefik/server/lib/sortedJson.mjs
2023-06-16 09:11:54 -05:00

23 lines
495 B
JavaScript

export const stringify = (value, space = 0) => {
return JSON.stringify(value, replacer, space);
};
const replacer = (key, value) => {
if (
typeof value === 'object' &&
value !== null &&
!(value instanceof Array)
) {
return sortObject(value);
}
return value;
};
export const sortObject = (object) => {
const sortedObject = {};
for (const key of Object.keys(object).sort()) {
sortedObject[key] = object[key];
}
return sortedObject;
};