forked from MapComplete/MapComplete
		
	Improve typing
This commit is contained in:
		
							parent
							
								
									09b230b5be
								
							
						
					
					
						commit
						e1faf5595d
					
				
					 2 changed files with 27 additions and 9 deletions
				
			
		| 
						 | 
				
			
			@ -7,10 +7,10 @@ export class Translation extends BaseUIElement {
 | 
			
		|||
 | 
			
		||||
    public static forcedLanguage = undefined;
 | 
			
		||||
 | 
			
		||||
    public readonly translations: object
 | 
			
		||||
    public readonly translations: Record<string, string>
 | 
			
		||||
    context?: string;
 | 
			
		||||
 | 
			
		||||
    constructor(translations: object, context?: string) {
 | 
			
		||||
    constructor(translations: Record<string, string>, context?: string) {
 | 
			
		||||
        super()
 | 
			
		||||
        if (translations === undefined) {
 | 
			
		||||
            console.error("Translation without content at "+context)
 | 
			
		||||
| 
						 | 
				
			
			@ -264,7 +264,7 @@ export class Translation extends BaseUIElement {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
export class TypedTranslation<T> extends Translation {
 | 
			
		||||
    constructor(translations: object, context?: string) {
 | 
			
		||||
    constructor(translations: Record<string, string>, context?: string) {
 | 
			
		||||
        super(translations, context);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										30
									
								
								Utils.ts
									
										
									
									
									
								
							
							
						
						
									
										30
									
								
								Utils.ts
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -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];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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 keys = Array.from(d.keys())
 | 
			
		||||
        keys.sort();
 | 
			
		||||
        for (const key of keys) {
 | 
			
		||||
            let value = d.get(key)
 | 
			
		||||
            if (onValue !== undefined) {
 | 
			
		||||
                value = onValue(value, key)
 | 
			
		||||
            }
 | 
			
		||||
            o[key] = value;
 | 
			
		||||
            o[key] = onValue(d.get(key), key);
 | 
			
		||||
        }
 | 
			
		||||
        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(undefined) // => undefined
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue