@malloy-publisher/sdk 0.0.247 → 0.0.249

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/README.md +32 -17
  2. package/dist/ServerProvider-CcfeBpt3.cjs.js +1 -0
  3. package/dist/{ServerProvider-BxEh3nFI.es.js → ServerProvider-EqBlMMqx.es.js} +1056 -905
  4. package/dist/client/api.d.ts +298 -7
  5. package/dist/client/index.cjs.js +1 -1
  6. package/dist/client/index.es.js +74 -70
  7. package/dist/components/Dashboard/Dashboard.d.ts +49 -0
  8. package/dist/components/Dashboard/DashboardTile.d.ts +48 -0
  9. package/dist/components/Dashboard/index.d.ts +2 -0
  10. package/dist/components/Notebook/Notebook.d.ts +46 -8
  11. package/dist/components/Notebook/NotebookCell.d.ts +37 -3
  12. package/dist/components/Notebook/runPlan.d.ts +29 -0
  13. package/dist/components/Notebook/types.d.ts +10 -0
  14. package/dist/components/Package/ContentTypeIcon.d.ts +17 -1
  15. package/dist/components/Package/Package.d.ts +1 -4
  16. package/dist/components/RenderedResult/RenderedResult.d.ts +7 -1
  17. package/dist/components/RenderedResult/ResultContainer.d.ts +3 -1
  18. package/dist/components/ResultsDialog.d.ts +4 -1
  19. package/dist/components/ServerProvider.d.ts +2 -1
  20. package/dist/components/click_helper.d.ts +10 -1
  21. package/dist/components/drill/index.d.ts +3 -0
  22. package/dist/components/drill/markDrillableCells.d.ts +55 -0
  23. package/dist/components/drill/resolveDrill.d.ts +159 -0
  24. package/dist/components/drill/useDrill.d.ts +63 -0
  25. package/dist/components/given/GivenInput.d.ts +27 -3
  26. package/dist/components/given/GivensPanel.d.ts +36 -7
  27. package/dist/components/given/filterValue.d.ts +117 -0
  28. package/dist/components/given/index.d.ts +1 -1
  29. package/dist/components/given/paramCodec.d.ts +60 -0
  30. package/dist/components/index.d.ts +4 -1
  31. package/dist/components/styles.d.ts +23 -0
  32. package/dist/constants/docLinks.d.ts +6 -0
  33. package/dist/{core-BuvlzSIX.es.js → core-DFBwNhvb.es.js} +1 -1
  34. package/dist/{core-C86Rb7tJ.cjs.js → core-DZk7z7ko.cjs.js} +1 -1
  35. package/dist/hooks/givenValue.d.ts +10 -0
  36. package/dist/hooks/index.d.ts +3 -1
  37. package/dist/hooks/useGivensState.d.ts +64 -0
  38. package/dist/hooks/useSuggestOptions.d.ts +78 -0
  39. package/dist/{index-BdtAZ5k6.es.js → index-CLcRrkxd.es.js} +41221 -39894
  40. package/dist/index-CWQXhe0D.cjs.js +338 -0
  41. package/dist/index.cjs.js +1 -1
  42. package/dist/index.es.js +77 -62
  43. package/dist/theme/types.d.ts +9 -0
  44. package/package.json +3 -1
  45. package/src/components/Connections/AddConnectionDialog.tsx +10 -1
  46. package/src/components/Dashboard/Dashboard.tsx +395 -0
  47. package/src/components/Dashboard/DashboardTile.tsx +150 -0
  48. package/src/components/Dashboard/index.ts +2 -0
  49. package/src/components/Home/Home.tsx +114 -23
  50. package/src/components/Notebook/Notebook.tsx +444 -390
  51. package/src/components/Notebook/NotebookCell.tsx +117 -3
  52. package/src/components/Notebook/runPlan.spec.ts +152 -0
  53. package/src/components/Notebook/runPlan.ts +64 -0
  54. package/src/components/Notebook/types.ts +11 -0
  55. package/src/components/Package/ContentTypeIcon.spec.ts +88 -0
  56. package/src/components/Package/ContentTypeIcon.tsx +77 -6
  57. package/src/components/Package/Package.tsx +148 -38
  58. package/src/components/RenderedResult/RenderedResult.tsx +309 -19
  59. package/src/components/RenderedResult/ResultContainer.tsx +28 -1
  60. package/src/components/ResultsDialog.tsx +5 -0
  61. package/src/components/ServerProvider.tsx +2 -0
  62. package/src/components/click_helper.ts +14 -1
  63. package/src/components/drill/index.ts +25 -0
  64. package/src/components/drill/markDrillableCells.dom.spec.ts +310 -0
  65. package/src/components/drill/markDrillableCells.spec.ts +106 -0
  66. package/src/components/drill/markDrillableCells.ts +311 -0
  67. package/src/components/drill/resolveDrill.spec.ts +446 -0
  68. package/src/components/drill/resolveDrill.ts +372 -0
  69. package/src/components/drill/useDrill.tsx +200 -0
  70. package/src/components/given/GivenInput.spec.tsx +495 -15
  71. package/src/components/given/GivenInput.tsx +562 -30
  72. package/src/components/given/GivensPanel.spec.tsx +172 -0
  73. package/src/components/given/GivensPanel.tsx +139 -32
  74. package/src/components/given/filterValue.spec.ts +361 -0
  75. package/src/components/given/filterValue.ts +204 -0
  76. package/src/components/given/index.ts +5 -1
  77. package/src/components/given/paramCodec.spec.ts +854 -0
  78. package/src/components/given/paramCodec.ts +480 -0
  79. package/src/components/index.ts +4 -1
  80. package/src/components/styles.ts +27 -3
  81. package/src/constants/docLinks.spec.ts +51 -0
  82. package/src/constants/docLinks.ts +26 -0
  83. package/src/hooks/givenValue.ts +10 -0
  84. package/src/hooks/index.ts +6 -4
  85. package/src/hooks/useGivensState.spec.ts +446 -0
  86. package/src/hooks/useGivensState.ts +337 -0
  87. package/src/hooks/useSuggestOptions.spec.ts +109 -0
  88. package/src/hooks/useSuggestOptions.ts +260 -0
  89. package/src/theme/buildTableCssVars.ts +4 -0
  90. package/src/theme/resolveTheme.spec.ts +13 -0
  91. package/src/theme/resolveTheme.ts +3 -0
  92. package/src/theme/types.ts +9 -0
  93. package/test/README.md +16 -10
  94. package/dist/ServerProvider-Dg5S1qBU.cjs.js +0 -1
  95. package/dist/hooks/useGivensForm.d.ts +0 -35
  96. package/dist/index-MES4MGpQ.cjs.js +0 -309
  97. package/src/hooks/useGivensForm.spec.ts +0 -99
  98. package/src/hooks/useGivensForm.ts +0 -107
@@ -0,0 +1,361 @@
1
+ import { StringFilterExpression } from "@malloydata/malloy-filter";
2
+ import { describe, expect, it } from "bun:test";
3
+ import {
4
+ decodeAtLeast,
5
+ decodeFilterList,
6
+ encodeAtLeast,
7
+ encodeFilterList,
8
+ filterInnerType,
9
+ isFilterType,
10
+ isPlainFilterList,
11
+ } from "./filterValue";
12
+
13
+ /**
14
+ * Values that are ordinary to a person and syntax to Malloy. Every one of these
15
+ * was a live corruption before the encoder delegated to the filter package:
16
+ * `-Outerwear` ran as a negation over the whole catalogue, `%` matched every
17
+ * row, `null` hit the null operator, and `Ben & Jerry, Inc` filtered for two
18
+ * brands, which is the exact case this module's docstring exists to prevent.
19
+ */
20
+ const HAZARDS = [
21
+ "Outerwear",
22
+ "-Outerwear",
23
+ "%",
24
+ "100%",
25
+ "%off%",
26
+ "_",
27
+ "a_b",
28
+ "null",
29
+ "NULL",
30
+ "empty",
31
+ "none",
32
+ "Ben & Jerry, Inc",
33
+ "a;b",
34
+ "a|b",
35
+ "(parens)",
36
+ "back\\slash",
37
+ "Levi's",
38
+ ' say "hi" ',
39
+ " padded ",
40
+ "-",
41
+ ];
42
+
43
+ /**
44
+ * Read the filter back with Malloy's own parser and return the values it will
45
+ * actually match on, or undefined if it is not a plain "is one of" clause.
46
+ *
47
+ * This is the assertion that matters. Checking `decode(encode(v))` only proves
48
+ * the two halves of this module agree with each other, which the previous
49
+ * quoting scheme also managed while agreeing with nothing else.
50
+ */
51
+ function valuesMalloyWillMatch(filter: string): string[] | undefined {
52
+ const { parsed } = StringFilterExpression.parse(filter);
53
+ if (parsed === null) return undefined;
54
+ if (parsed.operator !== "=" || parsed.not === true) return undefined;
55
+ return parsed.values;
56
+ }
57
+
58
+ describe("filter type helpers", () => {
59
+ it("recognizes the filter family and its inner type", () => {
60
+ expect(isFilterType("filter<string>")).toBe(true);
61
+ expect(isFilterType("string")).toBe(false);
62
+ expect(isFilterType(undefined)).toBe(false);
63
+ expect(filterInnerType("filter<timestamp>")).toBe("timestamp");
64
+ expect(filterInnerType("number")).toBe(undefined);
65
+ });
66
+ });
67
+
68
+ describe("encodeFilterList, against the real Malloy filter parser", () => {
69
+ it("makes every hazardous value match itself and nothing else", () => {
70
+ for (const value of HAZARDS) {
71
+ expect(valuesMalloyWillMatch(encodeFilterList([value]))).toEqual([
72
+ value,
73
+ ]);
74
+ }
75
+ });
76
+
77
+ it("keeps a multi-value selection intact whatever the values are", () => {
78
+ for (const first of HAZARDS) {
79
+ for (const second of HAZARDS) {
80
+ expect(
81
+ valuesMalloyWillMatch(encodeFilterList([first, second])),
82
+ ).toEqual([first, second]);
83
+ }
84
+ }
85
+ });
86
+
87
+ it("joins plain values the way a string filter reads them", () => {
88
+ expect(encodeFilterList(["us-east", "us-west"])).toBe(
89
+ "us\\-east, us\\-west",
90
+ );
91
+ expect(valuesMalloyWillMatch("us\\-east, us\\-west")).toEqual([
92
+ "us-east",
93
+ "us-west",
94
+ ]);
95
+ });
96
+
97
+ it("is empty for no selection, which is the natural All", () => {
98
+ expect(encodeFilterList([])).toBe("");
99
+ });
100
+
101
+ it("drops an empty value rather than inventing a spelling for it", () => {
102
+ // Malloy's `empty` operator means null-or-empty, a wider question than
103
+ // "exactly the empty string", so there is nothing faithful to emit. An
104
+ // all-empty selection therefore encodes to the empty filter, i.e. All.
105
+ expect(encodeFilterList([""])).toBe("");
106
+ expect(valuesMalloyWillMatch(encodeFilterList(["a", "", "b"]))).toEqual([
107
+ "a",
108
+ "b",
109
+ ]);
110
+ });
111
+ });
112
+
113
+ describe("a value that is entirely whitespace", () => {
114
+ it("is refused rather than encoded, because encoding it matches everything", () => {
115
+ // `unparse` emits the whitespace, `parse` returns a NULL clause for it with
116
+ // an empty log rather than an error, and Malloy compiles a null clause to
117
+ // the SQL constant `true`. Encoding it would silently select every row,
118
+ // which is the widening this module exists to prevent.
119
+ for (const raw of ["\t", "\n", "\u00a0", "\u2003", " "]) {
120
+ expect(encodeFilterList([raw])).toBe("");
121
+ }
122
+ });
123
+
124
+ it("drops such a value from a list without dropping the rest", () => {
125
+ expect(encodeFilterList(["Nike", "\u00a0", "Loft"])).toBe(
126
+ encodeFilterList(["Nike", "Loft"]),
127
+ );
128
+ });
129
+
130
+ it("still keeps whitespace INSIDE a value", () => {
131
+ // The rule is "no non-whitespace character at all", not "no whitespace".
132
+ const encoded = encodeFilterList([" Nike "]);
133
+ expect(encoded).not.toBe("");
134
+ expect(decodeFilterList(encoded)).toEqual([" Nike "]);
135
+ });
136
+
137
+ it("pins the codepoints that reach the null-clause path", () => {
138
+ // The COUNTS are asserted, not just the behaviour. A comment above
139
+ // `encodeFilterList` used to state a number that was wrong in both
140
+ // readings, and this test could not catch it because it asserted no
141
+ // count at all. The loop also stopped one short of U+3000, so it missed
142
+ // the ideographic space that the module's own docstring names.
143
+ const whitespace: string[] = [];
144
+ for (let cp = 1; cp <= 0x3000; cp++) {
145
+ const ch = String.fromCodePoint(cp);
146
+ if (ch.trim() === "") whitespace.push(ch);
147
+ }
148
+ expect(whitespace.length).toBe(24);
149
+
150
+ // Every one of them is dropped, U+00A0 included: it is ordinary in
151
+ // pasted or scraped data and would otherwise select every row.
152
+ for (const ch of whitespace) expect(encodeFilterList([ch])).toBe("");
153
+
154
+ // And 23 of the 24 genuinely reach the null clause. The one that does
155
+ // not is the ASCII space, which the escaper covers and which round-trips
156
+ // faithfully, so dropping THAT one is the uniformity choice the
157
+ // docstring describes rather than a necessity.
158
+ const nullClause = whitespace.filter(
159
+ (ch) =>
160
+ StringFilterExpression.parse(
161
+ StringFilterExpression.unparse({ operator: "=", values: [ch] }),
162
+ ).parsed === null,
163
+ );
164
+ expect(nullClause.length).toBe(23);
165
+ expect(whitespace.filter((ch) => !nullClause.includes(ch))).toEqual([
166
+ " ",
167
+ ]);
168
+ });
169
+ });
170
+
171
+ describe("decodeFilterList", () => {
172
+ it("round-trips the list form in either argument order", () => {
173
+ // Order matters: the previous decoder passed only when the value needing
174
+ // escaping came first, and returned a leading space otherwise.
175
+ for (const values of [
176
+ ["us-east", "us-west"],
177
+ ["Levi's"],
178
+ ["Ben & Jerry, Inc", "Nike"],
179
+ ["Nike", "Ben & Jerry, Inc"],
180
+ ['say "hi"'],
181
+ ["back\\slash"],
182
+ [" padded "],
183
+ ["-Outerwear", "%", "null"],
184
+ ]) {
185
+ expect(decodeFilterList(encodeFilterList(values))).toEqual(values);
186
+ }
187
+ });
188
+
189
+ it("round-trips every pair drawn from the hazard set", () => {
190
+ for (const first of HAZARDS) {
191
+ for (const second of HAZARDS) {
192
+ const values = [first, second];
193
+ expect(decodeFilterList(encodeFilterList(values))).toEqual(values);
194
+ }
195
+ }
196
+ });
197
+
198
+ it("reads an empty filter as no selection", () => {
199
+ expect(decodeFilterList("")).toEqual([]);
200
+ expect(decodeFilterList(" ")).toEqual([]);
201
+ });
202
+
203
+ it("survives leading and trailing spaces, which the escaper does cover", () => {
204
+ for (const value of ["a b", " padded", "padded ", " padded "]) {
205
+ expect(valuesMalloyWillMatch(encodeFilterList([value]))).toEqual([
206
+ value,
207
+ ]);
208
+ expect(decodeFilterList(encodeFilterList([value]))).toEqual([value]);
209
+ }
210
+ });
211
+
212
+ it("loses a trailing tab, and narrows rather than widens when it does", () => {
213
+ // Documented in the module docstring: the escaper covers the space
214
+ // character but not the tab, and the parser trims trailing whitespace.
215
+ // Pinned so a change on either side is visible rather than silent. The
216
+ // result is a filter for a DIFFERENT single value, never a broader one,
217
+ // which is why it is documented rather than worked around.
218
+ expect(valuesMalloyWillMatch(encodeFilterList(["a\t"]))).toEqual(["a"]);
219
+ // A tab that is not trailing is unaffected.
220
+ expect(valuesMalloyWillMatch(encodeFilterList(["a\tb"]))).toEqual([
221
+ "a\tb",
222
+ ]);
223
+ // LEADING too, which the docstring used to claim only of trailing.
224
+ expect(valuesMalloyWillMatch(encodeFilterList(["\ta"]))).toEqual(["a"]);
225
+ // A space in either position IS escaped, so it survives. This is the
226
+ // difference the docstring turns on, so it is pinned next to it.
227
+ expect(valuesMalloyWillMatch(encodeFilterList([" a "]))).toEqual([" a "]);
228
+ });
229
+
230
+ it("drops a space by CHOICE, and a tab by necessity", () => {
231
+ // The docstring used to say the all-whitespace refusal was forced by the
232
+ // grammar. It is forced only for the values the escaper does not cover.
233
+ // An ASCII space round-trips faithfully through the real parser, so
234
+ // dropping it is a uniformity choice, and this pins the difference the
235
+ // prose now turns on.
236
+ const space = StringFilterExpression.unparse({
237
+ operator: "=",
238
+ values: [" "],
239
+ });
240
+ expect(StringFilterExpression.parse(space).parsed).toEqual({
241
+ operator: "=",
242
+ values: [" "],
243
+ });
244
+ // A tab does NOT: it parses to a null clause, which Malloy compiles to
245
+ // `true`, i.e. every row. That is the case the rule cannot bend for.
246
+ const tab = StringFilterExpression.unparse({
247
+ operator: "=",
248
+ values: ["\t"],
249
+ });
250
+ expect(StringFilterExpression.parse(tab).parsed).toBeNull();
251
+ // Both are dropped, because one rule beats two indistinguishable ones.
252
+ expect(encodeFilterList([" "])).toBe("");
253
+ expect(encodeFilterList(["\t"])).toBe("");
254
+ });
255
+
256
+ it("does not drop a zero-width space, which is where `trim()` stops", () => {
257
+ // The all-whitespace refusal tests with JavaScript's `trim()`, which
258
+ // treats NBSP and the ideographic space as whitespace but not U+200B. So
259
+ // a zero-width space is carried as an ordinary value. Benign, since it
260
+ // filters for something useless rather than for everything, and pinned
261
+ // because the boundary is worth knowing rather than rediscovering.
262
+ expect(encodeFilterList(["West", "\u00a0"])).toBe("West");
263
+ expect(encodeFilterList(["West", "\u3000"])).toBe("West");
264
+ expect(encodeFilterList(["West", "\u200b"])).toBe("West, \u200b");
265
+ });
266
+
267
+ it("fails a newline outright rather than filtering by the wrong thing", () => {
268
+ // The grammar excludes a bare newline from a match string, so this is
269
+ // unparseable and the query errors where the reader can see it. Escaping
270
+ // it would parse but come back carrying a stray backslash, because the
271
+ // library's `unescape` is /\\(.)/g and `.` does not match a newline.
272
+ expect(valuesMalloyWillMatch(encodeFilterList(["a\nb"]))).toBeUndefined();
273
+ });
274
+
275
+ it("keeps a filter it cannot represent in one piece", () => {
276
+ // A control that cannot express these should preserve them, not split or
277
+ // reinterpret them into something that means something else.
278
+ for (const filter of [
279
+ "-Nike",
280
+ "%foo%",
281
+ "null",
282
+ "empty",
283
+ "a; b",
284
+ "a | b",
285
+ ]) {
286
+ expect(decodeFilterList(filter)).toEqual([filter]);
287
+ }
288
+ });
289
+ });
290
+
291
+ describe("encodeAtLeast", () => {
292
+ it("round-trips a slider bound", () => {
293
+ expect(encodeAtLeast(100)).toBe(">= 100");
294
+ expect(decodeAtLeast(">= 100")).toBe(100);
295
+ expect(decodeAtLeast(">=2.5")).toBe(2.5);
296
+ expect(decodeAtLeast(">= -5")).toBe(-5);
297
+ });
298
+
299
+ it("reads spellings the parser accepts but a regex would not", () => {
300
+ // The reason this delegates instead of matching `/^>=\s*(-?\d+...)$/`:
301
+ // both of these are valid to `@malloydata/malloy-filter` and a regex of
302
+ // that shape reads neither, so the slider would silently show no lower
303
+ // bound for a filter that has one.
304
+ expect(decodeAtLeast(">= 1e3")).toBe(1000);
305
+ expect(decodeAtLeast(">= .5")).toBe(0.5);
306
+ expect(decodeAtLeast(" >= -7.25 ")).toBe(-7.25);
307
+ });
308
+
309
+ it("survives input the parser cannot read at all", () => {
310
+ // `parse` reports failure as a null `parsed` rather than by throwing, so
311
+ // an unguarded read of `.operator` is a crash, not a fallback.
312
+ for (const bad of ["((", "a,,b", ">=", "-", "\\"]) {
313
+ expect(decodeAtLeast(bad)).toBe(undefined);
314
+ }
315
+ });
316
+
317
+ it("declines to read a filter that is not a single lower bound", () => {
318
+ // The slider then leaves the value alone rather than showing a position
319
+ // that misrepresents it.
320
+ expect(decodeAtLeast("100 to 500")).toBe(undefined);
321
+ expect(decodeAtLeast("> 100")).toBe(undefined);
322
+ expect(decodeAtLeast(">= 1, 2")).toBe(undefined);
323
+ expect(decodeAtLeast("null")).toBe(undefined);
324
+ expect(decodeAtLeast("")).toBe(undefined);
325
+ });
326
+ });
327
+
328
+ describe("isPlainFilterList", () => {
329
+ it("is true for what a picker can represent and re-encode safely", () => {
330
+ expect(isPlainFilterList(undefined)).toBe(true);
331
+ expect(isPlainFilterList("")).toBe(true);
332
+ expect(isPlainFilterList(" ")).toBe(true);
333
+ expect(isPlainFilterList("Nike")).toBe(true);
334
+ expect(isPlainFilterList(encodeFilterList(["Nike", "Loft"]))).toBe(true);
335
+ });
336
+
337
+ it("is false for a filter the picker would silently rewrite", () => {
338
+ // Each of these decodes to one opaque entry, and re-encoding that entry
339
+ // escapes it into a literal: inverting a negation, or turning a wildcard
340
+ // into a search for a percent sign. A caller that sees false should show
341
+ // a text box instead.
342
+ for (const filter of [
343
+ "-Nike",
344
+ "%foo%",
345
+ "null",
346
+ "empty",
347
+ "a; b",
348
+ "a | b",
349
+ ]) {
350
+ expect(isPlainFilterList(filter)).toBe(false);
351
+ }
352
+ });
353
+
354
+ it("names the exact hazard: a negation re-encodes as a literal", () => {
355
+ const chips = decodeFilterList("-Nike");
356
+ expect(chips).toEqual(["-Nike"]);
357
+ // This is what the picker WOULD do, and why it must not.
358
+ expect(encodeFilterList(chips)).toBe("\\-Nike");
359
+ expect(isPlainFilterList("-Nike")).toBe(false);
360
+ });
361
+ });
@@ -0,0 +1,204 @@
1
+ /**
2
+ * Encoding between control widgets and `filter<…>` given values.
3
+ *
4
+ * A `filter<T>` given takes Malloy filter syntax as a string: `"us-east,
5
+ * us-west"` for a string filter, `">= 100"` for a number one (see
6
+ * `docs/givens.md` §Accepted JS Shapes). A select or a slider works in plain
7
+ * values, so something has to translate, and it is worth keeping that
8
+ * translation here rather than inline in a widget: it is the one part of the
9
+ * control layer that can silently produce a filter meaning something other than
10
+ * what the user picked.
11
+ *
12
+ * Which is why the translation is not hand-written. Malloy's filter languages
13
+ * are parsed by `@malloydata/malloy-filter`, and that package can also print a
14
+ * clause back out, escaping exactly what its own grammar treats as special. So
15
+ * a value goes out through `unparse` and comes back through `parse`, and the
16
+ * two agree by construction rather than by our reading of the grammar. An
17
+ * earlier version of this module quoted values with `"…"`, which the string
18
+ * grammar has no notion of: it escaped nothing, and a picked `-Outerwear`
19
+ * silently ran as a negation.
20
+ */
21
+
22
+ import {
23
+ isStringCondition,
24
+ NumberFilterExpression,
25
+ StringFilterExpression,
26
+ type StringCondition,
27
+ type StringFilter,
28
+ } from "@malloydata/malloy-filter";
29
+
30
+ /** True for the `filter<…>` family, whose values are filter syntax, not plain. */
31
+ export function isFilterType(type: string | undefined): boolean {
32
+ return (type ?? "").startsWith("filter<");
33
+ }
34
+
35
+ /** The `T` of a `filter<T>`, or undefined for any other type. */
36
+ export function filterInnerType(type: string | undefined): string | undefined {
37
+ const match = /^filter<(.+)>$/.exec(type ?? "");
38
+ return match ? match[1] : undefined;
39
+ }
40
+
41
+ /**
42
+ * Join picked values into one string filter (`"Nike, Levi's"`), which Malloy
43
+ * reads as "any of these".
44
+ *
45
+ * Every value is escaped by the filter package's own printer, so a value that
46
+ * would otherwise read as syntax survives as a literal. A comma does not split
47
+ * it in two, a leading `-` is not a negation, `%` and `_` are not wildcards, and
48
+ * a bare `null` or `empty` is the word rather than the operator.
49
+ *
50
+ * An empty value cannot be carried and is dropped. Malloy's `empty` operator
51
+ * means "null or the empty string", which is a wider question than "the rows
52
+ * whose value is exactly `''`", so there is no faithful spelling for a picked
53
+ * empty option. Dropping it means an all-empty selection encodes to `""`, the
54
+ * empty filter, which is the "All" state.
55
+ *
56
+ * Three kinds of value do not survive, all involving whitespace the grammar
57
+ * treats as structure. None is fixable here without re-introducing the drift
58
+ * this module delegates to avoid, and none can WIDEN a filter, which is the
59
+ * failure that would matter:
60
+ *
61
+ * - **A leading or trailing tab is dropped.** The escaper covers the space
62
+ * character but not the tab, and the parser trims surrounding whitespace, so
63
+ * `"a\t"` comes back as `"a"`: a filter for a different single value, not a
64
+ * broader one. A space in the same position is escaped and does round-trip,
65
+ * and a tab INSIDE a value is untouched.
66
+ * - **An embedded newline makes the whole filter unparseable.** The grammar
67
+ * excludes a bare newline from a match string, so `["a\nb", "c"]` emits
68
+ * `"a\nb, c"` and the parser refuses it outright: `parsed` is null and the
69
+ * log reads `Expected "," … but "\n" found`. The query then errors where the
70
+ * reader can see it, which is the good failure. `decodeFilterList` hands that
71
+ * string back as one opaque value, but that is its display fallback for a
72
+ * filter it cannot represent, NOT a filter that runs and matches the wrong
73
+ * thing. Escaping is not the fix: the library's `unescape` is `/\\(.)/g` and
74
+ * `.` does not match a newline, so the value would return carrying a stray
75
+ * backslash.
76
+ * - **A value that is ENTIRELY whitespace is refused**, rather than encoded.
77
+ * For a tab or an NBSP it HAS to be: the escaper does not cover them, `parse`
78
+ * returns a null clause with an empty log rather than an error, and Malloy
79
+ * compiles a null clause to the SQL constant `true`, so encoding one would
80
+ * match every row. That is the silent widening this module exists to prevent.
81
+ *
82
+ * For a run of ASCII SPACES it is a choice, not a necessity, and the earlier
83
+ * wording here claimed otherwise. Measured: `unparse({operator:"=", values:
84
+ * [" "]})` gives `"\ "` and parses straight back to `[" "]`, so spaces do
85
+ * round-trip. They are dropped anyway to keep ONE rule. The alternative
86
+ * carries spaces and drops tabs, and nothing on screen distinguishes them,
87
+ * so a reader could not predict which of two identical-looking values
88
+ * filtered and which quietly matched everything.
89
+ *
90
+ * The test is JavaScript's `trim()`, which is where this stops: it treats
91
+ * NBSP and the ideographic space as whitespace but NOT U+200B, so a
92
+ * zero-width space survives as an ordinary value. Harmless, since it filters
93
+ * for something useless rather than for everything, but worth the boundary.
94
+ *
95
+ * All three are pinned in the spec so a future change to either side is visible.
96
+ */
97
+ export function encodeFilterList(values: readonly string[]): string {
98
+ // Dropped alongside `""`: a value with no non-whitespace character cannot be
99
+ // carried by this grammar, and carrying it anyway WIDENS the filter to match
100
+ // everything. `unparse` emits the whitespace as-is, `parse` returns a null
101
+ // clause with an EMPTY log rather than an error, and Malloy compiles a null
102
+ // clause to the SQL constant `true`. So a picked or drilled `\u00a0`, which
103
+ // is ordinary in pasted and scraped data, silently selected every row.
104
+ //
105
+ // Counted rather than guessed: 24 codepoints up to U+3000 satisfy `trim()`,
106
+ // 23 of them parse to a null clause, and the single exception is U+0020,
107
+ // the ASCII space the escaper does cover. All 24 are dropped here anyway,
108
+ // for the uniformity reason in the docstring above. The spec asserts those
109
+ // three numbers, so a change on either side of the boundary fails a test
110
+ // rather than quietly making this comment wrong, which is what happened to
111
+ // the number that used to sit here.
112
+ const present = values.filter((value) => value.trim() !== "");
113
+ if (present.length === 0) return "";
114
+ return StringFilterExpression.unparse({
115
+ operator: "=",
116
+ values: [...present],
117
+ });
118
+ }
119
+
120
+ /**
121
+ * Split a string filter back into the values a multiselect should show
122
+ * selected, undoing {@link encodeFilterList}.
123
+ *
124
+ * Only the list form round-trips. Anything else a filter can express (`-Nike`,
125
+ * `%foo%`, `null`, a `;` conjunction) comes back as a single opaque entry rather
126
+ * than being reinterpreted, so a hand-written filter is preserved rather than
127
+ * mangled by a control that cannot represent it.
128
+ */
129
+ export function decodeFilterList(value: string): string[] {
130
+ if (value.trim() === "") return [];
131
+ const { parsed } = StringFilterExpression.parse(value);
132
+ if (!isPlainEquality(parsed)) return [value];
133
+ return parsed.values;
134
+ }
135
+
136
+ /**
137
+ * Whether a filter is one a picker can represent, and therefore one it may
138
+ * safely re-encode.
139
+ *
140
+ * The picker's selection round-trips through {@link decodeFilterList} and
141
+ * {@link encodeFilterList}, and that is only faithful for the list form. A
142
+ * negation, a wildcard, or a conjunction comes back as one opaque entry so the
143
+ * control can show *something*, but the next edit would re-encode that entry as
144
+ * a literal: turning `-Nike` from "everything except Nike" into a search for
145
+ * the five-character string `-Nike`. A caller that can fall back to a plain text
146
+ * box should ask this first and do so, which keeps the author's filter both
147
+ * visible and intact.
148
+ */
149
+ export function isPlainFilterList(value: string | undefined): boolean {
150
+ if (value === undefined || value.trim() === "") return true;
151
+ return isPlainEquality(StringFilterExpression.parse(value).parsed);
152
+ }
153
+
154
+ /**
155
+ * A clause the multiselect can represent: "the value is one of these", with no
156
+ * negation. `parse` already unescaped the values, so they are the literals the
157
+ * user picked.
158
+ */
159
+ function isPlainEquality(
160
+ clause: StringFilter | null,
161
+ ): clause is StringCondition {
162
+ return (
163
+ clause !== null &&
164
+ isStringCondition(clause) &&
165
+ clause.operator === "=" &&
166
+ clause.not !== true
167
+ );
168
+ }
169
+
170
+ /** A lower-bound number filter, which is what a one-handled slider means. */
171
+ export function encodeAtLeast(value: number): string {
172
+ return NumberFilterExpression.unparse({
173
+ operator: ">=",
174
+ values: [String(value)],
175
+ });
176
+ }
177
+
178
+ /**
179
+ * The bound back out of {@link encodeAtLeast}, or undefined for any other filter.
180
+ *
181
+ * Through the library for the same reason the string side is: a hand-rolled
182
+ * `/^>=\s*(-?\d+(?:\.\d+)?)$/` was narrower than the grammar it was standing
183
+ * in for, so `>= 1e3` and `>= .5`, both of which the parser accepts and
184
+ * `unparse` round-trips, read back as undefined and the slider silently showed
185
+ * no lower bound at all.
186
+ */
187
+ export function decodeAtLeast(value: string): number | undefined {
188
+ // Trimmed, and `parsed` null-checked: the number parser rejects surrounding
189
+ // whitespace where the string one absorbs it, and it reports anything it
190
+ // cannot read as a null `parsed` rather than by throwing.
191
+ const text = value.trim();
192
+ if (text === "") return undefined;
193
+ const { parsed } = NumberFilterExpression.parse(text);
194
+ if (
195
+ parsed === null ||
196
+ parsed.operator !== ">=" ||
197
+ parsed.not === true ||
198
+ parsed.values?.length !== 1
199
+ ) {
200
+ return undefined;
201
+ }
202
+ const bound = Number(parsed.values[0]);
203
+ return Number.isFinite(bound) ? bound : undefined;
204
+ }
@@ -1,2 +1,6 @@
1
1
  export { GivenInput, type GivenInputProps } from "./GivenInput";
2
- export { GivensPanel, type GivensPanelProps } from "./GivensPanel";
2
+ export {
3
+ GivensPanel,
4
+ type GivensLayout,
5
+ type GivensPanelProps,
6
+ } from "./GivensPanel";