x2js.d.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * Transforms between XML string and JavaScript object trees.
  3. *
  4. * @class X2JS
  5. */
  6. declare class X2JS {
  7. /**
  8. * Creates an instance of X2JS.
  9. *
  10. * @param {X2JS.Options} [config]
  11. *
  12. * @memberOf X2JS
  13. */
  14. constructor(config?: X2JS.Options);
  15. /**
  16. * Converts the provided property into an array. If the property is already an Array then it will return unchanged.
  17. *
  18. * @template T
  19. * @param {(T | Array<T>)} prop
  20. * @returns {Array<T>}
  21. *
  22. * @memberOf X2JS
  23. */
  24. asArray<T>(prop: T | Array<T>): Array<T>;
  25. /**
  26. * Converts the provided date value to a valid XML string.
  27. *
  28. * @param {(Date | number)} dt
  29. * @returns {string}
  30. *
  31. * @memberOf X2JS
  32. */
  33. toXmlDateTime(dt: Date | number): string;
  34. /**
  35. * Converts the provided date string into a javascript Date instance.
  36. *
  37. * @param {string} prop
  38. * @returns {Date}
  39. *
  40. * @memberOf X2JS
  41. */
  42. asDateTime(prop: string): Date;
  43. /**
  44. * Transformns an XML string into DOM-tree
  45. *
  46. * @param {string} xml
  47. * @returns {Node}
  48. *
  49. * @memberOf X2JS
  50. */
  51. xml2dom(xml: string): Document;
  52. /**
  53. * Transforms a DOM tree to JavaScript objects.
  54. *
  55. * @template T
  56. * @param {Document | Node} domNode
  57. * @returns {T}
  58. *
  59. * @memberOf X2JS
  60. */
  61. dom2js<T>(domNode: Document | Node): T;
  62. /**
  63. *
  64. *
  65. * @template T
  66. * @param {T} jsObject
  67. * @returns {Node}
  68. *
  69. * @memberOf X2JS
  70. */
  71. js2dom<T>(jsObject: T): Document;
  72. /**
  73. * Transformns an XML string into JavaScript objects.
  74. *
  75. * @template T
  76. * @param {string} xml
  77. * @returns {T}
  78. *
  79. * @memberOf X2JS
  80. */
  81. xml2js<T>(xml: string): T;
  82. /**
  83. * Transforms JavaScript objects into an XML string.
  84. *
  85. * @template T
  86. * @param {T} json
  87. * @returns {string}
  88. *
  89. * @memberOf X2JS
  90. */
  91. js2xml<T>(json: T): string;
  92. /**
  93. * Gets the current version of x2js.
  94. *
  95. * @returns {string}
  96. *
  97. * @memberOf X2JS
  98. */
  99. getVersion(): string;
  100. }
  101. declare namespace X2JS {
  102. export interface Options {
  103. /**
  104. * If set to "property" then <element>_asArray will be created to allow you to access any element as an array (even if there is only one of it).
  105. *
  106. * @type {('property' | 'none')}
  107. * @memberOf X2JS.Options
  108. */
  109. arrayAccessForm?: 'property' | 'none';
  110. /**
  111. * If "text" then <empty></empty> will be transformed to "". If "object" then <empty></empty> will be transformed to {}.
  112. *
  113. * @type {('object' | 'text')}
  114. * @memberOf X2JS.Options
  115. */
  116. emptyNodeForm?: 'object' | 'text';
  117. /**
  118. * If "object" then <empty></empty> will be transformed to {}.
  119. *
  120. * @type {'object'}
  121. * @memberOf X2JS.Options
  122. */
  123. xmldomOptions?: 'object';
  124. /**
  125. * Allows attribute values to be converted on the fly during parsing to objects.
  126. *
  127. * @type {Array<X2JS.AttributeConverter>}
  128. * @memberOf X2JS.Options
  129. */
  130. attributeConverters?: Array<AttributeConverter>;
  131. /**
  132. * Any elements that match the paths here will have their text parsed as an XML datetime value (2011-11-12T13:00:00-07:00 style). The path can be a plain string (parent.child1.child2), a regex (/.*\.child2/) or function(elementPath).
  133. *
  134. * @type {(Array<string | RegExp | ((elementPath: string) => boolean)>)}
  135. * @memberOf X2JS.Options
  136. */
  137. datetimeAccessFormPaths?: Array<string | RegExp | ((elementPath: string) => boolean)>;
  138. /**
  139. * Any elements that match the paths listed here will be stored in JavaScript objects as arrays even if there is only one of them. The path can be a plain string (parent.child1.child2), a regex (/.*\.child2/) or function(elementName, elementPath).
  140. *
  141. * @type {(Array<string | RegExp | ((elementName: string, elementPath: string) => boolean)>)}
  142. * @memberOf X2JS.Options
  143. */
  144. arrayAccessFormPaths?: Array<string | RegExp | ((elementName: string, elementPath: string) => boolean)>;
  145. /**
  146. * If true, a toString function is generated to print nodes containing text or cdata. Useful if you want to accept both plain text and CData as equivalent inputs.
  147. *
  148. * @type {boolean}
  149. * @memberOf X2JS.Options
  150. */
  151. enableToStringFunc?: boolean;
  152. /**
  153. * If true, empty text tags are ignored for elements with child nodes.
  154. *
  155. * @type {boolean}
  156. * @memberOf X2JS.Options
  157. */
  158. skipEmptyTextNodesForObj?: boolean;
  159. /**
  160. * If true, whitespace is trimmed from text nodes.
  161. *
  162. * @type {boolean}
  163. * @memberOf X2JS.Options
  164. */
  165. stripWhitespaces?: boolean;
  166. /**
  167. * If true, double quotes are used in generated XML.
  168. *
  169. * @type {boolean}
  170. * @memberOf X2JS.Options
  171. */
  172. useDoubleQuotes?: boolean;
  173. /**
  174. * If true, the root element of the XML document is ignored when converting to objects. The result will directly have the root element's children as its own properties.
  175. *
  176. * @type {boolean}
  177. * @memberOf X2JS.Options
  178. */
  179. ignoreRoot?: boolean;
  180. /**
  181. * Whether XML characters in text are escaped when reading/writing XML.
  182. *
  183. * @type {boolean}
  184. * @memberOf X2JS.Options
  185. */
  186. escapeMode?: boolean;
  187. /**
  188. * Prefix to use for properties that are created to represent XML attributes.
  189. *
  190. * @type {string}
  191. * @memberOf X2JS.Options
  192. */
  193. attributePrefix?: string;
  194. /**
  195. * If true, empty elements will created as self closing elements (<element />). If false, empty elements will be created with start and end tags (<element></element>).
  196. *
  197. * @type {boolean}
  198. * @memberOf X2JS.Options
  199. */
  200. selfClosingElements?: boolean;
  201. /**
  202. * If this property defined as false and an XML element has CData node ONLY, it will be converted to text without additional property "__cdata".
  203. *
  204. * @type {boolean}
  205. * @memberOf X2JS.Options
  206. */
  207. keepCData?: boolean;
  208. /**
  209. * If this property defined as true, use { __text: 'abc' } over 'abc'
  210. *
  211. * @type {boolean}
  212. * @memberOf X2JS.Options
  213. */
  214. keepText?: boolean;
  215. }
  216. export interface AttributeConverter {
  217. /**
  218. * Indicates whether an attribute should be converted.
  219. *
  220. * @param {string} name
  221. * @param {*} value
  222. * @returns {boolean}
  223. *
  224. * @memberOf X2JS.AttributeConverter
  225. */
  226. test(name: string, value: any): boolean;
  227. /**
  228. * Will be called for every attribute where test() returns true, replacing the original value of the attribute.
  229. *
  230. * @param {string} name
  231. * @param {*} value
  232. * @returns {string}
  233. *
  234. * @memberOf X2JS.AttributeConverter
  235. */
  236. convert(name: string, value: any): string;
  237. }
  238. }
  239. export = X2JS;