@js-ak/excel-toolbox 1.4.0 → 1.5.0

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.
Files changed (42) hide show
  1. package/build/cjs/lib/template/index.js +1 -0
  2. package/build/cjs/lib/template/memory-write-stream.js +17 -0
  3. package/build/cjs/lib/template/template-fs.js +4 -223
  4. package/build/cjs/lib/template/template-memory.js +531 -0
  5. package/build/cjs/lib/template/utils/extract-xml-declaration.js +1 -1
  6. package/build/cjs/lib/template/utils/get-max-row-number.js +1 -1
  7. package/build/cjs/lib/template/utils/index.js +4 -0
  8. package/build/cjs/lib/template/utils/prepare-row-to-cells.js +13 -0
  9. package/build/cjs/lib/template/utils/process-merge-cells.js +40 -0
  10. package/build/cjs/lib/template/utils/process-merge-finalize.js +51 -0
  11. package/build/cjs/lib/template/utils/process-rows.js +160 -0
  12. package/build/cjs/lib/template/utils/process-shared-strings.js +45 -0
  13. package/build/cjs/lib/template/utils/to-excel-column-object.js +2 -10
  14. package/build/cjs/lib/template/utils/validate-worksheet-xml.js +65 -51
  15. package/build/cjs/lib/template/utils/write-rows-to-stream.js +15 -10
  16. package/build/esm/lib/template/index.js +1 -0
  17. package/build/esm/lib/template/memory-write-stream.js +13 -0
  18. package/build/esm/lib/template/template-fs.js +4 -223
  19. package/build/esm/lib/template/template-memory.js +494 -0
  20. package/build/esm/lib/template/utils/extract-xml-declaration.js +1 -1
  21. package/build/esm/lib/template/utils/get-max-row-number.js +1 -1
  22. package/build/esm/lib/template/utils/index.js +4 -0
  23. package/build/esm/lib/template/utils/prepare-row-to-cells.js +10 -0
  24. package/build/esm/lib/template/utils/process-merge-cells.js +37 -0
  25. package/build/esm/lib/template/utils/process-merge-finalize.js +48 -0
  26. package/build/esm/lib/template/utils/process-rows.js +157 -0
  27. package/build/esm/lib/template/utils/process-shared-strings.js +42 -0
  28. package/build/esm/lib/template/utils/to-excel-column-object.js +2 -10
  29. package/build/esm/lib/template/utils/validate-worksheet-xml.js +65 -51
  30. package/build/esm/lib/template/utils/write-rows-to-stream.js +15 -10
  31. package/build/types/lib/template/index.d.ts +1 -0
  32. package/build/types/lib/template/memory-write-stream.d.ts +6 -0
  33. package/build/types/lib/template/template-memory.d.ts +85 -0
  34. package/build/types/lib/template/utils/index.d.ts +4 -0
  35. package/build/types/lib/template/utils/prepare-row-to-cells.d.ts +1 -0
  36. package/build/types/lib/template/utils/process-merge-cells.d.ts +20 -0
  37. package/build/types/lib/template/utils/process-merge-finalize.d.ts +38 -0
  38. package/build/types/lib/template/utils/process-rows.d.ts +31 -0
  39. package/build/types/lib/template/utils/process-shared-strings.d.ts +20 -0
  40. package/build/types/lib/template/utils/validate-worksheet-xml.d.ts +16 -0
  41. package/build/types/lib/template/utils/write-rows-to-stream.d.ts +6 -2
  42. package/package.json +5 -3
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Processes the sheet XML by extracting the initial <mergeCells> block and
3
+ * extracting all merge cell references. The function returns an object with
4
+ * three properties:
5
+ * - `initialMergeCells`: The initial <mergeCells> block as a string array.
6
+ * - `mergeCellMatches`: An array of objects with `from` and `to` properties,
7
+ * representing the merge cell references.
8
+ * - `modifiedXml`: The modified sheet XML with the <mergeCells> block removed.
9
+ *
10
+ * @param sheetXml - The sheet XML string.
11
+ * @returns An object with the above three properties.
12
+ */
13
+ export declare function processMergeCells(sheetXml: string): {
14
+ initialMergeCells: string[];
15
+ mergeCellMatches: {
16
+ from: string;
17
+ to: string;
18
+ }[];
19
+ modifiedXml: string;
20
+ };
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Finalizes the processing of the merged sheet by updating the merge cells and
3
+ * inserting them into the sheet XML. It also returns the modified sheet XML and
4
+ * shared strings.
5
+ *
6
+ * @param {object} data - An object containing the following properties:
7
+ * - `initialMergeCells`: The initial merge cells from the original sheet.
8
+ * - `lastIndex`: The last processed position in the sheet XML.
9
+ * - `mergeCellMatches`: An array of objects with `from` and `to` properties,
10
+ * describing the merge cells.
11
+ * - `resultRows`: An array of processed XML rows.
12
+ * - `rowShift`: The total row shift.
13
+ * - `sharedStrings`: An array of shared strings.
14
+ * - `sharedStringsHeader`: The XML declaration of the shared strings.
15
+ * - `sheetMergeCells`: An array of merge cell XML strings.
16
+ * - `sheetXml`: The original sheet XML string.
17
+ *
18
+ * @returns An object with two properties:
19
+ * - `shared`: The modified shared strings XML string.
20
+ * - `sheet`: The modified sheet XML string with updated merge cells.
21
+ */
22
+ export declare function processMergeFinalize(data: {
23
+ initialMergeCells: string[];
24
+ lastIndex: number;
25
+ mergeCellMatches: {
26
+ from: string;
27
+ to: string;
28
+ }[];
29
+ resultRows: string[];
30
+ rowShift: number;
31
+ sharedStrings: string[];
32
+ sharedStringsHeader: string | null;
33
+ sheetMergeCells: string[];
34
+ sheetXml: string;
35
+ }): {
36
+ shared: string;
37
+ sheet: string;
38
+ };
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Processes a sheet XML by replacing table placeholders with real data and adjusting row numbers accordingly.
3
+ *
4
+ * @param data - An object containing the following properties:
5
+ * - `replacements`: An object where keys are table names and values are arrays of objects with table data.
6
+ * - `sharedIndexMap`: A Map of shared string indexes by their text content.
7
+ * - `mergeCellMatches`: An array of objects with `from` and `to` properties, describing the merge cells.
8
+ * - `sharedStrings`: An array of shared strings.
9
+ * - `sheetMergeCells`: An array of merge cell XML strings.
10
+ * - `sheetXml`: The sheet XML string.
11
+ *
12
+ * @returns An object with the following properties:
13
+ * - `lastIndex`: The last processed position in the sheet XML.
14
+ * - `resultRows`: An array of processed XML rows.
15
+ * - `rowShift`: The total row shift.
16
+ */
17
+ export declare function processRows(data: {
18
+ replacements: Record<string, unknown>;
19
+ sharedIndexMap: Map<string, number>;
20
+ mergeCellMatches: {
21
+ from: string;
22
+ to: string;
23
+ }[];
24
+ sharedStrings: string[];
25
+ sheetMergeCells: string[];
26
+ sheetXml: string;
27
+ }): {
28
+ lastIndex: number;
29
+ resultRows: string[];
30
+ rowShift: number;
31
+ };
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Processes the shared strings XML by extracting the XML declaration,
3
+ * extracting individual <si> elements, and storing them in an array.
4
+ *
5
+ * The function returns an object with four properties:
6
+ * - sharedIndexMap: A map of shared string content to their corresponding index
7
+ * - sharedStrings: An array of shared strings
8
+ * - sharedStringsHeader: The XML declaration of the shared strings
9
+ * - sheetMergeCells: An empty array, which is only used for type compatibility
10
+ * with the return type of processBuild.
11
+ *
12
+ * @param sharedStringsXml - The XML string of the shared strings
13
+ * @returns An object with the four properties above
14
+ */
15
+ export declare function processSharedStrings(sharedStringsXml: string): {
16
+ sharedIndexMap: Map<string, number>;
17
+ sharedStrings: string[];
18
+ sharedStringsHeader: string | null;
19
+ sheetMergeCells: string[];
20
+ };
@@ -5,5 +5,21 @@ interface ValidationResult {
5
5
  details?: string;
6
6
  };
7
7
  }
8
+ /**
9
+ * Validates an Excel worksheet XML against the expected structure and rules.
10
+ *
11
+ * Checks the following:
12
+ * 1. XML starts with <?xml declaration
13
+ * 2. Root element is worksheet
14
+ * 3. Required elements are present
15
+ * 4. row numbers are in ascending order
16
+ * 5. No duplicate row numbers
17
+ * 6. No overlapping merge ranges
18
+ * 7. All cells are within the specified dimension
19
+ * 8. All mergeCell tags refer to existing cells
20
+ *
21
+ * @param xml The raw XML content of the worksheet
22
+ * @returns A ValidationResult object indicating if the XML is valid, and an error message if it's not
23
+ */
8
24
  export declare function validateWorksheetXml(xml: string): ValidationResult;
9
25
  export {};
@@ -1,4 +1,7 @@
1
- import * as fs from "node:fs";
1
+ interface WritableLike {
2
+ write(chunk: string | Buffer): boolean;
3
+ end?: () => void;
4
+ }
2
5
  /**
3
6
  * Writes an async iterable of rows to an Excel XML file.
4
7
  *
@@ -20,6 +23,7 @@ import * as fs from "node:fs";
20
23
  * last row number written to the file (i.e., the `startRowNumber`
21
24
  * plus the number of rows written).
22
25
  */
23
- export declare function writeRowsToStream(output: fs.WriteStream, rows: AsyncIterable<unknown[]>, startRowNumber: number): Promise<{
26
+ export declare function writeRowsToStream(output: WritableLike, rows: AsyncIterable<unknown[] | unknown[][]>, startRowNumber: number): Promise<{
24
27
  rowNumber: number;
25
28
  }>;
29
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@js-ak/excel-toolbox",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "excel-toolbox",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -45,7 +45,8 @@
45
45
  "postbuild:esm": "node scripts/write-esm-package.js",
46
46
  "postbuild:cjs": "node scripts/write-cjs-package.js",
47
47
  "test": "vitest run",
48
- "test:watch": "vitest"
48
+ "test:watch": "vitest",
49
+ "test:coverage": "vitest run --coverage"
49
50
  },
50
51
  "repository": {
51
52
  "type": "git",
@@ -70,6 +71,7 @@
70
71
  "@stylistic/eslint-plugin-ts": "4.2.0",
71
72
  "@types/node": "22.14.0",
72
73
  "@types/pako": "2.0.3",
74
+ "@vitest/coverage-v8": "3.1.2",
73
75
  "eslint": "9.24.0",
74
76
  "eslint-plugin-sort-destructure-keys": "2.0.0",
75
77
  "eslint-plugin-sort-exports": "0.9.1",
@@ -77,7 +79,7 @@
77
79
  "semantic-release": "24.0.0",
78
80
  "typescript": "5.8.3",
79
81
  "typescript-eslint": "8.29.0",
80
- "vitest": "3.1.1"
82
+ "vitest": "3.1.2"
81
83
  },
82
84
  "dependencies": {
83
85
  "pako": "2.1.0"