-
Notifications
You must be signed in to change notification settings - Fork 7
/
TextAssets.ts
58 lines (47 loc) · 1.67 KB
/
TextAssets.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
import { LanguageCode } from "../../client/CachedAssetsManager";
import { EnkaClient } from "../../client/EnkaClient";
import { AssetsNotFoundError } from "../../errors/AssetsNotFoundError";
export class TextAssets {
readonly id: number;
readonly enka: EnkaClient;
convertToHtmlFormat: boolean;
readonly directory: string | null;
constructor(id: number, enka: EnkaClient, convertToHtmlFormat = false, directory?: string) {
this.id = id;
this.enka = enka;
this.convertToHtmlFormat = convertToHtmlFormat;
this.directory = directory ?? null;
}
/**
* @throws {AssetsNotFoundError}
*/
get(lang?: LanguageCode): string {
lang ??= this.enka.options.defaultLanguage;
let text = this.enka.cachedAssetsManager.getLanguageData(lang, this.directory ?? undefined)[this.id];
if (!text) throw new AssetsNotFoundError("Text Assets", this.id);
if (this.convertToHtmlFormat) {
text = text
.replace(/<color=([^>]+)>/g, "<span style=\"color:$1\">")
.replace(/<\/color>/g, "</span>")
.replace(/\r\n|\n|\\n|\r/gm, "<br>");
}
return text;
}
/**
* @returns null instead of throwing AssetsNotFoundError.
*/
getNullable(lang?: LanguageCode): string | null {
try {
return this.get(lang);
} catch (e) {
return null;
}
}
setConvertToHtmlFormat(convertToHtmlFormat: boolean): this {
this.convertToHtmlFormat = convertToHtmlFormat;
return this;
}
toString(): string {
return this.getNullable() ?? `Unknown TextAssets(${this.id})`;
}
}