UNPKG

15.1 kBJavaScriptView Raw
1(function (global, factory) {
2 if (typeof define === "function" && define.amd) {
3 define(["exports"], factory);
4 } else if (typeof exports !== "undefined") {
5 factory(exports);
6 } else {
7 var mod = {
8 exports: {}
9 };
10 factory(mod.exports);
11 global.jstoxml = mod.exports;
12 }
13})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports) {
14 "use strict";
15
16 Object.defineProperty(_exports, "__esModule", {
17 value: true
18 });
19 _exports.default = _exports.toXML = void 0;
20
21 function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
22
23 function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
24
25 function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
26
27 function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
28
29 function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
30
31 function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
32
33 function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
34
35 function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
36
37 function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
38
39 function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
40
41 var ARRAY = 'array';
42 var BOOLEAN = 'boolean';
43 var DATE = 'date';
44 var NULL = 'null';
45 var NUMBER = 'number';
46 var OBJECT = 'object';
47 var SPECIAL_OBJECT = 'special-object';
48 var STRING = 'string';
49 var PRIVATE_VARS = ['_selfCloseTag', '_attrs'];
50 var PRIVATE_VARS_REGEXP = new RegExp(PRIVATE_VARS.join('|'), 'g');
51 /**
52 * Determines the indent string based on current tree depth.
53 */
54
55 var getIndentStr = function getIndentStr() {
56 var indent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
57 var depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
58 return indent.repeat(depth);
59 };
60 /**
61 * Sugar function supplementing JS's quirky typeof operator, plus some extra help to detect
62 * "special objects" expected by jstoxml.
63 * Example:
64 * getType(new Date());
65 * -> 'date'
66 */
67
68
69 var getType = function getType(val) {
70 return Array.isArray(val) && ARRAY || _typeof(val) === OBJECT && val !== null && val._name && SPECIAL_OBJECT || val instanceof Date && DATE || val === null && NULL || _typeof(val);
71 };
72 /**
73 * Replaces matching values in a string with a new value.
74 * Example:
75 * filterStr('foo&bar', { '&': '&amp;' });
76 * -> 'foo&amp;bar'
77 */
78
79
80 var filterStr = function filterStr() {
81 var inputStr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
82 var filter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
83 var regexp = new RegExp("(".concat(Object.keys(filter).join('|'), ")"), 'g');
84 return String(inputStr).replace(regexp, function (str, entity) {
85 return filter[entity] || '';
86 });
87 };
88 /**
89 * Maps an object or array of arribute keyval pairs to a string.
90 * Examples:
91 * { foo: 'bar', baz: 'g' } -> 'foo="bar" baz="g"'
92 * [ { ⚡: true }, { foo: 'bar' } ] -> '⚡ foo="bar"'
93 */
94
95
96 var getAttributeKeyVals = function getAttributeKeyVals() {
97 var attributes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
98 var filter = arguments.length > 1 ? arguments[1] : undefined;
99 var isArray = Array.isArray(attributes);
100 var keyVals = [];
101
102 if (isArray) {
103 // Array containing complex objects and potentially duplicate attributes.
104 keyVals = attributes.map(function (attr) {
105 var key = Object.keys(attr)[0];
106 var val = attr[key];
107 var filteredVal = filter ? filterStr(val, filter) : val;
108 var valStr = filteredVal === true ? '' : "=\"".concat(filteredVal, "\"");
109 return "".concat(key).concat(valStr);
110 });
111 } else {
112 var keys = Object.keys(attributes);
113 keyVals = keys.map(function (key) {
114 // Simple object - keyval pairs.
115 // For boolean true, simply output the key.
116 var filteredVal = filter ? filterStr(attributes[key], filter) : attributes[key];
117 var valStr = attributes[key] === true ? '' : "=\"".concat(filteredVal, "\"");
118 return "".concat(key).concat(valStr);
119 });
120 }
121
122 return keyVals;
123 };
124 /**
125 * Converts an attributes object/array to a string of keyval pairs.
126 * Example:
127 * formatAttributes({ a: 1, b: 2 })
128 * -> 'a="1" b="2"'
129 */
130
131
132 var formatAttributes = function formatAttributes() {
133 var attributes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
134 var filter = arguments.length > 1 ? arguments[1] : undefined;
135 var keyVals = getAttributeKeyVals(attributes, filter);
136 if (keyVals.length === 0) return '';
137 var keysValsJoined = keyVals.join(' ');
138 return " ".concat(keysValsJoined);
139 };
140 /**
141 * Converts an object to a jstoxml array.
142 * Example:
143 * objToArray({ foo: 'bar', baz: 2 });
144 * ->
145 * [
146 * {
147 * _name: 'foo',
148 * _content: 'bar'
149 * },
150 * {
151 * _name: 'baz',
152 * _content: 2
153 * }
154 * ]
155 */
156
157
158 var objToArray = function objToArray() {
159 var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
160 return Object.keys(obj).map(function (key) {
161 return {
162 _name: key,
163 _content: obj[key]
164 };
165 });
166 };
167 /**
168 * Determines if a value is a primitive JavaScript value (not including Symbol).
169 * Example:
170 * isPrimitive(4);
171 * -> true
172 */
173
174
175 var PRIMITIVE_TYPES = [STRING, NUMBER, BOOLEAN];
176
177 var isPrimitive = function isPrimitive(val) {
178 return PRIMITIVE_TYPES.includes(getType(val));
179 };
180 /**
181 * Determines if a value is a simple primitive type that can fit onto one line. Needed for
182 * determining any needed indenting and line breaks.
183 * Example:
184 * isSimpleType(new Date());
185 * -> true
186 */
187
188
189 var SIMPLE_TYPES = [].concat(PRIMITIVE_TYPES, [DATE, SPECIAL_OBJECT]);
190
191 var isSimpleType = function isSimpleType(val) {
192 return SIMPLE_TYPES.includes(getType(val));
193 };
194 /**
195 * Determines if an XML string is a simple primitive, or contains nested data.
196 * Example:
197 * isSimpleXML('<foo />');
198 * -> false
199 */
200
201
202 var isSimpleXML = function isSimpleXML(xmlStr) {
203 return !xmlStr.match('<');
204 };
205 /**
206 * Assembles an XML header as defined by the config.
207 */
208
209
210 var DEFAULT_XML_HEADER = '<?xml version="1.0" encoding="UTF-8"?>';
211
212 var getHeaderString = function getHeaderString(_ref) {
213 var header = _ref.header,
214 indent = _ref.indent,
215 depth = _ref.depth,
216 isOutputStart = _ref.isOutputStart;
217 var shouldOutputHeader = header && isOutputStart;
218 if (!shouldOutputHeader) return '';
219 var shouldUseDefaultHeader = _typeof(header) === BOOLEAN;
220 return "".concat(shouldUseDefaultHeader ? DEFAULT_XML_HEADER : header).concat(indent ? '\n' : '');
221 };
222 /**
223 * Recursively traverses an object tree and converts the output to an XML string.
224 * Example:
225 * toXML({ foo: 'bar' });
226 * -> <foo>bar</foo>
227 */
228
229
230 var toXML = function toXML() {
231 var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
232 var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
233 var _config$depth = config.depth,
234 depth = _config$depth === void 0 ? 0 : _config$depth,
235 indent = config.indent,
236 _isFirstItem = config._isFirstItem,
237 _isLastItem = config._isLastItem,
238 attributesFilter = config.attributesFilter,
239 header = config.header,
240 filter = config.filter; // Determine indent string based on depth.
241
242 var indentStr = getIndentStr(indent, depth); // For branching based on value type.
243
244 var valType = getType(obj);
245 var isSimple = isSimpleType(obj); // Determine if this is the start of the output. Needed for header and indenting.
246
247 var isOutputStart = depth === 0 && (isSimple || !isSimple && _isFirstItem);
248 var outputStr = '';
249
250 switch (valType) {
251 case 'special-object':
252 {
253 // Processes a specially-formatted object used by jstoxml.
254 var _name = obj._name,
255 _content = obj._content; // Output text content without a tag wrapper.
256
257 if (_content === null) {
258 outputStr = _name;
259 break;
260 } // Handles arrays of primitive values. (#33)
261
262
263 if (Array.isArray(_content) && _content.every(isPrimitive)) {
264 return _content.map(function (a) {
265 return toXML({
266 _name: _name,
267 _content: a
268 }, _objectSpread({}, config, {
269 depth: depth
270 }));
271 }).join('');
272 } // Don't output private vars (such as _attrs).
273
274
275 if (_name.match(PRIVATE_VARS_REGEXP)) break; // Process the nested new value and create new config.
276
277 var newVal = toXML(_content, _objectSpread({}, config, {
278 depth: depth + 1
279 }));
280 var newValType = getType(newVal);
281 var isNewValSimple = isSimpleXML(newVal); // Pre-tag output (indent and line breaks).
282
283 var preIndentStr = indent && !isOutputStart ? '\n' : '';
284 var preTag = "".concat(preIndentStr).concat(indentStr); // Tag output.
285
286 var valIsEmpty = newValType === 'undefined' || newVal === '';
287 var shouldSelfClose = _typeof(obj._selfCloseTag) === BOOLEAN ? valIsEmpty && obj._selfCloseTag : valIsEmpty;
288 var selfCloseStr = shouldSelfClose ? '/' : '';
289 var attributesString = formatAttributes(obj._attrs, attributesFilter);
290 var tag = "<".concat(_name).concat(attributesString).concat(selfCloseStr, ">"); // Post-tag output (closing tag, indent, line breaks).
291
292 var preTagCloseStr = indent && !isNewValSimple ? "\n".concat(indentStr) : '';
293 var postTag = !shouldSelfClose ? "".concat(newVal).concat(preTagCloseStr, "</").concat(_name, ">") : '';
294 outputStr = "".concat(preTag).concat(tag).concat(postTag);
295 break;
296 }
297
298 case 'object':
299 {
300 // Iterates over keyval pairs in an object, converting each item to a special-object.
301 var keys = Object.keys(obj);
302 var outputArr = keys.map(function (key, index) {
303 var newConfig = _objectSpread({}, config, {
304 _isFirstItem: index === 0,
305 _isLastItem: index + 1 === keys.length
306 });
307
308 var outputObj = {
309 _name: key
310 };
311
312 if (getType(obj[key]) === 'object') {
313 // Sub-object contains an object.
314 // Move private vars up as needed. Needed to support certain types of objects
315 // E.g. { foo: { _attrs: { a: 1 } } } -> <foo a="1"/>
316 PRIVATE_VARS.forEach(function (privateVar) {
317 var val = obj[key][privateVar];
318
319 if (typeof val !== 'undefined') {
320 outputObj[privateVar] = val;
321 delete obj[key][privateVar];
322 }
323 });
324 var hasContent = typeof obj[key]._content !== 'undefined';
325
326 if (hasContent) {
327 // _content has sibling keys, so pass as an array (edge case).
328 // E.g. { foo: 'bar', _content: { baz: 2 } } -> <foo>bar</foo><baz>2</baz>
329 if (Object.keys(obj[key]).length > 1) {
330 var newContentObj = Object.assign({}, obj[key]);
331 delete newContentObj._content;
332 outputObj._content = [].concat(_toConsumableArray(objToArray(newContentObj)), [obj[key]._content]);
333 }
334 }
335 } // Fallthrough: just pass the key as the content for the new special-object.
336
337
338 if (typeof outputObj._content === 'undefined') outputObj._content = obj[key];
339 var xml = toXML(outputObj, newConfig, key);
340 return xml;
341 }, config);
342 outputStr = outputArr.join('');
343 break;
344 }
345
346 case 'function':
347 {
348 // Executes a user-defined function and return output.
349 var fnResult = obj(config);
350 outputStr = toXML(fnResult, config);
351 break;
352 }
353
354 case 'array':
355 {
356 // Iterates and converts each value in an array.
357 var _outputArr = obj.map(function (singleVal, index) {
358 var newConfig = _objectSpread({}, config, {
359 _isFirstItem: index === 0,
360 _isLastItem: index + 1 === obj.length
361 });
362
363 return toXML(singleVal, newConfig);
364 });
365
366 outputStr = _outputArr.join('');
367 break;
368 }
369 // number, string, boolean, date, null, etc
370
371 default:
372 {
373 outputStr = filterStr(obj, filter);
374 break;
375 }
376 }
377
378 var headerStr = getHeaderString({
379 header: header,
380 indent: indent,
381 depth: depth,
382 isOutputStart: isOutputStart
383 });
384 return "".concat(headerStr).concat(outputStr);
385 };
386
387 _exports.toXML = toXML;
388 var _default = {
389 toXML: toXML
390 };
391 _exports.default = _default;
392});