The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
chore: convert isa.js to typescript (#71014)
Uladzimir Havenchyka9b9ad9d8546
diff --git a/frontend/src/metabase-lib/v1/types/constants.ts b/frontend/src/metabase-lib/v1/types/constants.tsindex 97f90198a11..d39896e45ab 100644--- a/frontend/src/metabase-lib/v1/types/constants.ts+++ b/frontend/src/metabase-lib/v1/types/constants.ts@@ -28,46 +28,71 @@ const DIMENSION = "DIMENSION";export const UNKNOWN = "UNKNOWN";+export type FieldTypeKey =+ | typeof TEMPORAL+ | typeof NUMBER+ | typeof INTEGER+ | typeof STRING+ | typeof STRING_LIKE+ | typeof BOOLEAN+ | typeof COORDINATE+ | typeof LOCATION+ | typeof ENTITY+ | typeof FOREIGN_KEY+ | typeof PRIMARY_KEY+ | typeof SUMMABLE+ | typeof SCOPE+ | typeof CATEGORY+ | typeof DIMENSION;++export type Hierarchy = {+ base_type?: string[];+ effective_type?: string[];+ semantic_type?: string[];+ include?: FieldTypeKey[];+ exclude?: FieldTypeKey[];+};+// NOTE: be sure not to create cycles using the "other" types-export const TYPE_HIERARCHIES = {+export const TYPE_HIERARCHIES: Record<FieldTypeKey, Hierarchy> = {[TEMPORAL]: {- base: [TYPE.Temporal],- effective: [TYPE.Temporal],+ base_type: [TYPE.Temporal],+ effective_type: [TYPE.Temporal],},[NUMBER]: {- base: [TYPE.Number],- effective: [TYPE.Number],+ base_type: [TYPE.Number],+ effective_type: [TYPE.Number],},[INTEGER]: {- base: [TYPE.Integer],- effective: [TYPE.Integer],+ base_type: [TYPE.Integer],+ effective_type: [TYPE.Integer],},[STRING]: {- base: [TYPE.Text],- effective: [TYPE.Text],+ base_type: [TYPE.Text],+ effective_type: [TYPE.Text],},[STRING_LIKE]: {- base: [TYPE.TextLike],- effective: [TYPE.TextLike],+ base_type: [TYPE.TextLike],+ effective_type: [TYPE.TextLike],},[BOOLEAN]: {- base: [TYPE.Boolean],- effective: [TYPE.Boolean],+ base_type: [TYPE.Boolean],+ effective_type: [TYPE.Boolean],},[COORDINATE]: {- semantic: [TYPE.Coordinate],+ semantic_type: [TYPE.Coordinate],},[LOCATION]: {- semantic: [TYPE.Address],+ semantic_type: [TYPE.Address],},[ENTITY]: {- semantic: [TYPE.FK, TYPE.PK, TYPE.Name],+ semantic_type: [TYPE.FK, TYPE.PK, TYPE.Name],},[FOREIGN_KEY]: {- semantic: [TYPE.FK],+ semantic_type: [TYPE.FK],},[PRIMARY_KEY]: {- semantic: [TYPE.PK],+ semantic_type: [TYPE.PK],},[SUMMABLE]: {include: [NUMBER],@@ -78,7 +103,7 @@ export const TYPE_HIERARCHIES = {exclude: [LOCATION],},[CATEGORY]: {- semantic: [TYPE.Category],+ semantic_type: [TYPE.Category],},// NOTE: this is defunct right now. see definition of isDimension below.[DIMENSION]: {diff --git a/frontend/src/metabase-lib/v1/types/utils/isa.js b/frontend/src/metabase-lib/v1/types/utils/isa.jsdeleted file mode 100644index fb140f4e981..00000000000--- a/frontend/src/metabase-lib/v1/types/utils/isa.js+++ /dev/null@@ -1,242 +0,0 @@-import { isa as cljs_isa } from "cljs/metabase.types.core";-import { isVirtualCardId } from "metabase-lib/v1/metadata/utils/saved-questions";-import {- BOOLEAN,- COORDINATE,- FOREIGN_KEY,- INTEGER,- LOCATION,- NUMBER,- PRIMARY_KEY,- STRING,- STRING_LIKE,- SUMMABLE,- TEMPORAL,- TYPE,- TYPE_HIERARCHIES,-} from "metabase-lib/v1/types/constants";--/**- * Is x the same as, or a descendant type of, y?- *- * @example- * isa(field.semantic_type, TYPE.Currency);- *- * @template X extends string- * @template Y extends string- * @param {X} x- * @param {Y} y- * @returns {x is Y}- */-export const isa = (x, y) => cljs_isa(x, y);--// convenience functions since these operations are super-common-// this will also make it easier to tweak how these checks work in the future,-// e.g. when we add an `is_pk` column and eliminate the PK semantic type we can just look for places that use isPK--export function isTypePK(type) {- return isa(type, TYPE.PK);-}--export function isTypeFK(type) {- return isa(type, TYPE.FK);-}--export function isTypeCurrency(type) {- return isa(type, TYPE.Currency);-}--export function isFieldType(type, field) {- if (!field) {- return false;- }-- const typeDefinition = TYPE_HIERARCHIES[type];- // check to see if it belongs to any of the field types:- const props = field.effective_type- ? ["effective", "semantic"]- : ["base", "semantic"];- for (const prop of props) {- const allowedTypes = typeDefinition[prop];- if (!allowedTypes) {- continue;- }-- const fieldType = field[prop + "_type"];- for (const allowedType of allowedTypes) {- if (isa(fieldType, allowedType)) {- return true;- }- }- }-- // recursively check to see if it's NOT another field type:- for (const excludedType of typeDefinition.exclude || []) {- if (isFieldType(excludedType, field)) {- return false;- }- }-- // recursively check to see if it's another field type:- for (const includedType of typeDefinition.include || []) {- if (isFieldType(includedType, field)) {- return true;- }- }- return false;-}--export function getFieldType(field) {- // try more specific types first, then more generic types- for (const type of [- TEMPORAL,- LOCATION,- COORDINATE,- FOREIGN_KEY,- PRIMARY_KEY,- BOOLEAN,- STRING,- STRING_LIKE,- NUMBER,- ]) {- if (isFieldType(type, field)) {- return type;- }- }-}--export const isDate = isFieldType.bind(null, TEMPORAL);-export const isNumeric = isFieldType.bind(null, NUMBER);-export const isInteger = isFieldType.bind(null, INTEGER);-export const isBoolean = isFieldType.bind(null, BOOLEAN);-export const isString = isFieldType.bind(null, STRING);-export const isStringLike = isFieldType.bind(null, STRING_LIKE);-export const isSummable = isFieldType.bind(null, SUMMABLE);--const hasNonMetricName = (col) => {- const name = col.name.toLowerCase();- return name === "id" || name.endsWith("_id") || name.endsWith("-id");-};--export const isDimension = (col) =>- col && (col.source !== "aggregation" || !!col.binning_info); // columns with binning_info are always dimensions (they represent categorical buckets)-export const isMetric = (col) =>- col &&- col.source !== "breakout" &&- isSummable(col) &&- !hasNonMetricName(col) &&- !col.binning_info; // do not treat column with binning_info as metric by default (metabase#10493)--/**- * @param {Field | DatasetColumn} field- * @returns {boolean}- */-export const isFK = (field) => field && isTypeFK(field.semantic_type);-export const isPK = (field) => field && isTypePK(field.semantic_type);-export const isEntityName = (field) =>- field && isa(field.semantic_type, TYPE.Name);-export const isTitle = (field) => field && isa(field.semantic_type, TYPE.Title);-export const isProduct = (field) =>- field && isa(field.semantic_type, TYPE.Product);-export const isSource = (field) =>- field && isa(field.semantic_type, TYPE.Source);-export const isAddress = (field) =>- field && isa(field.semantic_type, TYPE.Address);-export const isScore = (field) => field && isa(field.semantic_type, TYPE.Score);-export const isQuantity = (field) =>- field && isa(field.semantic_type, TYPE.Quantity);-export const isCategory = (field) =>- field && isa(field.semantic_type, TYPE.Category);--export const isAny = (col) => true;--export const isNumericBaseType = (field) => {- if (!field) {- return false;- }- if (field.effective_type) {- return isa(field.effective_type, TYPE.Number);- } else {- return isa(field.base_type, TYPE.Number);- }-};--export const isDateWithoutTime = (field) => {- if (!field) {- return false;- }- if (field.effective_type) {- return isa(field.effective_type, TYPE.Date);- } else {- return isa(field.base_type, TYPE.Date);- }-};--// ZipCode, ID, etc derive from Number but should not be formatted as numbers-export const isNumber = (field) =>- field &&- isNumericBaseType(field) &&- (field.semantic_type == null ||- isa(field.semantic_type, TYPE.Number) ||- isa(field.semantic_type, TYPE.Category));-export const isFloat = (field) => field && isa(field.semantic_type, TYPE.Float);--export const isTime = (field) => {- if (!field) {- return false;- }- if (field.effective_type) {- return isa(field.effective_type, TYPE.Time);- } else {- return isa(field.base_type, TYPE.Time);- }-};--export const isState = (field) => field && isa(field.semantic_type, TYPE.State);-export const isCountry = (field) =>- field && isa(field.semantic_type, TYPE.Country);-export const isCoordinate = (field) =>- field && isa(field.semantic_type, TYPE.Coordinate);-export const isLatitude = (field) =>- field && isa(field.semantic_type, TYPE.Latitude);-export const isLongitude = (field) =>- field && isa(field.semantic_type, TYPE.Longitude);--export const isCurrency = (field) =>- field && isa(field.semantic_type, TYPE.Currency);--export const isPercentage = (field) =>- field && isa(field.semantic_type, TYPE.Percentage);--export const isID = (field) => isFK(field) || isPK(field);--export const isURL = (field) => field && isa(field.semantic_type, TYPE.URL);-export const isEmail = (field) => field && isa(field.semantic_type, TYPE.Email);-export const isAvatarURL = (field) =>- field && isa(field.semantic_type, TYPE.AvatarURL);-export const isImageURL = (field) =>- field && isa(field.semantic_type, TYPE.ImageURL);--export function hasLatitudeAndLongitudeColumns(cols) {- let hasLatitude = false;- let hasLongitude = false;- for (const col of cols) {- if (isLatitude(col)) {- hasLatitude = true;- }- if (isLongitude(col)) {- hasLongitude = true;- }- }- return hasLatitude && hasLongitude;-}--export const getIsPKFromTablePredicate = (tableId) => (column) => {- const isPrimaryKey = isPK(column);-- // FIXME: columns of nested questions at this moment miss table_id value- // which makes it impossible to match them with their tables that are nested cards- return isVirtualCardId(tableId)- ? isPrimaryKey- : isPrimaryKey && column.table_id === tableId;-};diff --git a/frontend/src/metabase-lib/v1/types/utils/isa.ts b/frontend/src/metabase-lib/v1/types/utils/isa.tsnew file mode 100644index 00000000000..7476dda715e--- /dev/null+++ b/frontend/src/metabase-lib/v1/types/utils/isa.ts@@ -0,0 +1,274 @@+import { isa as cljs_isa } from "cljs/metabase.types.core";+import { isVirtualCardId } from "metabase-lib/v1/metadata/utils/saved-questions";+import {+ BOOLEAN,+ COORDINATE,+ FOREIGN_KEY,+ type FieldTypeKey,+ type Hierarchy,+ INTEGER,+ LOCATION,+ NUMBER,+ PRIMARY_KEY,+ STRING,+ STRING_LIKE,+ SUMMABLE,+ TEMPORAL,+ TYPE,+ TYPE_HIERARCHIES,+} from "metabase-lib/v1/types/constants";+import type { DatasetColumn, TableId } from "metabase-types/api";++/**+ * A minimal field-like shape shared by both Field and DatasetColumn,+ * used by functions that only need type-checking properties.+ */+export interface FieldTypeInfo {+ base_type?: string;+ effective_type?: string | null;+ semantic_type?: string | null;+}++/**+ * Is x the same as, or a descendant type of, y?+ *+ * @example+ * isa(field.semantic_type, TYPE.Currency);+ */+export const isa = (x: string | null | undefined, y: string): boolean =>+ cljs_isa(x, y);++// convenience functions since these operations are super-common+// this will also make it easier to tweak how these checks work in the future,+// e.g. when we add an `is_pk` column and eliminate the PK semantic type we can just look for places that use isPK++export function isTypePK(type: string | null | undefined): boolean {+ return isa(type, TYPE.PK);+}++export function isTypeFK(type: string | null | undefined): boolean {+ return isa(type, TYPE.FK);+}++export function isTypeCurrency(type: string | null | undefined): boolean {+ return isa(type, TYPE.Currency);+}++export function isFieldType(+ type: FieldTypeKey,+ field: FieldTypeInfo | null | undefined,+): boolean {+ if (!field) {+ return false;+ }++ const typeDefinition = TYPE_HIERARCHIES[type];+ // check to see if it belongs to any of the field types:+ const props: (keyof Hierarchy & keyof FieldTypeInfo)[] = field.effective_type+ ? ["effective_type", "semantic_type"]+ : ["base_type", "semantic_type"];+ for (const prop of props) {+ const allowedTypes = typeDefinition[prop];+ if (!allowedTypes) {+ continue;+ }++ const fieldType = field[prop];+ for (const allowedType of allowedTypes) {+ if (isa(fieldType, allowedType)) {+ return true;+ }+ }+ }++ // recursively check to see if it's NOT another field type:+ for (const excludedType of typeDefinition.exclude || []) {+ if (isFieldType(excludedType, field)) {+ return false;+ }+ }++ // recursively check to see if it's another field type:+ for (const includedType of typeDefinition.include || []) {+ if (isFieldType(includedType, field)) {+ return true;+ }+ }+ return false;+}++export function getFieldType(field: FieldTypeInfo) {+ // try more specific types first, then more generic types+ const types: FieldTypeKey[] = [+ TEMPORAL,+ LOCATION,+ COORDINATE,+ FOREIGN_KEY,+ PRIMARY_KEY,+ BOOLEAN,+ STRING,+ STRING_LIKE,+ NUMBER,+ ];+ for (const type of types) {+ if (isFieldType(type, field)) {+ return type;+ }+ }+}++export const isDate = (field: FieldTypeInfo | null | undefined) =>+ isFieldType(TEMPORAL, field);+export const isNumeric = (field: FieldTypeInfo | null | undefined) =>+ isFieldType(NUMBER, field);+export const isInteger = (field: FieldTypeInfo | null | undefined) =>+ isFieldType(INTEGER, field);+export const isBoolean = (field: FieldTypeInfo | null | undefined) =>+ isFieldType(BOOLEAN, field);+export const isString = (field: FieldTypeInfo | null | undefined) =>+ isFieldType(STRING, field);+export const isStringLike = (field: FieldTypeInfo | null | undefined) =>+ isFieldType(STRING_LIKE, field);+export const isSummable = (field: FieldTypeInfo | null | undefined) =>+ isFieldType(SUMMABLE, field);++const hasNonMetricName = (col: DatasetColumn) => {+ const name = col.name.toLowerCase();+ return name === "id" || name.endsWith("_id") || name.endsWith("-id");+};++export const isDimension = (col: DatasetColumn) =>+ col.source !== "aggregation" || !!col.binning_info; // columns with binning_info are always dimensions (they represent categorical buckets)+export const isMetric = (col: DatasetColumn) =>+ col.source !== "breakout" &&+ isSummable(col) &&+ !hasNonMetricName(col) &&+ !col.binning_info; // do not treat column with binning_info as metric by default (metabase#10493)++export const isFK = (field: FieldTypeInfo | null | undefined): boolean =>+ !!field && isTypeFK(field.semantic_type);+export const isPK = (field: FieldTypeInfo | null | undefined): boolean =>+ !!field && isTypePK(field.semantic_type);+export const isEntityName = (+ field: FieldTypeInfo | null | undefined,+): boolean => !!field && isa(field.semantic_type, TYPE.Name);+export const isTitle = (field: FieldTypeInfo | null | undefined): boolean =>+ !!field && isa(field.semantic_type, TYPE.Title);+export const isProduct = (field: FieldTypeInfo | null | undefined): boolean =>+ !!field && isa(field.semantic_type, TYPE.Product);+export const isSource = (field: FieldTypeInfo | null | undefined): boolean =>+ !!field && isa(field.semantic_type, TYPE.Source);+export const isAddress = (field: FieldTypeInfo | null | undefined): boolean =>+ !!field && isa(field.semantic_type, TYPE.Address);+export const isScore = (field: FieldTypeInfo | null | undefined): boolean =>+ !!field && isa(field.semantic_type, TYPE.Score);+export const isQuantity = (field: FieldTypeInfo | null | undefined): boolean =>+ !!field && isa(field.semantic_type, TYPE.Quantity);+export const isCategory = (field: FieldTypeInfo | null | undefined): boolean =>+ !!field && isa(field.semantic_type, TYPE.Category);++export const isAny = (_col: FieldTypeInfo | null | undefined) => true;++export const isNumericBaseType = (+ field: FieldTypeInfo | null | undefined,+): boolean => {+ if (!field) {+ return false;+ }+ if (field.effective_type) {+ return isa(field.effective_type, TYPE.Number);+ } else {+ return isa(field.base_type, TYPE.Number);+ }+};++export const isDateWithoutTime = (+ field: FieldTypeInfo | null | undefined,+): boolean => {+ if (!field) {+ return false;+ }+ if (field.effective_type) {+ return isa(field.effective_type, TYPE.Date);+ } else {+ return isa(field.base_type, TYPE.Date);+ }+};++// ZipCode, ID, etc derive from Number but should not be formatted as numbers+export const isNumber = (field: FieldTypeInfo | null | undefined): boolean =>+ !!field &&+ isNumericBaseType(field) &&+ (field.semantic_type == null ||+ isa(field.semantic_type, TYPE.Number) ||+ isa(field.semantic_type, TYPE.Category));+export const isFloat = (field: FieldTypeInfo | null | undefined): boolean =>+ !!field && isa(field.semantic_type, TYPE.Float);++export const isTime = (field: FieldTypeInfo | null | undefined): boolean => {+ if (!field) {+ return false;+ }+ if (field.effective_type) {+ return isa(field.effective_type, TYPE.Time);+ } else {+ return isa(field.base_type, TYPE.Time);+ }+};++export const isState = (field: FieldTypeInfo | null | undefined): boolean =>+ !!field && isa(field.semantic_type, TYPE.State);+export const isCountry = (field: FieldTypeInfo | null | undefined): boolean =>+ !!field && isa(field.semantic_type, TYPE.Country);+export const isCoordinate = (+ field: FieldTypeInfo | null | undefined,+): boolean => !!field && isa(field.semantic_type, TYPE.Coordinate);+export const isLatitude = (field: FieldTypeInfo | null | undefined): boolean =>+ !!field && isa(field.semantic_type, TYPE.Latitude);+export const isLongitude = (field: FieldTypeInfo | null | undefined): boolean =>+ !!field && isa(field.semantic_type, TYPE.Longitude);++export const isCurrency = (field: FieldTypeInfo | null | undefined): boolean =>+ !!field && isa(field.semantic_type, TYPE.Currency);++export const isPercentage = (+ field: FieldTypeInfo | null | undefined,+): boolean => !!field && isa(field.semantic_type, TYPE.Percentage);++export const isID = (field: FieldTypeInfo | null | undefined): boolean =>+ isFK(field) || isPK(field);++export const isURL = (field: FieldTypeInfo | null | undefined): boolean =>+ !!field && isa(field.semantic_type, TYPE.URL);+export const isEmail = (field: FieldTypeInfo | null | undefined): boolean =>+ !!field && isa(field.semantic_type, TYPE.Email);+export const isAvatarURL = (field: FieldTypeInfo | null | undefined): boolean =>+ !!field && isa(field.semantic_type, TYPE.AvatarURL);+export const isImageURL = (field: FieldTypeInfo | null | undefined): boolean =>+ !!field && isa(field.semantic_type, TYPE.ImageURL);++export function hasLatitudeAndLongitudeColumns(cols: FieldTypeInfo[]) {+ let hasLatitude = false;+ let hasLongitude = false;+ for (const col of cols) {+ if (isLatitude(col)) {+ hasLatitude = true;+ }+ if (isLongitude(col)) {+ hasLongitude = true;+ }+ }+ return hasLatitude && hasLongitude;+}++export const getIsPKFromTablePredicate =+ (tableId: TableId | undefined) =>+ (column: FieldTypeInfo & { table_id?: TableId }): boolean => {+ const isPrimaryKey = isPK(column);++ // FIXME: columns of nested questions at this moment miss table_id value+ // which makes it impossible to match them with their tables that are nested cards+ return isVirtualCardId(tableId)+ ? isPrimaryKey+ : isPrimaryKey && column.table_id === tableId;+ };diff --git a/frontend/src/metabase/visualizations/visualizations/PivotTable/partitions.tsx b/frontend/src/metabase/visualizations/visualizations/PivotTable/partitions.tsxindex f2c56d0a027..2751ba607fa 100644--- a/frontend/src/metabase/visualizations/visualizations/PivotTable/partitions.tsx+++ b/frontend/src/metabase/visualizations/visualizations/PivotTable/partitions.tsx@@ -7,7 +7,7 @@ import { PivotTableSettingLabel } from "./PivotTable.styled";export interface Partition {name: "rows" | "columns" | "values";- columnFilter: (col: DatasetColumn | undefined) => boolean;+ columnFilter: (col: DatasetColumn) => boolean;title: React.ReactNode;}