-
Notifications
You must be signed in to change notification settings - Fork 1
/
Label.ts
74 lines (67 loc) · 2.05 KB
/
Label.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
import { LabelAndDescription, LabelLanguages } from '@wmde/wikibase-datamodel-types';
import normalizeOutput from './utils/normalizeOutput';
/**
* class for labels
*
* @class
*/
export default class Label {
/** the language of the description */
language: LabelLanguages;
/** the value of the description */
value: string;
/**
* @param {LabelAndDescription} label the label for this class in json format
* @example
* const label = new Label({ language: 'en', value: 'Douglas Adams' });
*/
constructor(label: LabelAndDescription) {
this.language = label.language;
this.value = label.value;
}
/**
* create a unique id for the Label
*
* @returns {string} the id
*/
public get internalID(): string {
return `${this.language}:${this.value}`;
}
/**
* @returns {LabelAndDescription} the label as json
* @example
* const json = label.toJson();
*/
toJSON(): LabelAndDescription {
return normalizeOutput({
language: this.language,
value: this.value
});
}
/**
* this function checks if two Labels are equal
*
* @param {LabelAndDescription} other the other Label
* @returns {boolean} true if the Labels are equal
* @example
* const label1 = new Label({ language: 'en', value: 'foo' });
* const label2 = new Label({ language: 'en', value: 'bar' });
*
* label1.equals(label2); // false
*/
equals(other: LabelAndDescription): boolean {
return this.language === other.language && this.value === other.value;
}
/**
* create a label from a language and a value
*
* @param {string} language the language of the label
* @param {string} value the value of the label
* @returns {Label} the label object
* @example
* const label = Label.fromString('en', 'Douglas Adams')
*/
static fromString(language: LabelLanguages, value: string): Label {
return new Label({ language, value });
}
}