@nouxwell/tabula-core 0.0.0-stage → 0.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 (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +37 -2
  3. package/dist/adjust.d.ts +35 -0
  4. package/dist/async.d.ts +7 -0
  5. package/dist/changes.d.ts +63 -0
  6. package/dist/checks.d.ts +83 -0
  7. package/dist/clipboard.d.ts +19 -0
  8. package/dist/columns.d.ts +121 -0
  9. package/dist/contract.d.ts +157 -0
  10. package/dist/csv.d.ts +32 -0
  11. package/dist/decimal.d.ts +17 -0
  12. package/dist/draft.d.ts +170 -0
  13. package/dist/errors.d.ts +29 -0
  14. package/dist/fill.d.ts +35 -0
  15. package/dist/filters.d.ts +91 -0
  16. package/dist/find.d.ts +49 -0
  17. package/dist/formula/assist.d.ts +62 -0
  18. package/dist/formula/decimal.d.ts +35 -0
  19. package/dist/formula/engine.d.ts +44 -0
  20. package/dist/formula/error.d.ts +13 -0
  21. package/dist/formula/evaluate.d.ts +52 -0
  22. package/dist/formula/parse.d.ts +48 -0
  23. package/dist/history.d.ts +78 -0
  24. package/dist/ids.d.ts +7 -0
  25. package/dist/index.d.ts +79 -0
  26. package/dist/index.js +6203 -0
  27. package/dist/index.js.map +1 -0
  28. package/dist/listeners.d.ts +14 -0
  29. package/dist/lookup.d.ts +103 -0
  30. package/dist/marks.d.ts +68 -0
  31. package/dist/rows.d.ts +152 -0
  32. package/dist/saver.d.ts +72 -0
  33. package/dist/schema.d.ts +191 -0
  34. package/dist/selection.d.ts +58 -0
  35. package/dist/session.d.ts +217 -0
  36. package/dist/styles.d.ts +60 -0
  37. package/dist/summary.d.ts +29 -0
  38. package/dist/table.d.ts +163 -0
  39. package/dist/templates.d.ts +89 -0
  40. package/dist/validation.d.ts +117 -0
  41. package/dist/value.d.ts +23 -0
  42. package/dist/values/date.d.ts +24 -0
  43. package/dist/values/format.d.ts +21 -0
  44. package/dist/values/number.d.ts +33 -0
  45. package/dist/values/read.d.ts +61 -0
  46. package/dist/values/settings.d.ts +36 -0
  47. package/dist/values/text.d.ts +15 -0
  48. package/dist/view.d.ts +66 -0
  49. package/dist/views.d.ts +96 -0
  50. package/dist/workbook.d.ts +91 -0
  51. package/package.json +41 -3
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 IONSIS (https://ionsis.com)
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,38 @@
1
- # Temporary Holding Version
1
+ # @nouxwell/tabula-core
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The framework-free core of [Tabula Grid](https://github.com/nouxwell/tabula-grid), an Excel-like
4
+ workspace for preparing, validating and processing bulk data in ERP and CRM screens. No Vue and no
5
+ DOM: it runs in a browser, in a worker and in Node alike.
6
+
7
+ ```bash
8
+ npm install @nouxwell/tabula-core
9
+ ```
10
+
11
+ - **`ModuleSchema`** reads the schema a backend sends and refuses what it does not know.
12
+ - **`RowStore`** holds the rows under ids that never change, every change a transaction with a
13
+ revision; **`UndoHistory`** takes changes back.
14
+ - **`readInput`** and **`formatCell`** read and write values the way
15
+ [nouxwell/tabula](https://github.com/nouxwell/tabula) reads an imported file: "1.234,56",
16
+ "31.12.2026" and "Evet" with Turkish settings. A cell holds decimal text, ISO dates and booleans.
17
+ - **`SheetValidator`** checks the schema's rules as the user types; **`SheetSession`** takes the
18
+ sheet to the backend's pre-flight check and run, through adapters the application gives it.
19
+ - **`LookupResolver`**, **`FormulaEngine`**, **`DraftSaver`**, CSV import and export, and the
20
+ clipboard's TSV.
21
+
22
+ ```ts
23
+ import { ModuleSchema, RowStore, SheetValidator, VALUE_SETTINGS } from "@nouxwell/tabula-core"
24
+
25
+ const schema = ModuleSchema.from(await (await fetch("/api/modules/product.price_update")).json())
26
+ const store = new RowStore({ rows: [{ values: { newPrice: "389.90" } }] })
27
+ const validator = new SheetValidator(schema, store, VALUE_SETTINGS.tr)
28
+
29
+ validator.summary() // { errors, warnings, stale, rows }
30
+ ```
31
+
32
+ The grid that draws it is [@nouxwell/tabula-vue](https://www.npmjs.com/package/@nouxwell/tabula-vue).
33
+ Everything is explained in the [repository's README](https://github.com/nouxwell/tabula-grid#readme);
34
+ what each version changed is in its [changelog](https://github.com/nouxwell/tabula-grid/blob/main/CHANGELOG.md).
35
+
36
+ ## License
37
+
38
+ MIT
@@ -0,0 +1,35 @@
1
+ import type { FieldSchema } from "./schema.js";
2
+ import type { CellValue } from "./value.js";
3
+ import { type ValueSettings } from "./values/settings.js";
4
+ /**
5
+ * A change made to many numbers at once, as a price list is raised. Amounts are decimal text, so a
6
+ * rise of 5% is worked out exactly.
7
+ *
8
+ * - `percent`: `by` percent more: "5" raises by 5%, "-10" takes a tenth off.
9
+ * - `add`: `by` more: "10", "-2.5".
10
+ * - `round`: to a multiple of `step` ("0.05", "1", "10"): the nearest (`half`, the default, with a
11
+ * five going away from zero), the one away from zero (`up`), or the one towards it (`down`).
12
+ */
13
+ export type Adjustment = {
14
+ readonly kind: "percent";
15
+ readonly by: string;
16
+ } | {
17
+ readonly kind: "add";
18
+ readonly by: string;
19
+ } | {
20
+ readonly kind: "round";
21
+ readonly step: string;
22
+ readonly mode?: "half" | "up" | "down";
23
+ };
24
+ /** Whether the field holds numbers an adjustment can change. */
25
+ export declare function isAdjustable(field: FieldSchema): boolean;
26
+ /**
27
+ * The value a number cell holds after `adjustment`, as decimal text with no more digits after the
28
+ * point than the field shows (rounded half away from zero). Null when there is nothing to change:
29
+ * an empty cell, one that holds no number, a field that does not hold numbers, or a value the
30
+ * adjustment leaves as it was.
31
+ *
32
+ * @throws TabulaError `invalid_argument` for an amount that is not decimal text, or a step that is
33
+ * not above zero
34
+ */
35
+ export declare function adjustValue(field: FieldSchema, value: CellValue, adjustment: Adjustment, settings?: ValueSettings): string | null;
@@ -0,0 +1,7 @@
1
+ /** Settles as the promise does, or rejects once the signal aborts, whichever comes first. */
2
+ export declare function raced<T>(promise: Promise<T>, signal: AbortSignal): Promise<T>;
3
+ /** Resolves after `ms`, or rejects once the signal aborts. */
4
+ export declare function wait(ms: number, signal: AbortSignal): Promise<void>;
5
+ export declare function abortError(): Error;
6
+ /** A signal that never aborts, for a request nobody will cancel. */
7
+ export declare function neverAborted(): AbortSignal;
@@ -0,0 +1,63 @@
1
+ import type { RowId, RowInit, RowStore } from "./rows.js";
2
+ import type { ModuleSchema } from "./schema.js";
3
+ import type { RowFilter } from "./view.js";
4
+ import { type ValueSettings } from "./values/settings.js";
5
+ export interface SheetChangesOptions {
6
+ readonly schema: ModuleSchema;
7
+ readonly store: RowStore;
8
+ /**
9
+ * The rows as they were loaded, to compare the sheet with: the backend's rows, say, when a draft
10
+ * is opened over them. Left out, the store as it is now.
11
+ */
12
+ readonly baseline?: readonly RowInit[];
13
+ /**
14
+ * The field that names a row in the baseline and in the sheet alike, such as its product: rows
15
+ * are matched by it. Left out, rows are matched by their ids, which the baseline must then carry.
16
+ */
17
+ readonly key?: string;
18
+ readonly settings?: ValueSettings;
19
+ }
20
+ export interface ChangeSummary {
21
+ /** Rows with a change, the added ones among them. */
22
+ readonly rows: number;
23
+ /** Cells holding other than they did. */
24
+ readonly cells: number;
25
+ /** Rows the baseline did not have. */
26
+ readonly added: number;
27
+ /** Rows of the baseline the sheet no longer has. */
28
+ readonly missing: number;
29
+ }
30
+ /**
31
+ * What the user changed since the sheet was loaded: each cell that holds other than it did, and each
32
+ * row added since. Only fields that are sent are followed; a column worked out from others, or one
33
+ * shown for reference, moves with them and has no change of its own.
34
+ *
35
+ * Values are compared as the backend would read them: 12.5 and "12.50" are the same, a record is its
36
+ * id, a multi-select's options count in any order. A row whose key changes is another row: an added
37
+ * one, while the one it was goes missing. Taking a change back takes its mark away.
38
+ */
39
+ export declare class SheetChanges {
40
+ #private;
41
+ readonly schema: ModuleSchema;
42
+ readonly store: RowStore;
43
+ constructor(options: SheetChangesOptions);
44
+ /** Moves on every change of what is changed. */
45
+ get version(): number;
46
+ /** Whether the cell holds other than it did, or is in a row added since. */
47
+ isChanged(rowId: RowId, field: string): boolean;
48
+ /** Whether anything in the row changed, or the row was added. */
49
+ hasChanges(rowId: RowId): boolean;
50
+ /** Whether the baseline had no such row. */
51
+ isAdded(rowId: RowId): boolean;
52
+ /** The rows with a change, in sheet order. */
53
+ changedRows(): RowId[];
54
+ summary(): ChangeSummary;
55
+ /** Takes the sheet as it is now as what it is compared with, such as after its changes were sent. */
56
+ reset(): void;
57
+ /** Called after each change of what is changed. Returns the way to stop. */
58
+ subscribe(listener: (changes: SheetChanges) => void): () => void;
59
+ /** Stops following the store. */
60
+ dispose(): void;
61
+ }
62
+ /** The rows with a change since the baseline, the added ones among them. */
63
+ export declare function rowsChanged(changes: SheetChanges): RowFilter;
@@ -0,0 +1,83 @@
1
+ import type { RowId, RowStore } from "./rows.js";
2
+ import type { FieldSchema, ModuleSchema } from "./schema.js";
3
+ import type { CellValue } from "./value.js";
4
+ import type { ValueSettings } from "./values/settings.js";
5
+ /**
6
+ * Row checks: rules about a row rather than one cell, as a module schema or a template writes them.
7
+ * "Not is required when Aktif is no", "Yeni fiyat cannot be below Mevcut fiyat", "Geçerlilik cannot
8
+ * be before 2026-12-01 when Para birimi is USD".
9
+ *
10
+ * A check says what must hold for one field of the row (`assert`), and when (`when`: every condition
11
+ * holding, or always when there is none). What it asserts is a condition too: the field filled,
12
+ * empty, or compared with a value or with another field of the row. Its issue lands on that field's
13
+ * cell, as an error that keeps the row from being processed or as a warning that does not.
14
+ */
15
+ export declare const COMPARISONS: readonly ["=", "!=", "<", "<=", ">", ">="];
16
+ export type Comparison = (typeof COMPARISONS)[number];
17
+ export declare const CONDITION_OPS: readonly ["=", "!=", "<", "<=", ">", ">=", "filled", "empty"];
18
+ export type ConditionOp = (typeof CONDITION_OPS)[number];
19
+ /** One thing about one field of a row. */
20
+ export interface RowCondition {
21
+ readonly field: string;
22
+ readonly op: ConditionOp;
23
+ /** What a comparison compares with: a value in the form a cell holds it, or null… */
24
+ readonly value: CellValue;
25
+ /** …or another field of the row, by key. */
26
+ readonly other: string | null;
27
+ }
28
+ export type CheckSeverity = "error" | "warning";
29
+ export interface RowCheck {
30
+ /** Names the check: its issues' ids, and an editor's list. Unique in the schema with its template. */
31
+ readonly id: string;
32
+ /** What must hold for the row. The issue lands on this field's cell. */
33
+ readonly assert: RowCondition;
34
+ /** When the check applies: every condition holding. Empty: always. */
35
+ readonly when: readonly RowCondition[];
36
+ readonly severity: CheckSeverity;
37
+ /** The words to say, by language, such as `{ "tr": "…", "en": "…" }`; null leaves them to the grid. */
38
+ readonly message: Readonly<Record<string, string>> | null;
39
+ }
40
+ /** A condition as it is written in the JSON. */
41
+ export interface ConditionInput {
42
+ field: string;
43
+ op: ConditionOp;
44
+ value?: string | number | boolean | readonly (string | number)[];
45
+ other?: string;
46
+ }
47
+ /** A check as it is written in the JSON. */
48
+ export interface CheckInput {
49
+ id: string;
50
+ assert: ConditionInput;
51
+ when?: ConditionInput[];
52
+ severity?: CheckSeverity;
53
+ message?: Record<string, string>;
54
+ }
55
+ type Add = (path: string, message: string) => void;
56
+ /** Reads a schema's checks against its fields, reporting every problem with where it is. */
57
+ export declare function readChecks(value: unknown, fields: readonly FieldSchema[], add: Add): RowCheck[];
58
+ /** A check written out again as JSON. */
59
+ export declare function checkToJson(check: RowCheck): CheckInput;
60
+ /** The fields a check reads: what it asserts, what it compares with, and its conditions. */
61
+ export declare function checkFields(check: RowCheck): string[];
62
+ /**
63
+ * Whether a row breaks the check: every condition of `when` holds, and what it asserts does not. A
64
+ * comparison with nothing on one side asserts nothing: a field's being filled is a check of its own.
65
+ * A value that cannot be read as its field's is nothing to compare either; its cell says why.
66
+ */
67
+ export declare function breaksCheck(check: RowCheck, field: (key: string) => FieldSchema, get: (key: string) => CellValue, settings: ValueSettings): boolean;
68
+ /**
69
+ * The rows that break a check, in the order they stand in the sheet: what a rule editor counts and
70
+ * shows while the rule is still being written, before it is saved anywhere. `schema` is one that has
71
+ * the check, such as the template applied with it.
72
+ */
73
+ export declare function rowsBreaking(schema: ModuleSchema, store: RowStore, check: RowCheck, settings: ValueSettings): RowId[];
74
+ /**
75
+ * Whether a condition holds for a row: what a colour rule asks. A comparison with nothing on one side
76
+ * does not hold, nor does one with a value that cannot be read as its field's.
77
+ */
78
+ export declare function conditionHolds(condition: RowCondition, field: (key: string) => FieldSchema, get: (key: string) => CellValue, settings: ValueSettings): boolean;
79
+ /** Reads one condition against the fields, as a check's and a colour rule's are written. */
80
+ export declare function readCondition(raw: unknown, path: string, fields: ReadonlyMap<string, FieldSchema>, add: Add): RowCondition | null;
81
+ /** A condition written out again as JSON. */
82
+ export declare function conditionToJson(condition: RowCondition): ConditionInput;
83
+ export {};
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Cells as a spreadsheet puts them on the clipboard: separated by tabs, rows separated by line
3
+ * breaks. A cell holding a tab, a line break or a quote is wrapped in quotes, its own quotes doubled.
4
+ * Excel, Google Sheets and LibreOffice all write this and read it back.
5
+ */
6
+ /**
7
+ * The rows and cells in clipboard text.
8
+ *
9
+ * One line break at the very end ends the last row rather than starting another: Excel puts one
10
+ * after every copied range. Rows may differ in length; nothing is padded. A line break inside a
11
+ * quoted cell is always "\n", however it arrived.
12
+ *
13
+ * A quote opens a quoted cell only at the start of a cell. One that never closes was a character,
14
+ * not a wrapper, and the cell is read again from it as plain text: otherwise a cell that merely
15
+ * starts with `"` would swallow every row after it.
16
+ */
17
+ export declare function parseTsv(text: string): string[][];
18
+ /** Rows of cells as clipboard text, quoting the cells that need it. No line break after the last row. */
19
+ export declare function toTsv(rows: readonly (readonly string[])[]): string;
@@ -0,0 +1,121 @@
1
+ /**
2
+ * How wide a column may be, and how much of its header is shown.
3
+ *
4
+ * Two upper limits, kept apart on purpose. A column sized from its header stops at `maxAutoWidth`,
5
+ * so one long header does not push the rest of the sheet off screen. A user dragging a column can go
6
+ * on to `maxWidth`, because they asked for it.
7
+ *
8
+ * Characters and pixels are separate too. A header longer than `headerDisplayMaxLength` characters
9
+ * is cut and ends in "…"; a shorter one that is still too wide for its column is cut by the column.
10
+ * Either way the full header stays available as the column's tooltip, and no cell value is ever cut
11
+ * in the data, only in how it is drawn.
12
+ */
13
+ export interface ColumnSizing {
14
+ /** Pixels. No column is narrower, however it is sized. */
15
+ readonly minWidth: number;
16
+ /** Pixels. The widest a column is made on its own. */
17
+ readonly maxAutoWidth: number;
18
+ /** Pixels. The widest a user can make a column. */
19
+ readonly maxWidth: number;
20
+ /** Characters of a header shown before it is cut with "…". */
21
+ readonly headerDisplayMaxLength: number;
22
+ /** Whether the user can drag the column's edge. */
23
+ readonly resizable: boolean;
24
+ }
25
+ export declare const DEFAULT_COLUMN_SIZING: ColumnSizing;
26
+ /**
27
+ * The sizing for one column: the defaults, then each layer over the one before, such as the
28
+ * module's settings and then the column's own.
29
+ *
30
+ * @throws TabulaError when the limits cannot all hold, such as a minimum above a maximum
31
+ */
32
+ export declare function resolveColumnSizing(...layers: readonly (Partial<ColumnSizing> | undefined)[]): ColumnSizing;
33
+ /**
34
+ * The header as it is drawn: whole when it fits the character limit, cut and ending in "…" when it
35
+ * does not. Counted in characters a reader sees, so a cut never splits one in half.
36
+ */
37
+ export declare function headerDisplayText(label: string, maxLength: number): string;
38
+ /** The width for content of this many pixels, kept between `minWidth` and the automatic maximum. */
39
+ export declare function autoColumnWidth(contentWidth: number, sizing: ColumnSizing): number;
40
+ /** A width the user set, kept between `minWidth` and `maxWidth`. */
41
+ export declare function clampColumnWidth(width: number, sizing: ColumnSizing): number;
42
+ export declare const COLUMN_ALIGNS: readonly ["left", "center", "right"];
43
+ export type ColumnAlign = (typeof COLUMN_ALIGNS)[number];
44
+ /** The most digits after the point a column can be told to show. */
45
+ export declare const MAX_SHOWN_DECIMALS = 6;
46
+ /**
47
+ * How a column's values are drawn. Never what they are: a number shown with fewer digits keeps every
48
+ * digit it has, and the backend is sent the same value whatever is shown.
49
+ */
50
+ export interface ColumnFormat {
51
+ /** Where the values sit in their cells. Left out, numbers on the right and the rest on the left. */
52
+ readonly align?: ColumnAlign;
53
+ /** Digits after the point a number column shows, 0 to `MAX_SHOWN_DECIMALS`. Left out, its field's. */
54
+ readonly decimals?: number;
55
+ }
56
+ /** Each column's format, by field key. */
57
+ export type ColumnFormats = Readonly<Record<string, ColumnFormat>>;
58
+ /**
59
+ * A user's arrangement of the columns, kept with their view of the sheet. Only how the columns are
60
+ * shown: hiding a required column relaxes nothing, and its issues are still listed.
61
+ *
62
+ * Keys the schema does not have are passed over, so a layout saved before a column was removed
63
+ * still applies.
64
+ */
65
+ export interface ColumnLayout {
66
+ /** Columns in the order to show them; those left out follow, in the schema's order. */
67
+ readonly order?: readonly string[];
68
+ readonly hidden?: readonly string[];
69
+ /** Columns kept in place on the left while the sheet scrolls sideways. They are shown first. */
70
+ readonly pinned?: readonly string[];
71
+ /** How each column's values are drawn, by field key. */
72
+ readonly formats?: ColumnFormats;
73
+ }
74
+ export interface ArrangedColumns<F> {
75
+ /** The columns shown, in order: the pinned ones first. */
76
+ readonly fields: readonly F[];
77
+ /** How many of the first are pinned. */
78
+ readonly pinned: number;
79
+ }
80
+ /** The columns a layout shows, in the order it shows them. */
81
+ export declare function arrangeColumns<F extends {
82
+ readonly key: string;
83
+ }>(fields: readonly F[], layout?: ColumnLayout): ArrangedColumns<F>;
84
+ /** The layout with one column hidden or shown, pinned or let go. */
85
+ export declare function withColumn(layout: ColumnLayout, key: string, change: {
86
+ readonly hidden?: boolean;
87
+ readonly pinned?: boolean;
88
+ }): ColumnLayout;
89
+ /**
90
+ * The layout with one column drawn another way: what `change` gives replaces what the column had,
91
+ * a property given as `undefined` goes back to the default, and `null` takes the whole format off.
92
+ *
93
+ * @throws TabulaError `invalid_argument` for an alignment that is not one, or decimals out of 0–6
94
+ */
95
+ export declare function withColumnFormat(layout: ColumnLayout, key: string, change: {
96
+ readonly align?: ColumnAlign | undefined;
97
+ readonly decimals?: number | undefined;
98
+ } | null): ColumnLayout;
99
+ /**
100
+ * Each column's format with each layer over the one before, property by property: a template's
101
+ * formats, then the user's own. What a layer leaves out, a column or one property of it, is the
102
+ * layer under's.
103
+ */
104
+ export declare function mergeColumnFormats(...layers: readonly (ColumnFormats | undefined)[]): ColumnFormats;
105
+ /**
106
+ * The layout without what `under` already draws: each property of a column's format that `under`
107
+ * gives the same is taken off, and a format left with none goes. What a user keeps of their own once
108
+ * a template says the same, so that a change to the template later reaches them.
109
+ */
110
+ export declare function withoutColumnFormats(layout: ColumnLayout, under: ColumnFormats | undefined): ColumnLayout;
111
+ /**
112
+ * A layout read from JSON, each problem reported under `path`: what a view, a draft and a template
113
+ * keep. Undefined when there is none.
114
+ */
115
+ export declare function readColumnLayout(input: unknown, path: string, problem: (path: string, message: string) => void): ColumnLayout | undefined;
116
+ /** A copy of the layout that cannot be changed, with nothing in it that it does not hold. */
117
+ export declare function freezeColumnLayout(layout: ColumnLayout): ColumnLayout;
118
+ /** The layout with its keys passed through `key`: kept, renamed, or dropped where it gives null. */
119
+ export declare function mapColumnLayout(layout: ColumnLayout, key: (key: string) => string | null): ColumnLayout;
120
+ /** Whether two layouts draw their columns alike: the same formats on the same columns. */
121
+ export declare function sameColumnFormats(a: ColumnLayout, b: ColumnLayout): boolean;
@@ -0,0 +1,157 @@
1
+ import type { RowId } from "./rows.js";
2
+ import type { IssueScope, IssueSeverity, IssueTarget } from "./validation.js";
3
+ import type { CellValue } from "./value.js";
4
+ /**
5
+ * What passes between a sheet and the backend of the application that embeds it.
6
+ *
7
+ * The package never chooses how: an application gives a `SheetAdapters` object, and each adapter
8
+ * calls its backend however it likes and turns the answer into the shapes here. That is also where
9
+ * a backend with answers of its own shape is adapted.
10
+ */
11
+ /** One row as a backend receives it, under the id it keeps in the sheet. */
12
+ export interface PayloadRow {
13
+ readonly rowId: RowId;
14
+ /**
15
+ * Every field of the schema that is submitted (`submit`), empty ones as null, in the form the grid
16
+ * writes: a number as decimal text with a point, a date as ISO 8601, a yes/no as a boolean, an
17
+ * option as its value. Text a host loaded, such as "1.234,56", goes as what it reads to; text the
18
+ * grid could not read goes as it stands. A lookup is sent as its id; one holding text never
19
+ * matched to a record is sent as `{ label }`, for the backend to match or refuse.
20
+ */
21
+ readonly values: Readonly<Record<string, CellValue>>;
22
+ }
23
+ /** What a pre-flight check is sent, and what a process run is sent with its key. */
24
+ export interface SheetRequest {
25
+ /** Pairs an answer with its question; new for every request. */
26
+ readonly requestId: string;
27
+ readonly module: string;
28
+ readonly schemaVersion: number;
29
+ readonly sheetId: string;
30
+ /** The revision of the sheet the rows were read at: what the answer will be about. */
31
+ readonly revision: number;
32
+ readonly rows: readonly PayloadRow[];
33
+ }
34
+ export interface ProcessRequest extends SheetRequest {
35
+ /**
36
+ * The same for every attempt at one run, so a backend that has seen it answers with what it did
37
+ * rather than doing it twice. A run that got a definite answer is never sent again; the next one
38
+ * gets a new key.
39
+ */
40
+ readonly idempotencyKey: string;
41
+ }
42
+ /** An issue as a backend reports it. Everything but `code` has a sensible default. */
43
+ export interface IssueInput {
44
+ /** Left out: `cell` with a row and a field, `row` with a row only, `sheet` with neither. */
45
+ readonly scope?: IssueScope;
46
+ /** Another sheet's issue is passed over. */
47
+ readonly sheetId?: string | null;
48
+ readonly rowId?: RowId | null;
49
+ readonly field?: string | null;
50
+ readonly targets?: readonly IssueTarget[];
51
+ readonly code: string;
52
+ /** `error` by default. */
53
+ readonly severity?: IssueSeverity;
54
+ /** Whether it stops the sheet from being processed; by default, an error does and a warning does not. */
55
+ readonly blocking?: boolean;
56
+ /** Shown when the interface has no wording of its own for the code; the code itself by default. */
57
+ readonly message?: string;
58
+ readonly params?: Readonly<Record<string, string | number | boolean | null>>;
59
+ }
60
+ /**
61
+ * What processing a row would do: make a new record, change one, or leave one as it is. Only the
62
+ * backend can tell, for only it holds the records.
63
+ */
64
+ export type RowAction = "create" | "update" | "unchanged";
65
+ /** What a pre-flight check says a row would do, for a preview before the run. */
66
+ export interface RowPlanInput {
67
+ readonly rowId: RowId;
68
+ readonly action: RowAction;
69
+ /**
70
+ * For an update, each field that would change, with the value the backend holds now, in the form
71
+ * rows are sent in; a lookup may come as `{ id, label }`, to be shown by its label. A field left
72
+ * out is not changing.
73
+ */
74
+ readonly previous?: Readonly<Record<string, CellValue>>;
75
+ }
76
+ export interface CheckResponse {
77
+ /** When given, it must be the request's. */
78
+ readonly requestId?: string;
79
+ readonly issues?: readonly IssueInput[];
80
+ /**
81
+ * What each row would do if it were processed now. A backend that cannot tell leaves it out, and
82
+ * a preview then has only the count of rows to show; so does a row it leaves out.
83
+ */
84
+ readonly plan?: readonly RowPlanInput[];
85
+ }
86
+ /**
87
+ * `pending`: accepted and still running; the session asks `processStatus` until it is not.
88
+ * `partial`: some rows went through and some did not, which only a backend that allows it says.
89
+ * `cancelled`: stopped on request. Rows done before the stop stay done: a cancel takes nothing back.
90
+ */
91
+ export type ProcessStatus = "pending" | "succeeded" | "partial" | "failed" | "cancelled";
92
+ /** `skipped`: not processed at all, such as after another row failed in an all-or-nothing run. */
93
+ export type RowOutcome = "succeeded" | "failed" | "skipped";
94
+ export interface RowResultInput {
95
+ readonly rowId: RowId;
96
+ readonly result: RowOutcome;
97
+ /** Bound to this row unless they name another. */
98
+ readonly issues?: readonly IssueInput[];
99
+ /** Whatever the backend wants kept with the row, such as the id of the record it created. */
100
+ readonly data?: Readonly<Record<string, CellValue>>;
101
+ }
102
+ export interface ProcessProgress {
103
+ readonly done: number;
104
+ readonly total: number;
105
+ }
106
+ export interface ProcessResponse {
107
+ /** When given, it must be the request's. */
108
+ readonly requestId?: string;
109
+ /** The backend's name for the run, to ask about it or cancel it by. */
110
+ readonly operationId?: string | null;
111
+ readonly status: ProcessStatus;
112
+ /**
113
+ * A row of the request left out is taken as not processed, unless the run as a whole succeeded.
114
+ * A row the request did not hold is passed over.
115
+ */
116
+ readonly rows?: readonly RowResultInput[];
117
+ /** About the run as a whole rather than one row. */
118
+ readonly issues?: readonly IssueInput[];
119
+ readonly progress?: ProcessProgress | null;
120
+ }
121
+ /** How a run is asked about or cancelled: by its operation id when known, always by its key. */
122
+ export interface StatusQuery {
123
+ readonly requestId: string;
124
+ readonly idempotencyKey: string;
125
+ readonly operationId: string | null;
126
+ }
127
+ export interface AdapterContext {
128
+ /** Aborted when the answer is no longer wanted. An adapter passes it on to its HTTP client. */
129
+ readonly signal: AbortSignal;
130
+ readonly requestId: string;
131
+ }
132
+ /**
133
+ * How a sheet reaches its backend. Each is optional: a sheet with no `preflight` has no backend
134
+ * check, one with no `processStatus` cannot follow a run that answers `pending`.
135
+ *
136
+ * An adapter that throws, such as on a network error, is a failure of the request, kept apart from
137
+ * the issues of the data. One that answers in the wrong shape is a failure too.
138
+ */
139
+ export interface SheetAdapters {
140
+ preflight?(request: SheetRequest, context: AdapterContext): Promise<CheckResponse>;
141
+ process?(request: ProcessRequest, context: AdapterContext): Promise<ProcessResponse>;
142
+ processStatus?(query: StatusQuery, context: AdapterContext): Promise<ProcessResponse>;
143
+ cancelProcess?(query: StatusQuery, context: AdapterContext): Promise<void>;
144
+ }
145
+ /**
146
+ * Checks a pre-flight answer against the contract. Properties it does not know are passed over, so
147
+ * a backend may send more; a known one of the wrong kind is refused, with every problem at once.
148
+ *
149
+ * @throws TabulaError `invalid_response`
150
+ */
151
+ export declare function readCheckResponse(input: unknown): CheckResponse;
152
+ /**
153
+ * Checks a process answer, or a status answer, against the contract.
154
+ *
155
+ * @throws TabulaError `invalid_response`
156
+ */
157
+ export declare function readProcessResponse(input: unknown): ProcessResponse;
package/dist/csv.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ /**
2
+ * CSV as spreadsheets write it (RFC 4180): cells separated by a delimiter, rows by line breaks, a
3
+ * cell holding the delimiter, a quote or a line break wrapped in quotes, its own quotes doubled.
4
+ *
5
+ * Which delimiter depends on who wrote the file. Excel uses the list separator of the machine's
6
+ * language, and in Turkish, where the comma is the decimal separator, that is a semicolon.
7
+ */
8
+ export type CsvDelimiter = "," | ";" | "\t";
9
+ export interface CsvReadOptions {
10
+ /** Left out, it is told from the first line: whichever of `;`, `,` and tab splits it most. */
11
+ readonly delimiter?: CsvDelimiter;
12
+ }
13
+ /**
14
+ * The rows and cells of CSV text. A byte order mark at the start is dropped, line breaks are
15
+ * `\r\n` or `\n`, a line break inside a quoted cell is kept as `\n`, and the line break that ends the
16
+ * last row starts no other. Empty lines are rows with one empty cell, as in the file.
17
+ */
18
+ export declare function parseCsv(text: string, options?: CsvReadOptions): string[][];
19
+ /** The delimiter that splits the first line into the most cells, quotes respected. A semicolon wins a tie. */
20
+ export declare function detectDelimiter(text: string): CsvDelimiter;
21
+ export interface CsvWriteOptions {
22
+ readonly delimiter?: CsvDelimiter;
23
+ /** A byte order mark at the start, which Excel needs to read the text as UTF-8. True by default. */
24
+ readonly bom?: boolean;
25
+ }
26
+ /** CSV text for rows of cells, lines ending in `\r\n` as RFC 4180 and Excel have them. */
27
+ export declare function toCsv(rows: readonly (readonly string[])[], options?: CsvWriteOptions): string;
28
+ /**
29
+ * The text in a file's bytes: UTF-8 when they are UTF-8, otherwise the code page given, Windows-1254
30
+ * by default, which is what Excel writes a Turkish "CSV" in.
31
+ */
32
+ export declare function decodeText(bytes: Uint8Array, fallback?: string): string;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Decimal numbers written as text, compared without ever becoming a float.
3
+ *
4
+ * A limit such as a minimum price arrives from the backend as text ("0.30") so that it means what it
5
+ * says. As floats, 0.1 + 0.2 is not 0.3, and a rule saying "at least 0.30" would refuse a sum that
6
+ * meets it.
7
+ */
8
+ /**
9
+ * One way of writing each number: no sign on zero, no leading zeros, no trailing zeros after the
10
+ * point, no point with nothing after it. `"-0012.3400"` becomes `"-12.34"`.
11
+ *
12
+ * Null for anything else, exponents (`1e21`), thousands separators and a decimal comma included.
13
+ * Those belong to what a user types, and are read elsewhere with the sheet's own number settings.
14
+ */
15
+ export declare function normalizeDecimal(input: string | number): string | null;
16
+ /** -1, 0 or 1 as `a` is below, equal to or above `b`. Both must be as `normalizeDecimal()` writes them. */
17
+ export declare function compareDecimal(a: string, b: string): -1 | 0 | 1;