mirror of
https://github.com/rowyio/rowy.git
synced 2025-12-29 00:16:39 +01:00
detect type fixes
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user