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