Comparing version 2.1.1 to 2.2.0
@@ -1,1 +0,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))return n(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,e){if(!t)return;if("string"==typeof t)return n(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);"Object"===r&&t.constructor&&(r=t.constructor.name);if("Map"===r||"Set"===r)return Array.from(t);if("Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))return n(t,e)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function n(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function r(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 o(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{};e%2?r(Object(n),!0).forEach(function(e){a(t,e,n[e])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):r(Object(n)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e))})}return t}function a(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 i=["_selfCloseTag","_attrs"],u=new RegExp(i.join("|"),"g"),f=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)},l=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if("string"!=typeof t)return t;var n=new RegExp("(".concat(Object.keys(e).join("|"),")(?!(\\w|#)*;)"),"g");return String(t).replace(n,function(t,n){return e[n]||""})},s=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?l(r,e):r,a=!0===o?"":'="'.concat(o,'"');return"".concat(n).concat(a)}):Object.keys(t).map(function(n){var r=e?l(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)},p=["string","number","boolean"],b=function(t){return p.includes(f(t))},y=([].concat(p,["date","special-object"]),{"<":"<",">":">","&":"&"}),d=function t(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},a=r.depth,p=void 0===a?0:a,d=r.indent,v=r._isFirstItem,m=r._isOutputStart,g=void 0===m||m,h=r.header,j=r.attributesFilter,O=void 0===j?{}:j,_=r.filter,S=void 0===_?{}:_,w="boolean"==typeof O&&!O?{}:o(o(o({},y),{'"':"""}),O),k="boolean"==typeof S&&!S?{}:o(o({},y),S),A=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)}(d,p),x=f(n),P=function(t){var e=t.header,n=(t.indent,t.isOutputStart);return e&&n?"boolean"===c(e)?'<?xml version="1.0" encoding="UTF-8"?>':e:""}({header:h,indent:d,depth:p,isOutputStart:g}),I=g&&!P&&v&&0===p,T="";switch(x){case"special-object":var E=n._name,D=n._content;if(null===D){T=E;break}if(Array.isArray(D)&&D.every(b))return D.map(function(e){return t({_name:E,_content:e},o(o({},r),{},{depth:p,_isOutputStart:!1}))}).join("");if(E.match(u))break;var F=t(D,o(o({},r),{},{depth:p+1,_isOutputStart:I})),L=f(F),M=!F.match("<"),C="".concat(d&&!I?"\n":"").concat(A),X="undefined"===L||""===F,R="boolean"===c(n._selfCloseTag)?X&&n._selfCloseTag:X,U=R?"/":"",q=s(n._attrs,w),$="<".concat(E).concat(q).concat(U,">"),z=d&&!M?"\n".concat(A):"",B=R?"":"".concat(F).concat(z,"</").concat(E,">");T="".concat(C).concat($).concat(B);break;case"object":var G=Object.keys(n);T=G.map(function(a,c){var u=o(o({},r),{},{_isFirstItem:0===c,_isLastItem:c+1===G.length,_isOutputStart:I}),l={_name:a};if("object"===f(n[a])&&(i.forEach(function(t){var e=n[a][t];void 0!==e&&(l[t]=e,delete n[a][t])}),void 0!==n[a]._content&&Object.keys(n[a]).length>1)){var s=Object.assign({},n[a]);delete s._content,l._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]}})}(s)),[n[a]._content])}return void 0===l._content&&(l._content=n[a]),t(l,u,a)},r).join("");break;case"function":var H=n(r);T=t(H,r);break;case"array":T=n.map(function(e,a){var c=o(o({},r),{},{_isFirstItem:0===a,_isLastItem:a+1===n.length,_isOutputStart:I});return t(e,c)}).join("");break;default:T=l(n,k)}return"".concat(P).concat(T)};t.toXML=d;var v={toXML:d};t.default=v}); | ||
!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))return n(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,e){if(!t)return;if("string"==typeof t)return n(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);"Object"===r&&t.constructor&&(r=t.constructor.name);if("Map"===r||"Set"===r)return Array.from(t);if("Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))return n(t,e)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function n(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function r(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 o(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{};e%2?r(Object(n),!0).forEach(function(e){a(t,e,n[e])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):r(Object(n)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e))})}return t}function a(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 i=["_selfCloseTag","_attrs"],u=new RegExp(i.join("|"),"g"),f=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)},l=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if("string"!=typeof t)return t;var n=new RegExp("(".concat(Object.keys(e).join("|"),")(?!(\\w|#)*;)"),"g");return String(t).replace(n,function(t,n){return e[n]||""})},s=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?l(r,e):r,a=!0===o?"":'="'.concat(o,'"');return"".concat(n).concat(a)}):Object.keys(t).map(function(n){var r=e?l(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)},p=["string","number","boolean"],b=function(t){return p.includes(f(t))},y=([].concat(p,["date","special-object"]),{"<":"<",">":">","&":"&"}),d=function t(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},a=r.depth,p=void 0===a?0:a,d=r.indent,v=r._isFirstItem,m=r._isOutputStart,g=void 0===m||m,h=r.header,j=r.attributesFilter,O=void 0===j?{}:j,_=r.filter,S=void 0===_?{}:_,k="boolean"==typeof O&&!O?{}:o(o(o({},y),{'"':"""}),O),w="boolean"==typeof S&&!S?{}:o(o({},y),S),A=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)}(d,p),x=f(n),P=function(t){var e=t.header,n=(t.indent,t.isOutputStart);return e&&n?"boolean"===c(e)?'<?xml version="1.0" encoding="UTF-8"?>':e:""}({header:h,indent:d,depth:p,isOutputStart:g}),I=g&&!P&&v&&0===p,T="";switch(x){case"special-object":var E=n._name,D=n._content;if(null===D){T=E;break}if(Array.isArray(D)&&D.every(b))return D.map(function(e){return t({_name:E,_content:e},o(o({},r),{},{depth:p,_isOutputStart:!1}))}).join("");if(E.match(u))break;var F=t(D,o(o({},r),{},{depth:p+1,_isOutputStart:I})),L=f(F),M=!F.match("<"),C="".concat(d&&!I?"\n":"").concat(A);if("_comment"===E){T+="".concat(C,"\x3c!-- ").concat(D," --\x3e");break}var X="undefined"===L||""===F,R="boolean"===c(n._selfCloseTag)?X&&n._selfCloseTag:X,U=R?"/":"",q=s(n._attrs,k),$="<".concat(E).concat(q).concat(U,">"),z=d&&!M?"\n".concat(A):"",B=R?"":"".concat(F).concat(z,"</").concat(E,">");T+="".concat(C).concat($).concat(B);break;case"object":var G=Object.keys(n);T=G.map(function(a,c){var u=o(o({},r),{},{_isFirstItem:0===c,_isLastItem:c+1===G.length,_isOutputStart:I}),l={_name:a};if("object"===f(n[a])&&(i.forEach(function(t){var e=n[a][t];void 0!==e&&(l[t]=e,delete n[a][t])}),void 0!==n[a]._content&&Object.keys(n[a]).length>1)){var s=Object.assign({},n[a]);delete s._content,l._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]}})}(s)),[n[a]._content])}return void 0===l._content&&(l._content=n[a]),t(l,u,a)},r).join("");break;case"function":var H=n(r);T=t(H,r);break;case"array":T=n.map(function(e,a){var c=o(o({},r),{},{_isFirstItem:0===a,_isLastItem:a+1===n.length,_isOutputStart:I});return t(e,c)}).join("");break;default:T=l(n,w)}return"".concat(P).concat(T)};t.toXML=d;var v={toXML:d};t.default=v}); |
@@ -317,4 +317,10 @@ (function (global, factory) { | ||
var preIndentStr = indent && !isOutputStart ? "\n" : ""; | ||
var preTag = "".concat(preIndentStr).concat(indentStr); // Tag output. | ||
var preTag = "".concat(preIndentStr).concat(indentStr); // Special handling for comments, preserving preceding line breaks/indents. | ||
if (_name === '_comment') { | ||
outputStr += "".concat(preTag, "<!-- ").concat(_content, " -->"); | ||
break; | ||
} // Tag output. | ||
var valIsEmpty = newValType === "undefined" || newVal === ""; | ||
@@ -328,3 +334,3 @@ var shouldSelfClose = _typeof(obj._selfCloseTag) === BOOLEAN ? valIsEmpty && obj._selfCloseTag : valIsEmpty; | ||
var postTag = !shouldSelfClose ? "".concat(newVal).concat(preTagCloseStr, "</").concat(_name, ">") : ""; | ||
outputStr = "".concat(preTag).concat(tag).concat(postTag); | ||
outputStr += "".concat(preTag).concat(tag).concat(postTag); | ||
break; | ||
@@ -331,0 +337,0 @@ } |
@@ -259,2 +259,8 @@ const ARRAY = "array"; | ||
// Special handling for comments, preserving preceding line breaks/indents. | ||
if (_name === '_comment') { | ||
outputStr += `${preTag}<!-- ${_content} -->`; | ||
break; | ||
} | ||
// Tag output. | ||
@@ -275,3 +281,3 @@ const valIsEmpty = newValType === "undefined" || newVal === ""; | ||
: ""; | ||
outputStr = `${preTag}${tag}${postTag}`; | ||
outputStr += `${preTag}${tag}${postTag}`; | ||
break; | ||
@@ -278,0 +284,0 @@ } |
{ | ||
"name": "jstoxml", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "Converts JavaScript/JSON to XML (for RSS, Podcasts, AMP, etc.)", | ||
@@ -5,0 +5,0 @@ "homepage": "http://github.com/davidcalhoun/jstoxml", |
@@ -17,2 +17,8 @@ # jstoxml | ||
#### Version 2.2.0 | ||
* Initial support for XML comments ([#47](https://github.com/davidcalhoun/jstoxml/issues/47)) | ||
#### Version 2.1.1 | ||
* Fix for [#48](https://github.com/davidcalhoun/jstoxml/issues/48) (various 0-depth issues, bad "is output start" logic) | ||
#### Version 2.0.0 (breaking) | ||
@@ -25,31 +31,5 @@ | ||
#### Version 1.6.9 | ||
#### Past changes | ||
- See CHANGELOG.md for a full history of changes. | ||
- fix for [#40](/../../issues/40). Previously top-level objects and arrays were concatenated without proper line breaks. | ||
#### Version 1.4.2 | ||
- support for handling arrays of primitives, instead of simply concatenating [#33](/../../issues/33) | ||
#### Version 1.3.0 | ||
- restored `default` module export [#31](/../../issues/31) | ||
#### Version 1.2.0 | ||
- refactoring and cleanup | ||
#### Version 1.1.0 | ||
- Added support for attribute filtering (see Example 11b below). | ||
#### Version 1.0.0 | ||
- Complete rewrite! The code should now be easier to understand and maintain. | ||
- now supports emoji/UTF8 tag attributes (needed for AMP pages - e.g. `<html ⚡ lang="en">`) (see example 14) | ||
- now supports duplicate attribute key names (see example 15) | ||
- Fixed: functions returning objects now have now that output passed through toXML for XML conversion | ||
- Fixed: empty text strings now properly output self-closing tags | ||
- Migrated tests to mocha | ||
### Examples | ||
@@ -708,4 +688,50 @@ | ||
#### Example 16: XML comments | ||
```javascript | ||
toXML({ | ||
_comment: 'Some important comment', | ||
a: { | ||
b: [1, 2, 3] | ||
} | ||
}, { indent: ' ' }); | ||
``` | ||
Output: | ||
``` | ||
<!-- Some important comment --> | ||
<a> | ||
<b>1</b> | ||
<b>2</b> | ||
<b>3</b> | ||
</a> | ||
``` | ||
#### Example 17: Multiple XML comments | ||
```javascript | ||
toXML([ | ||
{ _comment: 'Some important comment' }, | ||
{ _comment: 'This is a very long comment!' }, | ||
{ _comment: 'More important exposition!' }, | ||
{ a: { b: [1, 2, 3] } } | ||
], { indent: ' ' }); | ||
``` | ||
Output: | ||
``` | ||
<!-- Some important comment --> | ||
<!-- This is a very long comment! --> | ||
<!-- More important exposition! --> | ||
<a> | ||
<b>1</b> | ||
<b>2</b> | ||
<b>3</b> | ||
</a> | ||
``` | ||
### License | ||
MIT |
93
test.js
@@ -1232,3 +1232,96 @@ const { toXML } = require("./dist/jstoxml"); | ||
}); | ||
it("comments 1", () => { | ||
const val = { | ||
_comment: "test comment", | ||
a: "foo" | ||
}; | ||
const result = toXML(val); | ||
const expectedResult = `<!-- test comment --><a>foo</a>`; | ||
assert.equal(result, expectedResult); | ||
}); | ||
it("comments 2", () => { | ||
const val = { | ||
_comment: "test comment", | ||
a: "foo" | ||
}; | ||
const result = toXML(val, { indent: ' ' }); | ||
const expectedResult = `<!-- test comment --> | ||
<a>foo</a>`; | ||
assert.equal(result, expectedResult); | ||
}); | ||
it("comments 3", () => { | ||
const val = { | ||
_comment: "comment 1", | ||
b: { | ||
_comment: "comment 2", | ||
a: "foo" | ||
} | ||
}; | ||
const result = toXML(val, { indent: ' ' }); | ||
const expectedResult = `<!-- comment 1 --> | ||
<b> | ||
<!-- comment 2 --> | ||
<a>foo</a> | ||
</b>`; | ||
assert.equal(result, expectedResult); | ||
}); | ||
it("comments 4", () => { | ||
const val = { | ||
_comment: "comment 1", | ||
b: [ | ||
{ _comment: "comment 2" }, | ||
{ _comment: "comment 3" }, | ||
{ a: "foo" } | ||
] | ||
}; | ||
const result = toXML(val, { indent: ' ' }); | ||
const expectedResult = `<!-- comment 1 --> | ||
<b> | ||
<!-- comment 2 --> | ||
<!-- comment 3 --> | ||
<a>foo</a> | ||
</b>`; | ||
assert.equal(result, expectedResult); | ||
}); | ||
}); | ||
it("comments 5", () => { | ||
const val = { | ||
_comment: 'Some important comment', | ||
a: { | ||
b: [1, 2, 3] | ||
} | ||
}; | ||
const result = toXML(val, { indent: ' ' }); | ||
const expectedResult = `<!-- Some important comment --> | ||
<a> | ||
<b>1</b> | ||
<b>2</b> | ||
<b>3</b> | ||
</a>`; | ||
assert.equal(result, expectedResult); | ||
}); | ||
it("comments 6", () => { | ||
const val = [ | ||
{ _comment: 'Some important comment' }, | ||
{ _comment: 'This is a very long comment!' }, | ||
{ _comment: 'More important exposition!' }, | ||
{ a: { b: [1, 2, 3] } } | ||
]; | ||
const result = toXML(val, { indent: ' ' }); | ||
const expectedResult = `<!-- Some important comment --> | ||
<!-- This is a very long comment! --> | ||
<!-- More important exposition! --> | ||
<a> | ||
<b>1</b> | ||
<b>2</b> | ||
<b>3</b> | ||
</a>`; | ||
assert.equal(result, expectedResult); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
90000
13
1922
735