UNPKG

4.93 kBJavaScriptView Raw
1!function(t,e){if("function"==typeof define&&define.amd)define(["exports"],e);else if("undefined"!=typeof exports)e(exports);else{var n={exports:{}};e(n.exports),t.jstoxml=n.exports}}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,function(t){"use strict";function e(t){return function(t){if(Array.isArray(t)){for(var e=0,n=new Array(t.length);e<t.length;e++)n[e]=t[e];return n}}(t)||function(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}function n(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),n.push.apply(n,r)}return n}function r(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?n(Object(r),!0).forEach(function(e){o(t,e,r[e])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):n(Object(r)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))})}return t}function o(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function c(t){"@babel/helpers - typeof";return(c="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}Object.defineProperty(t,"__esModule",{value:!0}),t.default=t.toXML=void 0;var a=["_selfCloseTag","_attrs"],i=new RegExp(a.join("|"),"g"),u=function(t){return(Array.isArray(t)?"array":"object"===c(t)&&null!==t&&t._name&&"special-object")||t instanceof Date&&"date"||null===t&&"null"||c(t)},f=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=new RegExp("(".concat(Object.keys(e).join("|"),")"),"g");return String(t).replace(n,function(t,n){return e[n]||""})},l=function(){var t=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=arguments.length>1?arguments[1]:void 0,n=[];n=Array.isArray(t)?t.map(function(t){var n=Object.keys(t)[0],r=t[n],o=e?f(r,e):r,c=!0===o?"":'="'.concat(o,'"');return"".concat(n).concat(c)}):Object.keys(t).map(function(n){var r=e?f(t[n],e):t[n],o=!0===t[n]?"":'="'.concat(r,'"');return"".concat(n).concat(o)});return n}(arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},arguments.length>1?arguments[1]:void 0);if(0===t.length)return"";var e=t.join(" ");return" ".concat(e)},s=["string","number","boolean"],p=function(t){return s.includes(u(t))},b=[].concat(s,["date","special-object"]),d=function t(){var n,o=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},s=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},d=s.depth,y=void 0===d?0:d,v=s.indent,g=s._isFirstItem,h=(s._isLastItem,s.attributesFilter),m=s.header,j=s.filter,O=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return t.repeat(e)}(v,y),_=u(o),k=(n=o,b.includes(u(n))),w=0===y&&(k||!k&&g),S="";switch(_){case"special-object":var A=o._name,x=o._content;if(null===x){S=A;break}if(Array.isArray(x)&&x.every(p))return x.map(function(e){return t({_name:A,_content:e},r({},s,{depth:y}))}).join("");if(A.match(i))break;var P=t(x,r({},s,{depth:y+1})),I=u(P),T=!P.match("<"),E="".concat(v&&!w?"\n":"").concat(O),L="undefined"===I||""===P,D="boolean"===c(o._selfCloseTag)?L&&o._selfCloseTag:L,F=D?"/":"",M=l(o._attrs,h),C="<".concat(A).concat(M).concat(F,">"),X=v&&!T?"\n".concat(O):"",R=D?"":"".concat(P).concat(X,"</").concat(A,">");S="".concat(E).concat(C).concat(R);break;case"object":var U=Object.keys(o);S=U.map(function(n,c){var i=r({},s,{_isFirstItem:0===c,_isLastItem:c+1===U.length}),f={_name:n};if("object"===u(o[n])&&(a.forEach(function(t){var e=o[n][t];void 0!==e&&(f[t]=e,delete o[n][t])}),void 0!==o[n]._content&&Object.keys(o[n]).length>1)){var l=Object.assign({},o[n]);delete l._content,f._content=[].concat(e(function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return Object.keys(t).map(function(e){return{_name:e,_content:t[e]}})}(l)),[o[n]._content])}return void 0===f._content&&(f._content=o[n]),t(f,i,n)},s).join("");break;case"function":var q=o(s);S=t(q,s);break;case"array":S=o.map(function(e,n){var c=r({},s,{_isFirstItem:0===n,_isLastItem:n+1===o.length});return t(e,c)}).join("");break;default:S=f(o,j)}var z=function(t){var e=t.header,n=t.indent,r=(t.depth,t.isOutputStart);if(!e||!r)return"";var o="boolean"===c(e);return"".concat(o?'<?xml version="1.0" encoding="UTF-8"?>':e).concat(n?"\n":"")}({header:m,indent:v,depth:y,isOutputStart:w});return"".concat(z).concat(S)};t.toXML=d;var y={toXML:d};t.default=y});
\No newline at end of file