From d8c0d5c9292d122f71efe5bdfca9111c5696294e Mon Sep 17 00:00:00 2001 From: blackshadev Date: Thu, 20 Jun 2019 23:03:28 +0200 Subject: [PATCH] xmljs typings. Closes #36202 (#36228) * added typings for xmljs. Closes #36202 * removed partial interface * removed I for interface --- types/xmljs/index.d.ts | 116 +++++++++++++++++++++++++++++++++++++ types/xmljs/tsconfig.json | 16 +++++ types/xmljs/tslint.json | 1 + types/xmljs/xmljs-tests.ts | 29 ++++++++++ 4 files changed, 162 insertions(+) create mode 100644 types/xmljs/index.d.ts create mode 100644 types/xmljs/tsconfig.json create mode 100644 types/xmljs/tslint.json create mode 100644 types/xmljs/xmljs-tests.ts diff --git a/types/xmljs/index.d.ts b/types/xmljs/index.d.ts new file mode 100644 index 0000000000..acfbafa6df --- /dev/null +++ b/types/xmljs/index.d.ts @@ -0,0 +1,116 @@ +// Type definitions for xmljs 0.3 +// Project: https://github.com/blackshadev/xmljs +// Definitions by: Vincent Hagen +// 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; +} diff --git a/types/xmljs/tsconfig.json b/types/xmljs/tsconfig.json new file mode 100644 index 0000000000..41ce05079a --- /dev/null +++ b/types/xmljs/tsconfig.json @@ -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"] +} diff --git a/types/xmljs/tslint.json b/types/xmljs/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/xmljs/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/xmljs/xmljs-tests.ts b/types/xmljs/xmljs-tests.ts new file mode 100644 index 0000000000..74c0a0fe9b --- /dev/null +++ b/types/xmljs/xmljs-tests.ts @@ -0,0 +1,29 @@ +import XmlParser = require("xmljs"); + +const p = new XmlParser({ strict: true }); +const xml = ` + + + + + 34.5 + 30.4 + + + +`; // 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); +});