forked from bitmage/node-easyxml
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
256 lines (226 loc) · 7.06 KB
/
index.js
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
254
255
256
'use strict';
var et = require('elementtree');
var inflect = require('inflect');
var ElementTree = et.ElementTree;
var element = et.Element;
var subElement = et.SubElement;
/**
* Instantiate a new EasyXml instance
*
* @param {Object} config
* @constructor
*/
var EasyXml = function(config) {
this.config = EasyXml.merge({
attributePrefix: '_',
dateFormat: 'ISO', // ISO = ISO8601, SQL = MySQL Timestamp, JS = (new Date).toString()
filterNulls: false,
indent: 2,
manifest: false,
rootArray: 'items',
rootElement: 'response',
singularize: true,
unwrapArrays: false
}, config);
};
EasyXml.ISO = 'ISO';
EasyXml.JS = 'JS';
EasyXml.SQL = 'SQL';
/**
* Merges two objects and returns the result
*
* @param {Object} obj1
* @param {Object} obj2
* @returns {Object} Properties from obj1 and obj2
* @static
*/
EasyXml.merge = function(obj1, obj2) {
var obj3 = {};
for (var attr1 in obj1) {
if (Object.prototype.hasOwnProperty.call(obj1, attr1)) {
obj3[attr1] = obj1[attr1];
}
}
for (var attr2 in obj2) {
if (Object.prototype.hasOwnProperty.call(obj2, attr2)) {
obj3[attr2] = obj2[attr2];
}
}
return obj3;
};
/**
* Pads a number so that it is two digits
*
* @param {Number} val
* @returns {String}
* @static
*/
EasyXml.zeroPadTen = function(val) {
if (val < 10) {
return "0" + val;
}
return val.toString();
};
/**
* Should we bother parsing this child attribute
* @param child
* @returns {Boolean}
*/
EasyXml.isChildKeyParsed = function(child) {
switch(typeof child) {
case 'number':
case 'string':
case 'boolean':
return false;
default:
// null, undefined, objects, functions
return true;
}
};
/**
* Takes an object and returns an XML string
*
* @param {Object|Array} object
* @param {String} [rootElementOverride]
* @returns {String} XML Document
*/
EasyXml.prototype.render = function(object, rootElementOverride) {
var root;
if (rootElementOverride) {
root = rootElementOverride;
} else if (object instanceof Array) {
root = this.config.rootArray;
} else {
root = this.config.rootElement;
}
var xml = element(root);
this.parseChildElement(xml, object);
return new ElementTree(xml).write({
xml_declaration: this.config.manifest,
indent: this.config.indent
});
};
/**
* Check if this item doesn't exist and if we should not render it
*
* @param child Attribute being checked
* @returns {Boolean}
*/
EasyXml.prototype.filterNull = function(child) {
return (child === null || child === undefined) && this.config.filterNulls === true;
};
/**
* Checks to see if the given key should be rendered as an attribute
*
* @param {String} key
* @returns {Boolean}
*/
EasyXml.prototype.isAttribute = function(key) {
return this.config.attributePrefix && key[0] === this.config.attributePrefix;
};
/**
* Takes an object and attaches it to the XML doc
*
* @param {Element} parentXmlNode
* @param {Element} parentObjectNode
* @retursive
*/
EasyXml.prototype.parseChildElement = function(parentXmlNode, parentObjectNode) {
for (var key in parentObjectNode) {
if (Object.prototype.hasOwnProperty.call(parentObjectNode, key)) {
var child = parentObjectNode[key];
var el = null;
if (this.filterNull(child)) {
// no element if we are skipping nulls and undefined
continue;
}
if (!isNaN(key)) {
key = inflect.singularize(this.config.rootArray);
}
if (!this.isAttribute(key)) {
el = subElement(parentXmlNode, key);
}
if (child === null || child === undefined) {
// allow for both null child and undefined child
el.text = "";
} else if (!this.config.singularize && typeof parentXmlNode === 'object' && typeof child === 'object') {
for (var subkey in child) {
if (Object.prototype.hasOwnProperty.call(child, subkey)) {
if (EasyXml.isChildKeyParsed(child[subkey])) {
this.parseChildElement(el, child[subkey]);
} else {
subElement(el, subkey).text = child[subkey].toString();
}
}
}
} else if (this.isAttribute(key)) {
if (typeof child === 'string' || typeof child === 'number') {
if (key === this.config.attributePrefix) {
parentXmlNode.text = child;
} else {
parentXmlNode.set(key.substring(1), child);
}
} else {
throw new Error(key + "contained non_string_attribute");
}
} else if (child instanceof Date) {
// Date
if (this.config.dateFormat === EasyXml.ISO) {
// ISO: YYYY-MM-DDTHH:MM:SS.mmmZ
el.text = child.toISOString();
} else if (this.config.dateFormat === EasyXml.SQL) {
// SQL: YYYY-MM-DD HH:MM:SS
var yyyy = child.getFullYear();
var mm = EasyXml.zeroPadTen(child.getMonth() + 1);
var dd = EasyXml.zeroPadTen(child.getDate());
var hh = EasyXml.zeroPadTen(child.getHours());
var min = EasyXml.zeroPadTen(child.getMinutes());
var ss = EasyXml.zeroPadTen(child.getSeconds());
el.text = [yyyy, '-', mm, '-', dd, ' ', hh, ':', min, ':', ss].join("");
} else if (this.config.dateFormat === EasyXml.JS) {
// JavaScript date format
el.text = child.toString();
} else {
throw new Error(key + "contained unknown_date_format");
}
} else if (child instanceof Array) {
// Array
var subElementName = inflect.singularize(key);
for (var key2 in child) {
if (Object.prototype.hasOwnProperty.call(child, key2)) {
if (this.filterNull(child[key2])) {
continue;
}
// If unwrap arrays, make new subelements on the parent
var el2 = this.config.unwrapArrays ? (el || subElement(parentXmlNode, key)) : (subElement(el, subElementName));
// Check type of child element
if (Object.prototype.hasOwnProperty.call(child, key2) && EasyXml.isChildKeyParsed(child[key2])) {
this.parseChildElement(el2, child[key2]);
} else {
// Just add element directly without parsing
el2.text = child[key2].toString();
}
// If unwrap arrays, the initial child element has been consumed
if (this.config.unwrapArrays) el = undefined;
}
}
} else if (typeof child === 'object') {
if (typeof child.toJSON === 'function') {
// .toJSON() is a common JS convention for serializing an object
el.text = child.toJSON();
} else {
// Object, go deeper
this.parseChildElement(el, child);
}
} else if (typeof child === 'number' || typeof child === 'boolean') {
el.text = child.toString();
/* istanbul ignore else */
} else if (typeof child === 'string') {
el.text = child;
} else {
throw new Error(key + " contained unknown_data_type: " + typeof child);
}
}
}
};
module.exports = EasyXml;