foldkit-data-grid 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +210 -1
- package/dist/columns.d.ts +48 -8
- package/dist/columns.d.ts.map +1 -1
- package/dist/columns.js +62 -16
- package/dist/columns.js.map +1 -1
- package/dist/crud.d.ts +10 -1
- package/dist/crud.d.ts.map +1 -1
- package/dist/crud.js.map +1 -1
- package/dist/filter.d.ts +120 -0
- package/dist/filter.d.ts.map +1 -0
- package/dist/filter.js +191 -0
- package/dist/filter.js.map +1 -0
- package/dist/grid.d.ts +551 -12
- package/dist/grid.d.ts.map +1 -1
- package/dist/grid.js +545 -263
- package/dist/grid.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/package.json +7 -7
package/dist/filter.d.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { Option, Schema } from "effect";
|
|
2
|
+
import { CellEditor } from "./columns.js";
|
|
3
|
+
/**
|
|
4
|
+
* What one column is filtered by: the application's to hold and to apply.
|
|
5
|
+
* One variant per kind of column: text `Contains`, a number `Range`, a choice
|
|
6
|
+
* `OneOf`, a checkbox `Is`, a date `Between`. A filter that keeps every row
|
|
7
|
+
* is never one of these: it is none.
|
|
8
|
+
*/
|
|
9
|
+
export declare const ColumnFilter: import("foldkit/schema").TaggedUnion<{
|
|
10
|
+
/** The text holds this, folding ASCII case, as `Expr.contains` does. */
|
|
11
|
+
readonly Contains: {
|
|
12
|
+
text: Schema.String;
|
|
13
|
+
};
|
|
14
|
+
/** At least `min`, at most `max`, each when given. */
|
|
15
|
+
readonly Range: {
|
|
16
|
+
min: Schema.OptionFromNullOr<Schema.Finite>;
|
|
17
|
+
max: Schema.OptionFromNullOr<Schema.Finite>;
|
|
18
|
+
};
|
|
19
|
+
/** One of these literals. */
|
|
20
|
+
readonly OneOf: {
|
|
21
|
+
options: Schema.$Array<Schema.String>;
|
|
22
|
+
};
|
|
23
|
+
readonly Is: {
|
|
24
|
+
checked: Schema.Boolean;
|
|
25
|
+
};
|
|
26
|
+
/** On or after `from`, on or before `to`: local days, each when given. */
|
|
27
|
+
readonly Between: {
|
|
28
|
+
from: Schema.OptionFromNullOr<Schema.String>;
|
|
29
|
+
to: Schema.OptionFromNullOr<Schema.String>;
|
|
30
|
+
};
|
|
31
|
+
}>;
|
|
32
|
+
export type ColumnFilter = typeof ColumnFilter.Type;
|
|
33
|
+
type Contains = typeof ColumnFilter.Contains.Type;
|
|
34
|
+
type Range = typeof ColumnFilter.Range.Type;
|
|
35
|
+
type Is = typeof ColumnFilter.Is.Type;
|
|
36
|
+
type Between = typeof ColumnFilter.Between.Type;
|
|
37
|
+
/** A choice's filter, its options the column's own literals. */
|
|
38
|
+
export type OneOf<V extends string> = {
|
|
39
|
+
readonly _tag: 'OneOf';
|
|
40
|
+
readonly options: ReadonlyArray<V>;
|
|
41
|
+
};
|
|
42
|
+
type IsTextLiteral<M> = M extends string ? string extends M ? false : {} extends Record<M, 1> ? false : true : false;
|
|
43
|
+
type AllTextLiterals<V> = [V] extends [string] ? false extends (V extends unknown ? IsTextLiteral<V> : never) ? false : true : false;
|
|
44
|
+
/** What `filterKindOf` reads from a schema's decoded side, at the type level. */
|
|
45
|
+
export type KindOfValue<V> = [V] extends [boolean] ? 'Checkbox' : [V] extends [Date] ? 'Date' : [V] extends [number] ? 'Number' : AllTextLiterals<V> extends true ? 'Choice' : 'Text';
|
|
46
|
+
/** A column's filter kind, as the grid reads it at run time. */
|
|
47
|
+
export type FilterKind<Spec> = Spec extends {
|
|
48
|
+
readonly filter: infer F;
|
|
49
|
+
} ? F extends true ? Spec extends {
|
|
50
|
+
readonly edit: {
|
|
51
|
+
readonly schema: Schema.Codec<infer V, string>;
|
|
52
|
+
};
|
|
53
|
+
} ? KindOfValue<V> : 'Text' : F extends {
|
|
54
|
+
readonly _tag: infer T;
|
|
55
|
+
} ? T : never : never;
|
|
56
|
+
type ChoiceOf<Spec> = Spec extends {
|
|
57
|
+
readonly filter: true;
|
|
58
|
+
} ? Spec extends {
|
|
59
|
+
readonly edit: {
|
|
60
|
+
readonly schema: Schema.Codec<infer V, string>;
|
|
61
|
+
};
|
|
62
|
+
} ? V & string : string : string;
|
|
63
|
+
/** The filter of a column, by its spec: the variant its kind takes. */
|
|
64
|
+
export type FilterOf<Spec> = {
|
|
65
|
+
Text: Contains;
|
|
66
|
+
Number: Range;
|
|
67
|
+
Choice: OneOf<ChoiceOf<Spec>>;
|
|
68
|
+
Checkbox: Is;
|
|
69
|
+
Date: Between;
|
|
70
|
+
}[FilterKind<Spec> & ('Text' | 'Number' | 'Choice' | 'Checkbox' | 'Date')];
|
|
71
|
+
/** What `keeps` reads from a row for a filter: a Range a number, a Between a `Date`. */
|
|
72
|
+
export type FilterValue<F> = F extends Contains ? string : F extends Range ? number : F extends OneOf<infer V> ? V : F extends Is ? boolean : F extends Between ? Date : never;
|
|
73
|
+
/** The ids of the columns that filter. */
|
|
74
|
+
export type FilterableId<Specs> = { [K in keyof Specs]: Specs[K] extends {
|
|
75
|
+
readonly filter: true | CellEditor;
|
|
76
|
+
} ? K : never; }[keyof Specs] & string;
|
|
77
|
+
/** Every filterable column's filter, none where it filters nothing: the application's value. */
|
|
78
|
+
export type Filters<Specs> = { readonly [K in FilterableId<Specs>]: Option.Option<FilterOf<Specs[K]>>; };
|
|
79
|
+
/** What `Out.Filtered` carries: one case per filterable column, its filter that column's variant. */
|
|
80
|
+
export type FilterChange<Specs> = { [K in FilterableId<Specs>]: {
|
|
81
|
+
readonly column: K;
|
|
82
|
+
readonly filter: Option.Option<FilterOf<Specs[K]>>;
|
|
83
|
+
}; }[FilterableId<Specs>];
|
|
84
|
+
/**
|
|
85
|
+
* The stored form of one column's filter: its own variant, never one that
|
|
86
|
+
* keeps every row, a choice only of its literals, each once, and bounds
|
|
87
|
+
* that are finite numbers.
|
|
88
|
+
*/
|
|
89
|
+
export declare const filterSchemaOf: (kind: CellEditor) => Schema.Codec<ColumnFilter, unknown>;
|
|
90
|
+
/**
|
|
91
|
+
* An open filter form as typed: each kind reads its own fields, and a bound
|
|
92
|
+
* is text, so it holds `1.` while it is typed.
|
|
93
|
+
*/
|
|
94
|
+
export declare const FilterForm: Schema.Struct<{
|
|
95
|
+
readonly text: Schema.String;
|
|
96
|
+
readonly min: Schema.String;
|
|
97
|
+
readonly max: Schema.String;
|
|
98
|
+
readonly options: Schema.$Array<Schema.String>;
|
|
99
|
+
readonly checked: Schema.Literals<readonly ["any", "true", "false"]>;
|
|
100
|
+
readonly from: Schema.String;
|
|
101
|
+
readonly to: Schema.String;
|
|
102
|
+
}>;
|
|
103
|
+
export type FilterForm = typeof FilterForm.Type;
|
|
104
|
+
export declare const emptyForm: FilterForm;
|
|
105
|
+
/** The form a filter is drawn as; none is the empty form. */
|
|
106
|
+
export declare const formOf: (filter: Option.Option<ColumnFilter>) => FilterForm;
|
|
107
|
+
/** What a form reads as: a filter or none, unless a bound is no number; and whether its bounds cross. */
|
|
108
|
+
export interface FormReading {
|
|
109
|
+
readonly filter: Option.Option<ColumnFilter>;
|
|
110
|
+
/** The bounds that are not numbers (or days): while any is, nothing is reported. */
|
|
111
|
+
readonly invalid: ReadonlyArray<'min' | 'max' | 'from' | 'to'>;
|
|
112
|
+
/** The lower bound is above the upper: reported, and it keeps no row. */
|
|
113
|
+
readonly crossed: boolean;
|
|
114
|
+
}
|
|
115
|
+
/** A kind's form read as its filter. */
|
|
116
|
+
export declare const readForm: (kind: CellEditor, form: FilterForm) => FormReading;
|
|
117
|
+
/** Whether one filter keeps a value read from a row. */
|
|
118
|
+
export declare const testOf: (filter: ColumnFilter) => ((value: unknown) => boolean);
|
|
119
|
+
export {};
|
|
120
|
+
//# sourceMappingURL=filter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filter.d.ts","names":[],"sources":["../src/filter.ts"],"mappings":";;;;;;;;qBAaa,uCAAY;;;IAEX,MAAI,OAAA;;;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kBAAgB;;iBAgB5C;WACN,QAAQ,OAAO,OAAO;;WAEtB,SAAS;;WAET;;;qBAmBE,WAAQ,MAAU,YAAU,MAAQ,eAAa;;qBAuEjD,SAAM,QAAY,mBAAiB"}
|
package/dist/filter.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { Option, Schema } from "effect";
|
|
2
|
+
import { CellEditor } from "./columns.js";
|
|
3
|
+
import { defineTaggedUnion } from "foldkit/schema";
|
|
4
|
+
// src/filter.ts
|
|
5
|
+
/** A local calendar day, `YYYY-MM-DD`, as a date input reads it. */
|
|
6
|
+
const Day = Schema.String.check(Schema.isPattern(/^\d{4}-\d{2}-\d{2}$/));
|
|
7
|
+
/**
|
|
8
|
+
* What one column is filtered by: the application's to hold and to apply.
|
|
9
|
+
* One variant per kind of column: text `Contains`, a number `Range`, a choice
|
|
10
|
+
* `OneOf`, a checkbox `Is`, a date `Between`. A filter that keeps every row
|
|
11
|
+
* is never one of these: it is none.
|
|
12
|
+
*/
|
|
13
|
+
const ColumnFilter = defineTaggedUnion({
|
|
14
|
+
/** The text holds this, folding ASCII case, as `Expr.contains` does. */
|
|
15
|
+
Contains: { text: Schema.String },
|
|
16
|
+
/** At least `min`, at most `max`, each when given. */
|
|
17
|
+
Range: {
|
|
18
|
+
min: Schema.OptionFromNullOr(Schema.Finite),
|
|
19
|
+
max: Schema.OptionFromNullOr(Schema.Finite)
|
|
20
|
+
},
|
|
21
|
+
/** One of these literals. */
|
|
22
|
+
OneOf: { options: Schema.Array(Schema.String) },
|
|
23
|
+
Is: { checked: Schema.Boolean },
|
|
24
|
+
/** On or after `from`, on or before `to`: local days, each when given. */
|
|
25
|
+
Between: {
|
|
26
|
+
from: Schema.OptionFromNullOr(Day),
|
|
27
|
+
to: Schema.OptionFromNullOr(Day)
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
/**
|
|
31
|
+
* The stored form of one column's filter: its own variant, never one that
|
|
32
|
+
* keeps every row, a choice only of its literals, each once, and bounds
|
|
33
|
+
* that are finite numbers.
|
|
34
|
+
*/
|
|
35
|
+
const filterSchemaOf = (kind) => {
|
|
36
|
+
const someBound = (a, b) => Option.isSome(a) || Option.isSome(b);
|
|
37
|
+
return CellEditor.match(kind, {
|
|
38
|
+
Text: () => Schema.TaggedStruct("Contains", { text: Schema.String.check(Schema.isMinLength(1)) }),
|
|
39
|
+
Number: () => ColumnFilter.Range.check(Schema.makeFilter(({ min, max }) => someBound(min, max), { expected: "a bound" })),
|
|
40
|
+
Choice: ({ options }) => Schema.TaggedStruct("OneOf", { options: Schema.Array(Schema.Literals(options)).check(Schema.isMinLength(1), Schema.isUnique()) }),
|
|
41
|
+
Checkbox: () => ColumnFilter.Is,
|
|
42
|
+
Date: () => ColumnFilter.Between.check(Schema.makeFilter(({ from, to }) => someBound(from, to), { expected: "a bound" }))
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* An open filter form as typed: each kind reads its own fields, and a bound
|
|
47
|
+
* is text, so it holds `1.` while it is typed.
|
|
48
|
+
*/
|
|
49
|
+
const FilterForm = Schema.Struct({
|
|
50
|
+
text: Schema.String,
|
|
51
|
+
min: Schema.String,
|
|
52
|
+
max: Schema.String,
|
|
53
|
+
options: Schema.Array(Schema.String),
|
|
54
|
+
checked: Schema.Literals([
|
|
55
|
+
"any",
|
|
56
|
+
"true",
|
|
57
|
+
"false"
|
|
58
|
+
]),
|
|
59
|
+
from: Schema.String,
|
|
60
|
+
to: Schema.String
|
|
61
|
+
});
|
|
62
|
+
const emptyForm = {
|
|
63
|
+
text: "",
|
|
64
|
+
min: "",
|
|
65
|
+
max: "",
|
|
66
|
+
options: [],
|
|
67
|
+
checked: "any",
|
|
68
|
+
from: "",
|
|
69
|
+
to: ""
|
|
70
|
+
};
|
|
71
|
+
const shown = (value) => Option.match(value, {
|
|
72
|
+
onNone: () => "",
|
|
73
|
+
onSome: String
|
|
74
|
+
});
|
|
75
|
+
/** The form a filter is drawn as; none is the empty form. */
|
|
76
|
+
const formOf = (filter) => Option.match(filter, {
|
|
77
|
+
onNone: () => emptyForm,
|
|
78
|
+
onSome: ColumnFilter.match({
|
|
79
|
+
Contains: ({ text }) => ({
|
|
80
|
+
...emptyForm,
|
|
81
|
+
text
|
|
82
|
+
}),
|
|
83
|
+
Range: ({ min, max }) => ({
|
|
84
|
+
...emptyForm,
|
|
85
|
+
min: shown(min),
|
|
86
|
+
max: shown(max)
|
|
87
|
+
}),
|
|
88
|
+
OneOf: ({ options }) => ({
|
|
89
|
+
...emptyForm,
|
|
90
|
+
options
|
|
91
|
+
}),
|
|
92
|
+
Is: ({ checked }) => ({
|
|
93
|
+
...emptyForm,
|
|
94
|
+
checked: checked ? "true" : "false"
|
|
95
|
+
}),
|
|
96
|
+
Between: ({ from, to }) => ({
|
|
97
|
+
...emptyForm,
|
|
98
|
+
from: shown(from),
|
|
99
|
+
to: shown(to)
|
|
100
|
+
})
|
|
101
|
+
})
|
|
102
|
+
});
|
|
103
|
+
const decimal = /^[+-]?(\d+(\.\d*)?|\.\d+)$/;
|
|
104
|
+
const day = /^\d{4}-\d{2}-\d{2}$/;
|
|
105
|
+
const isNumber = (text) => decimal.test(text) && Number.isFinite(Number(text));
|
|
106
|
+
const isDay = (text) => day.test(text);
|
|
107
|
+
/** A bound as typed: none when empty, refused (the outer none) when it is not one. */
|
|
108
|
+
const bound = (text, accepts) => {
|
|
109
|
+
const trimmed = text.trim();
|
|
110
|
+
if (trimmed === "") return Option.some(Option.none());
|
|
111
|
+
return accepts(trimmed) ? Option.some(Option.some(trimmed)) : Option.none();
|
|
112
|
+
};
|
|
113
|
+
/** A kind's form read as its filter. */
|
|
114
|
+
const readForm = (kind, form) => {
|
|
115
|
+
const none = {
|
|
116
|
+
filter: Option.none(),
|
|
117
|
+
invalid: [],
|
|
118
|
+
crossed: false
|
|
119
|
+
};
|
|
120
|
+
const range = (lowField, highField, accepts, make, above) => {
|
|
121
|
+
const low = bound(form[lowField], accepts);
|
|
122
|
+
const high = bound(form[highField], accepts);
|
|
123
|
+
const invalid = [...Option.isNone(low) ? [lowField] : [], ...Option.isNone(high) ? [highField] : []];
|
|
124
|
+
if (Option.isNone(low) || Option.isNone(high)) return {
|
|
125
|
+
...none,
|
|
126
|
+
invalid
|
|
127
|
+
};
|
|
128
|
+
if (Option.isNone(low.value) && Option.isNone(high.value)) return none;
|
|
129
|
+
const crossed = Option.isSome(low.value) && Option.isSome(high.value) && above(low.value.value, high.value.value);
|
|
130
|
+
return {
|
|
131
|
+
filter: Option.some(make(low.value, high.value)),
|
|
132
|
+
invalid: [],
|
|
133
|
+
crossed
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
return CellEditor.match(kind, {
|
|
137
|
+
Text: () => form.text === "" ? none : {
|
|
138
|
+
...none,
|
|
139
|
+
filter: Option.some(ColumnFilter.Contains({ text: form.text }))
|
|
140
|
+
},
|
|
141
|
+
Number: () => range("min", "max", isNumber, (min, max) => ColumnFilter.Range({
|
|
142
|
+
min: Option.map(min, Number),
|
|
143
|
+
max: Option.map(max, Number)
|
|
144
|
+
}), (low, high) => Number(low) > Number(high)),
|
|
145
|
+
Choice: () => form.options.length === 0 ? none : {
|
|
146
|
+
...none,
|
|
147
|
+
filter: Option.some(ColumnFilter.OneOf({ options: form.options }))
|
|
148
|
+
},
|
|
149
|
+
Checkbox: () => form.checked === "any" ? none : {
|
|
150
|
+
...none,
|
|
151
|
+
filter: Option.some(ColumnFilter.Is({ checked: form.checked === "true" }))
|
|
152
|
+
},
|
|
153
|
+
Date: () => range("from", "to", isDay, (from, to) => ColumnFilter.Between({
|
|
154
|
+
from,
|
|
155
|
+
to
|
|
156
|
+
}), (low, high) => low > high)
|
|
157
|
+
});
|
|
158
|
+
};
|
|
159
|
+
/** Whether `test` holds of the value, or there is none to test. */
|
|
160
|
+
const unlessNone = (value, test) => Option.match(value, {
|
|
161
|
+
onNone: () => true,
|
|
162
|
+
onSome: test
|
|
163
|
+
});
|
|
164
|
+
const fold = (text) => text.replace(/[A-Z]/g, (letter) => String.fromCharCode(letter.charCodeAt(0) + 32));
|
|
165
|
+
const localDay = (date) => {
|
|
166
|
+
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
167
|
+
const day = String(date.getDate()).padStart(2, "0");
|
|
168
|
+
return `${date.getFullYear()}-${month}-${day}`;
|
|
169
|
+
};
|
|
170
|
+
/** Whether one filter keeps a value read from a row. */
|
|
171
|
+
const testOf = (filter) => ColumnFilter.match(filter, {
|
|
172
|
+
Contains: ({ text }) => {
|
|
173
|
+
const wanted = fold(text);
|
|
174
|
+
return (value) => fold(value).includes(wanted);
|
|
175
|
+
},
|
|
176
|
+
Range: ({ min, max }) => (value) => unlessNone(min, (low) => value >= low) && unlessNone(max, (high) => value <= high),
|
|
177
|
+
OneOf: ({ options }) => {
|
|
178
|
+
const among = new Set(options);
|
|
179
|
+
return (value) => among.has(value);
|
|
180
|
+
},
|
|
181
|
+
Is: ({ checked }) => (value) => value === checked,
|
|
182
|
+
Between: ({ from, to }) => (value) => {
|
|
183
|
+
const date = value;
|
|
184
|
+
if (Number.isNaN(date.getTime())) return false;
|
|
185
|
+
const at = localDay(date);
|
|
186
|
+
return unlessNone(from, (low) => at >= low) && unlessNone(to, (high) => at <= high);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
export { ColumnFilter, FilterForm, emptyForm, filterSchemaOf, formOf, readForm, testOf };
|
|
190
|
+
|
|
191
|
+
//# sourceMappingURL=filter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filter.js","names":[],"sources":["../src/filter.ts"],"sourcesContent":["import { Option, Schema } from 'effect'\nimport { defineTaggedUnion } from 'foldkit/schema'\nimport { CellEditor } from './columns.js'\n\n/** A local calendar day, `YYYY-MM-DD`, as a date input reads it. */\nconst Day = Schema.String.check(Schema.isPattern(/^\\d{4}-\\d{2}-\\d{2}$/))\n\n/**\n * What one column is filtered by: the application's to hold and to apply.\n * One variant per kind of column: text `Contains`, a number `Range`, a choice\n * `OneOf`, a checkbox `Is`, a date `Between`. A filter that keeps every row\n * is never one of these: it is none.\n */\nexport const ColumnFilter = defineTaggedUnion({\n /** The text holds this, folding ASCII case, as `Expr.contains` does. */\n Contains: { text: Schema.String },\n /** At least `min`, at most `max`, each when given. */\n Range: {\n min: Schema.OptionFromNullOr(Schema.Finite),\n max: Schema.OptionFromNullOr(Schema.Finite),\n },\n /** One of these literals. */\n OneOf: { options: Schema.Array(Schema.String) },\n Is: { checked: Schema.Boolean },\n /** On or after `from`, on or before `to`: local days, each when given. */\n Between: { from: Schema.OptionFromNullOr(Day), to: Schema.OptionFromNullOr(Day) },\n})\nexport type ColumnFilter = typeof ColumnFilter.Type\n\ntype Contains = typeof ColumnFilter.Contains.Type\ntype Range = typeof ColumnFilter.Range.Type\ntype Is = typeof ColumnFilter.Is.Type\ntype Between = typeof ColumnFilter.Between.Type\n/** A choice's filter, its options the column's own literals. */\nexport type OneOf<V extends string> = {\n readonly _tag: 'OneOf'\n readonly options: ReadonlyArray<V>\n}\n\ntype IsTextLiteral<M> = M extends string\n ? string extends M\n ? false\n : // A template literal or a branded string keys an index signature, which `{}` fits.\n {} extends Record<M, 1>\n ? false\n : true\n : false\ntype AllTextLiterals<V> = [V] extends [string]\n ? false extends (V extends unknown ? IsTextLiteral<V> : never)\n ? false\n : true\n : false\n/** What `filterKindOf` reads from a schema's decoded side, at the type level. */\nexport type KindOfValue<V> = [V] extends [boolean]\n ? 'Checkbox'\n : [V] extends [Date]\n ? 'Date'\n : [V] extends [number]\n ? 'Number'\n : AllTextLiterals<V> extends true\n ? 'Choice'\n : 'Text'\n/** A column's filter kind, as the grid reads it at run time. */\nexport type FilterKind<Spec> = Spec extends { readonly filter: infer F }\n ? F extends true\n ? Spec extends { readonly edit: { readonly schema: Schema.Codec<infer V, string> } }\n ? KindOfValue<V>\n : 'Text'\n : F extends { readonly _tag: infer T }\n ? T\n : never\n : never\n// Inferred in two steps: matched against `filter: true` and the schema at\n// once, a spec that is an intersection (`GridCrud.columns`) infers `unknown`.\ntype ChoiceOf<Spec> = Spec extends { readonly filter: true }\n ? Spec extends { readonly edit: { readonly schema: Schema.Codec<infer V, string> } }\n ? V & string\n : string\n : string\n/** The filter of a column, by its spec: the variant its kind takes. */\nexport type FilterOf<Spec> = {\n Text: Contains\n Number: Range\n Choice: OneOf<ChoiceOf<Spec>>\n Checkbox: Is\n Date: Between\n}[FilterKind<Spec> & ('Text' | 'Number' | 'Choice' | 'Checkbox' | 'Date')]\n/** What `keeps` reads from a row for a filter: a Range a number, a Between a `Date`. */\nexport type FilterValue<F> = F extends Contains\n ? string\n : F extends Range\n ? number\n : F extends OneOf<infer V>\n ? V\n : F extends Is\n ? boolean\n : F extends Between\n ? Date\n : never\n/** The ids of the columns that filter. */\nexport type FilterableId<Specs> = {\n [K in keyof Specs]: Specs[K] extends { readonly filter: true | CellEditor } ? K : never\n}[keyof Specs] &\n string\n/** Every filterable column's filter, none where it filters nothing: the application's value. */\nexport type Filters<Specs> = {\n readonly [K in FilterableId<Specs>]: Option.Option<FilterOf<Specs[K]>>\n}\n/** What `Out.Filtered` carries: one case per filterable column, its filter that column's variant. */\nexport type FilterChange<Specs> = {\n [K in FilterableId<Specs>]: {\n readonly column: K\n readonly filter: Option.Option<FilterOf<Specs[K]>>\n }\n}[FilterableId<Specs>]\n\n/**\n * The stored form of one column's filter: its own variant, never one that\n * keeps every row, a choice only of its literals, each once, and bounds\n * that are finite numbers.\n */\nexport const filterSchemaOf = (kind: CellEditor): Schema.Codec<ColumnFilter, unknown> => {\n // A Range or a Between with neither bound keeps every row: that is none.\n const someBound = (a: Option.Option<unknown>, b: Option.Option<unknown>) =>\n Option.isSome(a) || Option.isSome(b)\n const schema: Schema.Top = CellEditor.match(kind, {\n Text: () =>\n Schema.TaggedStruct('Contains', { text: Schema.String.check(Schema.isMinLength(1)) }),\n Number: () =>\n ColumnFilter.Range.check(\n Schema.makeFilter<Range>(({ min, max }) => someBound(min, max), { expected: 'a bound' }),\n ),\n Choice: ({ options }) =>\n Schema.TaggedStruct('OneOf', {\n options: Schema.Array(Schema.Literals(options)).check(\n Schema.isMinLength(1),\n Schema.isUnique(),\n ),\n }),\n Checkbox: () => ColumnFilter.Is,\n Date: () =>\n ColumnFilter.Between.check(\n Schema.makeFilter<Between>(({ from, to }) => someBound(from, to), { expected: 'a bound' }),\n ),\n })\n // Each case decodes to its variant of `ColumnFilter`, from JSON.\n return schema as unknown as Schema.Codec<ColumnFilter, unknown>\n}\n\n/**\n * An open filter form as typed: each kind reads its own fields, and a bound\n * is text, so it holds `1.` while it is typed.\n */\nexport const FilterForm = Schema.Struct({\n text: Schema.String,\n min: Schema.String,\n max: Schema.String,\n options: Schema.Array(Schema.String),\n checked: Schema.Literals(['any', 'true', 'false']),\n from: Schema.String,\n to: Schema.String,\n})\nexport type FilterForm = typeof FilterForm.Type\n\nexport const emptyForm: FilterForm = {\n text: '',\n min: '',\n max: '',\n options: [],\n checked: 'any',\n from: '',\n to: '',\n}\n\nconst shown = (value: Option.Option<number | string>): string =>\n Option.match(value, { onNone: () => '', onSome: String })\n\n/** The form a filter is drawn as; none is the empty form. */\nexport const formOf = (filter: Option.Option<ColumnFilter>): FilterForm =>\n Option.match(filter, {\n onNone: () => emptyForm,\n onSome: ColumnFilter.match({\n Contains: ({ text }) => ({ ...emptyForm, text }),\n Range: ({ min, max }) => ({ ...emptyForm, min: shown(min), max: shown(max) }),\n OneOf: ({ options }) => ({ ...emptyForm, options }),\n Is: ({ checked }) => ({\n ...emptyForm,\n checked: checked ? ('true' as const) : ('false' as const),\n }),\n Between: ({ from, to }) => ({ ...emptyForm, from: shown(from), to: shown(to) }),\n }),\n })\n\n/** What a form reads as: a filter or none, unless a bound is no number; and whether its bounds cross. */\nexport interface FormReading {\n readonly filter: Option.Option<ColumnFilter>\n /** The bounds that are not numbers (or days): while any is, nothing is reported. */\n readonly invalid: ReadonlyArray<'min' | 'max' | 'from' | 'to'>\n /** The lower bound is above the upper: reported, and it keeps no row. */\n readonly crossed: boolean\n}\n\nconst decimal = /^[+-]?(\\d+(\\.\\d*)?|\\.\\d+)$/\nconst day = /^\\d{4}-\\d{2}-\\d{2}$/\n// A number as typed, and finite: 309 digits read as Infinity, which no Range stores.\nconst isNumber = (text: string): boolean => decimal.test(text) && Number.isFinite(Number(text))\nconst isDay = (text: string): boolean => day.test(text)\n/** A bound as typed: none when empty, refused (the outer none) when it is not one. */\nconst bound = (\n text: string,\n accepts: (trimmed: string) => boolean,\n): Option.Option<Option.Option<string>> => {\n const trimmed = text.trim()\n if (trimmed === '') return Option.some(Option.none())\n return accepts(trimmed) ? Option.some(Option.some(trimmed)) : Option.none()\n}\n\n/** A kind's form read as its filter. */\nexport const readForm = (kind: CellEditor, form: FilterForm): FormReading => {\n const none = { filter: Option.none(), invalid: [], crossed: false } as const\n const range = <F extends 'min' | 'from', T extends 'max' | 'to'>(\n lowField: F,\n highField: T,\n accepts: (trimmed: string) => boolean,\n make: (low: Option.Option<string>, high: Option.Option<string>) => ColumnFilter,\n above: (low: string, high: string) => boolean,\n ): FormReading => {\n const low = bound(form[lowField], accepts)\n const high = bound(form[highField], accepts)\n const invalid = [\n ...(Option.isNone(low) ? [lowField] : []),\n ...(Option.isNone(high) ? [highField] : []),\n ]\n if (Option.isNone(low) || Option.isNone(high)) return { ...none, invalid }\n if (Option.isNone(low.value) && Option.isNone(high.value)) return none\n const crossed =\n Option.isSome(low.value) &&\n Option.isSome(high.value) &&\n above(low.value.value, high.value.value)\n return { filter: Option.some(make(low.value, high.value)), invalid: [], crossed }\n }\n return CellEditor.match(kind, {\n Text: () =>\n form.text === ''\n ? none\n : { ...none, filter: Option.some(ColumnFilter.Contains({ text: form.text })) },\n Number: () =>\n range(\n 'min',\n 'max',\n isNumber,\n (min, max) =>\n ColumnFilter.Range({ min: Option.map(min, Number), max: Option.map(max, Number) }),\n (low, high) => Number(low) > Number(high),\n ),\n Choice: () =>\n form.options.length === 0\n ? none\n : { ...none, filter: Option.some(ColumnFilter.OneOf({ options: form.options })) },\n Checkbox: () =>\n form.checked === 'any'\n ? none\n : { ...none, filter: Option.some(ColumnFilter.Is({ checked: form.checked === 'true' })) },\n Date: () =>\n range(\n 'from',\n 'to',\n isDay,\n (from, to) => ColumnFilter.Between({ from, to }),\n (low, high) => low > high,\n ),\n })\n}\n\n/** Whether `test` holds of the value, or there is none to test. */\nconst unlessNone = <A>(value: Option.Option<A>, test: (a: A) => boolean): boolean =>\n Option.match(value, { onNone: () => true, onSome: test })\n\n// ASCII only, as `Expr.contains` folds on every backend.\nconst fold = (text: string): string =>\n text.replace(/[A-Z]/g, letter => String.fromCharCode(letter.charCodeAt(0) + 32))\n\nconst localDay = (date: Date): string => {\n const month = String(date.getMonth() + 1).padStart(2, '0')\n const day = String(date.getDate()).padStart(2, '0')\n return `${date.getFullYear()}-${month}-${day}`\n}\n\n/** Whether one filter keeps a value read from a row. */\nexport const testOf = (filter: ColumnFilter): ((value: unknown) => boolean) =>\n ColumnFilter.match(filter, {\n Contains: ({ text }) => {\n const wanted = fold(text)\n return (value: unknown) => fold(value as string).includes(wanted)\n },\n Range:\n ({ min, max }) =>\n (value: unknown) =>\n unlessNone(min, low => (value as number) >= low) &&\n unlessNone(max, high => (value as number) <= high),\n OneOf: ({ options }) => {\n const among = new Set<unknown>(options)\n return (value: unknown) => among.has(value)\n },\n Is:\n ({ checked }) =>\n (value: unknown) =>\n value === checked,\n Between:\n ({ from, to }) =>\n (value: unknown) => {\n const date = value as Date\n if (Number.isNaN(date.getTime())) return false\n const at = localDay(date)\n return unlessNone(from, low => at >= low) && unlessNone(to, high => at <= high)\n },\n })\n"],"mappings":";;;;;AAKA,MAAM,MAAM,OAAO,OAAO,MAAM,OAAO,UAAU,qBAAqB,CAAC;;;;;;;AAQvE,MAAa,eAAe,kBAAkB;;CAE5C,UAAU,EAAE,MAAM,OAAO,OAAO;;CAEhC,OAAO;EACL,KAAK,OAAO,iBAAiB,OAAO,MAAM;EAC1C,KAAK,OAAO,iBAAi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