diff --git a/xml-js/index.d.ts b/xml-js/index.d.ts new file mode 100644 index 0000000000..eb89067c86 --- /dev/null +++ b/xml-js/index.d.ts @@ -0,0 +1,82 @@ +// Type definitions for xml-js 0.9.6 +// Project: https://github.com/nashwaan/xml-js +// Definitions by: Denis Carriere +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export interface ElementCompact { + [key: string]: any + _attributes?: { + [key: string]: number | number + } + _cdata?: string | number + _comment?: string | number + _declaration?: { + _attributes?: { + version?: string | number + encoding?: string | number + } + } + _text?: string | number +} + +export interface Element { + attributes?: { + [key: string]: string | number + } + cdata?: string | number + comment?: string | number + declaration?: { + attributes?: { + version: string | number + encoding: string | number + } + } + elements?: Array + text?: string | number + type?: string | number + name?: string | number +} + +declare namespace Options { + interface XML2JS extends ChangingKeyNames, IgnoreOptions { + compact?: boolean + spaces?: number | string + trim?: boolean + sanitize?: boolean + nativeType?: boolean + addParent?: boolean + alwaysChildren?: boolean + } + + interface JS2XML extends ChangingKeyNames, IgnoreOptions { + spaces?: number | string + compact?: boolean + fullTagEmptyElement?: boolean + } + + interface IgnoreOptions { + ignoreDeclaration?: boolean + ignoreAttributes?: boolean + ignoreComment?: boolean + ignoreCdata?: boolean + ignoreText?: boolean + } + + interface ChangingKeyNames { + declarationKey?: string + attributesKey?: string + textKey?: string + cdataKey?: string + commentKey?: string + parentKey?: string + typeKey?: string + nameKey?: string + elementsKey?: string + } +} + +export function js2xml(json: Element | ElementCompact, options?: Options.JS2XML): string; +export function json2xml(json: Element | ElementCompact, options?: Options.JS2XML): string; +export function xml2json(xml: string, options?: Options.XML2JS): any; +export function xml2js(xml: string, options?: Options.XML2JS): any; + diff --git a/xml-js/tsconfig.json b/xml-js/tsconfig.json new file mode 100644 index 0000000000..5880b2f31d --- /dev/null +++ b/xml-js/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "xml-js-tests.ts" + ] +} diff --git a/xml-js/xml-js-tests.ts b/xml-js/xml-js-tests.ts new file mode 100644 index 0000000000..d437ee14f4 --- /dev/null +++ b/xml-js/xml-js-tests.ts @@ -0,0 +1,48 @@ +import { Element, ElementCompact } from 'xml-js' +import * as convert from 'xml-js' + +// Declaration +const declarationCompact1: ElementCompact = { _declaration: { _attributes: { version: 2 } }}; +const declarationCompact2: ElementCompact = { _declaration: { _attributes: { version: '1.0', encoding: 'utf-8' }}}; +const declaration1: Element = { declaration: { }}; +const declaration2: Element = { declaration: { attributes: { version: '1.0', encoding: 'utf-8' }}}; + +// Comment +const commentCompact: ElementCompact = { _comment : 'Hello, World!' }; +const comment: Element = { elements: [{ type: 'comment', comment: 'Hello, World!' }]}; + +// CDATA +const cdataCompact: ElementCompact = { _cdata: '' }; +const cdata: Element = { elements : [{ type: 'cdata', cdata: '' }]}; + +// Element +const elementCompact1: ElementCompact = { a: {} }; +const element1: Element = { elements:[{ type: 'element', name: 'a' }]}; + +const elementCompact2: ElementCompact = { a: { _attributes: { x: '1.234', y:'It\'s' }}}; +const element2: Element = { elements: [{ type: 'element', name: 'a', attributes: { x: '1.234', y: 'It\'s' }}]}; + +const elementCompact3: ElementCompact = { a: { _text: ' Hi ' }}; +const element3: Element = { elements:[{ type: 'element', name: 'a', elements: [{ type: 'text', text: ' Hi ' }]}]}; + +const elementCompact4: ElementCompact = { a: {}, b: {} }; +const element4: Element = { elements:[{ type: 'element', name: 'a' }, { type: 'element', name: 'b' }]}; + +const elementCompact5: ElementCompact = { a: { b: {} }}; +const element5: Element = { elements: [{ type: 'element', name: 'a', elements: [{ type: 'element', name: 'b' }]}]}; + +// xml2json +const xml = ` + + + Happy + Work + Play +`; +convert.xml2json(xml, { compact: true, spaces: 4 }); +convert.xml2json(xml, { compact: false }); + +// json2xml +convert.json2xml({ a: {} }, { compact: true, spaces: 4 }); +convert.json2xml({ elements:[{ type: 'element', name: 'a' }]}, { compact: false }); +