Improve typing

This commit is contained in:
pietervdvn 2022-06-24 16:47:00 +02:00
parent 09b230b5be
commit 978df7253c
3 changed files with 29 additions and 11 deletions

View file

@ -7,10 +7,10 @@ export class Translation extends BaseUIElement {
public static forcedLanguage = undefined; public static forcedLanguage = undefined;
public readonly translations: object public readonly translations: Record<string, string>
context?: string; context?: string;
constructor(translations: object, context?: string) { constructor(translations: Record<string, string>, context?: string) {
super() super()
if (translations === undefined) { if (translations === undefined) {
console.error("Translation without content at "+context) console.error("Translation without content at "+context)
@ -264,7 +264,7 @@ export class Translation extends BaseUIElement {
} }
export class TypedTranslation<T> extends Translation { export class TypedTranslation<T> extends Translation {
constructor(translations: object, context?: string) { constructor(translations: Record<string, string>, context?: string) {
super(translations, context); super(translations, context);
} }

View file

@ -931,20 +931,38 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
return track[str2.length][str1.length]; return track[str2.length][str1.length];
} }
public static MapToObj<T>(d: Map<string, T>, onValue: ((t: T, key: string) => any) = undefined): object { public static MapToObj<V, T>(d: Map<string, V>, onValue: ((t: V, key: string) => T)): Record<string, T> {
const o = {} const o = {}
const keys = Array.from(d.keys()) const keys = Array.from(d.keys())
keys.sort(); keys.sort();
for (const key of keys) { for (const key of keys) {
let value = d.get(key) o[key] = onValue(d.get(key), key);
if (onValue !== undefined) {
value = onValue(value, key)
}
o[key] = value;
} }
return o return o
} }
/**
* Switches keys and values around
*
* Utils.TransposeMap({"a" : ["b", "c"], "x" : ["b", "y"]}) // => {"b" : ["a", "x"], "c" : ["a"], "y" : ["x"]}
*/
public static TransposeMap<K extends string, V extends string>(d: Record<K, V[]>) : Record<V, K[]>{
const newD : Record<V, K[]> = <any> {};
for (const k in d) {
const vs = d[k]
for (let v of vs) {
const list = newD[v]
if(list === undefined){
newD[v] = [k] // Left: indexing; right: list with one element
}else{
list.push(k)
}
}
}
return newD;
}
/** /**
* Utils.colorAsHex({r: 255, g: 128, b: 0}) // => "#ff8000" * Utils.colorAsHex({r: 255, g: 128, b: 0}) // => "#ff8000"
* Utils.colorAsHex(undefined) // => undefined * Utils.colorAsHex(undefined) // => undefined

View file

@ -66,8 +66,8 @@ async function main(includeTags = true) {
writeFileSync("./assets/key_totals.json", writeFileSync("./assets/key_totals.json",
JSON.stringify( JSON.stringify(
{ {
keys: Utils.MapToObj(keyTotal), keys: Utils.MapToObj(keyTotal, t => t),
tags: Utils.MapToObj(tagTotal, v => Utils.MapToObj(v)) tags: Utils.MapToObj(tagTotal, v => Utils.MapToObj(v, t => t))
}, },
null, " " null, " "
) )