forked from animalnots/BetterChatGPT-PLUS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortModelsJsonKeys.js
48 lines (40 loc) · 1.62 KB
/
sortModelsJsonKeys.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import fs from 'fs/promises';
// Function to recursively sort object keys
function sortObjectKeys(obj) {
if (Array.isArray(obj)) {
// Check if the array contains objects with a 'name' field
if (obj.length > 0 && typeof obj[0] === 'object' && 'name' in obj[0]) {
// Sort the array by the 'name' field
obj.sort((a, b) => a.name.localeCompare(b.name));
}
return obj.map(sortObjectKeys);
} else if (obj !== null && typeof obj === 'object') {
return Object.keys(obj).sort().reduce((sortedObj, key) => {
sortedObj[key] = sortObjectKeys(obj[key]);
return sortedObj;
}, {});
}
return obj;
}
// Read the JSON file
async function processJsonFile(inputFilePath, outputFilePath) {
try {
const data = await fs.readFile(inputFilePath, 'utf8');
// Parse the JSON data
const jsonData = JSON.parse(data);
// Sort the JSON data
const sortedJsonData = sortObjectKeys(jsonData);
// Convert the sorted JSON data back to a string
const sortedJsonString = JSON.stringify(sortedJsonData, null, 2);
// Write the sorted JSON data to a new file
await fs.writeFile(outputFilePath, sortedJsonString, 'utf8');
console.log('File has been saved with sorted keys.');
} catch (err) {
console.error('Error processing the file:', err);
}
}
// Define file paths
const inputFilePath = 'models.json';
const outputFilePath = 'public/models.json';
// Process the JSON file
processJsonFile(inputFilePath, outputFilePath);