@ooxml-tools/xml 0.1.0 → 0.2.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/README.md CHANGED
@@ -42,7 +42,8 @@ safeXml`<foo>hello</bar>`
42
42
  Substitution of arrays "just works" so you can map values in the tagged template literals
43
43
 
44
44
  ```ts
45
- const outXml = safeXml`<test>${[1,2,3].map(n => safeXml`<name>item ${1}<name>`)}</test>`
45
+ const items = [1,2,3].map(n => safeXml`<name>item ${1}</name>`);
46
+ const outXml = safeXml`<test>${items}</test>`
46
47
  assert.equal(outXml, `<test><name>item 1</name><name>item 2</name><name>item 3</name></test>`);
47
48
  ```
48
49
 
@@ -60,7 +61,7 @@ const outXml = compact(`
60
61
  assert.equal(outXml, "<test>something</test>");
61
62
  ```
62
63
 
63
- ### `removeFragments`
64
+ ### `collapseFragments`
64
65
  For XML to be valid, there must be a single root node. When composing apps it's handy for that not to be true.
65
66
 
66
67
  For example the following would error
@@ -83,7 +84,7 @@ const xml = safeXml`
83
84
  `
84
85
  ```
85
86
 
86
- So we did just that, `removeFragments` walks over a tree removing `<XML_FRAGMENT>...</XML_FRAGMENT>` nodes.
87
+ So we did just that, `collapseFragments` walks over a tree removing `<XML_FRAGMENT>...</XML_FRAGMENT>` nodes.
87
88
 
88
89
  ```ts
89
90
  const innerBit = safeXml`
@@ -92,7 +93,7 @@ const innerBit = safeXml`
92
93
  <name>bar</name>
93
94
  </XML_FRAGMENT>
94
95
  `
95
- const newXml = removeFragments(
96
+ const newXml = collapseFragments(
96
97
  safeXml`
97
98
  <doc>
98
99
  ${innerBit}
@@ -118,6 +119,25 @@ But `<name><![CDATA[one < two]]><name>` is ugly and hard to read, so instead
118
119
  safeXml`<name>${cdata("one < two")}</name>`
119
120
  ```
120
121
 
122
+ ### `format`
123
+ Format XML in a consistent way, useful for logging and snapshot testing
124
+
125
+ ```ts
126
+ format(`
127
+ <test> one
128
+ </test>
129
+ `) /* =>
130
+ * <test>
131
+ * one
132
+ * </test>
133
+ */
134
+ ```
135
+
136
+ ## CI
137
+
138
+ [![codecov](https://codecov.io/gh/ooxml-tools/xml/graph/badge.svg?token=N82AKMVJM7)](https://codecov.io/gh/ooxml-tools/xml)
139
+
140
+
121
141
  ## License
122
142
 
123
143
  MIT
package/dist/npm/index.js CHANGED
@@ -98,4 +98,18 @@ function collapseFragments(root) {
98
98
  return out;
99
99
  }
100
100
 
101
- export { asXmlElement, cdata, collapseFragments, compact, createFragment, safeXml };
101
+ function format(input, { spaces = 2 } = {}) {
102
+ const elementJson = xml2js(input, {
103
+ compact: false,
104
+ captureSpacesBetweenElements: false,
105
+ });
106
+ return js2xml(elementJson, {
107
+ spaces: spaces,
108
+ compact: false,
109
+ indentText: true,
110
+ fullTagEmptyElement: true,
111
+ indentAttributes: true,
112
+ });
113
+ }
114
+
115
+ export { asXmlElement, cdata, collapseFragments, compact, createFragment, format, safeXml };
@@ -16,4 +16,10 @@ declare function compact(xml: string): string;
16
16
  declare function createFragment(elements: Element[]): Element;
17
17
  declare function collapseFragments(root: string | Element): Element;
18
18
 
19
- export { asXmlElement, cdata, collapseFragments, compact, createFragment, safeXml };
19
+ type FormatOptions = {
20
+ spaces?: number;
21
+ };
22
+ declare function format(input: string, { spaces }?: FormatOptions): string;
23
+
24
+ export { asXmlElement, cdata, collapseFragments, compact, createFragment, format, safeXml };
25
+ export type { FormatOptions };
package/package.json CHANGED
@@ -1,14 +1,15 @@
1
1
  {
2
2
  "name": "@ooxml-tools/xml",
3
3
  "description": "Some XML helpers to help with OOXML development",
4
- "version": "0.1.0",
4
+ "version": "0.2.1",
5
5
  "license": "MIT",
6
6
  "main": "./dist/npm/index.js",
7
7
  "types": "./dist/npm/types.d.ts",
8
8
  "type": "module",
9
9
  "scripts": {
10
10
  "lint": "npx prettier . --check",
11
- "test": "vitest",
11
+ "test": "vitest run --coverage",
12
+ "test:ci": "vitest run --coverage --reporter=junit --outputFile=test-report.junit.xml",
12
13
  "lint:format": "npx prettier . --write",
13
14
  "build": "rollup -c rollup.config.ts --configPlugin typescript"
14
15
  },
@@ -22,11 +23,13 @@
22
23
  "./README.md"
23
24
  ],
24
25
  "devDependencies": {
26
+ "@codecov/rollup-plugin": "^1.9.1",
25
27
  "@rollup/plugin-json": "^6.1.0",
26
28
  "@rollup/plugin-typescript": "^12.1.2",
27
29
  "@rollup/plugin-virtual": "^3.0.2",
28
30
  "@tsconfig/node22": "^22.0.0",
29
31
  "@types/yargs": "^17.0.32",
32
+ "@vitest/coverage-v8": "^3.2.4",
30
33
  "prettier": "^3.4.2",
31
34
  "rollup": "^4.18.1",
32
35
  "rollup-plugin-copy": "^3.5.0",
@@ -37,7 +40,7 @@
37
40
  "tslib": "^2.6.3",
38
41
  "tsx": "^4.17.0",
39
42
  "typescript": "^5.5.4",
40
- "vitest": "^2.1.6"
43
+ "vitest": "^3.2.4"
41
44
  },
42
45
  "engines": {
43
46
  "node": ">=20.x"