Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

simple-xml-to-json

Package Overview
Dependencies
Maintainers
0
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-xml-to-json - npm Package Compare versions

Comparing version 1.2.2 to 1.2.3

106

lib/simpleXmlToJson.js

@@ -15,3 +15,3 @@ 'use strict';

switch (node.type) {
case 'ELEMENT': {
case "ELEMENT": {
let element = {};

@@ -30,3 +30,3 @@ const attribs = buildAttributes(node.value.attributes);

}
case 'ATTRIBUTE': {
case "ATTRIBUTE": {
const attribNameAndValue = node.value;

@@ -36,3 +36,3 @@ json[attribNameAndValue.name] = attribNameAndValue.value;

}
case 'CONTENT': {
case "CONTENT": {
return {content: node.value}

@@ -57,3 +57,3 @@ }

const isContentChildren = children => children && Array.isArray(children) && children.length === 1 && children[0].type === 'CONTENT';
const isContentChildren = children => children && Array.isArray(children) && children.length === 1 && children[0].type === "CONTENT";

@@ -72,14 +72,2 @@ const buildAttributes = arrayNodes => {

const TOKEN_TYPE$2 = {
OPEN_BRACKET: 'OPEN_BRACKET',
ELEMENT_TYPE: 'ELEMENT_TYPE',
CLOSE_ELEMENT: 'CLOSE_ELEMENT',
ATTRIB_NAME: 'ATTRIB_NAME',
ATTRIB_VALUE: 'ATTRIB_VALUE',
ASSIGN: 'ASSIGN',
CLOSE_BRACKET: 'CLOSE_BRACKET',
CONTENT: 'CONTENT',
EOF: 'EOF'
};
const Token$2 = (type, value) => ({

@@ -91,9 +79,9 @@ type,

var model = {
Token: Token$2,
TOKEN_TYPE: TOKEN_TYPE$2
Token: Token$2
};
const { Token: Token$1, TOKEN_TYPE: TOKEN_TYPE$1 } = model;
const EOF_TOKEN = Token$1('EOF');
const { Token: Token$1 } = model;
const EOF_TOKEN = Token$1("EOF");
const isCharBlank = (char) =>

@@ -118,3 +106,3 @@ char === ' ' || char === '\n' || char === '\r' || char === '\t';

const replaceQuotes = (str) => str.replace(/'/g, '"');
const replaceQuotes = (str) => str.replace(/'/g, "'");

@@ -196,5 +184,5 @@ const getInitialPosForLexer = (xmlAsString) => {

const isElementBegin = () =>
currentToken && currentToken.type === TOKEN_TYPE$1.OPEN_BRACKET;
currentToken && currentToken.type === "OPEN_BRACKET";
const isAssignToAttribute = () =>
currentToken && currentToken.type === TOKEN_TYPE$1.ASSIGN;
currentToken && currentToken.type === "ASSIGN";

@@ -211,3 +199,3 @@ const next = () => {

const buffer = readAlphaNumericCharsOrBrackets(false);
currentToken = Token$1(TOKEN_TYPE$1.ELEMENT_TYPE, buffer);
currentToken = Token$1("ELEMENT_TYPE", buffer);
scopingElement.push(buffer);

@@ -217,7 +205,8 @@ } else if (isAssignToAttribute()) {

skipQuotes();
const openingQuote = xmlAsString[pos - 1];
let start = pos;
while (hasNext() && !isQuote(peek())) pos++;
while (hasNext() && peek() !== openingQuote) pos++;
const buffer = replaceQuotes(xmlAsString.substring(start, pos));
pos++;
currentToken = Token$1(TOKEN_TYPE$1.ATTRIB_VALUE, buffer);
currentToken = Token$1("ATTRIB_VALUE", buffer);
} else {

@@ -228,6 +217,6 @@ skipSpaces();

case '=': {
if (currentToken.type === TOKEN_TYPE$1.ATTRIB_NAME) {
currentToken = Token$1(TOKEN_TYPE$1.ASSIGN);
if (currentToken.type === "ATTRIB_NAME") {
currentToken = Token$1("ASSIGN");
} else {
currentToken = Token$1(TOKEN_TYPE$1.CONTENT, buffer);
currentToken = Token$1("CONTENT", buffer);
}

@@ -240,3 +229,3 @@ break

currentToken = Token$1(
TOKEN_TYPE$1.CLOSE_ELEMENT,
"CLOSE_ELEMENT",
xmlAsString.substring(start, pos)

@@ -251,3 +240,3 @@ );

currentToken = Token$1(
TOKEN_TYPE$1.CLOSE_ELEMENT,
"CLOSE_ELEMENT",
scopingElementName

@@ -273,7 +262,7 @@ );

case '>': {
currentToken = Token$1(TOKEN_TYPE$1.CLOSE_BRACKET);
currentToken = Token$1("CLOSE_BRACKET");
break
}
case '<': {
currentToken = Token$1(TOKEN_TYPE$1.OPEN_BRACKET);
currentToken = Token$1("OPEN_BRACKET");
break

@@ -284,3 +273,3 @@ }

if (buffer && buffer.length > 0) {
if (currentToken.type === TOKEN_TYPE$1.CLOSE_BRACKET) {
if (currentToken.type === "CLOSE_BRACKET") {
let suffix = '';

@@ -291,19 +280,19 @@ if (peek() !== '<') {

currentToken = Token$1(
TOKEN_TYPE$1.CONTENT,
"CONTENT",
buffer + suffix
);
} else if (
currentToken.type !== TOKEN_TYPE$1.ATTRIB_NAME &&
currentToken.type !== TOKEN_TYPE$1.CONTENT
currentToken.type !== "ATTRIB_NAME" &&
currentToken.type !== "CONTENT"
) {
if (
currentToken.type === TOKEN_TYPE$1.CLOSE_ELEMENT
currentToken.type === "CLOSE_ELEMENT"
) {
// we're assuming this is content, part of unstructured data
buffer = ' '.repeat(numOfSpacesSkipped) + buffer;
currentToken = Token$1(TOKEN_TYPE$1.CONTENT, buffer);
currentToken = Token$1("CONTENT", buffer);
} else {
// it should be an attribute name token
currentToken = Token$1(
TOKEN_TYPE$1.ATTRIB_NAME,
"ATTRIB_NAME",
buffer

@@ -316,3 +305,3 @@ );

currentToken = Token$1(
TOKEN_TYPE$1.CONTENT,
"CONTENT",
contentBuffer

@@ -337,3 +326,5 @@ );

next,
hasNext
hasNext,
// prettier-ignore
...({})
}

@@ -347,3 +338,3 @@ }

const { createLexer } = lexer;
const { Token, TOKEN_TYPE } = model;
const { Token } = model;
// AST Node types

@@ -405,3 +396,3 @@ const [ROOT, ELEMENT, ATTRIBUTE, CONTENT] = [

switch (lexem.type) {
case TOKEN_TYPE.OPEN_BRACKET: {
case "OPEN_BRACKET": {
const elementLexem = lexer.next();

@@ -411,3 +402,3 @@ const [elementAttributes, currentToken] =

let elementChildren = [];
if (currentToken.type !== TOKEN_TYPE.CLOSE_ELEMENT) {
if (currentToken.type !== "CLOSE_ELEMENT") {
elementChildren = parseExpr(lexer, elementLexem);

@@ -418,3 +409,3 @@ }

elementChildren.length > 0 &&
elementChildren[0].type === TOKEN_TYPE.CONTENT
elementChildren[0].type === "CONTENT"
) {

@@ -432,11 +423,11 @@ elementChildren = reduceChildrenElements(elementChildren);

}
case TOKEN_TYPE.CLOSE_ELEMENT: {
case "CLOSE_ELEMENT": {
if (lexem.value === scopingElement.value) return children
break
}
case TOKEN_TYPE.CONTENT: {
case "CONTENT": {
children.push(ContentNode(lexem.value));
break
}
case TOKEN_TYPE.EOF: {
case "EOF": {
return children

@@ -459,4 +450,4 @@ }

!lexer.hasNext() ||
(currentToken && currentToken.type === TOKEN_TYPE.CLOSE_BRACKET) ||
(currentToken && currentToken.type === TOKEN_TYPE.CLOSE_ELEMENT)
(currentToken && currentToken.type === "CLOSE_BRACKET") ||
(currentToken && currentToken.type === "CLOSE_ELEMENT")
) {

@@ -469,4 +460,4 @@ return [attribs, currentToken]

currentToken &&
currentToken.type !== TOKEN_TYPE.CLOSE_BRACKET &&
currentToken.type !== TOKEN_TYPE.CLOSE_ELEMENT
currentToken.type !== "CLOSE_BRACKET" &&
currentToken.type !== "CLOSE_ELEMENT"
) {

@@ -488,3 +479,3 @@ const attribName = currentToken;

elementChildren.forEach((child) => {
if (child.type === TOKEN_TYPE.CONTENT) {
if (child.type === "CONTENT") {
buffer += child.value;

@@ -515,6 +506,7 @@ } else {

var transpiler = {
transpile: transpile$1,
AttribNode,
ContentNode,
ElementNode,
Node,
ElementNode,
AttribNode
transpile: transpile$1
};

@@ -521,0 +513,0 @@

@@ -1,1 +0,1 @@

"use strict";function e(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}const t=e=>{if(!e)return null;const n={};switch(e.type){case"ELEMENT":{let s={};const a=E(e.value.attributes),T=t(e.value.children);if(a&&(s=Object.assign(s,a)),T){const t=r(e.value.children);s=Object.assign(s,t)}n[e.value.type]=s;break}case"ATTRIBUTE":{const t=e.value;n[t.name]=t.value;break}case"CONTENT":return{content:e.value}}return n},r=e=>e&&Array.isArray(e)&&0!==e.length?n(e)?{content:e[0].value}:{children:e.map(t)}:null,n=e=>e&&Array.isArray(e)&&1===e.length&&"CONTENT"===e[0].type,E=e=>{if(e&&Array.isArray(e)){return e.map(t).reduce(((e,t)=>Object.assign(e,t)),{})}return null};var s={convert:e=>t(e.value.children[0])};var a={Token:(e,t)=>({type:e,value:t}),TOKEN_TYPE:{OPEN_BRACKET:"OPEN_BRACKET",ELEMENT_TYPE:"ELEMENT_TYPE",CLOSE_ELEMENT:"CLOSE_ELEMENT",ATTRIB_NAME:"ATTRIB_NAME",ATTRIB_VALUE:"ATTRIB_VALUE",ASSIGN:"ASSIGN",CLOSE_BRACKET:"CLOSE_BRACKET",CONTENT:"CONTENT",EOF:"EOF"}};const{Token:T,TOKEN_TYPE:u}=a,c=T("EOF"),l=e=>" "===e||"\n"===e||"\r"===e||"\t"===e,o=e=>e.replace(/'/g,'"');var N={createLexer:function(e){let t=null,r=(e=>{let t=0;for(;t<e.length&&l(e[t]);)t++;return((e,t)=>{if(e.startsWith("<?xml",t)){const r=e.length;for(;t<r;)if("?"!==e[t])t++;else{if(">"===e[t+1])return t+2;t++}}return t})(e,t)})(e),n=[];const E=()=>e[r],s=()=>t!==c&&r<e.length,a=e=>'"'===e||"'"===e,N=()=>{for(;s()&&l(e[r]);)r++},i=t=>{if(s()){if("<"===e[r]){let t="<";return r++,s()&&"/"===e[r]?(r++,t="</"):s()&&"!"===e[r]&&"-"===e[r+1]&&"-"===e[r+2]&&(r++,r++,r++,t="\x3c!--"),t}if("/"===E()){let e="/";return r++,s()&&">"===E()&&(r++,e="/>"),e}if("="===e[r]||">"===e[r]){const t=e[r];return r++,t}}return p(!!t)},p=t=>{const n=t?/[^>=<]/u:/[a-zA-Z0-9_:-]/;let E=r;for(;s()&&e[r].match(n);)r++;return o(e.substring(E,r))},O=()=>{const l=r;N();const A=r-l;if(s())if(t&&t.type===u.OPEN_BRACKET){N();const e=i(!1);t=T(u.ELEMENT_TYPE,e),n.push(e)}else if(t&&t.type===u.ASSIGN){s()&&a(E())&&r++;let n=r;for(;s()&&!a(E());)r++;const c=o(e.substring(n,r));r++,t=T(u.ATTRIB_VALUE,c)}else{N();let a=i(!0);switch(a){case"=":t=t.type===u.ATTRIB_NAME?T(u.ASSIGN):T(u.CONTENT,a);break;case"</":{const E=r;for(;">"!==e[r];)r++;t=T(u.CLOSE_ELEMENT,e.substring(E,r)),r++,n.pop();break}case"/>":{const e=n.pop();t=T(u.CLOSE_ELEMENT,e);break}case"\x3c!--":{const t=["!","-","-"];for(;s()&&(">"!==t[2]||"-"!==t[1]||"-"!==t[0]);)t.shift(),t.push(e[r]),r++;return O()}case">":t=T(u.CLOSE_BRACKET);break;case"<":t=T(u.OPEN_BRACKET);break;default:if(a&&a.length>0){if(t.type===u.CLOSE_BRACKET){let e="";"<"!==E()&&(e=p(!0)),t=T(u.CONTENT,a+e)}else if(t.type!==u.ATTRIB_NAME&&t.type!==u.CONTENT)t.type===u.CLOSE_ELEMENT?(a=" ".repeat(A)+a,t=T(u.CONTENT,a)):t=T(u.ATTRIB_NAME,a);else{const e=" ".repeat(A)+a;t=T(u.CONTENT,e)}break}{const t='Unknown Syntax : "'+e[r]+'"';throw new Error(t)}}}else t=c;return t};return{peek:E,next:O,hasNext:s}}};const{createLexer:i}=N,{Token:p,TOKEN_TYPE:O}=a,[A,f,C,h]=["ROOT","ELEMENT","ATTRIBUTE","CONTENT"],_=(e,t)=>({type:e,value:t}),L=e=>_(h,e),y=(e,t,r)=>_(f,{type:e,attributes:t,children:r}),v=(e,t)=>_(C,{name:e,value:t}),S=(e,t)=>{const r=[];for(;e.hasNext();){const n=e.next();switch(n.type){case O.OPEN_BRACKET:{const t=e.next(),[n,E]=R(e);let s=[];E.type!==O.CLOSE_ELEMENT&&(s=S(e,t)),s&&s.length>0&&s[0].type===O.CONTENT&&(s=b(s)),r.push(y(t.value,n,s));break}case O.CLOSE_ELEMENT:if(n.value===t.value)return r;break;case O.CONTENT:r.push(L(n.value));break;case O.EOF:return r;default:throw new Error(`Unknown Lexem type: ${n.type} "${n.value}, scoping element: ${t.value}"`)}}return r},R=e=>{const t=[];let r=e.peek();if(!e.hasNext()||r&&r.type===O.CLOSE_BRACKET||r&&r.type===O.CLOSE_ELEMENT)return[t,r];for(r=e.next();e.hasNext()&&r&&r.type!==O.CLOSE_BRACKET&&r.type!==O.CLOSE_ELEMENT;){const n=r;e.next();const E=e.next(),s=v(n.value,E.value);t.push(s),r=e.next()}return[t,r]};function b(e){let t=[],r="";return e.forEach((e=>{e.type===O.CONTENT?r+=e.value:(r.length&&(t.push(L(r)),r=""),t.push(e))})),r.length&&t.push(L(r)),t}var B={transpile:function(e,t){const r=(e=>_(A,{children:S(e,p(A,"ROOT"))}))(i(e));return t?t.convert(r):r},Node:_,ElementNode:y,AttribNode:v};const M=s,{transpile:x}=B;var k=e({convertXML:function(e,t){return x(e,t||M)},createAST:function(e){return x(e)}});module.exports=k;
"use strict";function e(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}const t=e=>{if(!e)return null;const n={};switch(e.type){case"ELEMENT":{let a={};const u=s(e.value.attributes),c=t(e.value.children);if(u&&(a=Object.assign(a,u)),c){const t=r(e.value.children);a=Object.assign(a,t)}n[e.value.type]=a;break}case"ATTRIBUTE":{const t=e.value;n[t.name]=t.value;break}case"CONTENT":return{content:e.value}}return n},r=e=>e&&Array.isArray(e)&&0!==e.length?n(e)?{content:e[0].value}:{children:e.map(t)}:null,n=e=>e&&Array.isArray(e)&&1===e.length&&"CONTENT"===e[0].type,s=e=>{if(e&&Array.isArray(e)){return e.map(t).reduce(((e,t)=>Object.assign(e,t)),{})}return null};var a={convert:e=>t(e.value.children[0])};var u={Token:(e,t)=>({type:e,value:t})};const{Token:c}=u,l=c("EOF"),E=e=>" "===e||"\n"===e||"\r"===e||"\t"===e,o=e=>e.replace(/'/g,"'");var T={createLexer:function(e){let t=null,r=(e=>{let t=0;for(;t<e.length&&E(e[t]);)t++;return((e,t)=>{if(e.startsWith("<?xml",t)){const r=e.length;for(;t<r;)if("?"!==e[t])t++;else{if(">"===e[t+1])return t+2;t++}}return t})(e,t)})(e),n=[];const s=()=>e[r],a=()=>t!==l&&r<e.length,u=e=>'"'===e||"'"===e,T=()=>{for(;a()&&E(e[r]);)r++},i=t=>{if(a()){if("<"===e[r]){let t="<";return r++,a()&&"/"===e[r]?(r++,t="</"):a()&&"!"===e[r]&&"-"===e[r+1]&&"-"===e[r+2]&&(r++,r++,r++,t="\x3c!--"),t}if("/"===s()){let e="/";return r++,a()&&">"===s()&&(r++,e="/>"),e}if("="===e[r]||">"===e[r]){const t=e[r];return r++,t}}return p(!!t)},p=t=>{const n=t?/[^>=<]/u:/[a-zA-Z0-9_:-]/;let s=r;for(;a()&&e[r].match(n);)r++;return o(e.substring(s,r))},N=()=>{const E=r;T();const f=r-E;if(a())if(t&&"OPEN_BRACKET"===t.type){T();const e=i(!1);t=c("ELEMENT_TYPE",e),n.push(e)}else if(t&&"ASSIGN"===t.type){a()&&u(s())&&r++;const n=e[r-1];let l=r;for(;a()&&s()!==n;)r++;const E=o(e.substring(l,r));r++,t=c("ATTRIB_VALUE",E)}else{T();let u=i(!0);switch(u){case"=":t="ATTRIB_NAME"===t.type?c("ASSIGN"):c("CONTENT",u);break;case"</":{const s=r;for(;">"!==e[r];)r++;t=c("CLOSE_ELEMENT",e.substring(s,r)),r++,n.pop();break}case"/>":{const e=n.pop();t=c("CLOSE_ELEMENT",e);break}case"\x3c!--":{const t=["!","-","-"];for(;a()&&(">"!==t[2]||"-"!==t[1]||"-"!==t[0]);)t.shift(),t.push(e[r]),r++;return N()}case">":t=c("CLOSE_BRACKET");break;case"<":t=c("OPEN_BRACKET");break;default:if(u&&u.length>0){if("CLOSE_BRACKET"===t.type){let e="";"<"!==s()&&(e=p(!0)),t=c("CONTENT",u+e)}else if("ATTRIB_NAME"!==t.type&&"CONTENT"!==t.type)"CLOSE_ELEMENT"===t.type?(u=" ".repeat(f)+u,t=c("CONTENT",u)):t=c("ATTRIB_NAME",u);else{const e=" ".repeat(f)+u;t=c("CONTENT",e)}break}{const t='Unknown Syntax : "'+e[r]+'"';throw new Error(t)}}}else t=l;return t};return{peek:s,next:N,hasNext:a}}};const{createLexer:i}=T,{Token:p}=u,[N,f,h,O]=["ROOT","ELEMENT","ATTRIBUTE","CONTENT"],y=(e,t)=>({type:e,value:t}),C=e=>y(O,e),v=(e,t,r)=>y(f,{type:e,attributes:t,children:r}),A=(e,t)=>y(h,{name:e,value:t}),L=(e,t)=>{const r=[];for(;e.hasNext();){const n=e.next();switch(n.type){case"OPEN_BRACKET":{const t=e.next(),[n,s]=_(e);let a=[];"CLOSE_ELEMENT"!==s.type&&(a=L(e,t)),a&&a.length>0&&"CONTENT"===a[0].type&&(a=b(a)),r.push(v(t.value,n,a));break}case"CLOSE_ELEMENT":if(n.value===t.value)return r;break;case"CONTENT":r.push(C(n.value));break;case"EOF":return r;default:throw new Error(`Unknown Lexem type: ${n.type} "${n.value}, scoping element: ${t.value}"`)}}return r},_=e=>{const t=[];let r=e.peek();if(!e.hasNext()||r&&"CLOSE_BRACKET"===r.type||r&&"CLOSE_ELEMENT"===r.type)return[t,r];for(r=e.next();e.hasNext()&&r&&"CLOSE_BRACKET"!==r.type&&"CLOSE_ELEMENT"!==r.type;){const n=r;e.next();const s=e.next(),a=A(n.value,s.value);t.push(a),r=e.next()}return[t,r]};function b(e){let t=[],r="";return e.forEach((e=>{"CONTENT"===e.type?r+=e.value:(r.length&&(t.push(C(r)),r=""),t.push(e))})),r.length&&t.push(C(r)),t}var x={AttribNode:A,ContentNode:C,ElementNode:v,Node:y,transpile:function(e,t){const r=(e=>y(N,{children:L(e,p(N,"ROOT"))}))(i(e));return t?t.convert(r):r}};const k=a,{transpile:d}=x;var g=e({convertXML:function(e,t){return d(e,t||k)},createAST:function(e){return d(e)}});module.exports=g;

@@ -25,3 +25,3 @@ {

"license": "MIT",
"version": "1.2.2",
"version": "1.2.3",
"author": "Nir Moav <getnirm@gmail.com>",

@@ -42,3 +42,3 @@ "contributors": [

"scripts": {
"build": "npm run clean && rollup -c",
"build": "npm run clean && rollup -c --inline",
"clean": "rm -rf ./lib/ && mkdir lib ",

@@ -60,14 +60,19 @@ "lint": "eslint ./src ./test",

"devDependencies": {
"@rollup/plugin-commonjs": "^24.1.0",
"eslint": "^8.40.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-jest": "^27.9.0",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-replace": "^5.0.5",
"@rollup/plugin-terser": "^0.4.4",
"acorn": "^8.11.3",
"acorn-walk": "^8.3.2",
"eslint": "^9.1.1",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jest": "^28.3.0",
"globals": "^15.1.0",
"jest": "^29.7.0",
"prettier": "2.6.1",
"rollup": "^2.79.1",
"rollup-plugin-terser": "^7.0.2"
"magic-string": "^0.30.10",
"prettier": "3.2.5",
"rollup": "^4.17.1"
},
"engines": {
"node": ">=14.20.0"
"node": ">=20.12.2"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc