From 1d379aed921901623072202cd3d92f3bcbcb1e5b Mon Sep 17 00:00:00 2001 From: Evan Shortiss Date: Fri, 16 Aug 2019 12:56:01 -0700 Subject: [PATCH] add pixl-xml types (#37683) * add pixl-xml types * address linter errors --- types/pixl-xml/index.d.ts | 144 +++++++++++++++++++++++++++++++ types/pixl-xml/pixl-xml-tests.ts | 23 +++++ types/pixl-xml/tsconfig.json | 23 +++++ types/pixl-xml/tslint.json | 1 + 4 files changed, 191 insertions(+) create mode 100644 types/pixl-xml/index.d.ts create mode 100644 types/pixl-xml/pixl-xml-tests.ts create mode 100644 types/pixl-xml/tsconfig.json create mode 100644 types/pixl-xml/tslint.json diff --git a/types/pixl-xml/index.d.ts b/types/pixl-xml/index.d.ts new file mode 100644 index 0000000000..6c73b131e3 --- /dev/null +++ b/types/pixl-xml/index.d.ts @@ -0,0 +1,144 @@ +// Type definitions for pixl-xml 1.0 +// Project: https://github.com/jhuckaby/pixl-xml +// Definitions by: Evan Shortiss +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +/** + * Options that can be passed to parse function + */ +export interface PixlParseOptions { + preserveAttributes?: boolean; + lowerCase?: boolean; + preserveDocumentNode?: boolean; + preserveWhitespace?: boolean; + forceArrays?: boolean; +} + +/** + * Parser class can be used for a more object oriented style API + */ +export class Parser { + constructor(xml: string, options?: PixlParseOptions) + + attribsKey: string; + dataKey: string; + documentNodeName: string; + piNodeList: string[]; + dtdNodeList: string[]; + errors: any[]; + + /** + * The XML input that was provided to the constructor + */ + text: string; + + /** + * The resulting JSON from the parse operation + */ + tree: OutputJsonInterface; + + /** + * Returns the parsed XML tree. This tree is mutable and can be modified + * as necessary. + */ + getTree(): OutputJsonInterface; + + /** + * Returns an XML string with formatting determined by the params provided + * @param indentCharacter + * @param eol + */ + compose(indentCharacter?: string, eol?: string): string; +} + +/** + * Encode the three standard XML entities, ampersand (&), left-angle-bracket + * (<) and right-angle-bracket (>), into their XML-safe counterparts. + * @param xml + */ +export function encodeEntities(xml: string): string; + +/** + * Similar to encodeEntities, but it also includes encoding for single-quotes + * (') and double-quotes ("). It is used for encoding an XML string for + * composing into an attribute value. + * @param xml + */ +export function encodeAttribEntities(xml: string): string; + +/** + * This function decodes all the standard XML entities back into their + * original characters. This includes ampersand (&), left-angle-bracket (<), + * right-angle-bracket (>), single-quote (') and double-quote ("). + * @param xml + */ +export function decodeEntities(xml: string): string; + +/** + * This function will wrap anything passed to it into an array and return the + * array, unless the item passed is already an array, in which case it is + * simply returned verbatim. + * @param input + */ +export function alwaysArray(input: T|T[]): T[]; + +/** + * This function returns all the hash keys as an array. Useful for sorting and + * then iterating over the sorted list. + * @param input + */ +export function hashKeysToArray(input: object): string[]; + +/** + * This function returns true if the provided argument is a hash (object), + * false otherwise. + * @param input + */ +export function isaHash(input: any): boolean; + +/** + * This function returns true if the provided argument is an array + * (or is array-like), false otherwise. + * @param input + */ +export function isaArray(input: any): boolean; + +/** + * his function returns the number of keys in the specified hash. + * @param hash + */ +export function numKeys(hash: object): number; + +/** + * This function returns the first key of the hash when iterating over it. + * Note that hash keys are stored in an undefined order. + * @param hash + */ +export function firstKey(hash: object): string; + +/** + * Parse an XML string into a JSON Object. + * Throws if a parsing error occurs. + * + * @example + * + * pixl.parse(xmlstring) as SomeInterface + * + * @param xml + */ +export function parse(xml: string, options?: PixlParseOptions): {}; + +/** + * Composes XML Objects back to a string. Pass in your pre-parsed XML object, + * and an outer wrapper element name. It helps if you parsed the original XML + * using the preserveAttributes option for this, as it will honor the _Attribs + * sub-objects and convert them back into real XML attributes. + * @param doc + * @param outerElName + * @param indentSize + * @param indentCharacter + * @param eolCharacter + * @param preserveOrder + */ +export function stringify(doc: object, outerElName?: string, indentSize?: number, indentCharacter?: string, eolCharacter?: string, preserveOrder?: boolean): string; diff --git a/types/pixl-xml/pixl-xml-tests.ts b/types/pixl-xml/pixl-xml-tests.ts new file mode 100644 index 0000000000..1df69f8ce7 --- /dev/null +++ b/types/pixl-xml/pixl-xml-tests.ts @@ -0,0 +1,23 @@ +import * as XML from 'pixl-xml'; + +const xmlstring = 'OctocatEvan'; + +interface User { + id: string; + _Data: string; +} + +interface UserList { + users: User[]; +} + +// Verify type inference from template type with constructor +const parserInstance = new XML.Parser(xmlstring); +const usersFromParser = parserInstance.getTree().users; +usersFromParser[0].id; + +// Verify type inference from template type with function +const usersFromParseFn = XML.parse(xmlstring) as UserList; +usersFromParseFn.users[0].id; + +XML.stringify(usersFromParseFn); diff --git a/types/pixl-xml/tsconfig.json b/types/pixl-xml/tsconfig.json new file mode 100644 index 0000000000..2f150383ce --- /dev/null +++ b/types/pixl-xml/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "pixl-xml-tests.ts" + ] +} diff --git a/types/pixl-xml/tslint.json b/types/pixl-xml/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/pixl-xml/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }