From 9c8255100f9e63b2df14dda2233bdb05ed434d3d Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Sun, 25 Aug 2024 00:41:58 +0200 Subject: [PATCH] Tooling: use the same sort as weblate --- scripts/generateTranslations.ts | 50 +++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/scripts/generateTranslations.ts b/scripts/generateTranslations.ts index 901518039..375be5ca1 100644 --- a/scripts/generateTranslations.ts +++ b/scripts/generateTranslations.ts @@ -408,22 +408,49 @@ function transformTranslation( /** * - * const result = sortKeys({"b": 43, "a": 42}) - * JSON.stringify(result) // => '{"a":42,"b":43}' + * const result = stringifySorted({"b": 43, "a": 42}) + * result // => '{"a": 42,"b": 43}' + * + * // Should have correct newlines + * const result = stringifySorted({"b": {"x": "y"}, "a": 42}, " ") + * result // => '{\n "a": 42,\n "b": {\n "x": "y"\n }\n}' + * + * // Should sort like weblate does + * const result = stringifySorted({"1": "abc", "2": "def", "9": "ghi", "10": "xyz", "11": "uvw"}) + * result // => '{"1": "abc","10": "xyz","11": "uvw","2": "def","9", "ghi"}' */ -function sortKeys(o: object): object { +function stringifySorted(o: object, space: string = undefined, depth = 0): string { const keys = Object.keys(o) - keys.sort((a, b) => ("" + a < "" + b ? -1 : 1)) - const nw = {} - for (const key of keys) { + let obj = "{" + + obj += keys.sort().map(key => { const v = o[key] + let r = "" + if (space !== undefined) { + r += "\n" + for (let i = 0; i <= depth; i++) { + r += space + } + } + + r += JSON.stringify("" + key) + ": " if (typeof v === "object") { - nw[key] = sortKeys(v) + r += stringifySorted(v, space, depth + 1) + } else if (Array.isArray(v)) { + r += "[" + v.map(v_ => stringifySorted(v_, space, depth + 1)).join(",") + "]" } else { - nw[key] = v + r += JSON.stringify(v) + } + return r + }).join(",") + if (space !== undefined) { + obj += "\n" + for (let i = 0; i < depth; i++) { + obj += space } } - return nw + obj += "}" + return obj } function removeEmptyString(object: object) { @@ -446,9 +473,8 @@ function formatFile(path) { const original = readFileSync(path, "utf8") let contents = JSON.parse(original) contents = removeEmptyString(contents) - contents = sortKeys(contents) - const endsWithNewline = original.endsWith("\n") - writeFileSync(path, JSON.stringify(contents, null, " ") + (endsWithNewline ? "\n" : "")) + contents = stringifySorted(contents, " ") + writeFileSync(path, contents) }