chore: move sortJsonKeys helper

This commit is contained in:
Nybkox
2025-03-04 20:54:11 +01:00
parent 707e5bb8b0
commit 7b4b72166f
2 changed files with 1 additions and 1 deletions

View File

@@ -2,7 +2,7 @@
const path = require('path');
const fs = require('fs');
const { defaultLanguage } = require('./constants');
const sortJsonKeysAlphabetically = require('../sortJsonKeysAlphabetically');
const sortJsonKeysAlphabetically = require('./sortJsonKeysAlphabetically');
/**
* @param {string} file

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;