Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

chore: Convert metabase-lib/v1 operator and expression utils to TypeScript (#78968)

Andrei Chernykh0b550259ba28
diff --git a/frontend/src/metabase-lib/v1/operators/constants.js b/frontend/src/metabase-lib/v1/operators/constants.ts
similarity index 86%
rename from frontend/src/metabase-lib/v1/operators/constants.js
rename to frontend/src/metabase-lib/v1/operators/constants.ts
index db48a09c63e..97342ec2288 100644
--- a/frontend/src/metabase-lib/v1/operators/constants.js
+++ b/frontend/src/metabase-lib/v1/operators/constants.ts
@@ -19,20 +19,53 @@ import {
isLongitude,
isNumeric,
} from "metabase-lib/v1/types/utils/isa";
+import type { Field, ParameterOptions, Table } from "metabase-types/api";
-function freeformArgument(field, table) {
+type FilterArgument =
+ | { type: "text" }
+ | { type: "number" }
+ | { type: "date" }
+ | { type: "hidden"; default: Field["id"] }
+ | {
+ type: "select";
+ values: { key: boolean | Field["id"]; name: string }[];
+ default?: boolean;
+ };
+
+export type FilterArgumentFormatOptions = {
+ hide?: boolean;
+ compact?: boolean;
+ column?: { semantic_type: string };
+};
+
+export type FieldFilterOperator = {
+ validArgumentsFilters: ((field: Field, table: Table) => FilterArgument)[];
+ multi?: boolean;
+ placeholders?: string[];
+ formatOptions?: FilterArgumentFormatOptions[];
+ options?: Record<string, { defaultValue: boolean }>;
+ optionsDefaults?: ParameterOptions;
+};
+
+export type NamedFilterOperator = {
+ name: string;
+ readonly verboseName: string;
+ multi?: boolean;
+};
+
+function freeformArgument(_field: Field, _table: Table): FilterArgument {
return {
type: "text",
};
}
-function numberArgument(field, table) {
+function numberArgument(_field: Field, _table: Table): FilterArgument {
return {
type: "number",
};
}
-function comparableArgument(field, table) {
+function comparableArgument(field: Field, _table: Table): FilterArgument {
if (isDate(field)) {
return {
type: "date",
@@ -50,7 +83,7 @@ function comparableArgument(field, table) {
};
}
-function equivalentArgument(field, table) {
+function equivalentArgument(field: Field, _table: Table): FilterArgument {
if (isBoolean(field)) {
return {
type: "select",
@@ -79,24 +112,28 @@ function equivalentArgument(field, table) {
};
}
-function longitudeFieldSelectArgument(field, table) {
- const values = table.fields
+function longitudeFieldSelectArgument(
+ _field: Field,
+ table: Table,
+): FilterArgument {
+ const values = (table.fields ?? [])
.filter((field) => isLongitude(field))
.map((field) => ({
key: field.id,
name: field.display_name,
}));
+
if (values.length === 1) {
return {
type: "hidden",
default: values[0].key,
};
- } else {
- return {
- type: "select",
- values: values,
- };
}
+
+ return {
+ type: "select",
+ values,
+ };
}
const CASE_SENSITIVE_OPTION = {
@@ -194,9 +231,11 @@ export const FIELD_FILTER_OPERATORS = {
options: CASE_SENSITIVE_OPTION,
optionsDefaults: { "case-sensitive": false },
},
-};
+} satisfies Record<string, FieldFilterOperator>;
+
+export type FilterOperatorName = keyof typeof FIELD_FILTER_OPERATORS;
-const DEFAULT_FILTER_OPERATORS = [
+const DEFAULT_FILTER_OPERATORS: NamedFilterOperator[] = [
{
name: "=",
get verboseName() {
@@ -223,7 +262,7 @@ const DEFAULT_FILTER_OPERATORS = [
},
];
-const KEY_FILTER_OPERATORS = [
+const KEY_FILTER_OPERATORS: NamedFilterOperator[] = [
{
name: "=",
get verboseName() {
@@ -606,4 +645,6 @@ export const FILTER_OPERATORS_BY_TYPE_ORDERED = {
[FOREIGN_KEY]: KEY_FILTER_OPERATORS,
[PRIMARY_KEY]: KEY_FILTER_OPERATORS,
[UNKNOWN]: DEFAULT_FILTER_OPERATORS,
-};
+} satisfies Record<string, NamedFilterOperator[]>;
+
+export type FilterOperatorType = keyof typeof FILTER_OPERATORS_BY_TYPE_ORDERED;
diff --git a/frontend/src/metabase-lib/v1/operators/utils/index.js b/frontend/src/metabase-lib/v1/operators/utils/index.js
deleted file mode 100644
index d720ae96351..00000000000
--- a/frontend/src/metabase-lib/v1/operators/utils/index.js
+++ /dev/null
@@ -1,46 +0,0 @@
-import _ from "underscore";
-
-import {
- FIELD_FILTER_OPERATORS,
- FILTER_OPERATORS_BY_TYPE_ORDERED,
-} from "metabase-lib/v1/operators/constants";
-
-export function doesOperatorExist(operatorName) {
- return !!FIELD_FILTER_OPERATORS[operatorName];
-}
-
-export function getOperatorByTypeAndName(type, name) {
- const typedNamedOperator = _.findWhere(
- FILTER_OPERATORS_BY_TYPE_ORDERED[type],
- {
- name,
- },
- );
- const namedOperator = FIELD_FILTER_OPERATORS[name];
-
- return (
- typedNamedOperator && {
- ...typedNamedOperator,
- ...namedOperator,
- numFields: namedOperator.validArgumentsFilters.length,
- }
- );
-}
-
-export function isEqualsOperator(operator) {
- return !!operator && operator.name === "=";
-}
-
-export function isFuzzyOperator(operator) {
- const { name } = operator || {};
- return !["=", "!="].includes(name);
-}
-
-export function getFilterArgumentFormatOptions(filterOperator, index) {
- return (
- (filterOperator &&
- filterOperator.formatOptions &&
- filterOperator.formatOptions[index]) ||
- {}
- );
-}
diff --git a/frontend/src/metabase-lib/v1/operators/utils/index.ts b/frontend/src/metabase-lib/v1/operators/utils/index.ts
new file mode 100644
index 00000000000..7e551f2f11b
--- /dev/null
+++ b/frontend/src/metabase-lib/v1/operators/utils/index.ts
@@ -0,0 +1,68 @@
+import type {
+ FieldFilterOperator,
+ FilterArgumentFormatOptions,
+ FilterOperatorName,
+ FilterOperatorType,
+ NamedFilterOperator,
+} from "metabase-lib/v1/operators/constants";
+import {
+ FIELD_FILTER_OPERATORS,
+ FILTER_OPERATORS_BY_TYPE_ORDERED,
+} from "metabase-lib/v1/operators/constants";
+
+type NamedFieldFilterOperator = NamedFilterOperator &
+ FieldFilterOperator & {
+ numFields: number;
+ };
+
+export function doesOperatorExist(
+ operatorName?: string,
+): operatorName is FilterOperatorName {
+ return operatorName != null && operatorName in FIELD_FILTER_OPERATORS;
+}
+
+export function getOperatorByTypeAndName(
+ type: FilterOperatorType | undefined,
+ name: FilterOperatorName,
+): NamedFieldFilterOperator | undefined {
+ const operatorsForType: NamedFilterOperator[] | undefined = type
+ ? FILTER_OPERATORS_BY_TYPE_ORDERED[type]
+ : undefined;
+ const typedNamedOperator = operatorsForType?.find(
+ (operator) => operator.name === name,
+ );
+ const namedOperator: FieldFilterOperator = FIELD_FILTER_OPERATORS[name];
+
+ return (
+ typedNamedOperator && {
+ ...typedNamedOperator,
+ ...namedOperator,
+ numFields: namedOperator.validArgumentsFilters.length,
+ }
+ );
+}
+
+export function isEqualsOperator(
+ operator?: NamedFieldFilterOperator | null,
+): boolean {
+ return operator?.name === "=";
+}
+
+export function isFuzzyOperator(
+ operator?: NamedFieldFilterOperator | null,
+): boolean {
+ const { name } = operator || {};
+ return name !== "=" && name !== "!=";
+}
+
+export function getFilterArgumentFormatOptions(
+ filterOperator: FieldFilterOperator | null | undefined,
+ index: number,
+): FilterArgumentFormatOptions {
+ return (
+ (filterOperator &&
+ filterOperator.formatOptions &&
+ filterOperator.formatOptions[index]) ||
+ {}
+ );
+}
diff --git a/frontend/src/metabase-lib/v1/operators/utils/index.unit.spec.ts b/frontend/src/metabase-lib/v1/operators/utils/index.unit.spec.ts
index 1cdfb7ab2d0..8179ba53331 100644
--- a/frontend/src/metabase-lib/v1/operators/utils/index.unit.spec.ts
+++ b/frontend/src/metabase-lib/v1/operators/utils/index.unit.spec.ts
@@ -9,6 +9,7 @@ import {
FOREIGN_KEY,
NUMBER,
PRIMARY_KEY,
+ STRING,
} from "metabase-lib/v1/types/constants";
describe("metabase-lib/v1/operators/utils", () => {
@@ -23,14 +24,17 @@ describe("metabase-lib/v1/operators/utils", () => {
describe("isEqualsOperator", () => {
it("should evaluate whether it is an equals operator", () => {
expect(isEqualsOperator()).toBe(false);
- expect(isEqualsOperator({ name: "foo" })).toBe(false);
- expect(isEqualsOperator({ name: "=" })).toBe(true);
+ expect(
+ isEqualsOperator(getOperatorByTypeAndName(NUMBER, "between")),
+ ).toBe(false);
+ expect(isEqualsOperator(getOperatorByTypeAndName(NUMBER, "="))).toBe(
+ true,
+ );
});
});
describe("getOperatorByTypeAndName", () => {
it("should return undefined if operator does not exist", () => {
- expect(getOperatorByTypeAndName("FOO", "=")).toBe(undefined);
expect(getOperatorByTypeAndName(NUMBER, "contains")).toBe(undefined);
});
@@ -74,13 +78,21 @@ describe("metabase-lib/v1/operators/utils", () => {
describe("isFuzzyOperator", () => {
it("should return false for operators that expect an exact match", () => {
- expect(isFuzzyOperator({ name: "=" })).toBe(false);
- expect(isFuzzyOperator({ name: "!=" })).toBe(false);
+ expect(isFuzzyOperator(getOperatorByTypeAndName(NUMBER, "="))).toBe(
+ false,
+ );
+ expect(isFuzzyOperator(getOperatorByTypeAndName(NUMBER, "!="))).toBe(
+ false,
+ );
});
it("should return true for operators that are not exact", () => {
- expect(isFuzzyOperator({ name: "contains" })).toBe(true);
- expect(isFuzzyOperator({ name: "between" })).toBe(true);
+ expect(
+ isFuzzyOperator(getOperatorByTypeAndName(STRING, "contains")),
+ ).toBe(true);
+ expect(isFuzzyOperator(getOperatorByTypeAndName(NUMBER, "between"))).toBe(
+ true,
+ );
});
});
});
diff --git a/frontend/src/metabase-lib/v1/parameters/utils/operators.unit.spec.ts b/frontend/src/metabase-lib/v1/parameters/utils/operators.unit.spec.ts
index 56161dc3485..4983f8c2251 100644
--- a/frontend/src/metabase-lib/v1/parameters/utils/operators.unit.spec.ts
+++ b/frontend/src/metabase-lib/v1/parameters/utils/operators.unit.spec.ts
@@ -1,3 +1,4 @@
+import { checkNotNull } from "metabase/utils/types";
import { createMockUiParameter } from "metabase-lib/v1/parameters/mock";
import {
@@ -37,15 +38,19 @@ describe("parameters/utils/operators", () => {
describe("when parameter is associated with an operator", () => {
it("should return relevant operator object", () => {
- const operator2 = deriveFieldOperatorFromParameter(
- createMockUiParameter({
- type: "string/contains",
- }),
+ const operator2 = checkNotNull(
+ deriveFieldOperatorFromParameter(
+ createMockUiParameter({
+ type: "string/contains",
+ }),
+ ),
);
- const operator3 = deriveFieldOperatorFromParameter(
- createMockUiParameter({
- type: "number/between",
- }),
+ const operator3 = checkNotNull(
+ deriveFieldOperatorFromParameter(
+ createMockUiParameter({
+ type: "number/between",
+ }),
+ ),
);
expect(operator2.name).toEqual("contains");
expect(operator3.name).toEqual("between");
@@ -55,18 +60,22 @@ describe("parameters/utils/operators", () => {
describe("when parameter is location/category", () => {
it("should map to an = operator", () => {
expect(
- deriveFieldOperatorFromParameter(
- createMockUiParameter({
- type: "location/city",
- }),
+ checkNotNull(
+ deriveFieldOperatorFromParameter(
+ createMockUiParameter({
+ type: "location/city",
+ }),
+ ),
).name,
).toBe("=");
expect(
- deriveFieldOperatorFromParameter(
- createMockUiParameter({
- type: "category",
- }),
+ checkNotNull(
+ deriveFieldOperatorFromParameter(
+ createMockUiParameter({
+ type: "category",
+ }),
+ ),
).name,
).toBe("=");
});
diff --git a/frontend/src/metabase-lib/v1/queries/utils/expression.js b/frontend/src/metabase-lib/v1/queries/utils/expression.ts
similarity index 69%
rename from frontend/src/metabase-lib/v1/queries/utils/expression.js
rename to frontend/src/metabase-lib/v1/queries/utils/expression.ts
index d30bcab75af..98ff7bef029 100644
--- a/frontend/src/metabase-lib/v1/queries/utils/expression.js
+++ b/frontend/src/metabase-lib/v1/queries/utils/expression.ts
@@ -1,4 +1,5 @@
import { unique_expression_name } from "cljs/metabase.xrays.domain_entities.queries.util";
+import type { ExpressionClause } from "metabase-types/api";
/**
* Ensures expression's name uniqueness
@@ -9,11 +10,10 @@ import { unique_expression_name } from "cljs/metabase.xrays.domain_entities.quer
* the next one will be "Double Total (2)" and so on
*
* If the original name is already unique, the fn just returns it
- *
- * @param {string} originalName - expression's name
- * @param {object} expressions - object with existing query expressions
- * @returns {string}
*/
-export function getUniqueExpressionName(expressions, originalName) {
+export function getUniqueExpressionName(
+ expressions: ExpressionClause,
+ originalName: string,
+): string {
return unique_expression_name(expressions, originalName);
}
diff --git a/frontend/src/metabase/query_builder/reducers.ts b/frontend/src/metabase/query_builder/reducers.ts
index ef523e99e1b..9e2ffe967af 100644
--- a/frontend/src/metabase/query_builder/reducers.ts
+++ b/frontend/src/metabase/query_builder/reducers.ts
@@ -642,7 +642,7 @@ type NestedCardPayloadAction = {
const handleQueryCompleted = (
state: Card | null,
action: NestedCardPayloadAction,
-) => {
+): Card | null => {
if (!state) {
return state;
}