@ztimson/utils 0.28.17 → 0.29.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -3021,22 +3021,23 @@ ${err.message || err.toString()}`);
3021
3021
  if (xml[pos] !== "<") return parseText();
3022
3022
  pos++;
3023
3023
  if (xml[pos] === "?") {
3024
- parseDeclaration();
3025
- return parseNode();
3024
+ const declaration = parseDeclaration();
3025
+ return { ["?" + declaration]: "", ...parseNode() };
3026
3026
  }
3027
3027
  if (xml[pos] === "!") {
3028
3028
  parseComment();
3029
3029
  return parseNode();
3030
3030
  }
3031
3031
  const tagName = parseTagName();
3032
- const attributes = parseAttributes();
3032
+ parseAttributes();
3033
3033
  skipWhitespace();
3034
3034
  if (xml[pos] === "/" && xml[pos + 1] === ">") {
3035
3035
  pos += 2;
3036
- return { tag: tagName, attributes, children: [] };
3036
+ return { [tagName]: "" };
3037
3037
  }
3038
3038
  pos++;
3039
3039
  const children = [];
3040
+ let textContent = "";
3040
3041
  while (pos < xml.length) {
3041
3042
  skipWhitespace();
3042
3043
  if (xml[pos] === "<" && xml[pos + 1] === "/") {
@@ -3047,9 +3048,33 @@ ${err.message || err.toString()}`);
3047
3048
  break;
3048
3049
  }
3049
3050
  const child = parseNode();
3050
- if (child) children.push(child);
3051
+ if (typeof child === "string") {
3052
+ textContent += child;
3053
+ } else if (child) {
3054
+ children.push(child);
3055
+ }
3056
+ }
3057
+ if (children.length === 0 && textContent) {
3058
+ const value = isNumeric(textContent) ? Number(textContent) : textContent;
3059
+ return { [tagName]: value };
3060
+ }
3061
+ if (children.length === 0) {
3062
+ return { [tagName]: "" };
3063
+ }
3064
+ const result = {};
3065
+ for (const child of children) {
3066
+ for (const [key, value] of Object.entries(child)) {
3067
+ if (result[key]) {
3068
+ if (!Array.isArray(result[key])) {
3069
+ result[key] = [result[key]];
3070
+ }
3071
+ result[key].push(value);
3072
+ } else {
3073
+ result[key] = value;
3074
+ }
3075
+ }
3051
3076
  }
3052
- return { tag: tagName, attributes, children };
3077
+ return { [tagName]: result };
3053
3078
  }
3054
3079
  function parseTagName() {
3055
3080
  let name = "";
@@ -3082,8 +3107,14 @@ ${err.message || err.toString()}`);
3082
3107
  return text ? escapeXml(text, true) : null;
3083
3108
  }
3084
3109
  function parseDeclaration() {
3110
+ pos++;
3111
+ let name = "";
3112
+ while (pos < xml.length && xml[pos] !== " " && xml[pos] !== "?") {
3113
+ name += xml[pos++];
3114
+ }
3085
3115
  while (xml[pos] !== ">") pos++;
3086
3116
  pos++;
3117
+ return name;
3087
3118
  }
3088
3119
  function parseComment() {
3089
3120
  while (!(xml[pos] === "-" && xml[pos + 1] === "-" && xml[pos + 2] === ">")) pos++;
@@ -3092,6 +3123,9 @@ ${err.message || err.toString()}`);
3092
3123
  function skipWhitespace() {
3093
3124
  while (pos < xml.length && /\s/.test(xml[pos])) pos++;
3094
3125
  }
3126
+ function isNumeric(str) {
3127
+ return !isNaN(Number(str)) && !isNaN(parseFloat(str)) && str.trim() !== "";
3128
+ }
3095
3129
  return parseNode();
3096
3130
  }
3097
3131
  function toXml(obj, indent = "") {