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