-
Notifications
You must be signed in to change notification settings - Fork 1
/
CommonsMediaSnak.ts
80 lines (72 loc) · 2.23 KB
/
CommonsMediaSnak.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { CommonsMediaSnak as WikidataCommonsMediaSnak } from '@wmde/wikibase-datamodel-types';
import Snak from '../Snak';
import normalizeOutput from '../utils/normalizeOutput';
/**
* class for the CommonsMediaSnak
*
* most used property of this type P18 (image)
*
* @class
*/
export default class CommonsMediaSnak extends Snak {
/** the file name as used in the wiki commons url */
fileName: string | null;
datatype = 'commonsMedia';
/**
* @param {WikidataCommonsMediaSnak} snak the snak for this class in json format
* @example
* const snak = new CommonsMediaSnak(json);
*/
constructor(snak: WikidataCommonsMediaSnak) {
super(snak);
this.fileName = snak.datavalue?.value ?? null;
}
/**
* gets the link to the image.
* uses the special:redirect function of wiki commons to find the right url
*
* @returns {string} the link to the image
*/
public get imageLink(): string {
return `https://commons.wikimedia.org/wiki/Special:Redirect/file/${this.fileName}`;
}
/**
* gets the link to the wiki commons page
*
* @returns {string} the link to the wiki commons page
*/
public get commonsLink(): string {
return `https://commons.wikimedia.org/wiki/File:${this.fileName}`;
}
/**
*
* @returns {WikidataCommonsMediaSnak} the snak as json
* @example
* const json = CommonsSnak.toJson();
*/
toJSON(): WikidataCommonsMediaSnak {
return normalizeOutput({
snaktype: this.snaktype,
property: this.property,
hash: this.hash,
datavalue: this.hasValue ? {
value: this.fileName,
type: 'string'
} : undefined,
datatype: this.datatype
}) as WikidataCommonsMediaSnak;
}
/**
* this function checks if two snaks are equal
*
* @param {CommonsMediaSnak} other the other snak
* @returns {boolean} true if the snaks are equal
* @example
* if (snak.equals(other)) {
* // do something
* }
*/
equals(other: CommonsMediaSnak): boolean {
return this.fileName === other.fileName && this.property === other.property;
}
}