@gemologic/sheen-table 0.1.0-rc.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/LICENSE +19 -0
- package/README.md +19 -0
- package/dist/DataTable.d.ts +159 -0
- package/dist/DataTable.js +2724 -0
- package/dist/DataTableToolbar.d.ts +28 -0
- package/dist/DataTableToolbar.js +132 -0
- package/dist/FilterBar.d.ts +24 -0
- package/dist/FilterBar.js +681 -0
- package/dist/QueryBuilder.d.ts +11 -0
- package/dist/QueryBuilder.js +693 -0
- package/dist/cell-edit.d.ts +50 -0
- package/dist/cell-edit.js +178 -0
- package/dist/client-state.d.ts +28 -0
- package/dist/client-state.js +57 -0
- package/dist/client-view.d.ts +31 -0
- package/dist/client-view.js +30 -0
- package/dist/column-layout.d.ts +10 -0
- package/dist/column-layout.js +34 -0
- package/dist/columns.d.ts +108 -0
- package/dist/columns.js +217 -0
- package/dist/core.d.ts +54 -0
- package/dist/core.js +28 -0
- package/dist/export.d.ts +14 -0
- package/dist/export.js +27 -0
- package/dist/facets.d.ts +15 -0
- package/dist/facets.js +41 -0
- package/dist/filter-bar-state.d.ts +22 -0
- package/dist/filter-bar-state.js +128 -0
- package/dist/filter-client.d.ts +9 -0
- package/dist/filter-client.js +69 -0
- package/dist/filter.d.ts +64 -0
- package/dist/filter.js +132 -0
- package/dist/grouping.d.ts +37 -0
- package/dist/grouping.js +126 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +31 -0
- package/dist/messages.d.ts +115 -0
- package/dist/messages.js +121 -0
- package/dist/pagination.d.ts +36 -0
- package/dist/pagination.js +102 -0
- package/dist/query-builder-state.d.ts +28 -0
- package/dist/query-builder-state.js +128 -0
- package/dist/requests.d.ts +26 -0
- package/dist/requests.js +106 -0
- package/dist/row-hierarchy.d.ts +32 -0
- package/dist/row-hierarchy.js +170 -0
- package/dist/row-identity.d.ts +11 -0
- package/dist/row-identity.js +36 -0
- package/dist/saved-views.d.ts +52 -0
- package/dist/saved-views.js +153 -0
- package/dist/search-controller.d.ts +22 -0
- package/dist/search-controller.js +75 -0
- package/dist/search.d.ts +28 -0
- package/dist/search.js +111 -0
- package/dist/selection-client.d.ts +9 -0
- package/dist/selection-client.js +14 -0
- package/dist/selection.d.ts +25 -0
- package/dist/selection.js +60 -0
- package/dist/server-export.d.ts +27 -0
- package/dist/server-export.js +105 -0
- package/dist/sorting.d.ts +17 -0
- package/dist/sorting.js +58 -0
- package/dist/table-state-changes.d.ts +27 -0
- package/dist/table-state-changes.js +67 -0
- package/dist/table-state.d.ts +20 -0
- package/dist/table-state.js +77 -0
- package/dist/text-normalization.d.ts +2 -0
- package/dist/text-normalization.js +12 -0
- package/dist/url-state.d.ts +39 -0
- package/dist/url-state.js +75 -0
- package/package.json +66 -0
- package/src/DataTable.tsx +2322 -0
- package/src/DataTableToolbar.tsx +69 -0
- package/src/FilterBar.tsx +396 -0
- package/src/QueryBuilder.tsx +274 -0
- package/src/cell-edit.ts +187 -0
- package/src/client-state.ts +52 -0
- package/src/client-view.ts +61 -0
- package/src/column-layout.ts +47 -0
- package/src/columns.ts +271 -0
- package/src/core.ts +54 -0
- package/src/export.ts +39 -0
- package/src/facets.ts +46 -0
- package/src/filter-bar-state.ts +118 -0
- package/src/filter-client.ts +79 -0
- package/src/filter.ts +113 -0
- package/src/grouping.ts +185 -0
- package/src/index.ts +7 -0
- package/src/messages.ts +29 -0
- package/src/pagination.ts +81 -0
- package/src/query-builder-state.ts +105 -0
- package/src/requests.ts +85 -0
- package/src/row-hierarchy.ts +280 -0
- package/src/row-identity.ts +39 -0
- package/src/saved-views.ts +143 -0
- package/src/search-controller.ts +61 -0
- package/src/search.ts +137 -0
- package/src/selection-client.ts +23 -0
- package/src/selection.ts +62 -0
- package/src/server-export.ts +89 -0
- package/src/sorting.ts +63 -0
- package/src/styles.css +197 -0
- package/src/table-state-changes.ts +51 -0
- package/src/table-state.ts +72 -0
- package/src/text-normalization.ts +14 -0
- package/src/url-state.ts +62 -0
package/src/columns.ts
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import type { FilterColumn } from "./filter.ts";
|
|
2
|
+
import type { SortColumn } from "./sorting.ts";
|
|
3
|
+
import type { TableStateSchema } from "./table-state.ts";
|
|
4
|
+
import type { JSX } from "solid-js";
|
|
5
|
+
|
|
6
|
+
export type ColumnValue = string | number | boolean | null;
|
|
7
|
+
export type ColumnPin = "start" | "end" | false;
|
|
8
|
+
export type ColumnAlign = "start" | "center" | "end";
|
|
9
|
+
export type ColumnWidth = number | "fill" | "content";
|
|
10
|
+
export type ColumnAggregate = "sum" | "average" | "min" | "max" | "count";
|
|
11
|
+
export type ColumnSort = SortColumn["type"] | false;
|
|
12
|
+
export type ColumnSearchProjection<Row extends object> = (row: Row) => string | number | null;
|
|
13
|
+
interface ColumnEditorBase<Row extends object> {
|
|
14
|
+
readonly nullable?: boolean;
|
|
15
|
+
readonly validate?: (value: ColumnValue, row: Row) => string | null;
|
|
16
|
+
}
|
|
17
|
+
export type ColumnEditor<Row extends object> =
|
|
18
|
+
| (ColumnEditorBase<Row> & { readonly type: "text" | "number" | "boolean" })
|
|
19
|
+
| (ColumnEditorBase<Row> & { readonly type: "select"; readonly options: readonly string[] });
|
|
20
|
+
export type ColumnFilter =
|
|
21
|
+
| { readonly type: "text" }
|
|
22
|
+
| { readonly type: "number" }
|
|
23
|
+
| { readonly type: "date" }
|
|
24
|
+
| { readonly type: "enum"; readonly options: readonly string[]; readonly faceted?: boolean };
|
|
25
|
+
export interface UnsafeTanStackColumnOptions { readonly [option: string]: unknown }
|
|
26
|
+
|
|
27
|
+
interface ColumnBase<Row extends object> {
|
|
28
|
+
readonly id: string;
|
|
29
|
+
readonly header: string;
|
|
30
|
+
readonly footer?: string | ((rows: readonly Row[]) => JSX.Element);
|
|
31
|
+
readonly width?: ColumnWidth;
|
|
32
|
+
readonly minWidth?: number;
|
|
33
|
+
readonly maxWidth?: number;
|
|
34
|
+
readonly align?: ColumnAlign;
|
|
35
|
+
readonly numeric?: boolean;
|
|
36
|
+
readonly pin?: ColumnPin;
|
|
37
|
+
readonly visible?: boolean;
|
|
38
|
+
readonly __unsafe_tanstack?: UnsafeTanStackColumnOptions;
|
|
39
|
+
}
|
|
40
|
+
export interface SheenAccessorColumn<Row extends object> extends ColumnBase<Row> {
|
|
41
|
+
readonly accessor: (row: Row) => ColumnValue;
|
|
42
|
+
readonly cell?: (value: ColumnValue, row: Row) => JSX.Element;
|
|
43
|
+
/** Search the accessor value, or return a dedicated searchable text/number projection. */
|
|
44
|
+
readonly search?: true | ColumnSearchProjection<Row>;
|
|
45
|
+
/** Sensitive columns cannot participate in global search. */
|
|
46
|
+
readonly sensitive?: boolean;
|
|
47
|
+
readonly editor?: ColumnEditor<Row>;
|
|
48
|
+
readonly filter?: ColumnFilter;
|
|
49
|
+
/** Omit to infer number/date from filter or numeric, otherwise text. */
|
|
50
|
+
readonly sort?: ColumnSort;
|
|
51
|
+
readonly aggregate?: ColumnAggregate;
|
|
52
|
+
}
|
|
53
|
+
export interface SheenDisplayColumn<Row extends object> extends ColumnBase<Row> {
|
|
54
|
+
readonly accessor?: never;
|
|
55
|
+
/** Display columns receive null because they have no accessor value. */
|
|
56
|
+
readonly cell: (value: ColumnValue, row: Row) => JSX.Element;
|
|
57
|
+
readonly search?: never;
|
|
58
|
+
readonly sensitive?: never;
|
|
59
|
+
readonly filter?: never;
|
|
60
|
+
readonly sort?: never;
|
|
61
|
+
readonly aggregate?: never;
|
|
62
|
+
readonly editor?: never;
|
|
63
|
+
}
|
|
64
|
+
export type SheenColumnInput<Row extends object> = SheenAccessorColumn<Row> | SheenDisplayColumn<Row>;
|
|
65
|
+
|
|
66
|
+
interface ResolvedColumn<Row extends object> {
|
|
67
|
+
readonly id: string;
|
|
68
|
+
readonly header: string;
|
|
69
|
+
readonly accessor: ((row: Row) => ColumnValue) | null;
|
|
70
|
+
readonly cell: ((value: ColumnValue, row: Row) => JSX.Element) | null;
|
|
71
|
+
readonly footer: string | ((rows: readonly Row[]) => JSX.Element) | null;
|
|
72
|
+
readonly width: ColumnWidth;
|
|
73
|
+
readonly minWidth: number | null;
|
|
74
|
+
readonly maxWidth: number | null;
|
|
75
|
+
readonly align: ColumnAlign;
|
|
76
|
+
readonly numeric: boolean;
|
|
77
|
+
readonly pin: ColumnPin;
|
|
78
|
+
readonly visible: boolean;
|
|
79
|
+
readonly search: ColumnSearchProjection<Row> | null;
|
|
80
|
+
readonly sensitive: boolean;
|
|
81
|
+
readonly editor: ColumnEditor<Row> | null;
|
|
82
|
+
readonly filter: FilterColumn | null;
|
|
83
|
+
readonly faceted: boolean;
|
|
84
|
+
readonly sort: SortColumn | null;
|
|
85
|
+
readonly aggregate: ColumnAggregate | null;
|
|
86
|
+
readonly unsafeTanStack: UnsafeTanStackColumnOptions | null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const columnsBrand: unique symbol = Symbol("sheen-columns");
|
|
90
|
+
const inspectColumns: unique symbol = Symbol("inspect-sheen-columns");
|
|
91
|
+
export interface SheenColumns<Row extends object> {
|
|
92
|
+
readonly [columnsBrand]: Row;
|
|
93
|
+
readonly [inspectColumns]: () => readonly ResolvedColumn<Row>[];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
class DefinedColumns<Row extends object> implements SheenColumns<Row> {
|
|
97
|
+
declare readonly [columnsBrand]: Row;
|
|
98
|
+
readonly #definitions: readonly ResolvedColumn<Row>[];
|
|
99
|
+
constructor(definitions: readonly ResolvedColumn<Row>[]) {
|
|
100
|
+
this.#definitions = definitions;
|
|
101
|
+
Object.freeze(this);
|
|
102
|
+
}
|
|
103
|
+
[inspectColumns](): readonly ResolvedColumn<Row>[] { return this.#definitions; }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const baseKeys = new Set(["id", "header", "accessor", "cell", "footer", "width", "minWidth", "maxWidth", "align", "numeric", "pin", "visible", "search", "sensitive", "editor", "filter", "sort", "aggregate", "__unsafe_tanstack"]);
|
|
107
|
+
function assertRecord(value: unknown, label: string): asserts value is Record<string, unknown> {
|
|
108
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) throw new Error(`${label} must be an object`);
|
|
109
|
+
const prototype = Object.getPrototypeOf(value);
|
|
110
|
+
if (prototype !== Object.prototype && prototype !== null) throw new Error(`${label} must be a plain object`);
|
|
111
|
+
}
|
|
112
|
+
function exact(value: Record<string, unknown>, allowed: ReadonlySet<string>, label: string): void {
|
|
113
|
+
for (const key of Object.keys(value)) if (!allowed.has(key)) throw new Error(`${label} has unknown field ${key}`);
|
|
114
|
+
}
|
|
115
|
+
function dimension(value: unknown, label: string): number | null {
|
|
116
|
+
if (value === undefined) return null;
|
|
117
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) throw new Error(`${label} must be a positive finite number`);
|
|
118
|
+
return value;
|
|
119
|
+
}
|
|
120
|
+
function resolveFilter(value: unknown, id: string, label: string): FilterColumn | null {
|
|
121
|
+
if (value === undefined) return null;
|
|
122
|
+
assertRecord(value, label);
|
|
123
|
+
const input = value;
|
|
124
|
+
if (input.type === "text" || input.type === "number" || input.type === "date") {
|
|
125
|
+
exact(input, new Set(["type"]), label);
|
|
126
|
+
return Object.freeze({ id, type: input.type });
|
|
127
|
+
}
|
|
128
|
+
if (input.type !== "enum") throw new Error(`${label}.type is invalid`);
|
|
129
|
+
exact(input, new Set(["type", "options", "faceted"]), label);
|
|
130
|
+
if (!Array.isArray(input.options) || input.options.length === 0) throw new Error(`${label}.options must be a nonempty array`);
|
|
131
|
+
const options = new Set<string>();
|
|
132
|
+
for (const option of input.options) {
|
|
133
|
+
if (typeof option !== "string" || !option || options.has(option)) throw new Error(`${label}.options must contain unique nonempty strings`);
|
|
134
|
+
options.add(option);
|
|
135
|
+
}
|
|
136
|
+
if (input.faceted !== undefined && typeof input.faceted !== "boolean") throw new Error(`${label}.faceted must be boolean`);
|
|
137
|
+
return Object.freeze({ id, type: "enum", options: Object.freeze([...options]) });
|
|
138
|
+
}
|
|
139
|
+
function resolveSort(value: unknown, accessor: boolean, filter: FilterColumn | null, numeric: boolean, id: string, label: string): SortColumn | null {
|
|
140
|
+
if (!accessor || value === false) return null;
|
|
141
|
+
if (value !== undefined && value !== "text" && value !== "number" && value !== "date") throw new Error(`${label} is invalid`);
|
|
142
|
+
const type = value ?? (filter?.type === "number" || filter?.type === "date" ? filter.type : numeric ? "number" : "text");
|
|
143
|
+
return Object.freeze({ id, type });
|
|
144
|
+
}
|
|
145
|
+
function isFacetedFilter(value: unknown, label: string): boolean {
|
|
146
|
+
if (value === undefined) return false;
|
|
147
|
+
assertRecord(value, label);
|
|
148
|
+
return value.type === "enum" && value.faceted === true;
|
|
149
|
+
}
|
|
150
|
+
function resolveEditor<Row extends object>(value: ColumnEditor<Row> | undefined, accessor: boolean, label: string): ColumnEditor<Row> | null {
|
|
151
|
+
if (value === undefined) return null;
|
|
152
|
+
if (!accessor) throw new Error(`${label} requires an accessor column`);
|
|
153
|
+
assertRecord(value, label);
|
|
154
|
+
if (value.type !== "text" && value.type !== "number" && value.type !== "boolean" && value.type !== "select") throw new Error(`${label}.type is invalid`);
|
|
155
|
+
exact(value, new Set(value.type === "select" ? ["type", "options", "nullable", "validate"] : ["type", "nullable", "validate"]), label);
|
|
156
|
+
if (value.nullable !== undefined && typeof value.nullable !== "boolean") throw new Error(`${label}.nullable must be boolean`);
|
|
157
|
+
if (value.type === "boolean" && value.nullable === true) throw new Error(`${label}.boolean editors cannot be nullable`);
|
|
158
|
+
if (value.validate !== undefined && typeof value.validate !== "function") throw new Error(`${label}.validate must be a function`);
|
|
159
|
+
if (value.type !== "select") return Object.freeze({ type: value.type, nullable: value.nullable ?? false, ...(value.validate ? { validate: value.validate } : {}) });
|
|
160
|
+
if (!Array.isArray(value.options) || value.options.length === 0) throw new Error(`${label}.options must be a nonempty array`);
|
|
161
|
+
const options = new Set<string>();
|
|
162
|
+
for (const option of value.options) {
|
|
163
|
+
if (typeof option !== "string" || !option || options.has(option)) throw new Error(`${label}.options must contain unique nonempty strings`);
|
|
164
|
+
options.add(option);
|
|
165
|
+
}
|
|
166
|
+
return Object.freeze({ type: "select", options: Object.freeze([...options]), nullable: value.nullable ?? false, ...(value.validate ? { validate: value.validate } : {}) });
|
|
167
|
+
}
|
|
168
|
+
function checkedAccessor<Row extends object>(accessor: ((row: Row) => ColumnValue) | null, label: string): ((row: Row) => ColumnValue) | null {
|
|
169
|
+
if (accessor === null) return null;
|
|
170
|
+
return row => {
|
|
171
|
+
const value: unknown = accessor(row);
|
|
172
|
+
if (value !== null && typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean") throw new Error(`${label} returned an unsupported value`);
|
|
173
|
+
if (typeof value === "number" && !Number.isFinite(value)) throw new Error(`${label} returned a nonfinite number`);
|
|
174
|
+
return value;
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
function resolveSearch<Row extends object>(value: true | ColumnSearchProjection<Row> | undefined, accessor: ((row: Row) => ColumnValue) | null, visible: boolean, sensitive: boolean, label: string): ColumnSearchProjection<Row> | null {
|
|
178
|
+
if (value === undefined) return null;
|
|
179
|
+
if (accessor === null) throw new Error(`${label} requires an accessor column`);
|
|
180
|
+
if (!visible) throw new Error(`${label} cannot target an initially hidden column`);
|
|
181
|
+
if (sensitive) throw new Error(`${label} cannot target a sensitive column`);
|
|
182
|
+
if (value !== true && typeof value !== "function") throw new Error(`${label} must be true or a projection function`);
|
|
183
|
+
const project = value === true ? accessor : value;
|
|
184
|
+
return row => {
|
|
185
|
+
const projected: unknown = project(row);
|
|
186
|
+
if (projected === null || typeof projected === "string") return projected;
|
|
187
|
+
if (typeof projected === "number" && Number.isFinite(projected)) return projected;
|
|
188
|
+
if (value === true && typeof projected === "boolean") return null;
|
|
189
|
+
throw new Error(`${label} returned an unsupported search value`);
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
function columnFunctions<Row extends object>(value: SheenColumnInput<Row>): Pick<ResolvedColumn<Row>, "accessor" | "cell" | "footer"> {
|
|
193
|
+
return { accessor: value.accessor ?? null, cell: value.cell ?? null, footer: value.footer ?? null };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Validate and seal app definitions behind a non-enumerable type/runtime boundary. */
|
|
197
|
+
export function defineColumns<Row extends object>(values: readonly SheenColumnInput<Row>[]): SheenColumns<Row> {
|
|
198
|
+
if (!Array.isArray(values) || values.length === 0) throw new Error("defineColumns requires at least one column");
|
|
199
|
+
const ids = new Set<string>();
|
|
200
|
+
const definitions: ResolvedColumn<Row>[] = [];
|
|
201
|
+
let index = 0;
|
|
202
|
+
for (const value of values) {
|
|
203
|
+
const label = `columns[${index}]`;
|
|
204
|
+
const typedValue: SheenColumnInput<Row> = value;
|
|
205
|
+
assertRecord(value, label);
|
|
206
|
+
const input = value;
|
|
207
|
+
const functions = columnFunctions(typedValue);
|
|
208
|
+
exact(input, baseKeys, label);
|
|
209
|
+
if (typeof input.id !== "string" || !input.id || input.id.trim() !== input.id || ids.has(input.id)) throw new Error(`${label}.id must be a unique nonempty trimmed string`);
|
|
210
|
+
ids.add(input.id);
|
|
211
|
+
if (typeof input.header !== "string" || !input.header.trim()) throw new Error(`${label}.header must be a nonempty string`);
|
|
212
|
+
const sourceAccessor = functions.accessor;
|
|
213
|
+
if (sourceAccessor !== null && typeof sourceAccessor !== "function") throw new Error(`${label}.accessor must be a function`);
|
|
214
|
+
const accessor = checkedAccessor(sourceAccessor, `${label}.accessor`);
|
|
215
|
+
const cell = functions.cell;
|
|
216
|
+
if (cell !== null && typeof cell !== "function") throw new Error(`${label}.cell must be a function`);
|
|
217
|
+
if (accessor === null && cell === null) throw new Error(`${label} must define an accessor or display cell`);
|
|
218
|
+
if (accessor === null && (input.filter !== undefined || input.sort !== undefined || input.aggregate !== undefined || input.search !== undefined)) throw new Error(`${label} display columns cannot search, filter, sort, or aggregate`);
|
|
219
|
+
if (input.footer !== undefined && typeof input.footer !== "string" && typeof input.footer !== "function") throw new Error(`${label}.footer must be a string or function`);
|
|
220
|
+
const minWidth = dimension(input.minWidth, `${label}.minWidth`);
|
|
221
|
+
const maxWidth = dimension(input.maxWidth, `${label}.maxWidth`);
|
|
222
|
+
if (minWidth !== null && maxWidth !== null && minWidth > maxWidth) throw new Error(`${label}.minWidth cannot exceed maxWidth`);
|
|
223
|
+
const width = input.width ?? "fill";
|
|
224
|
+
if (width !== "fill" && width !== "content" && (typeof width !== "number" || !Number.isFinite(width) || width <= 0)) throw new Error(`${label}.width is invalid`);
|
|
225
|
+
if (typeof width === "number" && ((minWidth !== null && width < minWidth) || (maxWidth !== null && width > maxWidth))) throw new Error(`${label}.width must be within its bounds`);
|
|
226
|
+
if (input.numeric !== undefined && typeof input.numeric !== "boolean") throw new Error(`${label}.numeric must be boolean`);
|
|
227
|
+
const numeric = input.numeric ?? false;
|
|
228
|
+
const align = input.align ?? (numeric ? "end" : "start");
|
|
229
|
+
if (align !== "start" && align !== "center" && align !== "end") throw new Error(`${label}.align is invalid`);
|
|
230
|
+
const pin = input.pin ?? false;
|
|
231
|
+
if (pin !== false && pin !== "start" && pin !== "end") throw new Error(`${label}.pin is invalid`);
|
|
232
|
+
if (pin !== false && typeof width !== "number") throw new Error(`${label}.width must be numeric when initially pinned`);
|
|
233
|
+
if (input.visible !== undefined && typeof input.visible !== "boolean") throw new Error(`${label}.visible must be boolean`);
|
|
234
|
+
if (input.sensitive !== undefined && typeof input.sensitive !== "boolean") throw new Error(`${label}.sensitive must be boolean`);
|
|
235
|
+
const visible = input.visible ?? true;
|
|
236
|
+
const sensitive = input.sensitive ?? false;
|
|
237
|
+
const search = resolveSearch(typedValue.search, accessor, visible, sensitive, `${label}.search`);
|
|
238
|
+
const editor = resolveEditor(typedValue.editor, accessor !== null, `${label}.editor`);
|
|
239
|
+
const filter = resolveFilter(input.filter, input.id, `${label}.filter`);
|
|
240
|
+
const faceted = isFacetedFilter(input.filter, `${label}.filter`);
|
|
241
|
+
const sort = resolveSort(input.sort, accessor !== null, filter, numeric, input.id, `${label}.sort`);
|
|
242
|
+
const aggregate = input.aggregate ?? null;
|
|
243
|
+
if (aggregate !== null && aggregate !== "sum" && aggregate !== "average" && aggregate !== "min" && aggregate !== "max" && aggregate !== "count") throw new Error(`${label}.aggregate is invalid`);
|
|
244
|
+
if (aggregate !== null && aggregate !== "count" && !numeric && filter?.type !== "number") throw new Error(`${label}.${aggregate} requires a numeric column`);
|
|
245
|
+
let unsafeTanStack: UnsafeTanStackColumnOptions | null = null;
|
|
246
|
+
if (input.__unsafe_tanstack !== undefined) {
|
|
247
|
+
assertRecord(input.__unsafe_tanstack, `${label}.__unsafe_tanstack`);
|
|
248
|
+
unsafeTanStack = Object.freeze({ ...input.__unsafe_tanstack });
|
|
249
|
+
}
|
|
250
|
+
definitions.push(Object.freeze({ id: input.id, header: input.header, accessor, cell, footer: functions.footer, width, minWidth, maxWidth, align, numeric, pin, visible, search, sensitive, editor, filter, faceted, sort, aggregate, unsafeTanStack }));
|
|
251
|
+
index++;
|
|
252
|
+
}
|
|
253
|
+
return new DefinedColumns(Object.freeze(definitions));
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** Package-internal bridge used by the DataTable/TanStack adapter. */
|
|
257
|
+
export function readColumnDefinitions<Row extends object>(columns: SheenColumns<Row>): readonly ResolvedColumn<Row>[] {
|
|
258
|
+
const inspect = columns[inspectColumns];
|
|
259
|
+
if (typeof inspect !== "function") throw new Error("Expected columns returned by defineColumns");
|
|
260
|
+
return inspect.call(columns);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** Package-internal schema projection; apps never construct TanStack definitions. */
|
|
264
|
+
export function columnStateSchema<Row extends object>(columns: SheenColumns<Row>): TableStateSchema {
|
|
265
|
+
const definitions = readColumnDefinitions(columns);
|
|
266
|
+
return Object.freeze({
|
|
267
|
+
columns: Object.freeze(definitions.map(column => column.id)),
|
|
268
|
+
filterColumns: Object.freeze(definitions.flatMap(column => column.filter ? [column.filter] : [])),
|
|
269
|
+
sortColumns: Object.freeze(definitions.flatMap(column => column.sort ? [column.sort] : [])),
|
|
270
|
+
});
|
|
271
|
+
}
|
package/src/core.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export { parsePagination, resolvePagination, resetPagination, resizePagination, getPageRange, paginateClientRows, assessServerPage } from "./pagination.ts";
|
|
2
|
+
export type { PageState, TablePagination, PageRange, PageResult, ServerPageAssessment } from "./pagination.ts";
|
|
3
|
+
export { createTableRequests } from "./requests.ts";
|
|
4
|
+
export type { TableRequestState, TableRequestSnapshot, TableRequestOutcome, InitialTableResult } from "./requests.ts";
|
|
5
|
+
export { parseFilter, serializeFilter, deserializeFilter } from "./filter.ts";
|
|
6
|
+
export type { FilterNode, FilterColumn } from "./filter.ts";
|
|
7
|
+
export { queryBuilderFilterColumns, defaultQueryRule, queryNodeAt, replaceQueryNode, appendQueryChild, removeQueryNode, moveQueryNode, toggleQueryNegation } from "./query-builder-state.ts";
|
|
8
|
+
export type { QueryBuilderColumn, QueryBuilderPath } from "./query-builder-state.ts";
|
|
9
|
+
export { listFilterConditions, replaceFilterCondition, removeFilterCondition, appendFilterCondition, removeFilterColumnConditions } from "./filter-bar-state.ts";
|
|
10
|
+
export type { FilterCondition, FilterConditionEntry, FilterPathSegment } from "./filter-bar-state.ts";
|
|
11
|
+
export { compileFilter, filterClientRows } from "./filter-client.ts";
|
|
12
|
+
export type { FilterEvaluation } from "./filter-client.ts";
|
|
13
|
+
export { parseSorting, sortClientRows } from "./sorting.ts";
|
|
14
|
+
export type { SortColumn, SortEntry, SortState, SortEvaluation } from "./sorting.ts";
|
|
15
|
+
export { countClientFacets } from "./facets.ts";
|
|
16
|
+
export type { FacetOption, ColumnFacet, FacetEvaluation } from "./facets.ts";
|
|
17
|
+
export { formatSearchQuery, parseSearchQuery, searchClientRows } from "./search.ts";
|
|
18
|
+
export type { SearchEvaluation, SearchMatch, SearchMode, SearchQuery } from "./search.ts";
|
|
19
|
+
export { createDebouncedSearch } from "./search-controller.ts";
|
|
20
|
+
export type { SearchSnapshot, SearchOutcome, InitialSearchResult } from "./search-controller.ts";
|
|
21
|
+
export { createClientView } from "./client-view.ts";
|
|
22
|
+
export type { ClientView, ClientViewState, ClientViewOptions } from "./client-view.ts";
|
|
23
|
+
export { parseClientViewState, changeClientViewState } from "./client-state.ts";
|
|
24
|
+
export type { ClientStateSchema, ClientStateChange } from "./client-state.ts";
|
|
25
|
+
export { parseTableState, serializeState, deserializeState } from "./table-state.ts";
|
|
26
|
+
export type { ColumnState, TableState, TableStateSchema } from "./table-state.ts";
|
|
27
|
+
export { changeTableState } from "./table-state-changes.ts";
|
|
28
|
+
export type { TableStateChange } from "./table-state-changes.ts";
|
|
29
|
+
export { orderColumnStates, placeColumn, moveColumn, canMoveColumn } from "./column-layout.ts";
|
|
30
|
+
export type { ColumnMoveDirection, ColumnDropPlacement } from "./column-layout.ts";
|
|
31
|
+
export { createTableSelection } from "./selection.ts";
|
|
32
|
+
export type { BulkSelection } from "./selection.ts";
|
|
33
|
+
export { createClientExport } from "./export.ts";
|
|
34
|
+
export type { ExportValue, ExportColumn } from "./export.ts";
|
|
35
|
+
export { selectClientRows } from "./selection-client.ts";
|
|
36
|
+
export type { ClientSelectionOptions } from "./selection-client.ts";
|
|
37
|
+
export { createServerExport } from "./server-export.ts";
|
|
38
|
+
export type { TableExportRequest, ServerExportSnapshot, ServerExportOutcome } from "./server-export.ts";
|
|
39
|
+
export { createCellEdit } from "./cell-edit.ts";
|
|
40
|
+
export type { CellEditValue, CellCommitRequest, CellCommitResult, CellCommitAttempt, CellEditConflict, CellEditSnapshot, CellCommitOutcome, CellEditOptions } from "./cell-edit.ts";
|
|
41
|
+
export { defineColumns } from "./columns.ts";
|
|
42
|
+
export type { ColumnValue, ColumnPin, ColumnAlign, ColumnWidth, ColumnAggregate, ColumnSort, ColumnSearchProjection, ColumnEditor, ColumnFilter, UnsafeTanStackColumnOptions, SheenAccessorColumn, SheenDisplayColumn, SheenColumnInput, SheenColumns } from "./columns.ts";
|
|
43
|
+
export { createRowIdentity } from "./row-identity.ts";
|
|
44
|
+
export type { IdentifiedRow } from "./row-identity.ts";
|
|
45
|
+
export { groupClientRows, normalizeServerGroups } from "./grouping.ts";
|
|
46
|
+
export type { GroupAggregateColumn, GroupAggregateValue, ClientRowGroup, ServerRowGroup, ClientGroupingOptions } from "./grouping.ts";
|
|
47
|
+
export { createRowHierarchy } from "./row-hierarchy.ts";
|
|
48
|
+
export type { HierarchyLoadOutcome, RowHierarchyOptions, HierarchyRow } from "./row-hierarchy.ts";
|
|
49
|
+
export { createTableUrlState } from "./url-state.ts";
|
|
50
|
+
export type { TableUrlLocation, TableUrlRouter, TableUrlRead, TableUrlWrite } from "./url-state.ts";
|
|
51
|
+
export { createSavedViews } from "./saved-views.ts";
|
|
52
|
+
export type { SavedViewRecord, SavedViewSave, SavedViewsAdapter, SavedViewOperation, SavedViewsSnapshot, SavedViewOutcome, SavedViewRestore } from "./saved-views.ts";
|
|
53
|
+
export { englishTableMessages, resolveTableMessages } from "./messages.ts";
|
|
54
|
+
export type { TableMessages } from "./messages.ts";
|
package/src/export.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export type ExportValue = string | number | boolean | null;
|
|
2
|
+
export interface ExportColumn<Row> {
|
|
3
|
+
readonly id: string;
|
|
4
|
+
readonly header: string;
|
|
5
|
+
readonly visible?: boolean;
|
|
6
|
+
readonly value: (row: Row) => ExportValue;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function checkedValue(value: unknown, row: number, column: string): ExportValue {
|
|
10
|
+
if (value === null || typeof value === "string" || typeof value === "boolean" || (typeof value === "number" && Number.isFinite(value))) return value;
|
|
11
|
+
throw new Error(`Invalid export value at row ${row}, column ${column}: expected string, finite number, boolean, or null`);
|
|
12
|
+
}
|
|
13
|
+
function csvCell(value: ExportValue): string {
|
|
14
|
+
let text = value === null ? "" : String(value);
|
|
15
|
+
if (typeof value === "string" && (/^[\s\p{Cc}\p{Cf}]*[=+\-@=+-@]/u.test(text) || /^[\p{Cc}\p{Cf}]/u.test(text))) text = `'${text}`;
|
|
16
|
+
return `"${text.replaceAll('"', '""')}"`;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Snapshot selected rows from the complete client view and visible export columns before downloading. */
|
|
20
|
+
export function createClientExport<Row>(rows: readonly Row[], columns: readonly ExportColumn<Row>[]) {
|
|
21
|
+
const ids = new Set<string>();
|
|
22
|
+
const visible = Array.from(columns).filter(column => {
|
|
23
|
+
if (!column) throw new Error("Invalid export column: missing entry");
|
|
24
|
+
if (typeof column.id !== "string" || !column.id.trim() || ids.has(column.id)) throw new Error("Export column IDs must be unique nonempty strings");
|
|
25
|
+
ids.add(column.id);
|
|
26
|
+
if (typeof column.header !== "string" || typeof column.value !== "function" || (column.visible !== undefined && typeof column.visible !== "boolean")) throw new Error(`Invalid export column: ${column.id}`);
|
|
27
|
+
return column.visible !== false;
|
|
28
|
+
});
|
|
29
|
+
if (visible.length === 0) throw new Error("Export requires at least one visible column");
|
|
30
|
+
const headings = visible.map(column => column.header);
|
|
31
|
+
const keys = Object.freeze(visible.map(column => column.id));
|
|
32
|
+
const values = Array.from(rows, (row, index) => visible.map(column => checkedValue(column.value(row), index, column.id)));
|
|
33
|
+
return Object.freeze({
|
|
34
|
+
rowCount: values.length,
|
|
35
|
+
columnIds: keys,
|
|
36
|
+
toCSV: () => [headings.map(csvCell).join(","), ...values.map(row => row.map(csvCell).join(","))].join("\r\n") + "\r\n",
|
|
37
|
+
toJSON: () => JSON.stringify(values.map(row => Object.fromEntries(keys.map((key, index) => [key, row[index]])))),
|
|
38
|
+
});
|
|
39
|
+
}
|
package/src/facets.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { FilterColumn } from "./filter.ts";
|
|
2
|
+
|
|
3
|
+
export interface FacetOption {
|
|
4
|
+
readonly value: string;
|
|
5
|
+
readonly count: number;
|
|
6
|
+
}
|
|
7
|
+
export interface ColumnFacet {
|
|
8
|
+
readonly column: string;
|
|
9
|
+
readonly options: readonly FacetOption[];
|
|
10
|
+
readonly missing: number;
|
|
11
|
+
}
|
|
12
|
+
export interface FacetEvaluation<Row> {
|
|
13
|
+
readonly getValue: (row: Row, column: string) => unknown;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Count the complete filtered view, before pagination. Never removes AST predicates. */
|
|
17
|
+
export function countClientFacets<Row>(rows: readonly Row[], columns: readonly FilterColumn[], evaluation: FacetEvaluation<Row>): readonly ColumnFacet[] {
|
|
18
|
+
const ids = new Set<string>();
|
|
19
|
+
const facets = columns.flatMap(column => {
|
|
20
|
+
if (typeof column.id !== "string" || !column.id.trim() || ids.has(column.id)) throw new Error("Invalid or duplicate facet column");
|
|
21
|
+
ids.add(column.id);
|
|
22
|
+
if (column.type !== "enum") return [];
|
|
23
|
+
if (!Array.isArray(column.options)) throw new Error(`Invalid facet options for ${column.id}`);
|
|
24
|
+
const counts = new Map<string, number>();
|
|
25
|
+
for (const option of column.options) {
|
|
26
|
+
if (typeof option !== "string") throw new Error(`Invalid facet option for ${column.id}`);
|
|
27
|
+
counts.set(option, 0);
|
|
28
|
+
}
|
|
29
|
+
return [{ column: column.id, counts, missing: 0 }];
|
|
30
|
+
});
|
|
31
|
+
for (let index = 0; index < rows.length; index++) {
|
|
32
|
+
const row = rows[index];
|
|
33
|
+
if (row === undefined) throw new Error(`Missing facet row at index ${index}`);
|
|
34
|
+
for (const facet of facets) {
|
|
35
|
+
const value = evaluation.getValue(row, facet.column);
|
|
36
|
+
if (value === null || value === undefined) { facet.missing++; continue; }
|
|
37
|
+
if (typeof value !== "string" || !facet.counts.has(value)) throw new Error(`Invalid enum value for facet ${facet.column} at row ${index}`);
|
|
38
|
+
facet.counts.set(value, (facet.counts.get(value) ?? 0) + 1);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return Object.freeze(facets.map(facet => Object.freeze({
|
|
42
|
+
column: facet.column,
|
|
43
|
+
options: Object.freeze(Array.from(facet.counts, ([value, count]) => Object.freeze({ value, count }))),
|
|
44
|
+
missing: facet.missing,
|
|
45
|
+
})));
|
|
46
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { FilterNode } from "./filter.ts";
|
|
2
|
+
|
|
3
|
+
export type FilterCondition = Extract<FilterNode, { readonly column: string }>;
|
|
4
|
+
export type FilterPathSegment = number | "child";
|
|
5
|
+
|
|
6
|
+
export interface FilterConditionEntry {
|
|
7
|
+
readonly path: readonly FilterPathSegment[];
|
|
8
|
+
readonly condition: FilterCondition;
|
|
9
|
+
readonly directNegated: boolean;
|
|
10
|
+
readonly inheritedNegated: boolean;
|
|
11
|
+
readonly groups: readonly ("and" | "or")[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function isCondition(node: FilterNode): node is FilterCondition {
|
|
15
|
+
return node.kind !== "and" && node.kind !== "or" && node.kind !== "not";
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Project editable leaf conditions without flattening or changing the source expression. */
|
|
19
|
+
export function listFilterConditions(filter: FilterNode): readonly FilterConditionEntry[] {
|
|
20
|
+
const result: FilterConditionEntry[] = [];
|
|
21
|
+
function visit(node: FilterNode, path: readonly FilterPathSegment[], inheritedNegated: boolean, groups: readonly ("and" | "or")[]): void {
|
|
22
|
+
if (node.kind === "and" || node.kind === "or") {
|
|
23
|
+
const nextGroups = Object.freeze([...groups, node.kind]);
|
|
24
|
+
for (let index = 0; index < node.children.length; index++) visit(node.children[index]!, Object.freeze([...path, index]), inheritedNegated, nextGroups);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (node.kind === "not") {
|
|
28
|
+
if (isCondition(node.child)) {
|
|
29
|
+
result.push(Object.freeze({ path: Object.freeze([...path]), condition: node.child, directNegated: true, inheritedNegated, groups: Object.freeze([...groups]) }));
|
|
30
|
+
} else visit(node.child, Object.freeze([...path, "child"]), !inheritedNegated, groups);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (!isCondition(node)) throw new Error("Filter condition traversal reached a non-leaf node");
|
|
34
|
+
result.push(Object.freeze({ path: Object.freeze([...path]), condition: node, directNegated: false, inheritedNegated, groups: Object.freeze([...groups]) }));
|
|
35
|
+
}
|
|
36
|
+
visit(filter, Object.freeze([]), false, Object.freeze([]));
|
|
37
|
+
return Object.freeze(result);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function replacement(condition: FilterCondition, negated: boolean): FilterNode {
|
|
41
|
+
return negated ? Object.freeze({ kind: "not", child: condition }) : condition;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function replaceNode(node: FilterNode, path: readonly FilterPathSegment[], offset: number, next: FilterNode): FilterNode {
|
|
45
|
+
if (offset === path.length) return next;
|
|
46
|
+
const segment = path[offset];
|
|
47
|
+
if (typeof segment === "number") {
|
|
48
|
+
if ((node.kind !== "and" && node.kind !== "or") || segment < 0 || segment >= node.children.length) throw new Error("Filter condition path does not match the expression");
|
|
49
|
+
const children = node.children.map((child, index) => index === segment ? replaceNode(child, path, offset + 1, next) : child);
|
|
50
|
+
return Object.freeze({ kind: node.kind, children: Object.freeze(children) });
|
|
51
|
+
}
|
|
52
|
+
if (node.kind !== "not") throw new Error("Filter condition path does not match the expression");
|
|
53
|
+
return Object.freeze({ kind: "not", child: replaceNode(node.child, path, offset + 1, next) });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Replace one leaf, optionally wrapped in a direct not, while preserving surrounding groups. */
|
|
57
|
+
export function replaceFilterCondition(filter: FilterNode, path: readonly FilterPathSegment[], condition: FilterCondition, negated: boolean): FilterNode {
|
|
58
|
+
return replaceNode(filter, path, 0, replacement(condition, negated));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function removeNode(node: FilterNode, path: readonly FilterPathSegment[], offset: number): FilterNode | null {
|
|
62
|
+
if (offset === path.length) return null;
|
|
63
|
+
const segment = path[offset];
|
|
64
|
+
if (typeof segment === "number") {
|
|
65
|
+
if ((node.kind !== "and" && node.kind !== "or") || segment < 0 || segment >= node.children.length) throw new Error("Filter condition path does not match the expression");
|
|
66
|
+
const children: FilterNode[] = [];
|
|
67
|
+
for (let index = 0; index < node.children.length; index++) {
|
|
68
|
+
const child = index === segment ? removeNode(node.children[index]!, path, offset + 1) : node.children[index]!;
|
|
69
|
+
if (child) children.push(child);
|
|
70
|
+
}
|
|
71
|
+
if (children.length === 0) return null;
|
|
72
|
+
if (children.length === 1) return children[0]!;
|
|
73
|
+
return Object.freeze({ kind: node.kind, children: Object.freeze(children) });
|
|
74
|
+
}
|
|
75
|
+
if (node.kind !== "not") throw new Error("Filter condition path does not match the expression");
|
|
76
|
+
const child = removeNode(node.child, path, offset + 1);
|
|
77
|
+
return child ? Object.freeze({ kind: "not", child }) : null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Remove one leaf and simplify empty or single-child groups without changing truth semantics. */
|
|
81
|
+
export function removeFilterCondition(filter: FilterNode, path: readonly FilterPathSegment[]): FilterNode {
|
|
82
|
+
return removeNode(filter, path, 0) ?? Object.freeze({ kind: "and", children: Object.freeze([]) });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Add a new condition to the root conjunction, wrapping a non-conjunction expression intact. */
|
|
86
|
+
export function appendFilterCondition(filter: FilterNode, condition: FilterCondition, negated = false): FilterNode {
|
|
87
|
+
const next = replacement(condition, negated);
|
|
88
|
+
if (filter.kind === "and") return Object.freeze({ kind: "and", children: Object.freeze([...filter.children, next]) });
|
|
89
|
+
return Object.freeze({ kind: "and", children: Object.freeze([filter, next]) });
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function withoutColumn(node: FilterNode, column: string): FilterNode | null {
|
|
93
|
+
if (node.kind === "and" || node.kind === "or") {
|
|
94
|
+
const children: FilterNode[] = [];
|
|
95
|
+
let changed = false;
|
|
96
|
+
for (const child of node.children) {
|
|
97
|
+
const next = withoutColumn(child, column);
|
|
98
|
+
if (next) children.push(next);
|
|
99
|
+
if (next !== child) changed = true;
|
|
100
|
+
}
|
|
101
|
+
if (!changed) return node;
|
|
102
|
+
if (children.length === 0) return null;
|
|
103
|
+
if (children.length === 1) return children[0]!;
|
|
104
|
+
return Object.freeze({ kind: node.kind, children: Object.freeze(children) });
|
|
105
|
+
}
|
|
106
|
+
if (node.kind === "not") {
|
|
107
|
+
const child = withoutColumn(node.child, column);
|
|
108
|
+
return child === node.child ? node : child ? Object.freeze({ kind: "not", child }) : null;
|
|
109
|
+
}
|
|
110
|
+
if (!isCondition(node)) throw new Error("Facet traversal reached a non-leaf node");
|
|
111
|
+
return node.column === column ? null : node;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Remove every condition for one facet column while preserving all other constraints. */
|
|
115
|
+
export function removeFilterColumnConditions(filter: FilterNode, column: string): FilterNode {
|
|
116
|
+
if (typeof column !== "string" || !column.trim()) throw new Error("Facet column must be a nonempty string");
|
|
117
|
+
return withoutColumn(filter, column) ?? Object.freeze({ kind: "and", children: Object.freeze([]) });
|
|
118
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { parseFilter } from "./filter.ts";
|
|
2
|
+
import type { FilterColumn, FilterNode } from "./filter.ts";
|
|
3
|
+
import { createTextNormalizer } from "./text-normalization.ts";
|
|
4
|
+
|
|
5
|
+
export interface FilterEvaluation<Row> {
|
|
6
|
+
readonly locale: string;
|
|
7
|
+
readonly getValue: (row: Row, column: string) => unknown;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Validate and compile once per filter/schema/locale change, not once per row. */
|
|
11
|
+
export function compileFilter<Row>(filter: FilterNode, columns: readonly FilterColumn[], options: FilterEvaluation<Row>): (row: Row) => boolean {
|
|
12
|
+
const locale = Intl.getCanonicalLocales(options.locale)[0] ?? "";
|
|
13
|
+
if (!locale) throw new Error("Filter evaluation requires an explicit locale");
|
|
14
|
+
const getValue = options.getValue;
|
|
15
|
+
function compile(node: FilterNode): (row: Row) => boolean {
|
|
16
|
+
switch (node.kind) {
|
|
17
|
+
case "and": {
|
|
18
|
+
const predicates = node.children.map(compile);
|
|
19
|
+
return row => predicates.every(predicate => predicate(row));
|
|
20
|
+
}
|
|
21
|
+
case "or": {
|
|
22
|
+
const predicates = node.children.map(compile);
|
|
23
|
+
return row => predicates.some(predicate => predicate(row));
|
|
24
|
+
}
|
|
25
|
+
case "not": {
|
|
26
|
+
const predicate = compile(node.child);
|
|
27
|
+
return row => !predicate(row);
|
|
28
|
+
}
|
|
29
|
+
case "empty": return row => {
|
|
30
|
+
const value = getValue(row, node.column);
|
|
31
|
+
return value === null || value === undefined || value === "";
|
|
32
|
+
};
|
|
33
|
+
case "text": {
|
|
34
|
+
const normalize = createTextNormalizer(locale, node.caseSensitive);
|
|
35
|
+
const needle = normalize(node.value);
|
|
36
|
+
const match = (value: string) => {
|
|
37
|
+
switch (node.operator) {
|
|
38
|
+
case "eq": return value === needle;
|
|
39
|
+
case "contains": return value.includes(needle);
|
|
40
|
+
case "startsWith": return value.startsWith(needle);
|
|
41
|
+
case "endsWith": return value.endsWith(needle);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
return row => {
|
|
45
|
+
const value = getValue(row, node.column);
|
|
46
|
+
return typeof value === "string" && match(normalize(value));
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
case "enum": {
|
|
50
|
+
const selected = new Set(node.values);
|
|
51
|
+
return row => {
|
|
52
|
+
const value = getValue(row, node.column);
|
|
53
|
+
return typeof value === "string" && selected.has(value);
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
case "date":
|
|
57
|
+
case "number": return row => {
|
|
58
|
+
const value = getValue(row, node.column);
|
|
59
|
+
if (typeof value !== "number" || !Number.isFinite(value) || (node.kind === "date" && Math.abs(value) > 8.64e15)) return false;
|
|
60
|
+
switch (node.operator) {
|
|
61
|
+
case "eq": return value === node.value;
|
|
62
|
+
case "lt": return value < node.value;
|
|
63
|
+
case "lte": return value <= node.value;
|
|
64
|
+
case "gt": return value > node.value;
|
|
65
|
+
case "gte": return value >= node.value;
|
|
66
|
+
case "between": return value >= node.min && value <= node.max;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return compile(parseFilter(filter, columns));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Operates on the complete client dataset and preserves surviving row identities/order. */
|
|
75
|
+
export function filterClientRows<Row>(rows: readonly Row[], filter: FilterNode, columns: readonly FilterColumn[], options: FilterEvaluation<Row>): readonly Row[] {
|
|
76
|
+
const predicate = compileFilter(filter, columns, options);
|
|
77
|
+
if (filter.kind === "and" && filter.children.length === 0) return rows;
|
|
78
|
+
return rows.filter(predicate);
|
|
79
|
+
}
|