mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-18 06:46:00 +00:00
25 lines
489 B
JavaScript
25 lines
489 B
JavaScript
// @ts-check
|
|
|
|
/**
|
|
* @param {object|string} json
|
|
* @returns {object}
|
|
*/
|
|
function sortJsonKeysAlphabetically(json) {
|
|
const obj = typeof json === 'string' ? JSON.parse(json) : json;
|
|
|
|
if (obj === null || typeof obj !== 'object' || Array.isArray(obj)) {
|
|
return obj;
|
|
}
|
|
|
|
const sortedObj = Object.keys(obj)
|
|
.sort()
|
|
.reduce((result, key) => {
|
|
result[key] = obj[key];
|
|
return result;
|
|
}, {});
|
|
|
|
return sortedObj;
|
|
}
|
|
|
|
module.exports = sortJsonKeysAlphabetically;
|