@ls-stack/utils 3.11.0 → 3.11.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.
@@ -17,12 +17,12 @@ var __copyProps = (to, from, except, desc) => {
17
17
  };
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
- // src/xmlSerializer.ts
21
- var xmlSerializer_exports = {};
22
- __export(xmlSerializer_exports, {
20
+ // src/serializeXML.ts
21
+ var serializeXML_exports = {};
22
+ __export(serializeXML_exports, {
23
23
  serializeXML: () => serializeXML
24
24
  });
25
- module.exports = __toCommonJS(xmlSerializer_exports);
25
+ module.exports = __toCommonJS(serializeXML_exports);
26
26
 
27
27
  // src/arrayUtils.ts
28
28
  function filterAndMap(array, mapFilter) {
@@ -38,7 +38,7 @@ function filterAndMap(array, mapFilter) {
38
38
  return result;
39
39
  }
40
40
 
41
- // src/xmlSerializer.ts
41
+ // src/serializeXML.ts
42
42
  var XML_TAG_NAME_REGEX = /^[a-zA-Z_][a-zA-Z0-9._-]*$/;
43
43
  var XML_PREFIX_REGEX = /^xml/i;
44
44
  var XML_ESCAPE_MAP = {
@@ -50,25 +50,41 @@ var XML_ESCAPE_MAP = {
50
50
  };
51
51
  var XML_ESCAPE_REGEX = /[&<>"']/g;
52
52
  function serializeXML(node, options) {
53
+ if (Array.isArray(node)) {
54
+ const EMPTY_LINE_MARKER = "\0EMPTY_LINE\0";
55
+ return node.map((n) => {
56
+ if (n.type === "emptyLine") {
57
+ return EMPTY_LINE_MARKER;
58
+ }
59
+ return serializeWithLevel(n, options, 0);
60
+ }).filter(Boolean).join(options?.indent ? "\n" : "").replace(new RegExp(EMPTY_LINE_MARKER, "g"), "");
61
+ }
53
62
  return serializeWithLevel(node, options, 0);
54
63
  }
55
64
  function serializeWithLevel(node, options = {}, level) {
56
- const { name, attributes = {}, children, escapeText: nodeEscapeText } = node;
57
65
  const {
58
66
  indent,
59
67
  escapeText: globalEscapeText = true,
60
- validateTagName = "throw"
68
+ validateTagName = true,
69
+ invalidNodes = "throw"
61
70
  } = options;
62
- const shouldEscapeText = nodeEscapeText !== void 0 ? nodeEscapeText : globalEscapeText;
63
71
  const indentStr = indent ? getIndentString(indent, level) : "";
64
72
  const newline = indent ? "\n" : "";
65
- if (validateTagName) {
66
- if (!isValidXmlTagName(name)) {
67
- if (validateTagName === "throw") {
68
- throw new Error(`Invalid XML tag name: "${name}"`);
69
- }
70
- return "";
73
+ if (node.type === "comment") {
74
+ const shouldEscapeText2 = node.escapeText !== void 0 ? node.escapeText : globalEscapeText;
75
+ const content = shouldEscapeText2 ? escapeXml(node.content) : node.content;
76
+ return `${indentStr}<!-- ${content} -->`;
77
+ }
78
+ if (node.type === "emptyLine") {
79
+ return "";
80
+ }
81
+ const { name, attributes = {}, children, escapeText: nodeEscapeText } = node;
82
+ const shouldEscapeText = nodeEscapeText !== void 0 ? nodeEscapeText : globalEscapeText;
83
+ if (validateTagName && !isValidXmlTagName(name)) {
84
+ if (invalidNodes === "throw") {
85
+ throw new Error(`Invalid XML tag name: "${name}"`);
71
86
  }
87
+ return "";
72
88
  }
73
89
  const attributesStr = filterAndMap(
74
90
  Object.entries(attributes),
@@ -1,14 +1,22 @@
1
1
  type XMLNode = {
2
+ type?: 'node';
2
3
  name: string;
3
4
  attributes?: Record<string, string | number | boolean | null | undefined>;
4
5
  children?: (XMLNode | null | undefined | false)[] | string;
5
6
  escapeText?: boolean;
7
+ } | {
8
+ type: 'comment';
9
+ content: string;
10
+ escapeText?: boolean;
11
+ } | {
12
+ type: 'emptyLine';
6
13
  };
7
14
  type SerializeOptions = {
8
15
  indent?: number | string;
9
16
  escapeText?: boolean;
10
- validateTagName?: 'throw' | 'reject' | false;
17
+ validateTagName?: boolean;
18
+ invalidNodes?: 'throw' | 'reject';
11
19
  };
12
- declare function serializeXML(node: XMLNode, options?: SerializeOptions): string;
20
+ declare function serializeXML(node: XMLNode | XMLNode[], options?: SerializeOptions): string;
13
21
 
14
22
  export { type SerializeOptions, type XMLNode, serializeXML };
@@ -1,14 +1,22 @@
1
1
  type XMLNode = {
2
+ type?: 'node';
2
3
  name: string;
3
4
  attributes?: Record<string, string | number | boolean | null | undefined>;
4
5
  children?: (XMLNode | null | undefined | false)[] | string;
5
6
  escapeText?: boolean;
7
+ } | {
8
+ type: 'comment';
9
+ content: string;
10
+ escapeText?: boolean;
11
+ } | {
12
+ type: 'emptyLine';
6
13
  };
7
14
  type SerializeOptions = {
8
15
  indent?: number | string;
9
16
  escapeText?: boolean;
10
- validateTagName?: 'throw' | 'reject' | false;
17
+ validateTagName?: boolean;
18
+ invalidNodes?: 'throw' | 'reject';
11
19
  };
12
- declare function serializeXML(node: XMLNode, options?: SerializeOptions): string;
20
+ declare function serializeXML(node: XMLNode | XMLNode[], options?: SerializeOptions): string;
13
21
 
14
22
  export { type SerializeOptions, type XMLNode, serializeXML };
@@ -3,7 +3,7 @@ import {
3
3
  } from "./chunk-UTFE4P3P.js";
4
4
  import "./chunk-3XCS7FVO.js";
5
5
 
6
- // src/xmlSerializer.ts
6
+ // src/serializeXML.ts
7
7
  var XML_TAG_NAME_REGEX = /^[a-zA-Z_][a-zA-Z0-9._-]*$/;
8
8
  var XML_PREFIX_REGEX = /^xml/i;
9
9
  var XML_ESCAPE_MAP = {
@@ -15,25 +15,41 @@ var XML_ESCAPE_MAP = {
15
15
  };
16
16
  var XML_ESCAPE_REGEX = /[&<>"']/g;
17
17
  function serializeXML(node, options) {
18
+ if (Array.isArray(node)) {
19
+ const EMPTY_LINE_MARKER = "\0EMPTY_LINE\0";
20
+ return node.map((n) => {
21
+ if (n.type === "emptyLine") {
22
+ return EMPTY_LINE_MARKER;
23
+ }
24
+ return serializeWithLevel(n, options, 0);
25
+ }).filter(Boolean).join(options?.indent ? "\n" : "").replace(new RegExp(EMPTY_LINE_MARKER, "g"), "");
26
+ }
18
27
  return serializeWithLevel(node, options, 0);
19
28
  }
20
29
  function serializeWithLevel(node, options = {}, level) {
21
- const { name, attributes = {}, children, escapeText: nodeEscapeText } = node;
22
30
  const {
23
31
  indent,
24
32
  escapeText: globalEscapeText = true,
25
- validateTagName = "throw"
33
+ validateTagName = true,
34
+ invalidNodes = "throw"
26
35
  } = options;
27
- const shouldEscapeText = nodeEscapeText !== void 0 ? nodeEscapeText : globalEscapeText;
28
36
  const indentStr = indent ? getIndentString(indent, level) : "";
29
37
  const newline = indent ? "\n" : "";
30
- if (validateTagName) {
31
- if (!isValidXmlTagName(name)) {
32
- if (validateTagName === "throw") {
33
- throw new Error(`Invalid XML tag name: "${name}"`);
34
- }
35
- return "";
38
+ if (node.type === "comment") {
39
+ const shouldEscapeText2 = node.escapeText !== void 0 ? node.escapeText : globalEscapeText;
40
+ const content = shouldEscapeText2 ? escapeXml(node.content) : node.content;
41
+ return `${indentStr}<!-- ${content} -->`;
42
+ }
43
+ if (node.type === "emptyLine") {
44
+ return "";
45
+ }
46
+ const { name, attributes = {}, children, escapeText: nodeEscapeText } = node;
47
+ const shouldEscapeText = nodeEscapeText !== void 0 ? nodeEscapeText : globalEscapeText;
48
+ if (validateTagName && !isValidXmlTagName(name)) {
49
+ if (invalidNodes === "throw") {
50
+ throw new Error(`Invalid XML tag name: "${name}"`);
36
51
  }
52
+ return "";
37
53
  }
38
54
  const attributesStr = filterAndMap(
39
55
  Object.entries(attributes),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Typescript utils",
4
- "version": "3.11.0",
4
+ "version": "3.11.1",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "lib"
@@ -118,6 +118,10 @@
118
118
  "import": "./lib/saferTyping.js",
119
119
  "require": "./lib/saferTyping.cjs"
120
120
  },
121
+ "./serializeXML": {
122
+ "import": "./lib/serializeXML.js",
123
+ "require": "./lib/serializeXML.cjs"
124
+ },
121
125
  "./shallowEqual": {
122
126
  "import": "./lib/shallowEqual.js",
123
127
  "require": "./lib/shallowEqual.cjs"
@@ -154,10 +158,6 @@
154
158
  "import": "./lib/typingUtils.js",
155
159
  "require": "./lib/typingUtils.cjs"
156
160
  },
157
- "./xmlSerializer": {
158
- "import": "./lib/xmlSerializer.js",
159
- "require": "./lib/xmlSerializer.cjs"
160
- },
161
161
  "./yamlStringify": {
162
162
  "import": "./lib/yamlStringify.js",
163
163
  "require": "./lib/yamlStringify.cjs"