Skip to content
Snippets Groups Projects
Unverified Commit 58462139 authored by Dalton's avatar Dalton Committed by GitHub
Browse files

move parameter type utils into own file (#18632)

parent 233ee7ce
Branches
Tags
No related merge requests found
......@@ -30,6 +30,10 @@ import {
NUMBER,
PRIMARY_KEY,
} from "metabase/lib/schema_metadata";
import {
getParameterType,
getParameterSubType,
} from "metabase/parameters/utils/parameter-type";
import Variable, { TemplateTagVariable } from "metabase-lib/lib/Variable";
......@@ -213,21 +217,6 @@ export function getOperatorDisplayName(option, operatorType, sectionName) {
}
}
// sectionId will match a type of field (category, location, number, date, id, etc.)
// if sectionId is undefined, it is an old parameter that did have it set
// OR it is a PARAMETER_OPTION entry. In those situations,
// a `type` will exist like "category" or "location/city" or "string/="
// we split on the `/` and take the first entry to get the field type
function getParameterType(parameter) {
const { sectionId } = parameter;
return sectionId || splitType(parameter)[0];
}
function getParameterSubType(parameter) {
const [, subtype] = splitType(parameter);
return subtype;
}
function fieldFilterForParameter(parameter: Parameter): FieldPredicate {
const type = getParameterType(parameter);
const subtype = getParameterSubType(parameter);
......@@ -561,7 +550,7 @@ export function getParameterIconName(parameter: ?Parameter) {
}
export function normalizeParameterValue(type, value) {
const [fieldType] = splitType(type);
const fieldType = getParameterType(type);
if (["string", "number"].includes(fieldType)) {
return value == null ? [] : [].concat(value);
......@@ -599,13 +588,6 @@ function getParameterOperatorType(parameterType) {
}
}
function splitType(parameterOrType) {
const parameterType = _.isString(parameterOrType)
? parameterOrType
: (parameterOrType || {}).type || "";
return parameterType.split("/");
}
export function getValuePopulatedParameters(parameters, parameterValues) {
return parameterValues
? parameters.map(parameter => {
......
import _ from "underscore";
export function getParameterType(parameter) {
return parameter.sectionId || splitType(parameter)[0];
}
export function getParameterSubType(parameter) {
const [, subtype] = splitType(parameter);
return subtype;
}
function splitType(parameterOrType) {
const parameterType = _.isString(parameterOrType)
? parameterOrType
: parameterOrType?.type || "";
return parameterType.split("/");
}
import { getParameterType, getParameterSubType } from "./parameter-type";
describe("parameters/utils/parameter-type", () => {
describe("getParameterType", () => {
it("should return the string before the slash in a parameter type", () => {
expect(getParameterType({ type: "string/foo" })).toEqual("string");
expect(getParameterType({ type: "category" })).toEqual("category");
});
it("should return the string before the slash in a parameter's type", () => {
expect(getParameterType({ type: "string/foo" })).toEqual("string");
expect(getParameterType({ type: "category" })).toEqual("category");
});
it("should prefer using a sectionId for determing the type if it exists", () => {
expect(
getParameterType({ sectionId: "location", type: "string/=" }),
).toEqual("location");
});
});
describe("getParameterSubType", () => {
it("should return the string before the slash in a parameter type", () => {
expect(getParameterSubType("string/foo")).toEqual("foo");
expect(getParameterSubType("category")).toBeUndefined();
});
it("should return the string before the slash in a parameter's type", () => {
expect(getParameterSubType("string/foo")).toEqual("foo");
expect(getParameterSubType("category")).toBeUndefined();
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment