Update types for ltx

This commit is contained in:
Dimitri Benin
2018-11-23 13:14:50 +01:00
parent c2fac6d235
commit f03ea514f7
14 changed files with 250 additions and 96 deletions
+13 -13
View File
@@ -1,17 +1,17 @@
// Type definitions for ltx 2.6
// Type definitions for ltx 2.8
// Project: github.com/node-xmpp/ltx/
// Definitions by: PJakcson <https://github.com/PJakcson>
// BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
export {Element } from './lib/Element';
export {nameEqual, attrsEqual, childrenEqual, equal} from './lib/equal';
export {isNode, isElement, isText} from './lib/is';
export {clone} from './lib/clone';
export {createElement} from './lib/createElement';
export {escapeXML, unescapeXML, escapeXMLText, unescapeXMLText} from './lib/escape';
export {Parser} from './lib/Parser';
export {parse} from './lib/parse';
export {tag} from './lib/tag';
export {tagString} from './lib/tagString';
export {stringify} from './lib/stringify';
export * from './lib/Element';
export * from './lib/equal';
export * from './lib/is';
export * from './lib/clone';
export * from './lib/createElement';
export * from './lib/escape';
export * from './lib/Parser';
export * from './lib/parse';
export * from './lib/tag';
export * from './lib/tagString';
export * from './lib/stringify';
+52 -45
View File
@@ -1,21 +1,26 @@
import { clone } from './clone';
export type Node = Element | TextNode;
export type TextNode = string | number;
/**
* Element
*
* Attributes are in the element.attrs object. Children is a list of
* either other Elements or Strings for text content.
**/
export declare class Element {
*/
export class Element {
name: string;
parent: Element;
parent: Element | null;
children: Element[];
attrs: any;
attrs: { [attrName: string]: any };
constructor(name: string, attrs?: any);
constructor(name: string, attrs?: string | { [attrName: string]: any });
/**
* if (element.is('message', 'jabber:client')) ...
**/
is(name: string, xmlns?: any): boolean;
*/
is(name: string, xmlns?: string): boolean;
/**
* without prefix.
@@ -24,50 +29,44 @@ export declare class Element {
/**
* retrieves the namespace of the current element, upwards recursively
**/
getNS(): any;
*/
getNS(): string | undefined;
/**
* find the namespace to the given prefix, upwards recursively
**/
findNS(prefix: string): any;
*/
findNS(prefix: string): string | undefined;
/**
* Recursiverly gets all xmlns defined, in the form of {url:prefix}
**/
getXmlns(): any;
*/
getXmlns(): { [key: string]: string };
setAttrs(attrs: any): void;
setAttrs(attrs: string | { [attrName: string]: any }): void;
/**
* xmlns can be null, returns the matching attribute.
**/
getAttr(name: string, xmlns?: any): any;
* returns the matching attribute.
*/
getAttr(name: string, xmlns?: string): any;
/**
* xmlns can be null
**/
getChild(name: string, xmlns?: any): Element;
getChild(name: string, xmlns?: string): Element | undefined;
/**
* xmlns can be null
**/
getChildren(name: string, xmlns?: any): Element[];
getChildren(name: string, xmlns?: string): Element[];
/**
* xmlns and recursive can be null
**/
getChildByAttr(attr: any, val: any, xmlns?: any, recursive?: any): Element;
getChildByAttr(
attr: string,
val: any,
xmlns?: string,
recursive?: boolean
): Element | undefined;
getChildrenByAttr(attr: string, val: any, xmlns?: string, recursive?: boolean): Element[];
/**
* xmlns and recursive can be null
**/
getChildrenByAttr(attr: any, val: any, xmlns?: any, recursive?: any): Element[];
getChildrenByFilter(filter: (child: Node) => boolean, recursive?: boolean): Element[];
getText(): string;
getChildText(name: string, xmlns?: any): string;
getChildText(name: string, xmlns?: string): string | null;
/**
* Return all direct descendents that are Elements.
@@ -77,37 +76,39 @@ export declare class Element {
getChildElements(): Element[];
/** returns uppermost parent */
root(): Element;
root(): Element | this;
tree(): Element;
tree(): Element | this;
/** just parent or itself */
up(): Element;
up(): Element | this;
/** create child node and return it */
c(name: string, attrs?: any): Element;
c(name: string, attrs?: { [key: string]: any }): Element;
cnode<T extends Element>(child: T): T;
/** add text node and return element */
t(text: string): Element;
t(text: TextNode): this;
/**
* Either:
* el.remove(childEl)
* el.remove('author', 'urn:...')
*/
remove(el: Element, xmlns?: any): Element;
remove(el: Element | string, xmlns?: string): this;
clone(): Element;
clone: typeof clone;
text(val: string): string;
text(val?: string): string;
attr(attr: any, val: any): any;
attr(attr: string, val?: any): any;
toString(): string;
toJSON(): any;
toJSON(): ElementJson;
write(writer: any): void;
write(writer: (part: string) => void): void;
nameEquals(el: Element): boolean;
@@ -117,3 +118,9 @@ export declare class Element {
equals(el: Element): boolean;
}
export interface ElementJson {
name: string;
attrs: { [attrName: string]: any };
children: Array<ElementJson | TextNode>;
}
+12 -2
View File
@@ -1,5 +1,15 @@
/// <reference types="node" />
import { EventEmitter } from 'events';
export declare class Parser extends EventEmitter {
constructor(options?: any);
import { Element } from './Element';
export class Parser extends EventEmitter {
constructor(options?: ParserOptions);
write(data: string): void;
end(data: string): void;
}
export interface ParserOptions {
Parser?: typeof Parser;
Element?: typeof Element;
}
+3 -1
View File
@@ -1 +1,3 @@
export declare function clone(el: any): any;
import { Element } from './Element';
export function clone<T extends Element>(el: T): T;
+6 -2
View File
@@ -1,2 +1,6 @@
import { Element } from './Element';
export declare function createElement(name: string, attrs: any): Element;
import { Element, Node } from './Element';
export function createElement(
name: string,
attrs?: string | { [attrName: string]: any },
...children: Node[]
): Element;
+6 -4
View File
@@ -1,4 +1,6 @@
export declare function nameEqual(a: any, b: any): boolean;
export declare function attrsEqual(a: any, b: any): boolean;
export declare function childrenEqual(a: any, b: any): boolean;
export declare function equal(a: any, b: any): boolean;
import { Element } from './Element';
export function nameEqual(a: Element, b: Element): boolean;
export function attrsEqual(a: Element, b: Element): boolean;
export function childrenEqual(a: Element, b: Element): boolean;
export function equal(a: Element, b: Element): boolean;
+4 -4
View File
@@ -1,4 +1,4 @@
export declare function escapeXML(s: string): string;
export declare function unescapeXML(s: string): string;
export declare function escapeXMLText(s: string): string;
export declare function unescapeXMLText(s: string): string;
export function escapeXML(s: string): string;
export function unescapeXML(s: string): string;
export function escapeXMLText(s: string): string;
export function unescapeXMLText(s: string): string;
+5 -3
View File
@@ -1,3 +1,5 @@
export declare function isNode(el: any): boolean;
export declare function isElement(el: any): boolean;
export declare function isText(el: any): boolean;
import { Element, Node } from './Element';
export function isNode(el: any): el is Node;
export function isElement(el: any): el is Element;
export function isText(el: any): el is string;
+4 -1
View File
@@ -1 +1,4 @@
export declare function parse(data: any, options?: any): any;
import { ParserOptions, Parser } from './Parser';
import { Element } from './Element';
export function parse(data: string, options?: ParserOptions | Parser): Element;
+3 -1
View File
@@ -1 +1,3 @@
export declare function stringify(el: any, indent: any, level: any): string;
import { Element } from './Element';
export function stringify(el: Element, indent?: number, level?: number): string;
+3 -1
View File
@@ -1 +1,3 @@
export declare function tag(d: any): any;
import { Element } from './Element';
export function tag(literals: string[], ...substitutions: string[]): Element;
+1 -1
View File
@@ -1 +1 @@
export declare function tagString(d: any): string;
export function tagString(literals: string[], ...substitutions: string[]): string;
+137 -11
View File
@@ -1,21 +1,147 @@
import * as ltx from 'ltx';
ltx.parse('<document/>');
let el: ltx.Element = null as any;
let maybeEl: ltx.Element | undefined = null as any;
let els: ltx.Element[] = [];
let bool: boolean;
const any: any = null;
let str: string = null as any;
const getChildTextElement = ltx.parse('<test><child>body text</child></test>') as ltx.Element;
if (getChildTextElement.getChildText('child') !== 'body text') {
throw new Error("body does not match");
el = ltx.clone(el);
bool = ltx.nameEqual(el, el);
bool = ltx.attrsEqual(el, el);
bool = ltx.childrenEqual(el, el);
bool = ltx.equal(el, el);
el = ltx.createElement('el');
el = ltx.createElement('el', 'xml');
el = ltx.createElement('el', { foo: 'bar' });
el = ltx.createElement('el', { foo: 'bar' }, el);
el = ltx.createElement('el', { foo: 'bar' }, 'hi');
if (ltx.isNode(any)) {
// $ExpectType Node
any;
}
if (ltx.isElement(any)) {
// $ExpectType Element
any;
}
if (ltx.isText(any)) {
// $ExpectType string
any;
}
const p = new ltx.Parser();
str = ltx.escapeXML(str);
str = ltx.unescapeXML(str);
str = ltx.escapeXMLText(str);
str = ltx.unescapeXMLText(str);
el = ltx.parse('<document/>');
el = ltx.parse('<document/>', (null as any) as ltx.Parser);
el = ltx.parse('<document/>', { Parser: (null as any) as typeof ltx.Parser });
el = ltx.parse('<document/>', { Element: (null as any) as typeof ltx.Element });
el = ltx.tag(['document'], 'foo');
str = ltx.tagString(['document'], 'foo');
str = ltx.stringify(el);
str = ltx.stringify(el, 1);
str = ltx.stringify(el, 1, 1);
const getChildTextElement = ltx.parse('<test><child>body text</child></test>');
if (getChildTextElement.getChildText('child') !== 'body text') {
throw new Error('body does not match');
}
let p: ltx.Parser;
p = new ltx.Parser();
p = new ltx.Parser({ Parser: (null as any) as typeof ltx.Parser });
p = new ltx.Parser({ Element: (null as any) as typeof ltx.Element });
p.on('tree', (ignored: any) => {});
p.on('error', (ignored: any) => {});
const el = new ltx.Element('root').c('children');
el.c('child', {age: 5}).t('Hello').up()
.c('child', {age: 7}).t('Hello').up()
.c('child', {age: 99}).t('Hello').up();
el = new ltx.Element('root').c('children');
el.root().toString();
bool = el.is('el');
bool = el.is('el', 'ns');
str = el.getName();
let s: string | undefined = el.getNS();
s = el.findNS('ns');
const xmlns: { [key: string]: string } = el.getXmlns();
el.setAttrs('ho');
el.setAttrs({ my: 'attr' });
el.getAttr('ho');
el.getAttr('ho', 'ns');
maybeEl = el.getChild('el');
maybeEl = el.getChild('el', 'ns');
els = el.getChildren('el');
els = el.getChildren('el', 'ns');
maybeEl = el.getChildByAttr('my', 'attr');
maybeEl = el.getChildByAttr('my', 'attr', 'ns');
maybeEl = el.getChildByAttr('my', 'attr', 'ns', true);
els = el.getChildrenByAttr('my', 'attr');
els = el.getChildrenByAttr('my', 'attr', 'ns');
els = el.getChildrenByAttr('my', 'attr', 'ns', true);
els = el.getChildrenByFilter(child => {
// $ExpectType Node
child;
return true;
});
els = el.getChildrenByFilter(child => {
// $ExpectType Node
child;
return true;
}, true);
str = el.getText();
let maybeS: string | null = el.getChildText('hi');
maybeS = el.getChildText('hi', 'ns');
els = el.getChildElements();
class MyEl extends ltx.Element {
foo: 'bar';
}
let myEl = new MyEl('el');
// $ExpectType Element | MyEl
myEl.root();
// $ExpectType Element | MyEl
myEl.tree();
// $ExpectType Element | MyEl
myEl.up();
el = el.c('hi');
el = el.c('hi', { my: 'attr' });
myEl = myEl.cnode(myEl);
myEl = myEl.t('hi');
myEl = myEl.t(1);
myEl = myEl.remove(el);
myEl = myEl.remove('el');
myEl = myEl.remove('el', 'ns');
myEl = myEl.clone(myEl);
str = el.text();
str = el.text('val');
el.attr('my');
el.attr('my', 'attr');
str = el.toString();
const json: ltx.ElementJson = el.toJSON();
el.write(part => {
// $ExpectType string
part;
});
bool = el.nameEquals(el);
bool = el.attrsEquals(el);
bool = el.childrenEquals(el);
bool = el.equals(el);
el.c('child', { age: 5 })
.t('Hello')
.up()
.c('child', { age: 7 })
.t('Hello')
.up()
.c('child', { age: 99 })
.t('Hello')
.up();
+1 -7
View File
@@ -1,9 +1,3 @@
{
"extends": "dtslint/dt.json",
"rules": {
// All are TODOs
"jsdoc-format": false,
"no-consecutive-blank-lines": false,
"strict-export-declare-modifiers": false
}
"extends": "dtslint/dt.json"
}