@20baht/export 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +156 -2
- package/dist/csv.d.ts +28 -0
- package/dist/csv.js +80 -0
- package/dist/dates.d.ts +6 -0
- package/dist/dates.js +39 -0
- package/dist/download.d.ts +10 -0
- package/dist/download.js +32 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +4 -0
- package/dist/sheet.d.ts +78 -0
- package/dist/sheet.js +61 -0
- package/dist/table.d.ts +28 -0
- package/dist/table.js +57 -0
- package/dist/xlsx.d.ts +17 -0
- package/dist/xlsx.js +316 -0
- package/dist/zip.d.ts +8 -0
- package/dist/zip.js +114 -0
- package/package.json +55 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nattapong Promthong
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,157 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @20baht/export
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Rows as a file someone opens in Excel or Google Sheets: a CSV, or an .xlsx workbook. Thai comes out as Thai,
|
|
4
|
+
a cell that looks like a formula is never run, and dates and amounts keep their meaning. It works on a server or in
|
|
5
|
+
a browser, and has no dependencies.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @20baht/export
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## A sheet
|
|
12
|
+
|
|
13
|
+
Both formats read the same sheet: the columns (a header, and how a cell is read from a row) and the rows.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { csv, xlsx, type Sheet } from '@20baht/export';
|
|
17
|
+
|
|
18
|
+
interface Expense {
|
|
19
|
+
date: Date;
|
|
20
|
+
note: string;
|
|
21
|
+
amount: number;
|
|
22
|
+
account: { name: string };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const sheet: Sheet<Expense> = {
|
|
26
|
+
name: 'November', // the tab's name in a workbook
|
|
27
|
+
columns: [
|
|
28
|
+
{ label: 'Date', key: 'date', format: 'dd/mm/yyyy' },
|
|
29
|
+
{ label: 'Note', key: 'note' },
|
|
30
|
+
{ label: 'Amount', key: 'amount', format: '#,##0.00' },
|
|
31
|
+
{ label: 'Account', key: 'account.name' }, // a dotted key walks into the row
|
|
32
|
+
{ label: 'Over 1,000', value: (row) => row.amount > 1000 }, // or work the cell out
|
|
33
|
+
],
|
|
34
|
+
rows: expenses, // an array, any iterable, or an async iterable (a query read page by page)
|
|
35
|
+
};
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
A cell is a string, a number, a boolean, a `Date`, or empty (`null` / `undefined`). Anything else is made into one:
|
|
39
|
+
a list is its items joined by `, `, a bigint its digits, an object its JSON.
|
|
40
|
+
|
|
41
|
+
## CSV
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
csv(sheet); // one string
|
|
45
|
+
csv(sheet, { timeZone: 'Asia/Bangkok', delimiter: ';' });
|
|
46
|
+
csvStream(sheet); // a ReadableStream<Uint8Array>, read from the rows as the reader asks
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
What it writes, unless told otherwise:
|
|
50
|
+
|
|
51
|
+
- **RFC 4180**: records end in CRLF; a field holding a quote, the delimiter or a line break is quoted, a quote
|
|
52
|
+
inside doubled.
|
|
53
|
+
- **The UTF-8 byte order mark first** (`bom: false` to leave it out). Without it Excel reads a UTF-8 file in the
|
|
54
|
+
system's code page and Thai turns to mojibake.
|
|
55
|
+
- **No formula runs.** A text cell starting with `=`, `+`, `-`, `@`, a tab or a carriage return gets a `'` in front
|
|
56
|
+
(OWASP, CSV injection): `=HYPERLINK(…)` in a note shows as text instead of running. A number written as text
|
|
57
|
+
(`-120.50`) is left alone. `defuseFormulas: false` only for a file no spreadsheet opens.
|
|
58
|
+
- A date as `2026-11-24 09:30:00` on the clock of `timeZone` (default UTC), which Excel and Sheets read as a date.
|
|
59
|
+
A boolean as `TRUE` / `FALSE`. A column's `format` is for workbooks and is ignored here.
|
|
60
|
+
|
|
61
|
+
## Excel workbook
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
const bytes = await xlsx(sheet, { timeZone: 'Asia/Bangkok' });
|
|
65
|
+
const two = await xlsx([income, expenses]); // a tab per sheet
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
- Text is text (never a formula), a number a number, a boolean a boolean.
|
|
69
|
+
- A date is a real date cell (serial days in Excel's 1900 date system), on the clock of `timeZone`, shown in the
|
|
70
|
+
column's `format` or `yyyy-mm-dd hh:mm:ss`.
|
|
71
|
+
- A column's `format` is any Excel number format: `'#,##0.00'`, `'0%'`, `'dd/mm/yyyy'`, `'[$฿-41E]#,##0.00'`.
|
|
72
|
+
- The header row is bold, stays in view when scrolling, and carries a filter.
|
|
73
|
+
- A column is as wide as what its first 200 rows hold (8 to 60 characters; Thai vowel and tone marks count as no
|
|
74
|
+
width), or `width`.
|
|
75
|
+
- Tab names are made ones Excel takes: at most 31 characters, none of `\ / ? * [ ] :`, no two the same.
|
|
76
|
+
- The parts are deflated where the runtime has `CompressionStream('deflate-raw')` (Bun, current Node, browsers),
|
|
77
|
+
stored where it has not. The same sheets give the same bytes.
|
|
78
|
+
- Past Excel's limits (1,048,576 rows, 16,384 columns, 32,767 characters in a cell) it throws a `RangeError`
|
|
79
|
+
naming the cell, rather than write a file Excel cannot open.
|
|
80
|
+
|
|
81
|
+
It is a writer for reports and exports, not a spreadsheet library: no formulas, merged cells, colours or charts.
|
|
82
|
+
|
|
83
|
+
## A document, cell by cell
|
|
84
|
+
|
|
85
|
+
A list is rows under a header. A document (a checklist, a form to hand in) is cells put where they belong: a
|
|
86
|
+
`LaidSheet`. A workbook only; `csv()` does not take one.
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
import { xlsx, type LaidSheet } from '@20baht/export';
|
|
90
|
+
|
|
91
|
+
const head = { bold: true, shade: true, border: true, center: true };
|
|
92
|
+
const page: LaidSheet = {
|
|
93
|
+
name: 'Checklist',
|
|
94
|
+
cells: [
|
|
95
|
+
[{ value: 'Deployment Checklist', bold: true }],
|
|
96
|
+
[],
|
|
97
|
+
[{ value: 'No.', ...head }, { value: 'Plan', ...head }, { value: null, ...head }],
|
|
98
|
+
[{ value: null, ...head }, { value: 'Start', ...head }, { value: 'Finish', ...head }],
|
|
99
|
+
[{ value: 1, border: true }, { value: new Date(Date.UTC(2026, 10, 19, 13)), format: 'dd/mm/yyyy hh:mm', border: true }],
|
|
100
|
+
],
|
|
101
|
+
merges: [
|
|
102
|
+
{ row: 0, column: 0, columns: 3 }, // the title across the table
|
|
103
|
+
{ row: 2, column: 1, columns: 2 }, // "Plan" over Start and Finish
|
|
104
|
+
{ row: 2, column: 0, rows: 2 }, // "No." down both header rows
|
|
105
|
+
],
|
|
106
|
+
widths: [6, 18, 18], // characters; unset: what the column holds, merged cells aside
|
|
107
|
+
frozen: 4, // rows kept in view
|
|
108
|
+
};
|
|
109
|
+
const bytes = await xlsx([page, expenses]); // beside sheets of rows, a tab each
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
A cell is its value (`string | number | boolean | Date | null`) or `{ value, bold, shade, border, wrap, center, format }`.
|
|
113
|
+
`@20baht/sheet`'s `sheetWorkbook(sheet, data)` lays a filled sheet out this way.
|
|
114
|
+
|
|
115
|
+
## A table's columns
|
|
116
|
+
|
|
117
|
+
A table declared once with the engine's `defineTable` (the one `<baht-table>` shows) gives an export the same
|
|
118
|
+
columns: the label as the header, an option's label in place of its key.
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
import { tableColumns, xlsx } from '@20baht/export';
|
|
122
|
+
import { tickets } from './tables'; // defineTable(ticketForm)
|
|
123
|
+
|
|
124
|
+
const columns = tableColumns(tickets); // the visible columns, in order
|
|
125
|
+
const some = tableColumns(tickets, { keys: ['title', 'owner.name'], formats: { amount: '#,##0.00' } });
|
|
126
|
+
await xlsx({ name: 'Tickets', columns, rows });
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
A walked column (`owner.name`) is headed `Owner · Name`. A key the table does not have throws. The table is read
|
|
130
|
+
by its shape, so this package does not depend on `@20baht/engine`.
|
|
131
|
+
|
|
132
|
+
## A download
|
|
133
|
+
|
|
134
|
+
`download(body, filename)` is the `Response` a browser saves: Elysia, `Bun.serve`, Hono and Workers return it as
|
|
135
|
+
it is.
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import { csvStream, download, xlsx } from '@20baht/export';
|
|
139
|
+
|
|
140
|
+
app.get('/reports/expenses.csv', ({ query }) =>
|
|
141
|
+
download(csvStream({ columns, rows: expensesOf(query) }), 'ค่าใช้จ่าย พ.ย. 2569.csv'),
|
|
142
|
+
);
|
|
143
|
+
app.get('/reports/expenses.xlsx', async ({ query }) =>
|
|
144
|
+
download(await xlsx({ columns, rows: expensesOf(query) }), 'expenses.xlsx'),
|
|
145
|
+
);
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
- `Content-Disposition` names the file twice (RFC 6266, RFC 8187): in ASCII for old clients, and as written,
|
|
149
|
+
UTF-8 and %-escaped, so a Thai name arrives intact. A path or a line break in the name is dropped.
|
|
150
|
+
- `Content-Type` from the extension (`.csv`, `.xlsx`, `.json`, `.txt`; else `application/octet-stream`);
|
|
151
|
+
`Cache-Control: no-store`, so a report is not kept between users; `X-Content-Type-Options: nosniff`.
|
|
152
|
+
- A third argument is a `ResponseInit`: a status, or headers of your own.
|
|
153
|
+
|
|
154
|
+
In a browser, `URL.createObjectURL(await download(bytes, name).blob())` behind an `<a download>`.
|
|
155
|
+
|
|
156
|
+
`contentDisposition(filename)` and `contentTypeOf(filename)` are there on their own, as are `CSV_TYPE` and
|
|
157
|
+
`XLSX_TYPE`.
|
package/dist/csv.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type Sheet, type SyncSheet } from './sheet.js';
|
|
2
|
+
export interface CsvOptions {
|
|
3
|
+
/** The UTF-8 byte order mark first, so Excel reads the file as UTF-8. Default `true`. */
|
|
4
|
+
bom?: boolean;
|
|
5
|
+
/** Between fields. Default `,`. */
|
|
6
|
+
delimiter?: string;
|
|
7
|
+
/** After every record. Default `\r\n` (RFC 4180). */
|
|
8
|
+
eol?: string;
|
|
9
|
+
/** The header row first. Default `true`. */
|
|
10
|
+
header?: boolean;
|
|
11
|
+
/** The zone a date's wall clock is read in. Default `UTC`. */
|
|
12
|
+
timeZone?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Put `'` before a text cell that starts with `=`, `+`, `-`, `@`, a tab or a carriage return, so a
|
|
15
|
+
* spreadsheet shows it rather than runs it. A number written as text (`-120.50`) is left alone.
|
|
16
|
+
* Default `true`; turn it off only for a file no spreadsheet will open.
|
|
17
|
+
*/
|
|
18
|
+
defuseFormulas?: boolean;
|
|
19
|
+
}
|
|
20
|
+
/** The media type of a CSV file. */
|
|
21
|
+
export declare const CSV_TYPE = "text/csv; charset=utf-8";
|
|
22
|
+
/** The sheet as one CSV string. For rows that come page by page, `csvStream`. */
|
|
23
|
+
export declare const csv: <Row>(sheet: SyncSheet<Row>, options?: CsvOptions) => string;
|
|
24
|
+
/**
|
|
25
|
+
* The sheet as a stream of UTF-8 bytes, read from its rows as the reader asks for more: a large
|
|
26
|
+
* export goes out without being held in memory. The body of a `Response` as it is.
|
|
27
|
+
*/
|
|
28
|
+
export declare const csvStream: <Row>(sheet: Sheet<Row>, options?: CsvOptions) => ReadableStream<Uint8Array>;
|
package/dist/csv.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { checkTimeZone, dateText } from './dates.js';
|
|
2
|
+
import { cellOf, rowsOf } from './sheet.js';
|
|
3
|
+
export const CSV_TYPE = 'text/csv; charset=utf-8';
|
|
4
|
+
const BOM = '';
|
|
5
|
+
const looksLikeFormula = (value) => /^[=+\-@\t\r]/.test(value) && !/^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$/.test(value);
|
|
6
|
+
const resolve = (options) => {
|
|
7
|
+
const resolved = {
|
|
8
|
+
bom: options.bom ?? true,
|
|
9
|
+
delimiter: options.delimiter ?? ',',
|
|
10
|
+
eol: options.eol ?? '\r\n',
|
|
11
|
+
header: options.header ?? true,
|
|
12
|
+
timeZone: options.timeZone ?? 'UTC',
|
|
13
|
+
defuseFormulas: options.defuseFormulas ?? true,
|
|
14
|
+
};
|
|
15
|
+
if (resolved.delimiter.length === 0 || /["\r\n]/.test(resolved.delimiter))
|
|
16
|
+
throw new RangeError('a CSV delimiter is one or more characters, none of them a quote or a line break');
|
|
17
|
+
checkTimeZone(resolved.timeZone);
|
|
18
|
+
return resolved;
|
|
19
|
+
};
|
|
20
|
+
const field = (cell, options) => {
|
|
21
|
+
let text;
|
|
22
|
+
if (cell === null || cell === undefined)
|
|
23
|
+
text = '';
|
|
24
|
+
else if (cell instanceof Date)
|
|
25
|
+
text = dateText(cell, options.timeZone);
|
|
26
|
+
else if (typeof cell === 'number')
|
|
27
|
+
text = Number.isFinite(cell) ? String(cell) : '';
|
|
28
|
+
else if (typeof cell === 'boolean')
|
|
29
|
+
text = cell ? 'TRUE' : 'FALSE';
|
|
30
|
+
else
|
|
31
|
+
text = options.defuseFormulas && looksLikeFormula(cell) ? `'${cell}` : cell;
|
|
32
|
+
const quote = text.includes('"') ||
|
|
33
|
+
text.includes('\n') ||
|
|
34
|
+
text.includes('\r') ||
|
|
35
|
+
text.includes(options.delimiter);
|
|
36
|
+
return quote ? `"${text.replaceAll('"', '""')}"` : text;
|
|
37
|
+
};
|
|
38
|
+
const record = (cells, options) => cells.map((cell) => field(cell, options)).join(options.delimiter) + options.eol;
|
|
39
|
+
const headerOf = (columns, options) => (options.bom ? BOM : '') +
|
|
40
|
+
(options.header
|
|
41
|
+
? record(columns.map((column) => column.label), options)
|
|
42
|
+
: '');
|
|
43
|
+
const rowRecord = (columns, row, options) => record(columns.map((column) => cellOf(column, row)), options);
|
|
44
|
+
export const csv = (sheet, options = {}) => {
|
|
45
|
+
const resolved = resolve(options);
|
|
46
|
+
let out = headerOf(sheet.columns, resolved);
|
|
47
|
+
for (const row of sheet.rows)
|
|
48
|
+
out += rowRecord(sheet.columns, row, resolved);
|
|
49
|
+
return out;
|
|
50
|
+
};
|
|
51
|
+
const CHUNK = 500;
|
|
52
|
+
export const csvStream = (sheet, options = {}) => {
|
|
53
|
+
const resolved = resolve(options);
|
|
54
|
+
const encoder = new TextEncoder();
|
|
55
|
+
const rows = rowsOf(sheet.rows);
|
|
56
|
+
let started = false;
|
|
57
|
+
return new ReadableStream({
|
|
58
|
+
async pull(controller) {
|
|
59
|
+
let text = '';
|
|
60
|
+
if (!started) {
|
|
61
|
+
started = true;
|
|
62
|
+
text = headerOf(sheet.columns, resolved);
|
|
63
|
+
}
|
|
64
|
+
for (let n = 0; n < CHUNK; n++) {
|
|
65
|
+
const next = await rows.next();
|
|
66
|
+
if (next.done) {
|
|
67
|
+
if (text)
|
|
68
|
+
controller.enqueue(encoder.encode(text));
|
|
69
|
+
controller.close();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
text += rowRecord(sheet.columns, next.value, resolved);
|
|
73
|
+
}
|
|
74
|
+
controller.enqueue(encoder.encode(text));
|
|
75
|
+
},
|
|
76
|
+
async cancel() {
|
|
77
|
+
await rows.return(undefined);
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
};
|
package/dist/dates.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Throws a RangeError now when `timeZone` is not one Intl knows. */
|
|
2
|
+
export declare const checkTimeZone: (timeZone: string) => void;
|
|
3
|
+
/** `2026-11-24 09:30:00` on the zone's clock: what Excel and Sheets read as a date and time. */
|
|
4
|
+
export declare const dateText: (date: Date, timeZone: string) => string;
|
|
5
|
+
/** The instant as an Excel serial date (days since 1899-12-30, the time a fraction), on the zone's clock. */
|
|
6
|
+
export declare const serialDate: (date: Date, timeZone: string) => number;
|
package/dist/dates.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const formatters = new Map();
|
|
2
|
+
const formatterOf = (timeZone) => {
|
|
3
|
+
let found = formatters.get(timeZone);
|
|
4
|
+
if (!found) {
|
|
5
|
+
found = new Intl.DateTimeFormat('en-US', {
|
|
6
|
+
timeZone,
|
|
7
|
+
hourCycle: 'h23',
|
|
8
|
+
year: 'numeric',
|
|
9
|
+
month: '2-digit',
|
|
10
|
+
day: '2-digit',
|
|
11
|
+
hour: '2-digit',
|
|
12
|
+
minute: '2-digit',
|
|
13
|
+
second: '2-digit',
|
|
14
|
+
});
|
|
15
|
+
formatters.set(timeZone, found);
|
|
16
|
+
}
|
|
17
|
+
return found;
|
|
18
|
+
};
|
|
19
|
+
const wallClock = (date, timeZone) => {
|
|
20
|
+
const parts = {};
|
|
21
|
+
for (const part of formatterOf(timeZone).formatToParts(date))
|
|
22
|
+
if (part.type !== 'literal')
|
|
23
|
+
parts[part.type] = Number(part.value);
|
|
24
|
+
return Date.UTC(parts.year ?? 1970, (parts.month ?? 1) - 1, parts.day ?? 1, parts.hour ?? 0, parts.minute ?? 0, parts.second ?? 0, date.getUTCMilliseconds());
|
|
25
|
+
};
|
|
26
|
+
export const checkTimeZone = (timeZone) => {
|
|
27
|
+
formatterOf(timeZone);
|
|
28
|
+
};
|
|
29
|
+
const pad = (n) => String(n).padStart(2, '0');
|
|
30
|
+
export const dateText = (date, timeZone) => {
|
|
31
|
+
if (Number.isNaN(date.getTime()))
|
|
32
|
+
return '';
|
|
33
|
+
const at = new Date(wallClock(date, timeZone));
|
|
34
|
+
return (`${String(at.getUTCFullYear())}-${pad(at.getUTCMonth() + 1)}-${pad(at.getUTCDate())} ` +
|
|
35
|
+
`${pad(at.getUTCHours())}:${pad(at.getUTCMinutes())}:${pad(at.getUTCSeconds())}`);
|
|
36
|
+
};
|
|
37
|
+
const DAY = 86_400_000;
|
|
38
|
+
const EPOCH = Date.UTC(1899, 11, 30);
|
|
39
|
+
export const serialDate = (date, timeZone) => (wallClock(date, timeZone) - EPOCH) / DAY;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** `attachment; filename="report.csv"; filename*=UTF-8''…`: the name in ASCII, and as it is. */
|
|
2
|
+
export declare const contentDisposition: (filename: string) => string;
|
|
3
|
+
/** The media type for a file name's extension; `application/octet-stream` for one not known. */
|
|
4
|
+
export declare const contentTypeOf: (filename: string) => string;
|
|
5
|
+
/**
|
|
6
|
+
* The body as a download named `filename`: its type from the extension, `no-store` so a report is
|
|
7
|
+
* not kept by a cache between users, `nosniff` so it is never read as a page. `init` adds headers or
|
|
8
|
+
* sets a status.
|
|
9
|
+
*/
|
|
10
|
+
export declare const download: (body: string | Uint8Array | ReadableStream<Uint8Array>, filename: string, init?: ResponseInit) => Response;
|
package/dist/download.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { CSV_TYPE } from './csv.js';
|
|
2
|
+
import { XLSX_TYPE } from './xlsx.js';
|
|
3
|
+
const TYPES = {
|
|
4
|
+
csv: CSV_TYPE,
|
|
5
|
+
xlsx: XLSX_TYPE,
|
|
6
|
+
json: 'application/json; charset=utf-8',
|
|
7
|
+
txt: 'text/plain; charset=utf-8',
|
|
8
|
+
};
|
|
9
|
+
const safeName = (filename) => {
|
|
10
|
+
const name = (filename.split(/[\\/]/).pop() ?? '').replace(/[\u0000-\u001f\u007f]/g, '').trim();
|
|
11
|
+
return name || 'download';
|
|
12
|
+
};
|
|
13
|
+
const encoded = (name) => encodeURIComponent(name).replace(/['()*]/g, (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
14
|
+
export const contentDisposition = (filename) => {
|
|
15
|
+
const name = safeName(filename);
|
|
16
|
+
const ascii = name.replace(/[^\x20-\x7e]/g, '_').replace(/["\\]/g, '_');
|
|
17
|
+
return `attachment; filename="${ascii}"; filename*=UTF-8''${encoded(name)}`;
|
|
18
|
+
};
|
|
19
|
+
export const contentTypeOf = (filename) => {
|
|
20
|
+
const extension = /\.([^.]+)$/.exec(filename)?.[1]?.toLowerCase();
|
|
21
|
+
return (extension && TYPES[extension]) ?? 'application/octet-stream';
|
|
22
|
+
};
|
|
23
|
+
export const download = (body, filename, init = {}) => {
|
|
24
|
+
const headers = new Headers(init.headers);
|
|
25
|
+
if (!headers.has('content-type'))
|
|
26
|
+
headers.set('content-type', contentTypeOf(filename));
|
|
27
|
+
headers.set('content-disposition', contentDisposition(filename));
|
|
28
|
+
if (!headers.has('cache-control'))
|
|
29
|
+
headers.set('cache-control', 'no-store');
|
|
30
|
+
headers.set('x-content-type-options', 'nosniff');
|
|
31
|
+
return new Response(body, { ...init, headers });
|
|
32
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { csv, csvStream, CSV_TYPE, type CsvOptions } from './csv.js';
|
|
2
|
+
export { contentDisposition, contentTypeOf, download } from './download.js';
|
|
3
|
+
export type { Cell, CellLook, CellMerge, Column, LaidCell, LaidSheet, Sheet, SyncSheet, } from './sheet.js';
|
|
4
|
+
export { tableColumns, type TableColumnLike, type TableColumnsOptions, type TableLike, } from './table.js';
|
|
5
|
+
export { columnName, xlsx, XLSX_TYPE, type XlsxOptions } from './xlsx.js';
|
package/dist/index.js
ADDED
package/dist/sheet.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/** What one cell holds. `null` and `undefined` are an empty cell. */
|
|
2
|
+
export type Cell = string | number | boolean | Date | null | undefined;
|
|
3
|
+
/** One column: its header, and how its cell is read from a row. */
|
|
4
|
+
export interface Column<Row> {
|
|
5
|
+
/** The header. */
|
|
6
|
+
label: string;
|
|
7
|
+
/**
|
|
8
|
+
* The row's property the cell is. A dotted key (`owner.name`) is the row's own `owner.name` if it
|
|
9
|
+
* has one, else `row.owner.name`. With `value`, unused.
|
|
10
|
+
*/
|
|
11
|
+
key?: string;
|
|
12
|
+
/** The cell, worked out from the row. Wins over `key`. */
|
|
13
|
+
value?(row: Row): unknown;
|
|
14
|
+
/**
|
|
15
|
+
* An Excel number format for the column's numbers and dates in a workbook: `'#,##0.00'`,
|
|
16
|
+
* `'yyyy-mm-dd'`. A CSV writes values, not formats, and ignores it.
|
|
17
|
+
*/
|
|
18
|
+
format?: string;
|
|
19
|
+
/** The column's width in a workbook, in characters. Unset: as wide as what it holds (8 to 60). */
|
|
20
|
+
width?: number;
|
|
21
|
+
}
|
|
22
|
+
/** A sheet: a name (a workbook's tab), the columns and the rows. */
|
|
23
|
+
export interface Sheet<Row> {
|
|
24
|
+
/** The tab's name in a workbook. Unset: `Sheet1`, `Sheet2`, … */
|
|
25
|
+
name?: string;
|
|
26
|
+
columns: readonly Column<Row>[];
|
|
27
|
+
/** An array, any iterable, or an async iterable (a query read page by page). */
|
|
28
|
+
rows: Iterable<Row> | AsyncIterable<Row>;
|
|
29
|
+
}
|
|
30
|
+
/** How a laid-out cell looks in a workbook. */
|
|
31
|
+
export interface CellLook {
|
|
32
|
+
bold?: boolean;
|
|
33
|
+
/** A light grey fill: a header, a band. */
|
|
34
|
+
shade?: boolean;
|
|
35
|
+
/** A thin line on every side. */
|
|
36
|
+
border?: boolean;
|
|
37
|
+
/** Long text breaks onto more lines instead of running over the next cell. */
|
|
38
|
+
wrap?: boolean;
|
|
39
|
+
center?: boolean;
|
|
40
|
+
/** An Excel number format for a number or a date: `'#,##0.00'`, `'dd/mm/yyyy hh:mm'`. */
|
|
41
|
+
format?: string;
|
|
42
|
+
}
|
|
43
|
+
/** A cell of a laid-out sheet: its value alone, or with how it looks. */
|
|
44
|
+
export type LaidCell = Cell | ({
|
|
45
|
+
value: Cell;
|
|
46
|
+
} & CellLook);
|
|
47
|
+
/** Cells shown as one: from `row` and `column` (both from 0), `rows` down and `columns` across (1 when unset). */
|
|
48
|
+
export interface CellMerge {
|
|
49
|
+
row: number;
|
|
50
|
+
column: number;
|
|
51
|
+
rows?: number;
|
|
52
|
+
columns?: number;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* A sheet laid out cell by cell, for a document rather than a list: a title, labelled values, a
|
|
56
|
+
* table with a two-row header and bands across it. A workbook only; `csv()` does not take it.
|
|
57
|
+
*/
|
|
58
|
+
export interface LaidSheet {
|
|
59
|
+
/** The tab's name in a workbook. Unset: `Sheet1`, `Sheet2`, … */
|
|
60
|
+
name?: string;
|
|
61
|
+
/** The rows, top to bottom; a row is its cells, left to right. */
|
|
62
|
+
cells: readonly (readonly LaidCell[])[];
|
|
63
|
+
merges?: readonly CellMerge[];
|
|
64
|
+
/** Column widths in characters. Unset: as wide as what the column holds (8 to 60), merged cells aside. */
|
|
65
|
+
widths?: readonly (number | undefined)[];
|
|
66
|
+
/** How many rows from the top stay in view. */
|
|
67
|
+
frozen?: number;
|
|
68
|
+
}
|
|
69
|
+
/** A sheet whose rows are all at hand: what `csv()` takes. */
|
|
70
|
+
export interface SyncSheet<Row> extends Sheet<Row> {
|
|
71
|
+
rows: Iterable<Row>;
|
|
72
|
+
}
|
|
73
|
+
/** The value at `key` in a row: its own property, else walked along the dots. */
|
|
74
|
+
export declare const valueAt: (row: unknown, key: string) => unknown;
|
|
75
|
+
/** A column's cell in a row. */
|
|
76
|
+
export declare const cellOf: <Row>(column: Column<Row>, row: Row) => Cell;
|
|
77
|
+
/** The rows of a sheet, one at a time, whether they are an array or come page by page. */
|
|
78
|
+
export declare function rowsOf<Row>(rows: Iterable<Row> | AsyncIterable<Row>): AsyncGenerator<Row>;
|
package/dist/sheet.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const own = (value, key) => Object.prototype.hasOwnProperty.call(value, key)
|
|
2
|
+
? value[key]
|
|
3
|
+
: undefined;
|
|
4
|
+
export const valueAt = (row, key) => {
|
|
5
|
+
if (typeof row !== 'object' || row === null)
|
|
6
|
+
return undefined;
|
|
7
|
+
const direct = own(row, key);
|
|
8
|
+
if (direct !== undefined || !key.includes('.'))
|
|
9
|
+
return direct;
|
|
10
|
+
let at = row;
|
|
11
|
+
for (const part of key.split('.')) {
|
|
12
|
+
if (typeof at !== 'object' || at === null)
|
|
13
|
+
return undefined;
|
|
14
|
+
at = own(at, part);
|
|
15
|
+
}
|
|
16
|
+
return at;
|
|
17
|
+
};
|
|
18
|
+
const toCell = (value) => {
|
|
19
|
+
if (value === null || value === undefined)
|
|
20
|
+
return value;
|
|
21
|
+
if (value instanceof Date)
|
|
22
|
+
return value;
|
|
23
|
+
switch (typeof value) {
|
|
24
|
+
case 'string':
|
|
25
|
+
case 'number':
|
|
26
|
+
case 'boolean':
|
|
27
|
+
return value;
|
|
28
|
+
case 'bigint':
|
|
29
|
+
return value.toString();
|
|
30
|
+
case 'object':
|
|
31
|
+
return Array.isArray(value)
|
|
32
|
+
? value.map((item) => cellText(toCell(item))).join(', ')
|
|
33
|
+
: JSON.stringify(value);
|
|
34
|
+
case 'symbol':
|
|
35
|
+
case 'undefined':
|
|
36
|
+
case 'function':
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
export const cellOf = (column, row) => {
|
|
41
|
+
if (column.value)
|
|
42
|
+
return toCell(column.value(row));
|
|
43
|
+
return column.key === undefined ? undefined : toCell(valueAt(row, column.key));
|
|
44
|
+
};
|
|
45
|
+
const cellText = (cell) => {
|
|
46
|
+
if (cell === null || cell === undefined)
|
|
47
|
+
return '';
|
|
48
|
+
if (cell instanceof Date)
|
|
49
|
+
return Number.isNaN(cell.getTime()) ? '' : cell.toISOString();
|
|
50
|
+
return String(cell);
|
|
51
|
+
};
|
|
52
|
+
export async function* rowsOf(rows) {
|
|
53
|
+
if (Symbol.asyncIterator in rows) {
|
|
54
|
+
for await (const row of rows)
|
|
55
|
+
yield row;
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
for (const row of rows)
|
|
59
|
+
yield row;
|
|
60
|
+
}
|
|
61
|
+
}
|
package/dist/table.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type Column } from './sheet.js';
|
|
2
|
+
/** A column as `defineTable` declares it: the part of `TableColumn` an export reads. */
|
|
3
|
+
export interface TableColumnLike {
|
|
4
|
+
readonly label: string;
|
|
5
|
+
/** A cell's text by its value: an option's label for its key. */
|
|
6
|
+
readonly labels?: Readonly<Record<string, string>>;
|
|
7
|
+
/** The columns of the record a reference points at (`owner.name`). */
|
|
8
|
+
readonly fields?: Readonly<Record<string, TableColumnLike>>;
|
|
9
|
+
/** A heading over its fields only, never a cell. */
|
|
10
|
+
readonly group?: true;
|
|
11
|
+
}
|
|
12
|
+
/** A table as `defineTable` returns it: its columns by key, and the ones that show, in order. */
|
|
13
|
+
export interface TableLike {
|
|
14
|
+
readonly columns: Readonly<Record<string, TableColumnLike>>;
|
|
15
|
+
readonly visible?: readonly string[];
|
|
16
|
+
}
|
|
17
|
+
export interface TableColumnsOptions {
|
|
18
|
+
/** The columns, in order: a key of the table or a walked key (`owner.name`). Default: `visible`. */
|
|
19
|
+
keys?: readonly string[];
|
|
20
|
+
/** The number format of a column by key, for a workbook (`{ amount: '#,##0.00' }`). */
|
|
21
|
+
formats?: Readonly<Record<string, string>>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The columns of an export, read from a table: each `visible` column (or each of `keys`), its label as
|
|
25
|
+
* the header (a walked column's is `Owner · Name`), a choice's cell as its option's label. A group
|
|
26
|
+
* column, which is only a heading, is left out. A key the table does not have throws.
|
|
27
|
+
*/
|
|
28
|
+
export declare const tableColumns: <Row = Record<string, unknown>>(table: TableLike, options?: TableColumnsOptions) => Column<Row>[];
|
package/dist/table.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { valueAt } from './sheet.js';
|
|
2
|
+
const columnAt = (table, key) => {
|
|
3
|
+
const top = Object.prototype.hasOwnProperty.call(table.columns, key)
|
|
4
|
+
? table.columns[key]
|
|
5
|
+
: undefined;
|
|
6
|
+
if (top)
|
|
7
|
+
return { column: top, path: [top.label] };
|
|
8
|
+
const parts = key.split('.');
|
|
9
|
+
let fields = table.columns;
|
|
10
|
+
const path = [];
|
|
11
|
+
let column;
|
|
12
|
+
for (const part of parts) {
|
|
13
|
+
column =
|
|
14
|
+
fields && Object.prototype.hasOwnProperty.call(fields, part) ? fields[part] : undefined;
|
|
15
|
+
if (!column)
|
|
16
|
+
return undefined;
|
|
17
|
+
path.push(column.label);
|
|
18
|
+
fields = column.fields;
|
|
19
|
+
}
|
|
20
|
+
return column ? { column, path } : undefined;
|
|
21
|
+
};
|
|
22
|
+
export const tableColumns = (table, options = {}) => {
|
|
23
|
+
const keys = options.keys ?? table.visible ?? Object.keys(table.columns);
|
|
24
|
+
const columns = [];
|
|
25
|
+
for (const key of keys) {
|
|
26
|
+
const found = columnAt(table, key);
|
|
27
|
+
if (!found)
|
|
28
|
+
throw new RangeError(`the table has no column "${key}"`);
|
|
29
|
+
if (found.column.group)
|
|
30
|
+
continue;
|
|
31
|
+
const labels = found.column.labels;
|
|
32
|
+
const format = options.formats?.[key];
|
|
33
|
+
columns.push({
|
|
34
|
+
label: found.path.join(' · '),
|
|
35
|
+
key,
|
|
36
|
+
...(labels
|
|
37
|
+
? {
|
|
38
|
+
value: (row) => {
|
|
39
|
+
const value = valueAt(row, key);
|
|
40
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
41
|
+
const text = String(value);
|
|
42
|
+
return Object.prototype.hasOwnProperty.call(labels, text) ? labels[text] : value;
|
|
43
|
+
}
|
|
44
|
+
if (Array.isArray(value))
|
|
45
|
+
return value.map((item) => (typeof item === 'string' || typeof item === 'number') &&
|
|
46
|
+
Object.prototype.hasOwnProperty.call(labels, String(item))
|
|
47
|
+
? labels[String(item)]
|
|
48
|
+
: item);
|
|
49
|
+
return value;
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
: {}),
|
|
53
|
+
...(format === undefined ? {} : { format }),
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return columns;
|
|
57
|
+
};
|
package/dist/xlsx.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type LaidSheet, type Sheet } from './sheet.js';
|
|
2
|
+
export interface XlsxOptions {
|
|
3
|
+
/** The zone a date's wall clock is read in. Default `UTC`. */
|
|
4
|
+
timeZone?: string;
|
|
5
|
+
/** Deflate the parts where the runtime can (`CompressionStream`). Default `true`. */
|
|
6
|
+
compress?: boolean;
|
|
7
|
+
}
|
|
8
|
+
/** The media type of an .xlsx file. */
|
|
9
|
+
export declare const XLSX_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
|
10
|
+
/** `A`, `Z`, `AA`: the letters of a column counted from 0. */
|
|
11
|
+
export declare const columnName: (index: number) => string;
|
|
12
|
+
/**
|
|
13
|
+
* The sheets as the bytes of an .xlsx file. One sheet, or several (a tab each): rows under a header
|
|
14
|
+
* (`Sheet`), or cells laid out one by one (`LaidSheet`). Rows may come page by page (an async
|
|
15
|
+
* iterable); the workbook is still built whole before it is returned.
|
|
16
|
+
*/
|
|
17
|
+
export declare const xlsx: (sheets: Sheet<unknown> | LaidSheet | readonly (Sheet<unknown> | LaidSheet)[], options?: XlsxOptions) => Promise<Uint8Array>;
|
package/dist/xlsx.js
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import { checkTimeZone, serialDate } from './dates.js';
|
|
2
|
+
import { cellOf, rowsOf, } from './sheet.js';
|
|
3
|
+
import { zip } from './zip.js';
|
|
4
|
+
export const XLSX_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
|
5
|
+
const MAX_ROWS = 1_048_576;
|
|
6
|
+
const MAX_COLUMNS = 16_384;
|
|
7
|
+
const MAX_TEXT = 32_767;
|
|
8
|
+
const DATE_FORMAT = 'yyyy-mm-dd hh:mm:ss';
|
|
9
|
+
const escaped = (value) => value
|
|
10
|
+
.replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f]/g, '')
|
|
11
|
+
.replace(/[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/g, '')
|
|
12
|
+
.replaceAll('&', '&')
|
|
13
|
+
.replaceAll('<', '<')
|
|
14
|
+
.replaceAll('>', '>')
|
|
15
|
+
.replaceAll('"', '"');
|
|
16
|
+
export const columnName = (index) => {
|
|
17
|
+
let name = '';
|
|
18
|
+
for (let n = index + 1; n > 0; n = Math.floor((n - 1) / 26))
|
|
19
|
+
name = String.fromCharCode(65 + ((n - 1) % 26)) + name;
|
|
20
|
+
return name;
|
|
21
|
+
};
|
|
22
|
+
const textWidth = (text) => {
|
|
23
|
+
let width = 0;
|
|
24
|
+
for (const char of text) {
|
|
25
|
+
if (/\p{M}/u.test(char))
|
|
26
|
+
continue;
|
|
27
|
+
width += /[ᄀ-ᅟ⺀-가-힣豈-︰-﹏-⦆¢-₩\u{20000}-\u{3fffd}]/u.test(char) ? 2 : 1;
|
|
28
|
+
}
|
|
29
|
+
return width;
|
|
30
|
+
};
|
|
31
|
+
const SOLID = `<fill><patternFill patternType="solid"><fgColor rgb="FFE7E6E6"/><bgColor indexed="64"/></patternFill></fill>`;
|
|
32
|
+
const THIN = ['left', 'right', 'top', 'bottom']
|
|
33
|
+
.map((side) => `<${side} style="thin"><color auto="1"/></${side}>`)
|
|
34
|
+
.join('');
|
|
35
|
+
class Styles {
|
|
36
|
+
#formats = new Map();
|
|
37
|
+
#looks = new Map();
|
|
38
|
+
#xfs = [];
|
|
39
|
+
#shaded = false;
|
|
40
|
+
#bordered = false;
|
|
41
|
+
of(format) {
|
|
42
|
+
return this.look({ ...(format === undefined ? {} : { format }) });
|
|
43
|
+
}
|
|
44
|
+
look(look) {
|
|
45
|
+
const format = look.format === '' ? undefined : look.format;
|
|
46
|
+
const flags = [look.bold, look.shade, look.border, look.wrap, look.center]
|
|
47
|
+
.map((on) => (on === true ? '1' : '0'))
|
|
48
|
+
.join('');
|
|
49
|
+
const key = flags + (format ?? '');
|
|
50
|
+
if (key === '00000')
|
|
51
|
+
return 0;
|
|
52
|
+
if (key === '10000')
|
|
53
|
+
return 1;
|
|
54
|
+
let index = this.#looks.get(key);
|
|
55
|
+
if (index === undefined) {
|
|
56
|
+
index = this.#xfs.length + 2;
|
|
57
|
+
this.#looks.set(key, index);
|
|
58
|
+
this.#xfs.push(this.#xf(look, format));
|
|
59
|
+
}
|
|
60
|
+
return index;
|
|
61
|
+
}
|
|
62
|
+
#numFmt(format) {
|
|
63
|
+
if (format === undefined)
|
|
64
|
+
return 0;
|
|
65
|
+
let at = this.#formats.get(format);
|
|
66
|
+
if (at === undefined) {
|
|
67
|
+
at = this.#formats.size;
|
|
68
|
+
this.#formats.set(format, at);
|
|
69
|
+
}
|
|
70
|
+
return 164 + at;
|
|
71
|
+
}
|
|
72
|
+
#xf(look, format) {
|
|
73
|
+
const on = (flag, text) => (flag === true ? text : '');
|
|
74
|
+
this.#shaded ||= look.shade === true;
|
|
75
|
+
this.#bordered ||= look.border === true;
|
|
76
|
+
const aligned = look.wrap === true || look.center === true;
|
|
77
|
+
const attrs = `numFmtId="${String(this.#numFmt(format))}" fontId="${on(look.bold, '1') || '0'}"` +
|
|
78
|
+
` fillId="${on(look.shade, '2') || '0'}" borderId="${on(look.border, '1') || '0'}" xfId="0"` +
|
|
79
|
+
on(format !== undefined, ' applyNumberFormat="1"') +
|
|
80
|
+
on(look.bold, ' applyFont="1"') +
|
|
81
|
+
on(look.shade, ' applyFill="1"') +
|
|
82
|
+
on(look.border, ' applyBorder="1"') +
|
|
83
|
+
on(aligned, ' applyAlignment="1"');
|
|
84
|
+
if (!aligned)
|
|
85
|
+
return `<xf ${attrs}/>`;
|
|
86
|
+
const alignment = on(look.center, ' horizontal="center"') +
|
|
87
|
+
` vertical="${on(look.center, 'center') || 'top'}"` +
|
|
88
|
+
on(look.wrap, ' wrapText="1"');
|
|
89
|
+
return `<xf ${attrs}><alignment${alignment}/></xf>`;
|
|
90
|
+
}
|
|
91
|
+
xml() {
|
|
92
|
+
const formats = [...this.#formats.keys()];
|
|
93
|
+
const numFmts = formats.length
|
|
94
|
+
? `<numFmts count="${String(formats.length)}">${formats
|
|
95
|
+
.map((code, i) => `<numFmt numFmtId="${String(164 + i)}" formatCode="${escaped(code)}"/>`)
|
|
96
|
+
.join('')}</numFmts>`
|
|
97
|
+
: '';
|
|
98
|
+
return (`${XML}<styleSheet xmlns="${MAIN}">${numFmts}` +
|
|
99
|
+
`<fonts count="2"><font><sz val="11"/><name val="Calibri"/></font><font><b/><sz val="11"/><name val="Calibri"/></font></fonts>` +
|
|
100
|
+
`<fills count="${this.#shaded ? '3' : '2'}"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill>${this.#shaded ? SOLID : ''}</fills>` +
|
|
101
|
+
`<borders count="${this.#bordered ? '2' : '1'}"><border><left/><right/><top/><bottom/><diagonal/></border>${this.#bordered ? `<border>${THIN}<diagonal/></border>` : ''}</borders>` +
|
|
102
|
+
`<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs>` +
|
|
103
|
+
`<cellXfs count="${String(this.#xfs.length + 2)}"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>` +
|
|
104
|
+
`<xf numFmtId="0" fontId="1" fillId="0" borderId="0" xfId="0" applyFont="1"/>${this.#xfs.join('')}</cellXfs>` +
|
|
105
|
+
`<cellStyles count="1"><cellStyle name="Normal" xfId="0" builtinId="0"/></cellStyles>` +
|
|
106
|
+
`</styleSheet>`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const styleOf = (cell, plan, styles, look) => {
|
|
110
|
+
if (cell instanceof Date) {
|
|
111
|
+
if (Number.isNaN(cell.getTime()))
|
|
112
|
+
return 0;
|
|
113
|
+
return look
|
|
114
|
+
? styles.look({ ...look, format: look.format ?? DATE_FORMAT })
|
|
115
|
+
: plan.numberStyle || styles.of(DATE_FORMAT);
|
|
116
|
+
}
|
|
117
|
+
if (typeof cell === 'number') {
|
|
118
|
+
if (!Number.isFinite(cell))
|
|
119
|
+
return 0;
|
|
120
|
+
return look ? styles.look(look) : plan.numberStyle;
|
|
121
|
+
}
|
|
122
|
+
if (!look)
|
|
123
|
+
return 0;
|
|
124
|
+
const { format: _format, ...bare } = look;
|
|
125
|
+
return styles.look(bare);
|
|
126
|
+
};
|
|
127
|
+
const cellXml = (cell, ref, plan, styles, timeZone, look) => {
|
|
128
|
+
const index = styleOf(cell, plan, styles, look);
|
|
129
|
+
const s = index ? ` s="${String(index)}"` : '';
|
|
130
|
+
if (cell === null || cell === undefined || cell === '')
|
|
131
|
+
return s ? `<c r="${ref}"${s}/>` : '';
|
|
132
|
+
if (typeof cell === 'number') {
|
|
133
|
+
return Number.isFinite(cell) ? `<c r="${ref}"${s}><v>${String(cell)}</v></c>` : '';
|
|
134
|
+
}
|
|
135
|
+
if (typeof cell === 'boolean')
|
|
136
|
+
return `<c r="${ref}"${s} t="b"><v>${cell ? '1' : '0'}</v></c>`;
|
|
137
|
+
if (cell instanceof Date) {
|
|
138
|
+
if (Number.isNaN(cell.getTime()))
|
|
139
|
+
return '';
|
|
140
|
+
return `<c r="${ref}"${s}><v>${String(serialDate(cell, timeZone))}</v></c>`;
|
|
141
|
+
}
|
|
142
|
+
if (cell.length > MAX_TEXT)
|
|
143
|
+
throw new RangeError(`cell ${ref} holds ${String(cell.length)} characters; a workbook cell holds at most ${String(MAX_TEXT)}`);
|
|
144
|
+
return `<c r="${ref}"${s} t="inlineStr"><is><t xml:space="preserve">${escaped(cell)}</t></is></c>`;
|
|
145
|
+
};
|
|
146
|
+
const MEASURED_ROWS = 200;
|
|
147
|
+
const shownWidth = (cell) => {
|
|
148
|
+
if (cell === null || cell === undefined)
|
|
149
|
+
return 0;
|
|
150
|
+
if (cell instanceof Date)
|
|
151
|
+
return 19;
|
|
152
|
+
if (typeof cell === 'string')
|
|
153
|
+
return Math.max(...cell.split('\n').map(textWidth));
|
|
154
|
+
return String(cell).length;
|
|
155
|
+
};
|
|
156
|
+
const sheetXml = async (sheet, styles, timeZone) => {
|
|
157
|
+
const columns = sheet.columns;
|
|
158
|
+
if (columns.length > MAX_COLUMNS)
|
|
159
|
+
throw new RangeError(`a sheet has at most ${String(MAX_COLUMNS)} columns`);
|
|
160
|
+
const plans = columns.map((column) => ({
|
|
161
|
+
numberStyle: styles.of(column.format),
|
|
162
|
+
format: column.format,
|
|
163
|
+
width: column.width,
|
|
164
|
+
measured: textWidth(column.label),
|
|
165
|
+
}));
|
|
166
|
+
const header = `<row r="1">` +
|
|
167
|
+
columns
|
|
168
|
+
.map((column, i) => column.label
|
|
169
|
+
? `<c r="${columnName(i)}1" s="1" t="inlineStr"><is><t xml:space="preserve">${escaped(column.label)}</t></is></c>`
|
|
170
|
+
: '')
|
|
171
|
+
.join('') +
|
|
172
|
+
`</row>`;
|
|
173
|
+
const body = [];
|
|
174
|
+
let r = 1;
|
|
175
|
+
for await (const row of rowsOf(sheet.rows)) {
|
|
176
|
+
r++;
|
|
177
|
+
if (r > MAX_ROWS)
|
|
178
|
+
throw new RangeError(`a sheet has at most ${String(MAX_ROWS - 1)} rows under its header`);
|
|
179
|
+
let cells = '';
|
|
180
|
+
columns.forEach((column, i) => {
|
|
181
|
+
const cell = cellOf(column, row);
|
|
182
|
+
const plan = plans[i];
|
|
183
|
+
if (r <= MEASURED_ROWS + 1)
|
|
184
|
+
plan.measured = Math.max(plan.measured, shownWidth(cell));
|
|
185
|
+
cells += cellXml(cell, `${columnName(i)}${String(r)}`, plan, styles, timeZone);
|
|
186
|
+
});
|
|
187
|
+
body.push(`<row r="${String(r)}">${cells}</row>`);
|
|
188
|
+
}
|
|
189
|
+
const cols = plans
|
|
190
|
+
.map((plan, i) => {
|
|
191
|
+
const n = String(i + 1);
|
|
192
|
+
const width = plan.width ?? Math.min(60, Math.max(8, plan.measured + 2));
|
|
193
|
+
return `<col min="${n}" max="${n}" width="${String(width)}" customWidth="1"/>`;
|
|
194
|
+
})
|
|
195
|
+
.join('');
|
|
196
|
+
const last = `${columnName(Math.max(0, columns.length - 1))}${String(r)}`;
|
|
197
|
+
return (`${XML}<worksheet xmlns="${MAIN}">` +
|
|
198
|
+
`<sheetViews><sheetView workbookViewId="0"><pane ySplit="1" topLeftCell="A2" activePane="bottomLeft" state="frozen"/></sheetView></sheetViews>` +
|
|
199
|
+
`<sheetFormatPr defaultRowHeight="15"/>` +
|
|
200
|
+
(cols ? `<cols>${cols}</cols>` : '') +
|
|
201
|
+
`<sheetData>${header}${body.join('')}</sheetData>` +
|
|
202
|
+
(columns.length ? `<autoFilter ref="A1:${last}"/>` : '') +
|
|
203
|
+
`</worksheet>`);
|
|
204
|
+
};
|
|
205
|
+
const isLooked = (cell) => typeof cell === 'object' && cell !== null && !(cell instanceof Date);
|
|
206
|
+
const span = (n) => Math.max(1, Math.trunc(n ?? 1));
|
|
207
|
+
const laidXml = (sheet, styles, timeZone) => {
|
|
208
|
+
if (sheet.cells.length > MAX_ROWS)
|
|
209
|
+
throw new RangeError(`a sheet has at most ${String(MAX_ROWS)} rows`);
|
|
210
|
+
const merges = (sheet.merges ?? []).filter((m) => span(m.rows) > 1 || span(m.columns) > 1);
|
|
211
|
+
const across = new Set(merges.filter((m) => span(m.columns) > 1).map((m) => `${String(m.row)}:${String(m.column)}`));
|
|
212
|
+
const measured = [];
|
|
213
|
+
const rows = sheet.cells.map((row, r) => {
|
|
214
|
+
if (row.length > MAX_COLUMNS)
|
|
215
|
+
throw new RangeError(`a sheet has at most ${String(MAX_COLUMNS)} columns`);
|
|
216
|
+
const cells = row.map((cell, c) => {
|
|
217
|
+
const { value, ...look } = isLooked(cell) ? cell : { value: cell };
|
|
218
|
+
if (!across.has(`${String(r)}:${String(c)}`))
|
|
219
|
+
measured[c] = Math.max(measured[c] ?? 0, shownWidth(value));
|
|
220
|
+
return cellXml(value, `${columnName(c)}${String(r + 1)}`, { numberStyle: 0 }, styles, timeZone, look);
|
|
221
|
+
});
|
|
222
|
+
return `<row r="${String(r + 1)}">${cells.join('')}</row>`;
|
|
223
|
+
});
|
|
224
|
+
const cols = Array.from(measured, (width, i) => {
|
|
225
|
+
const n = String(i + 1);
|
|
226
|
+
const shown = sheet.widths?.[i] ?? Math.min(60, Math.max(8, width + 2));
|
|
227
|
+
return `<col min="${n}" max="${n}" width="${String(shown)}" customWidth="1"/>`;
|
|
228
|
+
}).join('');
|
|
229
|
+
const frozen = Math.max(0, Math.trunc(sheet.frozen ?? 0));
|
|
230
|
+
const pane = frozen
|
|
231
|
+
? `<pane ySplit="${String(frozen)}" topLeftCell="A${String(frozen + 1)}" activePane="bottomLeft" state="frozen"/>`
|
|
232
|
+
: '';
|
|
233
|
+
const merged = merges
|
|
234
|
+
.map((m) => {
|
|
235
|
+
const from = `${columnName(m.column)}${String(m.row + 1)}`;
|
|
236
|
+
const to = `${columnName(m.column + span(m.columns) - 1)}${String(m.row + span(m.rows))}`;
|
|
237
|
+
return `<mergeCell ref="${from}:${to}"/>`;
|
|
238
|
+
})
|
|
239
|
+
.join('');
|
|
240
|
+
return (`${XML}<worksheet xmlns="${MAIN}">` +
|
|
241
|
+
`<sheetViews><sheetView workbookViewId="0">${pane}</sheetView></sheetViews>` +
|
|
242
|
+
`<sheetFormatPr defaultRowHeight="15"/>` +
|
|
243
|
+
(cols ? `<cols>${cols}</cols>` : '') +
|
|
244
|
+
`<sheetData>${rows.join('')}</sheetData>` +
|
|
245
|
+
(merged ? `<mergeCells count="${String(merges.length)}">${merged}</mergeCells>` : '') +
|
|
246
|
+
`</worksheet>`);
|
|
247
|
+
};
|
|
248
|
+
const sheetNames = (sheets) => {
|
|
249
|
+
const taken = new Set();
|
|
250
|
+
return sheets.map((sheet, i) => {
|
|
251
|
+
let base = (sheet.name ?? '')
|
|
252
|
+
.replace(/[\\/?*[\]:]/g, ' ')
|
|
253
|
+
.replace(/^'+|'+$/g, '')
|
|
254
|
+
.trim()
|
|
255
|
+
.slice(0, 31);
|
|
256
|
+
if (!base)
|
|
257
|
+
base = `Sheet${String(i + 1)}`;
|
|
258
|
+
if (base.toLowerCase() === 'history')
|
|
259
|
+
base = `${base} 1`;
|
|
260
|
+
let name = base;
|
|
261
|
+
for (let n = 2; taken.has(name.toLowerCase()); n++) {
|
|
262
|
+
const suffix = ` (${String(n)})`;
|
|
263
|
+
name = base.slice(0, 31 - suffix.length) + suffix;
|
|
264
|
+
}
|
|
265
|
+
taken.add(name.toLowerCase());
|
|
266
|
+
return name;
|
|
267
|
+
});
|
|
268
|
+
};
|
|
269
|
+
const XML = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`;
|
|
270
|
+
const MAIN = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main';
|
|
271
|
+
const REL = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships';
|
|
272
|
+
const PKG = 'http://schemas.openxmlformats.org/package/2006';
|
|
273
|
+
const PART = 'application/vnd.openxmlformats-officedocument.spreadsheetml';
|
|
274
|
+
export const xlsx = async (sheets, options = {}) => {
|
|
275
|
+
const list = Array.isArray(sheets)
|
|
276
|
+
? sheets
|
|
277
|
+
: [sheets];
|
|
278
|
+
if (list.length === 0)
|
|
279
|
+
throw new RangeError('a workbook has at least one sheet');
|
|
280
|
+
const timeZone = options.timeZone ?? 'UTC';
|
|
281
|
+
checkTimeZone(timeZone);
|
|
282
|
+
const styles = new Styles();
|
|
283
|
+
const names = sheetNames(list);
|
|
284
|
+
const parts = [];
|
|
285
|
+
for (const sheet of list) {
|
|
286
|
+
parts.push('cells' in sheet ? laidXml(sheet, styles, timeZone) : await sheetXml(sheet, styles, timeZone));
|
|
287
|
+
}
|
|
288
|
+
const ids = list.map((_, i) => String(i + 1));
|
|
289
|
+
const encoder = new TextEncoder();
|
|
290
|
+
const file = (path, text) => ({ path, data: encoder.encode(text) });
|
|
291
|
+
return zip([
|
|
292
|
+
file('[Content_Types].xml', `${XML}<Types xmlns="${PKG}/content-types">` +
|
|
293
|
+
`<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>` +
|
|
294
|
+
`<Default Extension="xml" ContentType="application/xml"/>` +
|
|
295
|
+
`<Override PartName="/xl/workbook.xml" ContentType="${PART}.sheet.main+xml"/>` +
|
|
296
|
+
`<Override PartName="/xl/styles.xml" ContentType="${PART}.styles+xml"/>` +
|
|
297
|
+
ids
|
|
298
|
+
.map((id) => `<Override PartName="/xl/worksheets/sheet${id}.xml" ContentType="${PART}.worksheet+xml"/>`)
|
|
299
|
+
.join('') +
|
|
300
|
+
`</Types>`),
|
|
301
|
+
file('_rels/.rels', `${XML}<Relationships xmlns="${PKG}/relationships"><Relationship Id="rId1" Type="${REL}/officeDocument" Target="xl/workbook.xml"/></Relationships>`),
|
|
302
|
+
file('xl/workbook.xml', `${XML}<workbook xmlns="${MAIN}" xmlns:r="${REL}"><bookViews><workbookView/></bookViews><sheets>` +
|
|
303
|
+
ids
|
|
304
|
+
.map((id, i) => `<sheet name="${escaped(names[i] ?? '')}" sheetId="${id}" r:id="rId${id}"/>`)
|
|
305
|
+
.join('') +
|
|
306
|
+
`</sheets></workbook>`),
|
|
307
|
+
file('xl/_rels/workbook.xml.rels', `${XML}<Relationships xmlns="${PKG}/relationships">` +
|
|
308
|
+
ids
|
|
309
|
+
.map((id) => `<Relationship Id="rId${id}" Type="${REL}/worksheet" Target="worksheets/sheet${id}.xml"/>`)
|
|
310
|
+
.join('') +
|
|
311
|
+
`<Relationship Id="rId${String(list.length + 1)}" Type="${REL}/styles" Target="styles.xml"/>` +
|
|
312
|
+
`</Relationships>`),
|
|
313
|
+
file('xl/styles.xml', styles.xml()),
|
|
314
|
+
...parts.map((text, i) => file(`xl/worksheets/sheet${ids[i] ?? ''}.xml`, text)),
|
|
315
|
+
], options.compress ?? true);
|
|
316
|
+
};
|
package/dist/zip.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** CRC-32 (ISO 3309), the checksum a zip keeps of every file. */
|
|
2
|
+
export declare const crc32: (bytes: Uint8Array) => number;
|
|
3
|
+
export interface ZipFile {
|
|
4
|
+
path: string;
|
|
5
|
+
data: Uint8Array;
|
|
6
|
+
}
|
|
7
|
+
/** The files as one zip archive. */
|
|
8
|
+
export declare const zip: (files: readonly ZipFile[], compress: boolean) => Promise<Uint8Array>;
|
package/dist/zip.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
const CRC_TABLE = Uint32Array.from({ length: 256 }, (_, n) => {
|
|
2
|
+
let c = n;
|
|
3
|
+
for (let k = 0; k < 8; k++)
|
|
4
|
+
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
5
|
+
return c >>> 0;
|
|
6
|
+
});
|
|
7
|
+
export const crc32 = (bytes) => {
|
|
8
|
+
let crc = 0xffffffff;
|
|
9
|
+
for (const byte of bytes)
|
|
10
|
+
crc = (CRC_TABLE[(crc ^ byte) & 0xff] ?? 0) ^ (crc >>> 8);
|
|
11
|
+
return (crc ^ 0xffffffff) >>> 0;
|
|
12
|
+
};
|
|
13
|
+
const concat = (parts) => {
|
|
14
|
+
const out = new Uint8Array(parts.reduce((n, part) => n + part.length, 0));
|
|
15
|
+
let at = 0;
|
|
16
|
+
for (const part of parts) {
|
|
17
|
+
out.set(part, at);
|
|
18
|
+
at += part.length;
|
|
19
|
+
}
|
|
20
|
+
return out;
|
|
21
|
+
};
|
|
22
|
+
const deflateRaw = async (bytes) => {
|
|
23
|
+
let stream;
|
|
24
|
+
try {
|
|
25
|
+
stream = new CompressionStream('deflate-raw');
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
const writer = stream.writable.getWriter();
|
|
31
|
+
const writing = writer.write(bytes).then(() => writer.close());
|
|
32
|
+
const chunks = [];
|
|
33
|
+
const reader = stream.readable.getReader();
|
|
34
|
+
for (;;) {
|
|
35
|
+
const { done, value } = await reader.read();
|
|
36
|
+
if (done)
|
|
37
|
+
break;
|
|
38
|
+
chunks.push(value);
|
|
39
|
+
}
|
|
40
|
+
await writing;
|
|
41
|
+
return concat(chunks);
|
|
42
|
+
};
|
|
43
|
+
class Writer {
|
|
44
|
+
#parts = [];
|
|
45
|
+
#length = 0;
|
|
46
|
+
get length() {
|
|
47
|
+
return this.#length;
|
|
48
|
+
}
|
|
49
|
+
u16(n) {
|
|
50
|
+
return this.bytes(Uint8Array.of(n & 0xff, (n >>> 8) & 0xff));
|
|
51
|
+
}
|
|
52
|
+
u32(n) {
|
|
53
|
+
return this.u16(n & 0xffff).u16(n >>> 16);
|
|
54
|
+
}
|
|
55
|
+
bytes(bytes) {
|
|
56
|
+
this.#parts.push(bytes);
|
|
57
|
+
this.#length += bytes.length;
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
done() {
|
|
61
|
+
return concat(this.#parts);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const DOS_TIME = 0;
|
|
65
|
+
const DOS_DATE = (0 << 9) | (1 << 5) | 1;
|
|
66
|
+
const UTF8 = 0x0800;
|
|
67
|
+
const LIMIT = 0xffffffff;
|
|
68
|
+
export const zip = async (files, compress) => {
|
|
69
|
+
const encoder = new TextEncoder();
|
|
70
|
+
const out = new Writer();
|
|
71
|
+
const central = new Writer();
|
|
72
|
+
for (const file of files) {
|
|
73
|
+
const name = encoder.encode(file.path);
|
|
74
|
+
const deflated = compress ? await deflateRaw(file.data) : undefined;
|
|
75
|
+
const stored = !deflated || deflated.length >= file.data.length;
|
|
76
|
+
const body = stored ? file.data : deflated;
|
|
77
|
+
const crc = crc32(file.data);
|
|
78
|
+
const offset = out.length;
|
|
79
|
+
if (offset + body.length > LIMIT)
|
|
80
|
+
throw new RangeError('a workbook past 4 GB cannot be written');
|
|
81
|
+
const fields = (w) => w
|
|
82
|
+
.u16(20)
|
|
83
|
+
.u16(UTF8)
|
|
84
|
+
.u16(stored ? 0 : 8)
|
|
85
|
+
.u16(DOS_TIME)
|
|
86
|
+
.u16(DOS_DATE)
|
|
87
|
+
.u32(crc)
|
|
88
|
+
.u32(body.length)
|
|
89
|
+
.u32(file.data.length)
|
|
90
|
+
.u16(name.length)
|
|
91
|
+
.u16(0);
|
|
92
|
+
fields(out.u32(0x04034b50)).bytes(name).bytes(body);
|
|
93
|
+
fields(central.u32(0x02014b50).u16(20))
|
|
94
|
+
.u16(0)
|
|
95
|
+
.u16(0)
|
|
96
|
+
.u16(0)
|
|
97
|
+
.u32(0)
|
|
98
|
+
.u32(offset)
|
|
99
|
+
.bytes(name);
|
|
100
|
+
}
|
|
101
|
+
const directory = central.done();
|
|
102
|
+
const start = out.length;
|
|
103
|
+
return out
|
|
104
|
+
.bytes(directory)
|
|
105
|
+
.u32(0x06054b50)
|
|
106
|
+
.u16(0)
|
|
107
|
+
.u16(0)
|
|
108
|
+
.u16(files.length)
|
|
109
|
+
.u16(files.length)
|
|
110
|
+
.u32(directory.length)
|
|
111
|
+
.u32(start)
|
|
112
|
+
.u16(0)
|
|
113
|
+
.done();
|
|
114
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@20baht/export",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"engines": {
|
|
6
|
+
"node": ">=20.19.0"
|
|
7
|
+
},
|
|
8
|
+
"description": "Rows as a CSV or an .xlsx file, on a server or in a browser: Thai that opens in Excel, no formula a cell can smuggle in, dates and number formats, a table's columns as the header, and a sheet laid out cell by cell (merged cells, borders) for a document. No dependencies.",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"author": "Nattapong Promthong",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/bank20baht/20baht.git",
|
|
14
|
+
"directory": "packages/core/export"
|
|
15
|
+
},
|
|
16
|
+
"homepage": "https://github.com/bank20baht/20baht/tree/main/packages/core/export#readme",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/bank20baht/20baht/issues"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"csv",
|
|
22
|
+
"xlsx",
|
|
23
|
+
"excel",
|
|
24
|
+
"export",
|
|
25
|
+
"spreadsheet",
|
|
26
|
+
"download",
|
|
27
|
+
"report"
|
|
28
|
+
],
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"types": "./dist/index.d.ts",
|
|
38
|
+
"import": "./dist/index.js",
|
|
39
|
+
"default": "./dist/index.js"
|
|
40
|
+
},
|
|
41
|
+
"./package.json": "./package.json"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "rm -rf dist && tsc -p tsconfig.json --removeComments && tsc -p tsconfig.json --emitDeclarationOnly",
|
|
45
|
+
"test": "vitest run",
|
|
46
|
+
"test:watch": "vitest",
|
|
47
|
+
"test:coverage": "vitest run --coverage"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@20baht/engine": "workspace:*",
|
|
51
|
+
"typescript": "^5.9.3",
|
|
52
|
+
"vitest": "^3.2.4"
|
|
53
|
+
},
|
|
54
|
+
"main": "./dist/index.js",
|
|
55
|
+
"types": "./dist/index.d.ts",
|
|
56
|
+
"sideEffects": false
|
|
57
|
+
}
|