@universal-material/data-grid 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +155 -2
- package/custom-elements.json +4624 -0
- package/dist/attribute-converters.d.ts +53 -0
- package/dist/attribute-converters.js +98 -0
- package/dist/attribute-converters.js.map +1 -0
- package/dist/change-source.d.ts +3 -0
- package/dist/change-source.js +2 -0
- package/dist/change-source.js.map +1 -0
- package/dist/data/data-source.d.ts +164 -0
- package/dist/data/data-source.js +75 -0
- package/dist/data/data-source.js.map +1 -0
- package/dist/data/local-source.d.ts +20 -0
- package/dist/data/local-source.js +94 -0
- package/dist/data/local-source.js.map +1 -0
- package/dist/data/remote-loader.d.ts +190 -0
- package/dist/data/remote-loader.js +608 -0
- package/dist/data/remote-loader.js.map +1 -0
- package/dist/export/csv.d.ts +28 -0
- package/dist/export/csv.js +61 -0
- package/dist/export/csv.js.map +1 -0
- package/dist/export/download.d.ts +7 -0
- package/dist/export/download.js +17 -0
- package/dist/export/download.js.map +1 -0
- package/dist/export/rows.d.ts +75 -0
- package/dist/export/rows.js +134 -0
- package/dist/export/rows.js.map +1 -0
- package/dist/export/slice.d.ts +15 -0
- package/dist/export/slice.js +35 -0
- package/dist/export/slice.js.map +1 -0
- package/dist/export/types.d.ts +155 -0
- package/dist/export/types.js +2 -0
- package/dist/export/types.js.map +1 -0
- package/dist/export/write.d.ts +38 -0
- package/dist/export/write.js +178 -0
- package/dist/export/write.js.map +1 -0
- package/dist/export-pdf/client.d.ts +75 -0
- package/dist/export-pdf/client.js +155 -0
- package/dist/export-pdf/client.js.map +1 -0
- package/dist/export-pdf/engine.d.ts +73 -0
- package/dist/export-pdf/engine.js +131 -0
- package/dist/export-pdf/engine.js.map +1 -0
- package/dist/export-pdf/grid-pdf.wasm +0 -0
- package/dist/export-pdf/index.d.ts +27 -0
- package/dist/export-pdf/index.js +566 -0
- package/dist/export-pdf/index.js.map +1 -0
- package/dist/export-pdf/serialize.d.ts +23 -0
- package/dist/export-pdf/serialize.js +127 -0
- package/dist/export-pdf/serialize.js.map +1 -0
- package/dist/export-pdf/types.d.ts +150 -0
- package/dist/export-pdf/types.js +2 -0
- package/dist/export-pdf/types.js.map +1 -0
- package/dist/export-pdf/worker.d.ts +2 -0
- package/dist/export-pdf/worker.js +81 -0
- package/dist/export-pdf/worker.js.map +1 -0
- package/dist/export-xlsx/formats.d.ts +14 -0
- package/dist/export-xlsx/formats.js +40 -0
- package/dist/export-xlsx/formats.js.map +1 -0
- package/dist/export-xlsx/index.d.ts +74 -0
- package/dist/export-xlsx/index.js +137 -0
- package/dist/export-xlsx/index.js.map +1 -0
- package/dist/features/adaptive.d.ts +25 -0
- package/dist/features/adaptive.js +47 -0
- package/dist/features/adaptive.js.map +1 -0
- package/dist/features/cell-content-focus.d.ts +29 -0
- package/dist/features/cell-content-focus.js +95 -0
- package/dist/features/cell-content-focus.js.map +1 -0
- package/dist/features/cell-range.d.ts +97 -0
- package/dist/features/cell-range.js +75 -0
- package/dist/features/cell-range.js.map +1 -0
- package/dist/features/clipboard.d.ts +28 -0
- package/dist/features/clipboard.js +48 -0
- package/dist/features/clipboard.js.map +1 -0
- package/dist/features/column-chooser.d.ts +91 -0
- package/dist/features/column-chooser.js +450 -0
- package/dist/features/column-chooser.js.map +1 -0
- package/dist/features/column-dnd.d.ts +10 -0
- package/dist/features/column-dnd.js +126 -0
- package/dist/features/column-dnd.js.map +1 -0
- package/dist/features/context-menu.d.ts +122 -0
- package/dist/features/context-menu.js +153 -0
- package/dist/features/context-menu.js.map +1 -0
- package/dist/features/drag-helper.d.ts +39 -0
- package/dist/features/drag-helper.js +108 -0
- package/dist/features/drag-helper.js.map +1 -0
- package/dist/features/drop-position.d.ts +15 -0
- package/dist/features/drop-position.js +24 -0
- package/dist/features/drop-position.js.map +1 -0
- package/dist/features/editing.d.ts +111 -0
- package/dist/features/editing.js +244 -0
- package/dist/features/editing.js.map +1 -0
- package/dist/features/filter-row.d.ts +80 -0
- package/dist/features/filter-row.js +537 -0
- package/dist/features/filter-row.js.map +1 -0
- package/dist/features/filtering.d.ts +55 -0
- package/dist/features/filtering.js +209 -0
- package/dist/features/filtering.js.map +1 -0
- package/dist/features/group-dnd.d.ts +11 -0
- package/dist/features/group-dnd.js +119 -0
- package/dist/features/group-dnd.js.map +1 -0
- package/dist/features/header-filter.d.ts +96 -0
- package/dist/features/header-filter.js +579 -0
- package/dist/features/header-filter.js.map +1 -0
- package/dist/features/host-label.d.ts +36 -0
- package/dist/features/host-label.js +184 -0
- package/dist/features/host-label.js.map +1 -0
- package/dist/features/keyboard.d.ts +59 -0
- package/dist/features/keyboard.js +127 -0
- package/dist/features/keyboard.js.map +1 -0
- package/dist/features/menu.d.ts +113 -0
- package/dist/features/menu.js +491 -0
- package/dist/features/menu.js.map +1 -0
- package/dist/features/resize.d.ts +82 -0
- package/dist/features/resize.js +214 -0
- package/dist/features/resize.js.map +1 -0
- package/dist/features/search.d.ts +45 -0
- package/dist/features/search.js +123 -0
- package/dist/features/search.js.map +1 -0
- package/dist/features/selection.d.ts +37 -0
- package/dist/features/selection.js +103 -0
- package/dist/features/selection.js.map +1 -0
- package/dist/features/sorting.d.ts +39 -0
- package/dist/features/sorting.js +53 -0
- package/dist/features/sorting.js.map +1 -0
- package/dist/features/toolbar.d.ts +125 -0
- package/dist/features/toolbar.js +135 -0
- package/dist/features/toolbar.js.map +1 -0
- package/dist/features/tree.d.ts +53 -0
- package/dist/features/tree.js +94 -0
- package/dist/features/tree.js.map +1 -0
- package/dist/features/validation.d.ts +48 -0
- package/dist/features/validation.js +172 -0
- package/dist/features/validation.js.map +1 -0
- package/dist/grid-column-def.d.ts +509 -0
- package/dist/grid-column-def.js +32 -0
- package/dist/grid-column-def.js.map +1 -0
- package/dist/grid-column.d.ts +167 -0
- package/dist/grid-column.js +376 -0
- package/dist/grid-column.js.map +1 -0
- package/dist/grid-i18n.d.ts +469 -0
- package/dist/grid-i18n.js +3222 -0
- package/dist/grid-i18n.js.map +1 -0
- package/dist/grid.d.ts +1484 -0
- package/dist/grid.js +15579 -0
- package/dist/grid.js.map +1 -0
- package/dist/grid.min.js +3858 -0
- package/dist/grid.styles.d.ts +2 -0
- package/dist/grid.styles.js +2747 -0
- package/dist/grid.styles.js.map +1 -0
- package/dist/render/cell.d.ts +16 -0
- package/dist/render/cell.js +78 -0
- package/dist/render/cell.js.map +1 -0
- package/dist/render/column-window.d.ts +18 -0
- package/dist/render/column-window.js +50 -0
- package/dist/render/column-window.js.map +1 -0
- package/dist/render/content.d.ts +45 -0
- package/dist/render/content.js +140 -0
- package/dist/render/content.js.map +1 -0
- package/dist/render/editors.d.ts +28 -0
- package/dist/render/editors.js +41 -0
- package/dist/render/editors.js.map +1 -0
- package/dist/render/footer.d.ts +61 -0
- package/dist/render/footer.js +61 -0
- package/dist/render/footer.js.map +1 -0
- package/dist/render/group-row.d.ts +35 -0
- package/dist/render/group-row.js +59 -0
- package/dist/render/group-row.js.map +1 -0
- package/dist/render/header.d.ts +92 -0
- package/dist/render/header.js +103 -0
- package/dist/render/header.js.map +1 -0
- package/dist/render/keyed-rows.d.ts +25 -0
- package/dist/render/keyed-rows.js +98 -0
- package/dist/render/keyed-rows.js.map +1 -0
- package/dist/render/virtualizer.d.ts +110 -0
- package/dist/render/virtualizer.js +509 -0
- package/dist/render/virtualizer.js.map +1 -0
- package/dist/row-model/added-rows.d.ts +32 -0
- package/dist/row-model/added-rows.js +95 -0
- package/dist/row-model/added-rows.js.map +1 -0
- package/dist/row-model/cell-range.d.ts +71 -0
- package/dist/row-model/cell-range.js +187 -0
- package/dist/row-model/cell-range.js.map +1 -0
- package/dist/row-model/columns.d.ts +80 -0
- package/dist/row-model/columns.js +207 -0
- package/dist/row-model/columns.js.map +1 -0
- package/dist/row-model/compare.d.ts +40 -0
- package/dist/row-model/compare.js +590 -0
- package/dist/row-model/compare.js.map +1 -0
- package/dist/row-model/distinct.d.ts +39 -0
- package/dist/row-model/distinct.js +105 -0
- package/dist/row-model/distinct.js.map +1 -0
- package/dist/row-model/filter.d.ts +80 -0
- package/dist/row-model/filter.js +582 -0
- package/dist/row-model/filter.js.map +1 -0
- package/dist/row-model/flatten.d.ts +129 -0
- package/dist/row-model/flatten.js +511 -0
- package/dist/row-model/flatten.js.map +1 -0
- package/dist/row-model/fold.d.ts +25 -0
- package/dist/row-model/fold.js +104 -0
- package/dist/row-model/fold.js.map +1 -0
- package/dist/row-model/format.d.ts +58 -0
- package/dist/row-model/format.js +239 -0
- package/dist/row-model/format.js.map +1 -0
- package/dist/row-model/group.d.ts +30 -0
- package/dist/row-model/group.js +173 -0
- package/dist/row-model/group.js.map +1 -0
- package/dist/row-model/lru.d.ts +19 -0
- package/dist/row-model/lru.js +48 -0
- package/dist/row-model/lru.js.map +1 -0
- package/dist/row-model/sort.d.ts +30 -0
- package/dist/row-model/sort.js +107 -0
- package/dist/row-model/sort.js.map +1 -0
- package/dist/row-model/summary.d.ts +64 -0
- package/dist/row-model/summary.js +213 -0
- package/dist/row-model/summary.js.map +1 -0
- package/dist/row-model/tree.d.ts +116 -0
- package/dist/row-model/tree.js +478 -0
- package/dist/row-model/tree.js.map +1 -0
- package/dist/row-model/types.d.ts +109 -0
- package/dist/row-model/types.js +5 -0
- package/dist/row-model/types.js.map +1 -0
- package/dist/row-model/value.d.ts +20 -0
- package/dist/row-model/value.js +57 -0
- package/dist/row-model/value.js.map +1 -0
- package/dist/state/grid-state.d.ts +147 -0
- package/dist/state/grid-state.js +295 -0
- package/dist/state/grid-state.js.map +1 -0
- package/dist/state/storage.d.ts +33 -0
- package/dist/state/storage.js +62 -0
- package/dist/state/storage.js.map +1 -0
- package/dist/state/versioned.d.ts +18 -0
- package/dist/state/versioned.js +56 -0
- package/dist/state/versioned.js.map +1 -0
- package/package.json +141 -6
|
@@ -0,0 +1,582 @@
|
|
|
1
|
+
import { getBaseCollator } from './compare.js';
|
|
2
|
+
import { createTextMatcher, foldText } from './fold.js';
|
|
3
|
+
import { toDate, toNumber } from './format.js';
|
|
4
|
+
import { cellValueReader, toDisplayText } from './value.js';
|
|
5
|
+
/** Todos os operadores, na ordem dos menus. */
|
|
6
|
+
export const FILTER_OPERATIONS = [
|
|
7
|
+
'contains',
|
|
8
|
+
'notcontains',
|
|
9
|
+
'startswith',
|
|
10
|
+
'endswith',
|
|
11
|
+
'=',
|
|
12
|
+
'<>',
|
|
13
|
+
'<',
|
|
14
|
+
'>',
|
|
15
|
+
'<=',
|
|
16
|
+
'>=',
|
|
17
|
+
'between',
|
|
18
|
+
'anyof',
|
|
19
|
+
'noneof',
|
|
20
|
+
'isblank',
|
|
21
|
+
'isnotblank',
|
|
22
|
+
];
|
|
23
|
+
const OPERATION_SET = new Set(FILTER_OPERATIONS);
|
|
24
|
+
export const isFilterOperation = (value) => typeof value === 'string' && OPERATION_SET.has(value);
|
|
25
|
+
/** Expressão inválida (operador desconhecido, forma errada). */
|
|
26
|
+
export class FilterExpressionError extends TypeError {
|
|
27
|
+
}
|
|
28
|
+
// ------------------------------------------------------------------ formas
|
|
29
|
+
const isConnector = (value) => value === 'and' || value === 'or';
|
|
30
|
+
/** `[path, operador, valor?]` (ou o atalho `[path, valor]` = `=`). */
|
|
31
|
+
export const isFilterCondition = (expression) => Array.isArray(expression)
|
|
32
|
+
&& expression.length >= 2
|
|
33
|
+
&& expression.length <= 3
|
|
34
|
+
&& typeof expression[0] === 'string'
|
|
35
|
+
&& expression[0] !== '!'
|
|
36
|
+
&& !isConnector(expression[1])
|
|
37
|
+
&& (expression.length === 3 ? isFilterOperation(expression[1]) : !Array.isArray(expression[1]));
|
|
38
|
+
const conditionParts = (condition) => condition.length === 2 && !isFilterOperation(condition[1])
|
|
39
|
+
? [condition[0], '=', condition[1]]
|
|
40
|
+
: [condition[0], condition[1], condition[2]];
|
|
41
|
+
// ----------------------------------------------------------------- valores
|
|
42
|
+
const isBlankValue = (value) => value === null
|
|
43
|
+
|| value === undefined
|
|
44
|
+
|| value === ''
|
|
45
|
+
|| (typeof value === 'number' && Number.isNaN(value))
|
|
46
|
+
|| (value instanceof Date && Number.isNaN(value.getTime()));
|
|
47
|
+
const ISO_DATE_ONLY = /^\d{4}-\d{2}-\d{2}$/;
|
|
48
|
+
/** Dia local como inteiro `aaaammdd`. */
|
|
49
|
+
export const dayKey = (date) => date.getFullYear() * 10000 + (date.getMonth() + 1) * 100 + date.getDate();
|
|
50
|
+
const numberKey = value => {
|
|
51
|
+
const numeric = toNumber(value);
|
|
52
|
+
return numeric === undefined ? undefined : Number(numeric);
|
|
53
|
+
};
|
|
54
|
+
const timeKey = value => toDate(value)?.getTime();
|
|
55
|
+
const dayKeyOf = value => {
|
|
56
|
+
const date = toDate(value);
|
|
57
|
+
return date ? dayKey(date) : undefined;
|
|
58
|
+
};
|
|
59
|
+
const booleanKey = value => typeof value === 'boolean' ? value : value === 'true' ? true : value === 'false' ? false : undefined;
|
|
60
|
+
/**
|
|
61
|
+
* Chave do VALOR DO FILTRO e da célula. Em `date` compara o dia; em
|
|
62
|
+
* `datetime`, o instante — menos quando o valor do filtro é só data
|
|
63
|
+
* (`"2024-03-05"`), que compara o dia (é o que o filtro do cabeçalho grava).
|
|
64
|
+
*/
|
|
65
|
+
const keyFunctions = (dataType, target) => {
|
|
66
|
+
switch (dataType) {
|
|
67
|
+
case 'number':
|
|
68
|
+
return numberKey;
|
|
69
|
+
case 'date':
|
|
70
|
+
return dayKeyOf;
|
|
71
|
+
case 'datetime':
|
|
72
|
+
return typeof target === 'string' && ISO_DATE_ONLY.test(target) ? dayKeyOf : timeKey;
|
|
73
|
+
case 'boolean':
|
|
74
|
+
return booleanKey;
|
|
75
|
+
default:
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
/** Memoriza `keyOf` para strings (datas ISO repetem muito; `toDate` analisa o texto). Transitório: um por compilação. */
|
|
80
|
+
const memoizeStrings = (keyOf) => {
|
|
81
|
+
const memo = new Map();
|
|
82
|
+
return value => {
|
|
83
|
+
if (typeof value !== 'string') {
|
|
84
|
+
return keyOf(value);
|
|
85
|
+
}
|
|
86
|
+
if (memo.has(value)) {
|
|
87
|
+
return memo.get(value);
|
|
88
|
+
}
|
|
89
|
+
const key = keyOf(value);
|
|
90
|
+
if (memo.size < MEMO_LIMIT) {
|
|
91
|
+
memo.set(value, key);
|
|
92
|
+
}
|
|
93
|
+
return key;
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
/** Teto dos memorandos por compilação: colunas de valores únicos não ganham nada e gastariam memória. */
|
|
97
|
+
const MEMO_LIMIT = 4096;
|
|
98
|
+
const convertsTo = (dataType) => {
|
|
99
|
+
if (dataType === 'number') {
|
|
100
|
+
return value => toNumber(value) !== undefined;
|
|
101
|
+
}
|
|
102
|
+
if (dataType === 'date' || dataType === 'datetime') {
|
|
103
|
+
const time = memoizeStrings(timeKey);
|
|
104
|
+
return value => time(value) !== undefined;
|
|
105
|
+
}
|
|
106
|
+
return dataType === 'boolean' ? value => typeof value === 'boolean' : undefined;
|
|
107
|
+
};
|
|
108
|
+
/** Todo valor não vazio de `column` em `items` converte no tipo? (uma passada) */
|
|
109
|
+
export const allValuesConvert = (items, column, dataType) => {
|
|
110
|
+
const converts = convertsTo(dataType);
|
|
111
|
+
if (!converts) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
const read = cellValueReader(column);
|
|
115
|
+
for (const item of items) {
|
|
116
|
+
const value = read(item);
|
|
117
|
+
if (!isBlankValue(value) && !converts(value)) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return true;
|
|
122
|
+
};
|
|
123
|
+
const readerFor = (column, compiler) => {
|
|
124
|
+
let read = compiler.readers.get(column);
|
|
125
|
+
if (!read) {
|
|
126
|
+
read = cellValueReader(column);
|
|
127
|
+
compiler.readers.set(column, read);
|
|
128
|
+
}
|
|
129
|
+
return read;
|
|
130
|
+
};
|
|
131
|
+
/** Coluna do `path` (a primeira, se houver duas); sem coluna, lê o caminho do item. */
|
|
132
|
+
const resolveColumn = (field, compiler) => {
|
|
133
|
+
const known = compiler.context.columns.find(column => column.path === field);
|
|
134
|
+
if (known) {
|
|
135
|
+
return known;
|
|
136
|
+
}
|
|
137
|
+
let column = compiler.synthetic.get(field);
|
|
138
|
+
if (!column) {
|
|
139
|
+
column = { path: field };
|
|
140
|
+
compiler.synthetic.set(field, column);
|
|
141
|
+
}
|
|
142
|
+
return column;
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* Texto em que as operações de texto procuram: o valor cru nas colunas de
|
|
146
|
+
* texto sem `displayValue`/`format` (sem alocar), senão o texto exibido. O
|
|
147
|
+
* resultado é memorizado por valor primitivo quando o texto não depende do
|
|
148
|
+
* item (sem `displayValue` nem `format` função).
|
|
149
|
+
*/
|
|
150
|
+
const textTest = (column, compiler, test, term) => {
|
|
151
|
+
const read = readerFor(column, compiler);
|
|
152
|
+
const { context } = compiler;
|
|
153
|
+
const dataType = context.dataTypeOf(column);
|
|
154
|
+
const dependsOnItem = !!column.displayValue || typeof column.format === 'function';
|
|
155
|
+
if ((dataType === 'string' || dataType === undefined) && !dependsOnItem && !column.format) {
|
|
156
|
+
// Memo por valor enquanto houver repetição (colunas de categorias). Cheio
|
|
157
|
+
// o memo, se a maioria das consultas falhou (valores quase únicos), para
|
|
158
|
+
// de consultar: a consulta custaria tanto quanto o teste.
|
|
159
|
+
const memo = new Map();
|
|
160
|
+
let lookups = 0;
|
|
161
|
+
let useMemo = true;
|
|
162
|
+
return item => {
|
|
163
|
+
const value = read(item);
|
|
164
|
+
if (typeof value !== 'string') {
|
|
165
|
+
return test(isBlankValue(value) ? column.emptyText ?? '' : toDisplayText(value));
|
|
166
|
+
}
|
|
167
|
+
if (!useMemo) {
|
|
168
|
+
return test(value);
|
|
169
|
+
}
|
|
170
|
+
lookups++;
|
|
171
|
+
let result = memo.get(value);
|
|
172
|
+
if (result === undefined) {
|
|
173
|
+
result = test(value);
|
|
174
|
+
if (memo.size < MEMO_LIMIT) {
|
|
175
|
+
memo.set(value, result);
|
|
176
|
+
}
|
|
177
|
+
else if (lookups < memo.size * 2) {
|
|
178
|
+
useMemo = false;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return result;
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
// Termo com caractere que o texto formatado nunca tem: valores que
|
|
185
|
+
// convertem não casam, sem formatar (a busca em 24 colunas pula as de número/data).
|
|
186
|
+
const alphabet = term === undefined || dependsOnItem ? undefined : context.alphabetOf?.(column);
|
|
187
|
+
const converts = convertsTo(dataType);
|
|
188
|
+
if (alphabet && converts && !Array.from(foldText(term)).every(char => alphabet.has(char))) {
|
|
189
|
+
// O vazio mostra `emptyText`, que também está no alfabeto: nada casa.
|
|
190
|
+
if (context.allConvert?.(column)) {
|
|
191
|
+
return never;
|
|
192
|
+
}
|
|
193
|
+
return item => {
|
|
194
|
+
const value = read(item);
|
|
195
|
+
if (isBlankValue(value)) {
|
|
196
|
+
return test(column.emptyText ?? '');
|
|
197
|
+
}
|
|
198
|
+
return converts(value) ? false : test(context.textOf(item, column));
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
if (dependsOnItem) {
|
|
202
|
+
return item => test(context.textOf(item, column));
|
|
203
|
+
}
|
|
204
|
+
const memo = new Map();
|
|
205
|
+
return item => {
|
|
206
|
+
const value = read(item);
|
|
207
|
+
const key = value instanceof Date ? value.getTime() : value;
|
|
208
|
+
if (typeof key === 'object' && key !== null) {
|
|
209
|
+
return test(context.textOf(item, column));
|
|
210
|
+
}
|
|
211
|
+
let result = memo.get(key);
|
|
212
|
+
if (result === undefined) {
|
|
213
|
+
result = test(context.textOf(item, column));
|
|
214
|
+
memo.set(key, result);
|
|
215
|
+
}
|
|
216
|
+
return result;
|
|
217
|
+
};
|
|
218
|
+
};
|
|
219
|
+
const TEXT_MODES = {
|
|
220
|
+
contains: 'contains',
|
|
221
|
+
notcontains: 'contains',
|
|
222
|
+
startswith: 'startswith',
|
|
223
|
+
endswith: 'endswith',
|
|
224
|
+
};
|
|
225
|
+
const always = () => true;
|
|
226
|
+
/** Nunca casa (coluna que a busca pode pular inteira). */
|
|
227
|
+
const never = () => false;
|
|
228
|
+
const every = (list) => item => {
|
|
229
|
+
for (const predicate of list) {
|
|
230
|
+
if (!predicate(item)) {
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return true;
|
|
235
|
+
};
|
|
236
|
+
const some = (list) => item => {
|
|
237
|
+
for (const predicate of list) {
|
|
238
|
+
if (predicate(item)) {
|
|
239
|
+
return true;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return false;
|
|
243
|
+
};
|
|
244
|
+
const compileTextOperation = (column, operation, value, compiler) => {
|
|
245
|
+
const matcher = isBlankValue(value) ? null : createTextMatcher(toDisplayText(value), TEXT_MODES[operation]);
|
|
246
|
+
// Termo vazio: sem condição (como digitar e apagar na linha de filtro).
|
|
247
|
+
if (!matcher) {
|
|
248
|
+
return always;
|
|
249
|
+
}
|
|
250
|
+
const negate = operation === 'notcontains';
|
|
251
|
+
const term = toDisplayText(value);
|
|
252
|
+
return textTest(column, compiler, text => matcher.test(text) !== negate, negate ? undefined : term);
|
|
253
|
+
};
|
|
254
|
+
const compileEquality = (column, value, compiler) => {
|
|
255
|
+
const read = readerFor(column, compiler);
|
|
256
|
+
if (isBlankValue(value)) {
|
|
257
|
+
return item => isBlankValue(read(item));
|
|
258
|
+
}
|
|
259
|
+
const dataType = compiler.context.dataTypeOf(column);
|
|
260
|
+
const rawKeyOf = keyFunctions(dataType, value);
|
|
261
|
+
const keyOf = rawKeyOf && memoizeStrings(rawKeyOf);
|
|
262
|
+
if (!keyOf) {
|
|
263
|
+
const matcher = createTextMatcher(toDisplayText(value), 'equals');
|
|
264
|
+
return matcher ? textTest(column, compiler, text => matcher.test(text)) : always;
|
|
265
|
+
}
|
|
266
|
+
const target = keyOf(value);
|
|
267
|
+
return target === undefined
|
|
268
|
+
? () => false
|
|
269
|
+
: item => keyOf(read(item)) === target;
|
|
270
|
+
};
|
|
271
|
+
const compileOrdering = (column, operation, value, compiler) => {
|
|
272
|
+
const read = readerFor(column, compiler);
|
|
273
|
+
if (isBlankValue(value)) {
|
|
274
|
+
return always;
|
|
275
|
+
}
|
|
276
|
+
const { context } = compiler;
|
|
277
|
+
const dataType = context.dataTypeOf(column);
|
|
278
|
+
const rawKeyOf = keyFunctions(dataType, value);
|
|
279
|
+
const keyOf = rawKeyOf && memoizeStrings(rawKeyOf);
|
|
280
|
+
let compare;
|
|
281
|
+
if (keyOf) {
|
|
282
|
+
const target = keyOf(value);
|
|
283
|
+
if (target === undefined) {
|
|
284
|
+
return () => false;
|
|
285
|
+
}
|
|
286
|
+
compare = item => {
|
|
287
|
+
const key = keyOf(read(item));
|
|
288
|
+
return key === undefined ? undefined : Number(key) - Number(target);
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
const collator = getBaseCollator(context.language);
|
|
293
|
+
const target = toDisplayText(value);
|
|
294
|
+
compare = item => {
|
|
295
|
+
const cell = read(item);
|
|
296
|
+
return isBlankValue(cell) ? undefined : collator.compare(toDisplayText(cell), target);
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
switch (operation) {
|
|
300
|
+
case '<':
|
|
301
|
+
return item => (compare(item) ?? 0) < 0;
|
|
302
|
+
case '<=':
|
|
303
|
+
return item => {
|
|
304
|
+
const result = compare(item);
|
|
305
|
+
return result !== undefined && result <= 0;
|
|
306
|
+
};
|
|
307
|
+
case '>':
|
|
308
|
+
return item => (compare(item) ?? 0) > 0;
|
|
309
|
+
default:
|
|
310
|
+
return item => {
|
|
311
|
+
const result = compare(item);
|
|
312
|
+
return result !== undefined && result >= 0;
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
const compileBetween = (column, value, compiler) => {
|
|
317
|
+
const read = readerFor(column, compiler);
|
|
318
|
+
const [start, end] = Array.isArray(value) ? value : [];
|
|
319
|
+
const lower = isBlankValue(start) ? always : compileOrdering(column, '>=', start, compiler);
|
|
320
|
+
const upper = isBlankValue(end) ? always : compileOrdering(column, '<=', end, compiler);
|
|
321
|
+
if (lower === always && upper === always) {
|
|
322
|
+
return always;
|
|
323
|
+
}
|
|
324
|
+
return item => !isBlankValue(read(item)) && lower(item) && upper(item);
|
|
325
|
+
};
|
|
326
|
+
const compileSet = (column, operation, value, compiler) => {
|
|
327
|
+
const read = readerFor(column, compiler);
|
|
328
|
+
const values = Array.isArray(value) ? value : [];
|
|
329
|
+
const dataType = compiler.context.dataTypeOf(column);
|
|
330
|
+
const blanks = values.some(isBlankValue);
|
|
331
|
+
const keys = new Set();
|
|
332
|
+
const cellKeyOfs = new Set();
|
|
333
|
+
for (const entry of values) {
|
|
334
|
+
if (isBlankValue(entry)) {
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
const keyOf = keyFunctions(dataType, entry) ?? ((v) => foldText(toDisplayText(v)));
|
|
338
|
+
const key = keyOf(entry);
|
|
339
|
+
if (key !== undefined) {
|
|
340
|
+
keys.add(key);
|
|
341
|
+
cellKeyOfs.add(keyOf);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
const keyOfs = [...cellKeyOfs];
|
|
345
|
+
const memo = new Map();
|
|
346
|
+
const matches = (cell) => {
|
|
347
|
+
if (isBlankValue(cell)) {
|
|
348
|
+
return blanks;
|
|
349
|
+
}
|
|
350
|
+
const primitive = typeof cell !== 'object';
|
|
351
|
+
const cached = primitive ? memo.get(cell) : undefined;
|
|
352
|
+
if (cached !== undefined) {
|
|
353
|
+
return cached;
|
|
354
|
+
}
|
|
355
|
+
const result = keyOfs.some(keyOf => keys.has(keyOf(cell)));
|
|
356
|
+
if (primitive) {
|
|
357
|
+
memo.set(cell, result);
|
|
358
|
+
}
|
|
359
|
+
return result;
|
|
360
|
+
};
|
|
361
|
+
const negate = operation === 'noneof';
|
|
362
|
+
return item => matches(read(item)) !== negate;
|
|
363
|
+
};
|
|
364
|
+
const compileCondition = (condition, compiler) => {
|
|
365
|
+
const [field, operation, value] = conditionParts(condition);
|
|
366
|
+
if (!isFilterOperation(operation)) {
|
|
367
|
+
throw new FilterExpressionError(`Unknown filter operation "${String(operation)}".`);
|
|
368
|
+
}
|
|
369
|
+
const column = resolveColumn(field, compiler);
|
|
370
|
+
const read = readerFor(column, compiler);
|
|
371
|
+
switch (operation) {
|
|
372
|
+
case 'contains':
|
|
373
|
+
case 'notcontains':
|
|
374
|
+
case 'startswith':
|
|
375
|
+
case 'endswith':
|
|
376
|
+
return compileTextOperation(column, operation, value, compiler);
|
|
377
|
+
case '=':
|
|
378
|
+
return compileEquality(column, value, compiler);
|
|
379
|
+
case '<>': {
|
|
380
|
+
const equals = compileEquality(column, value, compiler);
|
|
381
|
+
return item => !equals(item);
|
|
382
|
+
}
|
|
383
|
+
case '<':
|
|
384
|
+
case '<=':
|
|
385
|
+
case '>':
|
|
386
|
+
case '>=':
|
|
387
|
+
return compileOrdering(column, operation, value, compiler);
|
|
388
|
+
case 'between':
|
|
389
|
+
return compileBetween(column, value, compiler);
|
|
390
|
+
case 'anyof':
|
|
391
|
+
case 'noneof':
|
|
392
|
+
return compileSet(column, operation, value, compiler);
|
|
393
|
+
case 'isblank':
|
|
394
|
+
return item => isBlankValue(read(item));
|
|
395
|
+
case 'isnotblank':
|
|
396
|
+
return item => !isBlankValue(read(item));
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
/**
|
|
400
|
+
* Grupo `[a, 'and', b, 'or', c]`: `and` liga mais forte que `or`; dois
|
|
401
|
+
* itens seguidos sem conector valem `and`.
|
|
402
|
+
*/
|
|
403
|
+
const compileGroup = (group, compiler) => {
|
|
404
|
+
const alternatives = [[]];
|
|
405
|
+
let expectOperand = true;
|
|
406
|
+
for (const entry of group) {
|
|
407
|
+
if (isConnector(entry)) {
|
|
408
|
+
if (expectOperand) {
|
|
409
|
+
throw new FilterExpressionError(`Misplaced "${entry}" in filter expression.`);
|
|
410
|
+
}
|
|
411
|
+
if (entry === 'or') {
|
|
412
|
+
alternatives.push([]);
|
|
413
|
+
}
|
|
414
|
+
expectOperand = true;
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
alternatives[alternatives.length - 1].push(compileNode(entry, compiler));
|
|
418
|
+
expectOperand = false;
|
|
419
|
+
}
|
|
420
|
+
if (expectOperand && group.length) {
|
|
421
|
+
throw new FilterExpressionError('Filter expression ends with a connector.');
|
|
422
|
+
}
|
|
423
|
+
const clauses = alternatives
|
|
424
|
+
.filter(list => list.length)
|
|
425
|
+
.map(list => list.filter(predicate => predicate !== always))
|
|
426
|
+
.map(list => list.includes(never)
|
|
427
|
+
? never
|
|
428
|
+
: list.length === 1 ? list[0] : list.length === 0 ? always : every(list));
|
|
429
|
+
if (!clauses.length || clauses.includes(always)) {
|
|
430
|
+
return always;
|
|
431
|
+
}
|
|
432
|
+
const possible = clauses.filter(clause => clause !== never);
|
|
433
|
+
if (!possible.length) {
|
|
434
|
+
return never;
|
|
435
|
+
}
|
|
436
|
+
return possible.length === 1 ? possible[0] : some(possible);
|
|
437
|
+
};
|
|
438
|
+
const compileNode = (node, compiler) => {
|
|
439
|
+
if (!Array.isArray(node)) {
|
|
440
|
+
throw new FilterExpressionError('Filter expression must be an array.');
|
|
441
|
+
}
|
|
442
|
+
if (node[0] === '!') {
|
|
443
|
+
const inner = compileNode(node[1], compiler);
|
|
444
|
+
return inner === always ? never : inner === never ? always : item => !inner(item);
|
|
445
|
+
}
|
|
446
|
+
return isFilterCondition(node)
|
|
447
|
+
? compileCondition(node, compiler)
|
|
448
|
+
: compileGroup(node, compiler);
|
|
449
|
+
};
|
|
450
|
+
/**
|
|
451
|
+
* Compila a expressão num predicado (uma vez por mudança de filtro; avaliar
|
|
452
|
+
* linha a linha fica barato). `null` quando não há filtro efetivo.
|
|
453
|
+
* Lança {@link FilterExpressionError} para expressão malformada.
|
|
454
|
+
*/
|
|
455
|
+
export const compileFilter = (expression, context) => {
|
|
456
|
+
if (!expression || (Array.isArray(expression) && !expression.length)) {
|
|
457
|
+
return null;
|
|
458
|
+
}
|
|
459
|
+
const predicate = compileNode(expression, { context, synthetic: new Map(), readers: new Map() });
|
|
460
|
+
return predicate === always ? null : predicate;
|
|
461
|
+
};
|
|
462
|
+
/** Junta expressões com `and`, ignorando as vazias. */
|
|
463
|
+
export const combineFilters = (...expressions) => {
|
|
464
|
+
const present = expressions.filter((expression) => !!expression && expression.length > 0);
|
|
465
|
+
if (present.length < 2) {
|
|
466
|
+
return present[0] ?? null;
|
|
467
|
+
}
|
|
468
|
+
return present.flatMap((expression, index) => index ? ['and', expression] : [expression]);
|
|
469
|
+
};
|
|
470
|
+
// ------------------------------------------------------------- aplicação
|
|
471
|
+
/** Máscara de `items` que passam (1 = passa). */
|
|
472
|
+
export const filterMask = (items, predicate, mask = new Uint8Array(items.length), start = 0, end = items.length) => {
|
|
473
|
+
for (let index = start; index < end; index++) {
|
|
474
|
+
mask[index] = predicate(items[index]) ? 1 : 0;
|
|
475
|
+
}
|
|
476
|
+
return mask;
|
|
477
|
+
};
|
|
478
|
+
/**
|
|
479
|
+
* Itens na ordem `order` (índices de `items`; `null` = ordem original) que
|
|
480
|
+
* passam pela máscara (`null` = todos). Sem ordem nem máscara devolve o
|
|
481
|
+
* próprio `items`.
|
|
482
|
+
*/
|
|
483
|
+
export const applyMask = (items, order, mask) => {
|
|
484
|
+
if (!mask) {
|
|
485
|
+
if (!order) {
|
|
486
|
+
return items;
|
|
487
|
+
}
|
|
488
|
+
const sorted = new Array(order.length);
|
|
489
|
+
for (let i = 0; i < order.length; i++) {
|
|
490
|
+
sorted[i] = items[order[i]];
|
|
491
|
+
}
|
|
492
|
+
return sorted;
|
|
493
|
+
}
|
|
494
|
+
const result = [];
|
|
495
|
+
if (order) {
|
|
496
|
+
for (const index of order) {
|
|
497
|
+
if (mask[index]) {
|
|
498
|
+
result.push(items[index]);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
else {
|
|
503
|
+
for (let index = 0; index < items.length; index++) {
|
|
504
|
+
if (mask[index]) {
|
|
505
|
+
result.push(items[index]);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
return result;
|
|
510
|
+
};
|
|
511
|
+
/** Itens que passam, na ordem de `items` (nunca muta `items`). */
|
|
512
|
+
export const filterItems = (items, predicate) => predicate ? items.filter(item => predicate(item)) : items;
|
|
513
|
+
// ----------------------------------------------------------------- busca
|
|
514
|
+
/**
|
|
515
|
+
* Expressão da busca: `contains` em qualquer das colunas, ligadas por `or`.
|
|
516
|
+
* `null` sem texto ou sem colunas.
|
|
517
|
+
*/
|
|
518
|
+
export const searchExpression = (text, columns) => {
|
|
519
|
+
const conditions = !text.trim() ? [] : columns.filter(column => column.path).map(column => [column.path, 'contains', text]);
|
|
520
|
+
if (!conditions.length) {
|
|
521
|
+
return null;
|
|
522
|
+
}
|
|
523
|
+
return conditions.length === 1
|
|
524
|
+
? conditions[0]
|
|
525
|
+
: conditions.flatMap((condition, index) => index ? ['or', condition] : [condition]);
|
|
526
|
+
};
|
|
527
|
+
// ------------------------------------------- sincronia com a interface
|
|
528
|
+
/**
|
|
529
|
+
* Partes de uma expressão ligadas por `and` no nível de cima. Uma expressão
|
|
530
|
+
* com `or` no topo (ou negada) é uma parte só, que a interface não edita.
|
|
531
|
+
*/
|
|
532
|
+
export const splitConjuncts = (expression) => {
|
|
533
|
+
if (!expression?.length) {
|
|
534
|
+
return [];
|
|
535
|
+
}
|
|
536
|
+
if (isFilterCondition(expression) || expression[0] === '!') {
|
|
537
|
+
return [expression];
|
|
538
|
+
}
|
|
539
|
+
const group = expression;
|
|
540
|
+
if (group.includes('or')) {
|
|
541
|
+
return [expression];
|
|
542
|
+
}
|
|
543
|
+
return group.filter(entry => !isConnector(entry));
|
|
544
|
+
};
|
|
545
|
+
const sameField = (field, column) => !!column.path && field === column.path;
|
|
546
|
+
const kindOf = (operation) => operation === 'anyof' || operation === 'noneof' ? 'header' : 'row';
|
|
547
|
+
/** Condição da coluna (no nível de cima da expressão) para a linha de filtro ou o cabeçalho. */
|
|
548
|
+
export const findColumnCondition = (expression, column, kind) => {
|
|
549
|
+
for (const part of splitConjuncts(expression)) {
|
|
550
|
+
if (!isFilterCondition(part)) {
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
const [field, operation, value] = conditionParts(part);
|
|
554
|
+
if (isFilterOperation(operation) && sameField(field, column) && kindOf(operation) === kind) {
|
|
555
|
+
return [operation, value];
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
return undefined;
|
|
559
|
+
};
|
|
560
|
+
/** A coluna tem alguma condição no nível de cima (ícone de funil ativo)? */
|
|
561
|
+
export const hasColumnCondition = (expression, column) => splitConjuncts(expression).some(part => isFilterCondition(part) && sameField(conditionParts(part)[0], column));
|
|
562
|
+
/**
|
|
563
|
+
* Troca a condição da coluna (do `kind`) por `condition` — ou só tira, com
|
|
564
|
+
* `null`. O resto da expressão fica como estava. Devolve uma expressão nova
|
|
565
|
+
* (`null` se ficou vazia); nunca muta a de entrada.
|
|
566
|
+
*/
|
|
567
|
+
export const replaceColumnCondition = (expression, column, kind, condition) => {
|
|
568
|
+
const parts = splitConjuncts(expression).filter(part => {
|
|
569
|
+
if (!isFilterCondition(part)) {
|
|
570
|
+
return true;
|
|
571
|
+
}
|
|
572
|
+
const [field, operation] = conditionParts(part);
|
|
573
|
+
return !sameField(field, column) || (kind !== 'all' && isFilterOperation(operation) && kindOf(operation) !== kind);
|
|
574
|
+
});
|
|
575
|
+
if (condition) {
|
|
576
|
+
parts.push(condition);
|
|
577
|
+
}
|
|
578
|
+
return combineFilters(...parts);
|
|
579
|
+
};
|
|
580
|
+
/** Cópia profunda dos arrays da expressão (colunas e valores não-array ficam por referência). */
|
|
581
|
+
export const cloneExpression = (expression) => Array.isArray(expression) ? expression.map(cloneExpression) : expression;
|
|
582
|
+
//# sourceMappingURL=filter.js.map
|