23 lines
495 B
JavaScript
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;
|
|
};
|