xmljs typings. Closes #36202 (#36228)

* added typings for xmljs. Closes #36202

* removed partial interface

* removed I for interface
This commit is contained in:
blackshadev
2019-06-20 14:03:28 -07:00
committed by Daniel Rosenwasser
parent 8deb2a8c32
commit d8c0d5c929
4 changed files with 162 additions and 0 deletions
+116
View File
@@ -0,0 +1,116 @@
// Type definitions for xmljs 0.3
// Project: https://github.com/blackshadev/xmljs
// Definitions by: Vincent Hagen <https://github.com/me>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/*~ Note that ES6 modules cannot directly export class objects.
*~ This file should be imported using the CommonJS-style:
*~ import x = require('someLibrary');
*~
*~ Refer to the documentation to understand common
*~ workarounds for this limitation of ES6 modules.
*/
/*~ If this module is a UMD module that exposes a global variable 'myClassLib' when
*~ loaded outside a module loader environment, declare that global here.
*~ Otherwise, delete this declaration.
*/
export as namespace XmlParser;
/*~ This declaration specifies that the class constructor function
*~ is the exported object from the file
*/
export = XmlParser;
interface ParserParameters {
/**
* Whenever or not to use a strict parser
*/
strict?: boolean;
/**
* Do not trimtext and comment nodes
*/
noTrim?: boolean;
/**
* Do not normalize whitespaces in text
*/
noNormalizeWhitespaces?: boolean;
/**
* Turn the tagsNames to lowercase
*/
lowercaseTagnames?: boolean;
/**
* Disables xml namespaces
*/
noNamespaces?: boolean;
/**
* Disable position tracing of sax
*/
noTracing?: boolean;
/**
* Allow only predefined entities
*/
strictEntities?: boolean;
}
/*~ Write your module's methods and properties in this class */
declare class XmlParser {
constructor(oPar: ParserParameters);
/**
* Parses a xml string
* @param xml XML string to parse
* @param cb Callback function with error and the result (an Node)
* @returns whenever or not there where any errors
*/
parseString(xml: string, cb: (err: null | Error[], xmlNode: Node) => void): boolean;
errors: Error[];
/**
* Returns whenever a node is a XmlNode or not
* @param n
*/
static isXmlNode(n: Node): n is XmlNode;
}
/**
* Attribute
*/
declare class Attribute {
name: string;
text: string;
}
/**
* XML Attribute, an attribute with a namespace
*/
declare class XmlAttribute extends Attribute {
ns: string;
}
declare class Node {
/**
* Gets an node attribute by name
* @param attrName Name of the attribute
* @param ignoreCase whenever or not to ignore the name casing
*/
getAttribute(attrName: string, ignoreCase: boolean): XmlAttribute;
/**
* Traverses the XML Nodes and iterates through specified path
* @param path Node names to traverse down
* @param ignoreCase Ignore the casing of the path / node names
*/
path(path: string[], ignoreCase: boolean): XmlNode[];
/**
* Recursively traverses the nodes and calls the given function in-order
* @param fn Function to call for each node
*/
visit(fn: (n: Node) => void): void;
}
declare class XmlNode extends Node {
name: string;
ns: string;
prefix: string;
localName: string;
text: string;
}
+16
View File
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es5"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": ["index.d.ts", "xmljs-tests.ts"]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }
+29
View File
@@ -0,0 +1,29 @@
import XmlParser = require("xmljs");
const p = new XmlParser({ strict: true });
const xml = `<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns:m="http://www.example.org/stock">
<m:GetStockPriceResponse>
<m:Price>34.5</m:Price>
<m:Price>30.4</m:Price>
</m:GetStockPriceResponse>
</soap:Body>
</soap:Envelope>`; // XML in the examples direct
p.parseString(xml, (err, xmlNode) => {
if (err) {
// console.error(err);
return;
}
const nodes = xmlNode.path(
["Envelope", "Body", "GetstockpriceResponse", "Price"],
true
);
const arr = nodes.map(n => n.text);
// console.log(arr);
});