UNPKG

2.94 kBSource Map (JSON)View Raw
1{"version":3,"file":"value.js","names":["_util","require","Value","constructor","type","TYPE","castTo","Type","castAs","casted","Error","fromXml","xml","documentElement","xmlDecode","fromXmlElement","exports"],"sources":["../src/value.ts"],"sourcesContent":["import {IToXmlOptions} from './options';\nimport {IElement, xmlDecode} from './util';\n\n/**\n * Value object.\n */\nexport abstract class Value {\n\t/**\n\t * Value type.\n\t */\n\tpublic static readonly TYPE: string;\n\n\t/**\n\t * Tag names.\n\t */\n\tpublic static readonly TAG_NAMES: string[];\n\n\t/**\n\t * Value constructor.\n\t */\n\tconstructor() {}\n\n\t/**\n\t * Value type.\n\t *\n\t * @returns Type string.\n\t */\n\tpublic get type() {\n\t\treturn (this.constructor as typeof Value).TYPE;\n\t}\n\n\t/**\n\t * Cast to specific type or null.\n\t *\n\t * @param Type Type constructor.\n\t * @returns This object or null.\n\t */\n\tpublic castTo<T extends typeof Value>(Type: T): T['prototype'] | null {\n\t\treturn this.type === Type.TYPE ? this : null;\n\t}\n\n\t/**\n\t * Cast to specific type or throw.\n\t *\n\t * @param Type Type constructor.\n\t * @returns This object.\n\t */\n\tpublic castAs<T extends typeof Value>(Type: T): T['prototype'] {\n\t\tconst casted = this.castTo(Type);\n\t\tif (!casted) {\n\t\t\tthrow new Error(\n\t\t\t\t`Cannot cast type '${this.type}' to '${Type.TYPE}'`\n\t\t\t);\n\t\t}\n\t\treturn casted;\n\t}\n\n\t/**\n\t * Decode document from string.\n\t *\n\t * @param xml XML string.\n\t */\n\tpublic fromXml(xml: string) {\n\t\tconst {documentElement} = xmlDecode(xml);\n\t\tthis.fromXmlElement(documentElement);\n\t}\n\n\t/**\n\t * Decode value from element.\n\t *\n\t * @param element XML element.\n\t */\n\tpublic abstract fromXmlElement(element: Readonly<IElement>): void;\n\n\t/**\n\t * Encode Value to string.\n\t *\n\t * @param options Options object.\n\t * @param depth Indent depth.\n\t * @returns Xml string.\n\t */\n\tpublic abstract toXml(\n\t\toptions?: Readonly<IToXmlOptions> | null,\n\t\tdepth?: number\n\t): string;\n}\n"],"mappings":";;;;;;AACA,IAAAA,KAAA,GAAAC,OAAA;AAEA;AACA;AACA;AACO,MAAeC,KAAK,CAAC;EAC3B;AACD;AACA;;EAGC;AACD;AACA;;EAGC;AACD;AACA;EACCC,WAAWA,CAAA,EAAG,CAAC;;EAEf;AACD;AACA;AACA;AACA;EACC,IAAWC,IAAIA,CAAA,EAAG;IACjB,OAAQ,IAAI,CAACD,WAAW,CAAkBE,IAAI;EAC/C;;EAEA;AACD;AACA;AACA;AACA;AACA;EACQC,MAAMA,CAAyBC,IAAO,EAAyB;IACrE,OAAO,IAAI,CAACH,IAAI,KAAKG,IAAI,CAACF,IAAI,GAAG,IAAI,GAAG,IAAI;EAC7C;;EAEA;AACD;AACA;AACA;AACA;AACA;EACQG,MAAMA,CAAyBD,IAAO,EAAkB;IAC9D,MAAME,MAAM,GAAG,IAAI,CAACH,MAAM,CAACC,IAAI,CAAC;IAChC,IAAI,CAACE,MAAM,EAAE;MACZ,MAAM,IAAIC,KAAK,CACb,qBAAoB,IAAI,CAACN,IAAK,SAAQG,IAAI,CAACF,IAAK,GAClD,CAAC;IACF;IACA,OAAOI,MAAM;EACd;;EAEA;AACD;AACA;AACA;AACA;EACQE,OAAOA,CAACC,GAAW,EAAE;IAC3B,MAAM;MAACC;IAAe,CAAC,GAAG,IAAAC,eAAS,EAACF,GAAG,CAAC;IACxC,IAAI,CAACG,cAAc,CAACF,eAAe,CAAC;EACrC;;EAEA;AACD;AACA;AACA;AACA;;EAGC;AACD;AACA;AACA;AACA;AACA;AACA;AAKA;AAACG,OAAA,CAAAd,KAAA,GAAAA,KAAA"}
\No newline at end of file