Skip to content

Commit 374829b

Browse files
committed
sort objects alphabetically, to make diffing easier
1 parent 41d2cd2 commit 374829b

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

lib/build.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import marky from 'marky';
99
import { createRequire } from 'module';
1010
import { compile, toSafeIdentifier } from 'json-schema-to-typescript-lite';
1111

12-
import fetchTranslations from './translations.js';
12+
import fetchTranslations, { sortObject } from './translations.js';
1313

1414
const require = createRequire(import.meta.url);
1515

@@ -162,6 +162,10 @@ function processData(options, type) {
162162
shell.rm('-rf', [distDir + '/translations']);
163163
}
164164

165+
categories = sortObject(categories);
166+
fields = sortObject(fields);
167+
presets = sortObject(presets);
168+
165169
fs.writeFileSync(distDir + '/preset_categories.json', JSON.stringify(categories, null, 4));
166170
fs.writeFileSync(distDir + '/fields.json', JSON.stringify(fields, null, 4));
167171
fs.writeFileSync(distDir + '/presets.json', JSON.stringify(presets, null, 4));

lib/translations.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ import fetch from 'node-fetch';
44
import YAML from 'js-yaml';
55
import { transifexApi } from '@transifex/api';
66

7+
/**
8+
* Sorts the keys of an object alphabetically
9+
* @template {object} T @param {T} original @returns {T}
10+
*/
11+
export function sortObject(original) {
12+
const sorted = {};
13+
for (const k of Object.keys(original).sort()) {
14+
sorted[k] = original[k];
15+
}
16+
return sorted;
17+
}
718

819
function fetchTranslations(options) {
920

@@ -99,9 +110,7 @@ function fetchTranslations(options) {
99110
};
100111
}
101112

102-
const keys = Object.keys(dataLocales).sort();
103-
let sortedLocales = {};
104-
keys.forEach(k => sortedLocales[k] = dataLocales[k]);
113+
const sortedLocales = sortObject(dataLocales);
105114
fs.writeFileSync(`${outDir}/index.json`, JSON.stringify(sortedLocales));
106115
fs.writeFileSync(`${outDir}/index.min.json`, JSON.stringify(sortedLocales, null, 4));
107116
}

0 commit comments

Comments
 (0)