detect type fixes

This commit is contained in:
iamanishroy
2023-02-14 18:43:28 +05:30
parent f9b54d70a2
commit 98b316eb3c

View File

@@ -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;
@@ -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;