UNPKG

1.27 kBJavaScriptView Raw
1import { xmlDecode } from "./util.mjs";
2
3/**
4 * Value object.
5 */
6export class Value {
7 /**
8 * Value type.
9 */
10
11 /**
12 * Tag names.
13 */
14
15 /**
16 * Value constructor.
17 */
18 constructor() {}
19
20 /**
21 * Value type.
22 *
23 * @returns Type string.
24 */
25 get type() {
26 return this.constructor.TYPE;
27 }
28
29 /**
30 * Cast to specific type or null.
31 *
32 * @param Type Type constructor.
33 * @returns This object or null.
34 */
35 castTo(Type) {
36 return this.type === Type.TYPE ? this : null;
37 }
38
39 /**
40 * Cast to specific type or throw.
41 *
42 * @param Type Type constructor.
43 * @returns This object.
44 */
45 castAs(Type) {
46 const casted = this.castTo(Type);
47 if (!casted) {
48 throw new Error(`Cannot cast type '${this.type}' to '${Type.TYPE}'`);
49 }
50 return casted;
51 }
52
53 /**
54 * Decode document from string.
55 *
56 * @param xml XML string.
57 */
58 fromXml(xml) {
59 const {
60 documentElement
61 } = xmlDecode(xml);
62 this.fromXmlElement(documentElement);
63 }
64
65 /**
66 * Decode value from element.
67 *
68 * @param element XML element.
69 */
70
71 /**
72 * Encode Value to string.
73 *
74 * @param options Options object.
75 * @param depth Indent depth.
76 * @returns Xml string.
77 */
78}
79//# sourceMappingURL=value.mjs.map
\No newline at end of file