Skip to content

Commit

Permalink
detect type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
iamanishroy committed Feb 14, 2023
1 parent f9b54d7 commit 98b316e
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/components/TableModals/ImportExistingWizard/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,24 @@ export const REGEX_URL =
export const REGEX_HTML = /<\/?[a-z][\s\S]*>/;

const inferTypeFromValue = (value: any) => {
// by default the type of value is string, so trying to convert it to JSON/Object.
try {
value = JSON.parse(value);
} catch (e) {}
if (!value || typeof value === "function") return;

if (Array.isArray(value) && typeof value[0] === "string")
return FieldType.multiSelect;
if (typeof value === "boolean") return FieldType.checkbox;
if (isDate(value)) return FieldType.dateTime;
// trying to convert the value to date
if (+new Date(value)) {
// date and time are separated by a blank space, checking if time present.
if (value.split(" ").length > 1) {
return FieldType.dateTime;
}
return FieldType.date;
}

if (typeof value === "object") {
if ("hex" in value && "rgb" in value) return FieldType.color;
Expand Down Expand Up @@ -71,6 +83,7 @@ const inferTypeFromValue = (value: any) => {
export const suggestType = (data: { [key: string]: any }[], field: string) => {
const results: Record<string, number> = {};

// console.log(data)
data.forEach((row) => {
const result = inferTypeFromValue(row[field]);
if (!result) return;
Expand Down

0 comments on commit 98b316e

Please sign in to comment.