forked from MapComplete/MapComplete
Tooling: use the same sort as weblate
This commit is contained in:
parent
c36280eb48
commit
9c8255100f
1 changed files with 38 additions and 12 deletions
|
@ -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)
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue