Prefer using HTTPS when merging IW maps

This commit is contained in:
Florine W. Dekker 2021-05-14 19:40:15 +02:00
parent 596be9aea1
commit 53f9dc7303
Signed by: FWDekker
GPG Key ID: 78B3EAF58145AF25
3 changed files with 17 additions and 7 deletions

View File

@ -1,6 +1,6 @@
{
"name": "interlanguage-checker",
"version": "1.13.2",
"version": "1.13.3",
"description": "Check the consistency of MediaWiki interlanguage links in a simple overview.",
"author": "Felix W. Dekker",
"browser": "dist/bundle.js",

View File

@ -598,7 +598,10 @@ export class MediaWikiManager {
* @private
*/
private updateIwMap(): void {
this.iwMap = mergeMaps([...this.mws.values()].map(mw => mw.interwikiMap));
this.iwMap = mergeMaps(
[...this.mws.values()].map(mw => mw.interwikiMap),
(k, v1, v2) => (v2.startsWith("https://")) ? v2 : v1
);
}
}

View File

@ -5,16 +5,23 @@ export const couldNotConnectMessage: string =
"Could not to connect to API. Is the URL correct? Are you using a script blocker? " +
"See the <b>About</b> section for more information.";
// TODO: Add a merge strategy (to prefer HTTPS)
/**
* Merges the given maps into a new map containing all their elements.
*
* @param maps the maps to merge into a single map
* @param merge a function to execute when both maps have the same key `k` with different values `v1` and `v2`; by
* default, the old value is used
* @return the combined map
*/
export const mergeMaps = <K, V>(maps: Map<K, V>[]): Map<K, V> => {
return maps.reduce((combined, map) => new Map([...combined, ...map]), new Map());
}
export const mergeMaps = <K, V>(maps: Map<K, V>[], merge: (k: K, v1: V, v2: V) => V = (k, v1, _) => v1): Map<K, V> => {
return maps.reduce((combined, map) => {
map.forEach((v, k) => {
if (combined.has(k)) combined.set(k, merge(k, combined.get(k), v));
combined.set(k, v);
});
return combined;
}, new Map());
};
/**
* Merges the given sets into a new set containing all their elements.
@ -24,4 +31,4 @@ export const mergeMaps = <K, V>(maps: Map<K, V>[]): Map<K, V> => {
*/
export const mergeSets = <T>(sets: Set<T>[]): Set<T> => {
return sets.reduce((combined, set) => new Set([...combined, ...set]), new Set());
}
};