feat: sort translation json keys alphabetically

This commit is contained in:
Nybkox
2025-02-25 16:23:23 +01:00
parent 3e6aab6b00
commit 82eabc41fe
2 changed files with 30 additions and 2 deletions

View File

@@ -0,0 +1,24 @@
// @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;