Replace IWMap with actual Map
This commit is contained in:
parent
355ec7ac57
commit
a8c05f62eb
|
@ -120,69 +120,8 @@ export class Redirect {
|
|||
|
||||
/**
|
||||
* A map of interwiki links.
|
||||
*
|
||||
* Not implemented as a map but as a list of objects. Therefore, when there are duplicate keys, the original value is
|
||||
* always retained.
|
||||
*/
|
||||
// TODO: Replace entire class with a `Map`
|
||||
export class InterwikiMap {
|
||||
/**
|
||||
* The mapping from interwiki abbreviations/prefixes to URLs.
|
||||
*/
|
||||
readonly map: Map<string, string>;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a new interwiki map.
|
||||
*
|
||||
* @param map the mapping from interwiki abbreviations/prefixes to URLs
|
||||
*/
|
||||
constructor(map: { prefix: string, url: string }[]) {
|
||||
this.map = new Map();
|
||||
map.forEach(({prefix, url}) => this.map.set(prefix, url.replace("http://", "https://")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new interwiki map from the given map.
|
||||
*
|
||||
* @param map the map to construct an interwiki map from
|
||||
*/
|
||||
static fromMap(map: Map<string, string>): InterwikiMap {
|
||||
return new InterwikiMap([...map.entries()].map(it => ({prefix: it[0], url: it[1]})));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the URL for the given prefix, or `undefined` if the prefix could not be found.
|
||||
*
|
||||
* @param prefix the prefix to return the URL of
|
||||
* @return the URL for the given prefix, or `undefined` if the prefix could not be found
|
||||
*/
|
||||
getUrl(prefix: string): string | undefined {
|
||||
return this.map.get(prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if and only if this map has a URL for the given prefix.
|
||||
*
|
||||
* @param prefix the prefix to check for
|
||||
* @return `true` if and only if this map has a URL for the given prefix
|
||||
*/
|
||||
hasUrl(prefix: string): boolean {
|
||||
return this.map.has(prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a deep copy of this `InterwikiMap`.
|
||||
*
|
||||
* This is a deep copy because the constructor performs copies of the received variables.
|
||||
*
|
||||
* @return the deep copy
|
||||
*/
|
||||
copy(): InterwikiMap {
|
||||
return InterwikiMap.fromMap(this.map);
|
||||
}
|
||||
}
|
||||
export type InterwikiMap = Map<string, string>;
|
||||
|
||||
/**
|
||||
* Describes a page, i.e. what you get if you follow an `InterlangLink`.
|
||||
|
@ -425,7 +364,8 @@ export class MediaWiki {
|
|||
|
||||
// Set fields
|
||||
this.general = query.general;
|
||||
this.interwikiMap = new InterwikiMap(query.interwikimap);
|
||||
this.interwikiMap =
|
||||
new Map(query.interwikimap.map((it: { prefix: string, url: string }) => [it.prefix, it.url]));
|
||||
this.namespaces = query.namespaces;
|
||||
|
||||
return this;
|
||||
|
@ -567,7 +507,7 @@ export class MediaWikiManager {
|
|||
*/
|
||||
constructor() {
|
||||
this.mws = new Map();
|
||||
this.iwMap = new InterwikiMap([]);
|
||||
this.iwMap = new Map();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -604,10 +544,10 @@ export class MediaWikiManager {
|
|||
if (this.hasMw(lang))
|
||||
return this.mws.get(lang);
|
||||
|
||||
if (!this.iwMap.hasUrl(lang))
|
||||
if (!this.iwMap.has(lang))
|
||||
return undefined;
|
||||
|
||||
const url = this.iwMap.getUrl(lang);
|
||||
const url = this.iwMap.get(lang);
|
||||
if (url === undefined) return undefined;
|
||||
|
||||
let newMw;
|
||||
|
@ -656,7 +596,7 @@ export class MediaWikiManager {
|
|||
* @return the URL to the given article
|
||||
*/
|
||||
getArticlePath(link: InterlangLink): URL {
|
||||
const articlePath = this.iwMap.getUrl(link.lang);
|
||||
const articlePath = this.iwMap.get(link.lang);
|
||||
if (articlePath === undefined) throw Error(`Could not find article path for '${link}'.`);
|
||||
|
||||
return new URL(articlePath.replace("$1", link.title));
|
||||
|
@ -669,11 +609,10 @@ export class MediaWikiManager {
|
|||
* @private
|
||||
*/
|
||||
private updateIwMap(): void {
|
||||
this.iwMap = InterwikiMap.fromMap(
|
||||
this.iwMap =
|
||||
[...this.mws.values()]
|
||||
.map(mw => mw.interwikiMap.map)
|
||||
.reduce((combined, map) => new Map([...combined, ...map]), new Map())
|
||||
);
|
||||
.map(mw => mw.interwikiMap)
|
||||
.reduce((combined, map) => new Map([...combined, ...map]), new Map());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue