interlanguage-checker/src/main/js/Shared.ts

35 lines
1.2 KiB
TypeScript

/**
* The message that is displayed when the application fails to connect to the API.
*/
export const couldNotConnectMessage: string =
"Could not connect to API. Is the URL correct? Are you using a script blocker? " +
"See the <b>About</b> section for more information.";
/**
* 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>[], 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.
*
* @param sets the sets to merge into a single set
* @return the combined set
*/
export const mergeSets = <T>(sets: Set<T>[]): Set<T> => {
return sets.reduce((combined, set) => new Set([...combined, ...set]), new Set());
};