documents.js 2.0.0 → 2.1.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.
- package/README.md +34 -28
- package/dist/codecs/registry.cjs +7 -0
- package/dist/codecs/registry.js +7 -0
- package/dist/convert/capability.cjs +5 -0
- package/dist/convert/capability.js +5 -0
- package/dist/convert/codec.cjs +20 -0
- package/dist/convert/codec.d.cts +5 -1
- package/dist/convert/codec.d.ts +5 -1
- package/dist/convert/codec.js +19 -3
- package/dist/convert/composition.cjs +32 -7
- package/dist/convert/composition.d.cts +9 -5
- package/dist/convert/composition.d.ts +9 -5
- package/dist/convert/composition.js +32 -7
- package/dist/convert/convert.cjs +32 -0
- package/dist/convert/convert.d.cts +18 -1
- package/dist/convert/convert.d.ts +18 -1
- package/dist/convert/convert.js +25 -1
- package/dist/convert/local.cjs +2 -0
- package/dist/convert/local.js +2 -0
- package/dist/convert/port.cjs +1 -0
- package/dist/convert/port.d.cts +3 -0
- package/dist/convert/port.d.ts +3 -0
- package/dist/convert/port.js +1 -0
- package/dist/csv/read.cjs +85 -0
- package/dist/csv/read.d.cts +10 -0
- package/dist/csv/read.d.ts +10 -0
- package/dist/csv/read.js +84 -0
- package/dist/csv/records.cjs +85 -0
- package/dist/csv/records.d.cts +10 -0
- package/dist/csv/records.d.ts +10 -0
- package/dist/csv/records.js +80 -0
- package/dist/csv/text.cjs +22 -0
- package/dist/csv/text.d.cts +8 -0
- package/dist/csv/text.d.ts +8 -0
- package/dist/csv/text.js +19 -0
- package/dist/csv/write.cjs +75 -0
- package/dist/csv/write.d.cts +22 -0
- package/dist/csv/write.d.ts +22 -0
- package/dist/csv/write.js +71 -0
- package/dist/index.cjs +27 -1
- package/dist/index.d.cts +9 -5
- package/dist/index.d.ts +9 -5
- package/dist/index.js +9 -5
- package/dist/layout/reconstruct.cjs +1 -1
- package/dist/layout/reconstruct.js +1 -1
- package/dist/metadata/write.cjs +1 -0
- package/dist/metadata/write.js +1 -0
- package/dist/model/bytes.cjs +2 -0
- package/dist/model/bytes.d.cts +2 -1
- package/dist/model/bytes.d.ts +2 -1
- package/dist/model/bytes.js +2 -1
- package/dist/odb/csv.cjs +3 -6
- package/dist/odb/csv.js +3 -6
- package/package.json +1 -1
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/csv/records.ts
|
|
3
|
+
const DEFAULT_CSV_DELIMITER = ",";
|
|
4
|
+
const TSV_DELIMITER = " ";
|
|
5
|
+
var CsvParseError = class extends Error {
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "CsvParseError";
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
function requireSingleCharacterDelimiter(delimiter) {
|
|
12
|
+
if (delimiter.length !== 1) throw new CsvParseError(`delimiter must be exactly one character, got ${JSON.stringify(delimiter)}`);
|
|
13
|
+
}
|
|
14
|
+
function isBlankRecord(record) {
|
|
15
|
+
return record.length === 1 && record[0] === "";
|
|
16
|
+
}
|
|
17
|
+
function parseCsvRecords(text, delimiter = ",") {
|
|
18
|
+
requireSingleCharacterDelimiter(delimiter);
|
|
19
|
+
const records = [];
|
|
20
|
+
let record = [];
|
|
21
|
+
let field = "";
|
|
22
|
+
let inQuotedField = false;
|
|
23
|
+
let fieldStarted = false;
|
|
24
|
+
const endField = () => {
|
|
25
|
+
record.push(field);
|
|
26
|
+
field = "";
|
|
27
|
+
fieldStarted = false;
|
|
28
|
+
};
|
|
29
|
+
const endRecord = () => {
|
|
30
|
+
endField();
|
|
31
|
+
records.push(record);
|
|
32
|
+
record = [];
|
|
33
|
+
};
|
|
34
|
+
let index = 0;
|
|
35
|
+
while (index < text.length) {
|
|
36
|
+
const ch = text[index];
|
|
37
|
+
if (inQuotedField) {
|
|
38
|
+
if (ch === "\"") {
|
|
39
|
+
if (text[index + 1] === "\"") {
|
|
40
|
+
field += "\"";
|
|
41
|
+
index += 2;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
inQuotedField = false;
|
|
45
|
+
index += 1;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
field += ch;
|
|
49
|
+
index += 1;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (ch === "\"" && !fieldStarted) {
|
|
53
|
+
inQuotedField = true;
|
|
54
|
+
fieldStarted = true;
|
|
55
|
+
index += 1;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (ch === delimiter) {
|
|
59
|
+
endField();
|
|
60
|
+
index += 1;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (ch === "\r" || ch === "\n") {
|
|
64
|
+
if (ch === "\r" && text[index + 1] === "\n") index += 2;
|
|
65
|
+
else index += 1;
|
|
66
|
+
endRecord();
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
field += ch;
|
|
70
|
+
fieldStarted = true;
|
|
71
|
+
index += 1;
|
|
72
|
+
}
|
|
73
|
+
if (inQuotedField) throw new CsvParseError(`unterminated quoted field: no closing double quote before end of input (field so far: ${field.slice(0, 40)})`);
|
|
74
|
+
if (fieldStarted || field !== "" || record.length > 0) endRecord();
|
|
75
|
+
return records.filter((candidate) => !isBlankRecord(candidate));
|
|
76
|
+
}
|
|
77
|
+
function quoteCsvField(field, delimiter = ",") {
|
|
78
|
+
return field.includes(delimiter) || field.includes("\"") || field.includes("\r") || field.includes("\n") ? `"${field.replaceAll("\"", "\"\"")}"` : field;
|
|
79
|
+
}
|
|
80
|
+
//#endregion
|
|
81
|
+
exports.CsvParseError = CsvParseError;
|
|
82
|
+
exports.DEFAULT_CSV_DELIMITER = DEFAULT_CSV_DELIMITER;
|
|
83
|
+
exports.TSV_DELIMITER = TSV_DELIMITER;
|
|
84
|
+
exports.parseCsvRecords = parseCsvRecords;
|
|
85
|
+
exports.quoteCsvField = quoteCsvField;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region src/csv/records.d.ts
|
|
2
|
+
declare const DEFAULT_CSV_DELIMITER = ",";
|
|
3
|
+
declare const TSV_DELIMITER = "\t";
|
|
4
|
+
declare class CsvParseError extends Error {
|
|
5
|
+
constructor(message: string);
|
|
6
|
+
}
|
|
7
|
+
declare function parseCsvRecords(text: string, delimiter?: string): string[][];
|
|
8
|
+
declare function quoteCsvField(field: string, delimiter?: string): string;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { CsvParseError, DEFAULT_CSV_DELIMITER, TSV_DELIMITER, parseCsvRecords, quoteCsvField };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region src/csv/records.d.ts
|
|
2
|
+
declare const DEFAULT_CSV_DELIMITER = ",";
|
|
3
|
+
declare const TSV_DELIMITER = "\t";
|
|
4
|
+
declare class CsvParseError extends Error {
|
|
5
|
+
constructor(message: string);
|
|
6
|
+
}
|
|
7
|
+
declare function parseCsvRecords(text: string, delimiter?: string): string[][];
|
|
8
|
+
declare function quoteCsvField(field: string, delimiter?: string): string;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { CsvParseError, DEFAULT_CSV_DELIMITER, TSV_DELIMITER, parseCsvRecords, quoteCsvField };
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
//#region src/csv/records.ts
|
|
2
|
+
const DEFAULT_CSV_DELIMITER = ",";
|
|
3
|
+
const TSV_DELIMITER = " ";
|
|
4
|
+
var CsvParseError = class extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "CsvParseError";
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
function requireSingleCharacterDelimiter(delimiter) {
|
|
11
|
+
if (delimiter.length !== 1) throw new CsvParseError(`delimiter must be exactly one character, got ${JSON.stringify(delimiter)}`);
|
|
12
|
+
}
|
|
13
|
+
function isBlankRecord(record) {
|
|
14
|
+
return record.length === 1 && record[0] === "";
|
|
15
|
+
}
|
|
16
|
+
function parseCsvRecords(text, delimiter = ",") {
|
|
17
|
+
requireSingleCharacterDelimiter(delimiter);
|
|
18
|
+
const records = [];
|
|
19
|
+
let record = [];
|
|
20
|
+
let field = "";
|
|
21
|
+
let inQuotedField = false;
|
|
22
|
+
let fieldStarted = false;
|
|
23
|
+
const endField = () => {
|
|
24
|
+
record.push(field);
|
|
25
|
+
field = "";
|
|
26
|
+
fieldStarted = false;
|
|
27
|
+
};
|
|
28
|
+
const endRecord = () => {
|
|
29
|
+
endField();
|
|
30
|
+
records.push(record);
|
|
31
|
+
record = [];
|
|
32
|
+
};
|
|
33
|
+
let index = 0;
|
|
34
|
+
while (index < text.length) {
|
|
35
|
+
const ch = text[index];
|
|
36
|
+
if (inQuotedField) {
|
|
37
|
+
if (ch === "\"") {
|
|
38
|
+
if (text[index + 1] === "\"") {
|
|
39
|
+
field += "\"";
|
|
40
|
+
index += 2;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
inQuotedField = false;
|
|
44
|
+
index += 1;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
field += ch;
|
|
48
|
+
index += 1;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (ch === "\"" && !fieldStarted) {
|
|
52
|
+
inQuotedField = true;
|
|
53
|
+
fieldStarted = true;
|
|
54
|
+
index += 1;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (ch === delimiter) {
|
|
58
|
+
endField();
|
|
59
|
+
index += 1;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (ch === "\r" || ch === "\n") {
|
|
63
|
+
if (ch === "\r" && text[index + 1] === "\n") index += 2;
|
|
64
|
+
else index += 1;
|
|
65
|
+
endRecord();
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
field += ch;
|
|
69
|
+
fieldStarted = true;
|
|
70
|
+
index += 1;
|
|
71
|
+
}
|
|
72
|
+
if (inQuotedField) throw new CsvParseError(`unterminated quoted field: no closing double quote before end of input (field so far: ${field.slice(0, 40)})`);
|
|
73
|
+
if (fieldStarted || field !== "" || record.length > 0) endRecord();
|
|
74
|
+
return records.filter((candidate) => !isBlankRecord(candidate));
|
|
75
|
+
}
|
|
76
|
+
function quoteCsvField(field, delimiter = ",") {
|
|
77
|
+
return field.includes(delimiter) || field.includes("\"") || field.includes("\r") || field.includes("\n") ? `"${field.replaceAll("\"", "\"\"")}"` : field;
|
|
78
|
+
}
|
|
79
|
+
//#endregion
|
|
80
|
+
export { CsvParseError, DEFAULT_CSV_DELIMITER, TSV_DELIMITER, parseCsvRecords, quoteCsvField };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/csv/text.ts
|
|
3
|
+
var CsvInvalidUtf8Error = class extends Error {
|
|
4
|
+
constructor() {
|
|
5
|
+
super("csv text must be well-formed UTF-8");
|
|
6
|
+
this.name = "CsvInvalidUtf8Error";
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
function decodeCsvText(bytes) {
|
|
10
|
+
try {
|
|
11
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
12
|
+
} catch {
|
|
13
|
+
throw new CsvInvalidUtf8Error();
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function encodeCsvText(text) {
|
|
17
|
+
return new TextEncoder().encode(text);
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
exports.CsvInvalidUtf8Error = CsvInvalidUtf8Error;
|
|
21
|
+
exports.decodeCsvText = decodeCsvText;
|
|
22
|
+
exports.encodeCsvText = encodeCsvText;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
//#region src/csv/text.d.ts
|
|
2
|
+
declare class CsvInvalidUtf8Error extends Error {
|
|
3
|
+
constructor();
|
|
4
|
+
}
|
|
5
|
+
declare function decodeCsvText(bytes: Uint8Array): string;
|
|
6
|
+
declare function encodeCsvText(text: string): Uint8Array<ArrayBuffer>;
|
|
7
|
+
//#endregion
|
|
8
|
+
export { CsvInvalidUtf8Error, decodeCsvText, encodeCsvText };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
//#region src/csv/text.d.ts
|
|
2
|
+
declare class CsvInvalidUtf8Error extends Error {
|
|
3
|
+
constructor();
|
|
4
|
+
}
|
|
5
|
+
declare function decodeCsvText(bytes: Uint8Array): string;
|
|
6
|
+
declare function encodeCsvText(text: string): Uint8Array<ArrayBuffer>;
|
|
7
|
+
//#endregion
|
|
8
|
+
export { CsvInvalidUtf8Error, decodeCsvText, encodeCsvText };
|
package/dist/csv/text.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/csv/text.ts
|
|
2
|
+
var CsvInvalidUtf8Error = class extends Error {
|
|
3
|
+
constructor() {
|
|
4
|
+
super("csv text must be well-formed UTF-8");
|
|
5
|
+
this.name = "CsvInvalidUtf8Error";
|
|
6
|
+
}
|
|
7
|
+
};
|
|
8
|
+
function decodeCsvText(bytes) {
|
|
9
|
+
try {
|
|
10
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
11
|
+
} catch {
|
|
12
|
+
throw new CsvInvalidUtf8Error();
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function encodeCsvText(text) {
|
|
16
|
+
return new TextEncoder().encode(text);
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
export { CsvInvalidUtf8Error, decodeCsvText, encodeCsvText };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_csv_records = require("./records.cjs");
|
|
3
|
+
//#region src/csv/write.ts
|
|
4
|
+
var CsvUnsupportedDocumentKindError = class extends Error {
|
|
5
|
+
kind;
|
|
6
|
+
constructor(kind) {
|
|
7
|
+
super(`buildCsvText: expected a spreadsheet ContentDocument, got kind '${kind}'`);
|
|
8
|
+
this.name = "CsvUnsupportedDocumentKindError";
|
|
9
|
+
this.kind = kind;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var CsvSheetNotSpecifiedError = class extends Error {
|
|
13
|
+
availableSheets;
|
|
14
|
+
constructor(availableSheets) {
|
|
15
|
+
super(`buildCsvText: this document has more than one sheet (${availableSheets.join(", ")}) -- pass { sheet: '<name>' } to select one`);
|
|
16
|
+
this.name = "CsvSheetNotSpecifiedError";
|
|
17
|
+
this.availableSheets = availableSheets;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
var CsvSheetNotFoundError = class extends Error {
|
|
21
|
+
sheet;
|
|
22
|
+
availableSheets;
|
|
23
|
+
constructor(sheet, availableSheets) {
|
|
24
|
+
super(`buildCsvText: sheet "${sheet}" not found -- available sheet(s): ${availableSheets.length === 0 ? "(none)" : availableSheets.join(", ")}`);
|
|
25
|
+
this.name = "CsvSheetNotFoundError";
|
|
26
|
+
this.sheet = sheet;
|
|
27
|
+
this.availableSheets = availableSheets;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
function selectSheet(sheets, sheetName) {
|
|
31
|
+
const availableNames = sheets.map((candidate) => candidate.name);
|
|
32
|
+
if (sheetName !== void 0) {
|
|
33
|
+
const found = sheets.find((candidate) => candidate.name === sheetName);
|
|
34
|
+
if (found === void 0) throw new CsvSheetNotFoundError(sheetName, availableNames);
|
|
35
|
+
return found;
|
|
36
|
+
}
|
|
37
|
+
if (sheets.length === 0) throw new CsvSheetNotFoundError("(unspecified)", availableNames);
|
|
38
|
+
if (sheets.length > 1) throw new CsvSheetNotSpecifiedError(availableNames);
|
|
39
|
+
const only = sheets[0];
|
|
40
|
+
if (only === void 0) throw new CsvSheetNotFoundError("(unspecified)", availableNames);
|
|
41
|
+
return only;
|
|
42
|
+
}
|
|
43
|
+
function buildCsvText(content, options) {
|
|
44
|
+
if (content.kind !== "spreadsheet") throw new CsvUnsupportedDocumentKindError(content.kind);
|
|
45
|
+
const sheet = selectSheet(content.sheets, options?.sheet);
|
|
46
|
+
const delimiter = options?.delimiter ?? ",";
|
|
47
|
+
const fieldsByRow = /* @__PURE__ */ new Map();
|
|
48
|
+
let maxRowIndex = -1;
|
|
49
|
+
let maxColumnIndex = -1;
|
|
50
|
+
for (const cell of sheet.cells) {
|
|
51
|
+
let row = fieldsByRow.get(cell.row);
|
|
52
|
+
if (row === void 0) {
|
|
53
|
+
row = /* @__PURE__ */ new Map();
|
|
54
|
+
fieldsByRow.set(cell.row, row);
|
|
55
|
+
}
|
|
56
|
+
row.set(cell.column, cell.displayText);
|
|
57
|
+
maxRowIndex = Math.max(maxRowIndex, cell.row);
|
|
58
|
+
maxColumnIndex = Math.max(maxColumnIndex, cell.column);
|
|
59
|
+
}
|
|
60
|
+
const rowCount = Math.max(sheet.rows.length, maxRowIndex + 1);
|
|
61
|
+
const columnCount = Math.max(sheet.columns.length, maxColumnIndex + 1);
|
|
62
|
+
const lines = [];
|
|
63
|
+
for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
|
|
64
|
+
const row = fieldsByRow.get(rowIndex);
|
|
65
|
+
const fields = [];
|
|
66
|
+
for (let columnIndex = 0; columnIndex < columnCount; columnIndex++) fields.push(require_csv_records.quoteCsvField(row?.get(columnIndex) ?? "", delimiter));
|
|
67
|
+
lines.push(fields.join(delimiter));
|
|
68
|
+
}
|
|
69
|
+
return lines.length === 0 ? "" : `${lines.join("\r\n")}\r\n`;
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
72
|
+
exports.CsvSheetNotFoundError = CsvSheetNotFoundError;
|
|
73
|
+
exports.CsvSheetNotSpecifiedError = CsvSheetNotSpecifiedError;
|
|
74
|
+
exports.CsvUnsupportedDocumentKindError = CsvUnsupportedDocumentKindError;
|
|
75
|
+
exports.buildCsvText = buildCsvText;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ContentDocument } from "document-schema.js";
|
|
2
|
+
//#region src/csv/write.d.ts
|
|
3
|
+
declare class CsvUnsupportedDocumentKindError extends Error {
|
|
4
|
+
readonly kind: ContentDocument['kind'];
|
|
5
|
+
constructor(kind: ContentDocument['kind']);
|
|
6
|
+
}
|
|
7
|
+
declare class CsvSheetNotSpecifiedError extends Error {
|
|
8
|
+
readonly availableSheets: readonly string[];
|
|
9
|
+
constructor(availableSheets: readonly string[]);
|
|
10
|
+
}
|
|
11
|
+
declare class CsvSheetNotFoundError extends Error {
|
|
12
|
+
readonly sheet: string;
|
|
13
|
+
readonly availableSheets: readonly string[];
|
|
14
|
+
constructor(sheet: string, availableSheets: readonly string[]);
|
|
15
|
+
}
|
|
16
|
+
interface BuildCsvTextOptions {
|
|
17
|
+
readonly delimiter?: string;
|
|
18
|
+
readonly sheet?: string;
|
|
19
|
+
}
|
|
20
|
+
declare function buildCsvText(content: ContentDocument, options?: BuildCsvTextOptions): string;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { BuildCsvTextOptions, CsvSheetNotFoundError, CsvSheetNotSpecifiedError, CsvUnsupportedDocumentKindError, buildCsvText };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ContentDocument } from "document-schema.js";
|
|
2
|
+
//#region src/csv/write.d.ts
|
|
3
|
+
declare class CsvUnsupportedDocumentKindError extends Error {
|
|
4
|
+
readonly kind: ContentDocument['kind'];
|
|
5
|
+
constructor(kind: ContentDocument['kind']);
|
|
6
|
+
}
|
|
7
|
+
declare class CsvSheetNotSpecifiedError extends Error {
|
|
8
|
+
readonly availableSheets: readonly string[];
|
|
9
|
+
constructor(availableSheets: readonly string[]);
|
|
10
|
+
}
|
|
11
|
+
declare class CsvSheetNotFoundError extends Error {
|
|
12
|
+
readonly sheet: string;
|
|
13
|
+
readonly availableSheets: readonly string[];
|
|
14
|
+
constructor(sheet: string, availableSheets: readonly string[]);
|
|
15
|
+
}
|
|
16
|
+
interface BuildCsvTextOptions {
|
|
17
|
+
readonly delimiter?: string;
|
|
18
|
+
readonly sheet?: string;
|
|
19
|
+
}
|
|
20
|
+
declare function buildCsvText(content: ContentDocument, options?: BuildCsvTextOptions): string;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { BuildCsvTextOptions, CsvSheetNotFoundError, CsvSheetNotSpecifiedError, CsvUnsupportedDocumentKindError, buildCsvText };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { quoteCsvField } from "./records.js";
|
|
2
|
+
//#region src/csv/write.ts
|
|
3
|
+
var CsvUnsupportedDocumentKindError = class extends Error {
|
|
4
|
+
kind;
|
|
5
|
+
constructor(kind) {
|
|
6
|
+
super(`buildCsvText: expected a spreadsheet ContentDocument, got kind '${kind}'`);
|
|
7
|
+
this.name = "CsvUnsupportedDocumentKindError";
|
|
8
|
+
this.kind = kind;
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
var CsvSheetNotSpecifiedError = class extends Error {
|
|
12
|
+
availableSheets;
|
|
13
|
+
constructor(availableSheets) {
|
|
14
|
+
super(`buildCsvText: this document has more than one sheet (${availableSheets.join(", ")}) -- pass { sheet: '<name>' } to select one`);
|
|
15
|
+
this.name = "CsvSheetNotSpecifiedError";
|
|
16
|
+
this.availableSheets = availableSheets;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var CsvSheetNotFoundError = class extends Error {
|
|
20
|
+
sheet;
|
|
21
|
+
availableSheets;
|
|
22
|
+
constructor(sheet, availableSheets) {
|
|
23
|
+
super(`buildCsvText: sheet "${sheet}" not found -- available sheet(s): ${availableSheets.length === 0 ? "(none)" : availableSheets.join(", ")}`);
|
|
24
|
+
this.name = "CsvSheetNotFoundError";
|
|
25
|
+
this.sheet = sheet;
|
|
26
|
+
this.availableSheets = availableSheets;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
function selectSheet(sheets, sheetName) {
|
|
30
|
+
const availableNames = sheets.map((candidate) => candidate.name);
|
|
31
|
+
if (sheetName !== void 0) {
|
|
32
|
+
const found = sheets.find((candidate) => candidate.name === sheetName);
|
|
33
|
+
if (found === void 0) throw new CsvSheetNotFoundError(sheetName, availableNames);
|
|
34
|
+
return found;
|
|
35
|
+
}
|
|
36
|
+
if (sheets.length === 0) throw new CsvSheetNotFoundError("(unspecified)", availableNames);
|
|
37
|
+
if (sheets.length > 1) throw new CsvSheetNotSpecifiedError(availableNames);
|
|
38
|
+
const only = sheets[0];
|
|
39
|
+
if (only === void 0) throw new CsvSheetNotFoundError("(unspecified)", availableNames);
|
|
40
|
+
return only;
|
|
41
|
+
}
|
|
42
|
+
function buildCsvText(content, options) {
|
|
43
|
+
if (content.kind !== "spreadsheet") throw new CsvUnsupportedDocumentKindError(content.kind);
|
|
44
|
+
const sheet = selectSheet(content.sheets, options?.sheet);
|
|
45
|
+
const delimiter = options?.delimiter ?? ",";
|
|
46
|
+
const fieldsByRow = /* @__PURE__ */ new Map();
|
|
47
|
+
let maxRowIndex = -1;
|
|
48
|
+
let maxColumnIndex = -1;
|
|
49
|
+
for (const cell of sheet.cells) {
|
|
50
|
+
let row = fieldsByRow.get(cell.row);
|
|
51
|
+
if (row === void 0) {
|
|
52
|
+
row = /* @__PURE__ */ new Map();
|
|
53
|
+
fieldsByRow.set(cell.row, row);
|
|
54
|
+
}
|
|
55
|
+
row.set(cell.column, cell.displayText);
|
|
56
|
+
maxRowIndex = Math.max(maxRowIndex, cell.row);
|
|
57
|
+
maxColumnIndex = Math.max(maxColumnIndex, cell.column);
|
|
58
|
+
}
|
|
59
|
+
const rowCount = Math.max(sheet.rows.length, maxRowIndex + 1);
|
|
60
|
+
const columnCount = Math.max(sheet.columns.length, maxColumnIndex + 1);
|
|
61
|
+
const lines = [];
|
|
62
|
+
for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
|
|
63
|
+
const row = fieldsByRow.get(rowIndex);
|
|
64
|
+
const fields = [];
|
|
65
|
+
for (let columnIndex = 0; columnIndex < columnCount; columnIndex++) fields.push(quoteCsvField(row?.get(columnIndex) ?? "", delimiter));
|
|
66
|
+
lines.push(fields.join(delimiter));
|
|
67
|
+
}
|
|
68
|
+
return lines.length === 0 ? "" : `${lines.join("\r\n")}\r\n`;
|
|
69
|
+
}
|
|
70
|
+
//#endregion
|
|
71
|
+
export { CsvSheetNotFoundError, CsvSheetNotSpecifiedError, CsvUnsupportedDocumentKindError, buildCsvText };
|
package/dist/index.cjs
CHANGED
|
@@ -62,13 +62,17 @@ const require_odf_odp_read = require("./odf/odp/read.cjs");
|
|
|
62
62
|
const require_odf_ods_read = require("./odf/ods/read.cjs");
|
|
63
63
|
const require_odf_odg_read = require("./odf/odg/read.cjs");
|
|
64
64
|
const require_markdown_text = require("./markdown/text.cjs");
|
|
65
|
+
const require_csv_text = require("./csv/text.cjs");
|
|
66
|
+
const require_layout_cell_typing = require("./layout/cell-typing.cjs");
|
|
67
|
+
const require_csv_records = require("./csv/records.cjs");
|
|
68
|
+
const require_csv_read = require("./csv/read.cjs");
|
|
69
|
+
const require_csv_write = require("./csv/write.cjs");
|
|
65
70
|
const require_markdown_render = require("./markdown/render.cjs");
|
|
66
71
|
const require_layout_engine = require("./layout/engine.cjs");
|
|
67
72
|
const require_layout_slides = require("./layout/slides.cjs");
|
|
68
73
|
const require_ports_abort = require("./ports/abort.cjs");
|
|
69
74
|
const require_layout_sheets = require("./layout/sheets.cjs");
|
|
70
75
|
const require_layout_drawing = require("./layout/drawing.cjs");
|
|
71
|
-
const require_layout_cell_typing = require("./layout/cell-typing.cjs");
|
|
72
76
|
const require_layout_lattice = require("./layout/lattice.cjs");
|
|
73
77
|
const require_layout_reconstruct = require("./layout/reconstruct.cjs");
|
|
74
78
|
const require_hsqldb_script = require("./hsqldb/script.cjs");
|
|
@@ -313,6 +317,12 @@ Object.defineProperty(exports, "ContentVectorSchema", {
|
|
|
313
317
|
return document_schema_js.ContentVectorSchema;
|
|
314
318
|
}
|
|
315
319
|
});
|
|
320
|
+
exports.CsvBytesSchema = require_model_bytes.CsvBytesSchema;
|
|
321
|
+
exports.CsvInvalidUtf8Error = require_csv_text.CsvInvalidUtf8Error;
|
|
322
|
+
exports.CsvParseError = require_csv_records.CsvParseError;
|
|
323
|
+
exports.CsvSheetNotFoundError = require_csv_write.CsvSheetNotFoundError;
|
|
324
|
+
exports.CsvSheetNotSpecifiedError = require_csv_write.CsvSheetNotSpecifiedError;
|
|
325
|
+
exports.CsvUnsupportedDocumentKindError = require_csv_write.CsvUnsupportedDocumentKindError;
|
|
316
326
|
Object.defineProperty(exports, "DEFAULT_LAYOUT_FONT", {
|
|
317
327
|
enumerable: true,
|
|
318
328
|
get: function() {
|
|
@@ -576,6 +586,7 @@ Object.defineProperty(exports, "base64ToBytes", {
|
|
|
576
586
|
return ooxml_js.base64ToBytes;
|
|
577
587
|
}
|
|
578
588
|
});
|
|
589
|
+
exports.buildCsvText = require_csv_write.buildCsvText;
|
|
579
590
|
exports.buildDocumentBytes = require_convert_from_package.buildDocumentBytes;
|
|
580
591
|
exports.buildDocxPackage = require_edit_docx_content.buildDocxPackage;
|
|
581
592
|
exports.buildDrawingBlock = require_model_embedded_drawing.buildDrawingBlock;
|
|
@@ -682,12 +693,19 @@ Object.defineProperty(exports, "createStandardFontMeasurer", {
|
|
|
682
693
|
return pdf_codec.createStandardFontMeasurer;
|
|
683
694
|
}
|
|
684
695
|
});
|
|
696
|
+
exports.csvMarkdownCodec = require_convert_codec.csvMarkdownCodec;
|
|
697
|
+
exports.csvPdfCodec = require_convert_codec.csvPdfCodec;
|
|
698
|
+
exports.csvToMarkdown = require_convert_convert.csvToMarkdown;
|
|
699
|
+
exports.csvToOds = require_convert_convert.csvToOds;
|
|
700
|
+
exports.csvToPdf = require_convert_convert.csvToPdf;
|
|
701
|
+
exports.csvToXlsx = require_convert_convert.csvToXlsx;
|
|
685
702
|
Object.defineProperty(exports, "decodeCompactPackage", {
|
|
686
703
|
enumerable: true,
|
|
687
704
|
get: function() {
|
|
688
705
|
return ooxml_js.decodeCompactPackage;
|
|
689
706
|
}
|
|
690
707
|
});
|
|
708
|
+
exports.decodeCsvText = require_csv_text.decodeCsvText;
|
|
691
709
|
exports.decodeDocumentPackage = require_package_codec.decodeDocumentPackage;
|
|
692
710
|
Object.defineProperty(exports, "decodeEntities", {
|
|
693
711
|
enumerable: true,
|
|
@@ -751,6 +769,7 @@ Object.defineProperty(exports, "encodeCompactPackage", {
|
|
|
751
769
|
return ooxml_js.encodeCompactPackage;
|
|
752
770
|
}
|
|
753
771
|
});
|
|
772
|
+
exports.encodeCsvText = require_csv_text.encodeCsvText;
|
|
754
773
|
exports.encodeDocumentPackage = require_package_codec.encodeDocumentPackage;
|
|
755
774
|
exports.encodeMarkdownText = require_markdown_text.encodeMarkdownText;
|
|
756
775
|
Object.defineProperty(exports, "encodePackage", {
|
|
@@ -820,6 +839,7 @@ exports.mapMathVariant = require_mathml_variant.mapMathVariant;
|
|
|
820
839
|
exports.markdownDocxCodec = require_convert_codec.markdownDocxCodec;
|
|
821
840
|
exports.markdownOdtCodec = require_convert_codec.markdownOdtCodec;
|
|
822
841
|
exports.markdownPdfCodec = require_convert_codec.markdownPdfCodec;
|
|
842
|
+
exports.markdownToCsv = require_convert_convert.markdownToCsv;
|
|
823
843
|
exports.markdownToDocx = require_convert_convert.markdownToDocx;
|
|
824
844
|
exports.markdownToOdt = require_convert_convert.markdownToOdt;
|
|
825
845
|
exports.markdownToPdf = require_convert_convert.markdownToPdf;
|
|
@@ -842,7 +862,9 @@ exports.odpPptxCodec = require_convert_codec.odpPptxCodec;
|
|
|
842
862
|
exports.odpToOdt = require_convert_convert.odpToOdt;
|
|
843
863
|
exports.odpToPdf = require_convert_convert.odpToPdf;
|
|
844
864
|
exports.odpToPptx = require_convert_convert.odpToPptx;
|
|
865
|
+
exports.odsCsvCodec = require_convert_codec.odsCsvCodec;
|
|
845
866
|
exports.odsPdfCodec = require_convert_codec.odsPdfCodec;
|
|
867
|
+
exports.odsToCsv = require_convert_convert.odsToCsv;
|
|
846
868
|
exports.odsToPdf = require_convert_convert.odsToPdf;
|
|
847
869
|
exports.odsToXlsx = require_convert_convert.odsToXlsx;
|
|
848
870
|
exports.odsXlsxCodec = require_convert_codec.odsXlsxCodec;
|
|
@@ -901,6 +923,7 @@ Object.defineProperty(exports, "pdfCodec", {
|
|
|
901
923
|
return pdf_codec.pdfCodec;
|
|
902
924
|
}
|
|
903
925
|
});
|
|
926
|
+
exports.pdfToCsv = require_convert_convert.pdfToCsv;
|
|
904
927
|
exports.pdfToDocx = require_convert_convert.pdfToDocx;
|
|
905
928
|
exports.pdfToMarkdown = require_convert_convert.pdfToMarkdown;
|
|
906
929
|
exports.pdfToOdg = require_convert_convert.pdfToOdg;
|
|
@@ -919,6 +942,7 @@ Object.defineProperty(exports, "rangeReference", {
|
|
|
919
942
|
return document_schema_js.rangeReference;
|
|
920
943
|
}
|
|
921
944
|
});
|
|
945
|
+
exports.readCsvContent = require_csv_read.readCsvContent;
|
|
922
946
|
exports.readDocumentMetadata = require_metadata_read.readDocumentMetadata;
|
|
923
947
|
exports.readDocxContent = require_ooxml_docx_read.readDocxContent;
|
|
924
948
|
exports.readDocxExtras = require_ooxml_docx_extras.readDocxExtras;
|
|
@@ -1048,8 +1072,10 @@ Object.defineProperty(exports, "writePdf", {
|
|
|
1048
1072
|
return pdf_codec.writePdf;
|
|
1049
1073
|
}
|
|
1050
1074
|
});
|
|
1075
|
+
exports.xlsxCsvCodec = require_convert_codec.xlsxCsvCodec;
|
|
1051
1076
|
exports.xlsxMarkdownCodec = require_convert_codec.xlsxMarkdownCodec;
|
|
1052
1077
|
exports.xlsxPdfCodec = require_convert_codec.xlsxPdfCodec;
|
|
1078
|
+
exports.xlsxToCsv = require_convert_convert.xlsxToCsv;
|
|
1053
1079
|
exports.xlsxToMarkdown = require_convert_convert.xlsxToMarkdown;
|
|
1054
1080
|
exports.xlsxToOds = require_convert_convert.xlsxToOds;
|
|
1055
1081
|
exports.xlsxToPdf = require_convert_convert.xlsxToPdf;
|