@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.mjs CHANGED
@@ -3017,22 +3017,23 @@ function fromXml(xml) {
3017
3017
  if (xml[pos] !== "<") return parseText();
3018
3018
  pos++;
3019
3019
  if (xml[pos] === "?") {
3020
- parseDeclaration();
3021
- return parseNode();
3020
+ const declaration = parseDeclaration();
3021
+ return { ["?" + declaration]: "", ...parseNode() };
3022
3022
  }
3023
3023
  if (xml[pos] === "!") {
3024
3024
  parseComment();
3025
3025
  return parseNode();
3026
3026
  }
3027
3027
  const tagName = parseTagName();
3028
- const attributes = parseAttributes();
3028
+ parseAttributes();
3029
3029
  skipWhitespace();
3030
3030
  if (xml[pos] === "/" && xml[pos + 1] === ">") {
3031
3031
  pos += 2;
3032
- return { tag: tagName, attributes, children: [] };
3032
+ return { [tagName]: "" };
3033
3033
  }
3034
3034
  pos++;
3035
3035
  const children = [];
3036
+ let textContent = "";
3036
3037
  while (pos < xml.length) {
3037
3038
  skipWhitespace();
3038
3039
  if (xml[pos] === "<" && xml[pos + 1] === "/") {
@@ -3043,9 +3044,33 @@ function fromXml(xml) {
3043
3044
  break;
3044
3045
  }
3045
3046
  const child = parseNode();
3046
- if (child) children.push(child);
3047
+ if (typeof child === "string") {
3048
+ textContent += child;
3049
+ } else if (child) {
3050
+ children.push(child);
3051
+ }
3052
+ }
3053
+ if (children.length === 0 && textContent) {
3054
+ const value = isNumeric(textContent) ? Number(textContent) : textContent;
3055
+ return { [tagName]: value };
3056
+ }
3057
+ if (children.length === 0) {
3058
+ return { [tagName]: "" };
3059
+ }
3060
+ const result = {};
3061
+ for (const child of children) {
3062
+ for (const [key, value] of Object.entries(child)) {
3063
+ if (result[key]) {
3064
+ if (!Array.isArray(result[key])) {
3065
+ result[key] = [result[key]];
3066
+ }
3067
+ result[key].push(value);
3068
+ } else {
3069
+ result[key] = value;
3070
+ }
3071
+ }
3047
3072
  }
3048
- return { tag: tagName, attributes, children };
3073
+ return { [tagName]: result };
3049
3074
  }
3050
3075
  function parseTagName() {
3051
3076
  let name = "";
@@ -3078,8 +3103,14 @@ function fromXml(xml) {
3078
3103
  return text ? escapeXml(text, true) : null;
3079
3104
  }
3080
3105
  function parseDeclaration() {
3106
+ pos++;
3107
+ let name = "";
3108
+ while (pos < xml.length && xml[pos] !== " " && xml[pos] !== "?") {
3109
+ name += xml[pos++];
3110
+ }
3081
3111
  while (xml[pos] !== ">") pos++;
3082
3112
  pos++;
3113
+ return name;
3083
3114
  }
3084
3115
  function parseComment() {
3085
3116
  while (!(xml[pos] === "-" && xml[pos + 1] === "-" && xml[pos + 2] === ">")) pos++;
@@ -3088,6 +3119,9 @@ function fromXml(xml) {
3088
3119
  function skipWhitespace() {
3089
3120
  while (pos < xml.length && /\s/.test(xml[pos])) pos++;
3090
3121
  }
3122
+ function isNumeric(str) {
3123
+ return !isNaN(Number(str)) && !isNaN(parseFloat(str)) && str.trim() !== "";
3124
+ }
3091
3125
  return parseNode();
3092
3126
  }
3093
3127
  function toXml(obj, indent = "") {