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