Update typings for xmldoc

This commit is contained in:
Andrew Sheehan
2017-05-23 09:58:22 -05:00
parent 5a60b831fb
commit 5ae5974c52
2 changed files with 90 additions and 5 deletions
+48 -3
View File
@@ -1,11 +1,13 @@
// Type definitions for xmldoc 0.5.1
// Type definitions for xmldoc 1.1.0
// Project: https://www.npmjs.com/package/xmldoc
// Definitions by: Xavier Stouder <https://github.com/Xstoudi>
// Definitions by: Xavier Stouder <https://github.com/Xstoudi>, Andrew Sheehan <https://github.com/ajsheehan>
// Definitions: https://github.com/DefinitelyTyped/
export class XmlDocument {
constructor(xmlString: string);
doctype: string;
eachChild(func: (child: XmlElement, index?: number, array?: XmlElement[]) => void): void;
childNamed(name: string): XmlElement;
childrenNamed(name: string): XmlElement[];
@@ -13,8 +15,19 @@ export class XmlDocument {
descendantWithPath(path: string): XmlElement;
valueWithPath(path: string): string;
toString(opts?: XmlOptions): string;
toStringWithIndent(indent: string, opts?: XmlOptions): string;
}
export class XmlElement {
constructor(tag: XmlElement);
name: string;
attr: XmlAttributes;
val: string;
children: XmlElement[];
firstChild: XmlElement;
lastChild: XmlElement;
eachChild(func: (child: XmlElement, index?: number, array?: XmlElement[]) => void): void;
childNamed(name: string): XmlElement;
childrenNamed(name: string): XmlElement[];
@@ -22,10 +35,42 @@ export class XmlElement {
descendantWithPath(path: string): XmlElement;
valueWithPath(path: string): string;
toString(opts?: XmlOptions): string;
toStringWithIndent(indent: string, opts?: XmlOptions): string;
}
export class XmlTextNode {
constructor(text: string);
toString(opts?: XmlOptions): string;
toStringWithIndent(indent: string, opts?: XmlOptions): string;
}
export class XmlCDataNode {
constructor(cdata: string);
toString(opts?: XmlOptions): string;
toStringWithIndent(indent: string, opts?: XmlOptions): string;
}
export class XmlCommentNode {
constructor(comment: string);
toString(opts?: XmlOptions): string;
toStringWithIndent(indent: string, opts?: XmlOptions): string;
}
export interface XmlOptions {
compressed?: boolean;
trimmed?: boolean;
html?: boolean;
preserveWhitespace?: boolean;
trimmed?: boolean;
}
export interface XmlTag {
name: string;
attributes: XmlAttributes;
}
export interface XmlAttributes {
[key: string]: string;
}
+42 -2
View File
@@ -1,4 +1,44 @@
import * as xmldoc from "xmldoc";
const doc = new xmldoc.XmlDocument("<poney>magic</poney>");
console.log(doc.toString());
//
// https://github.com/nfarina/xmldoc#usage
//
const document = new xmldoc.XmlDocument("<some>xml</some>");
//
// https://github.com/nfarina/xmldoc#descendantwithpathpath
//
const bookNode = new xmldoc.XmlDocument(`
<book>
<author>
<name isProper="true">George R. R. Martin</name>
...
</author>
...
</book>`);
const nameNode = bookNode.descendantWithPath("author.name"); // return <name> node
//
// https://github.com/nfarina/xmldoc#valuewithpathpath
//
const authorName = bookNode.valueWithPath("author.name"); // return "George R. R. Martin"
const authorIsProper = bookNode.valueWithPath("author.name@isProper"); // return "true"
//
// https://github.com/nfarina/xmldoc#tostringoptions
//
document.toString({ compressed: true }) // strips indents and linebreaks
document.toString({ trimmed: true }) // trims long strings for easier debugging
document.toString({ preserveWhitespace: true }) // prevents whitespace being removed from around element values
const xml = "<author><name>looooooong value</name></author>";
console.log("My document: \n" + new xmldoc.XmlDocument(xml).toString({ trimmed: true }))
/* Prints:
My Document:
<hello>
loooooooo…
</hello>
*/