@firecms/schema_inference 3.0.0-canary.19 → 3.0.0-canary.191
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +114 -21
- package/dist/collection_builder.d.ts +3 -2
- package/dist/index.es.js +390 -202
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +433 -1
- package/dist/index.umd.js.map +1 -1
- package/package.json +6 -7
- package/src/builders/string_property_builder.ts +1 -2
- package/src/collection_builder.ts +127 -38
- package/src/strings.ts +1 -1
- package/src/test_schemas/pop_products.json +948 -0
- package/src/test_schemas/test_schema.ts +5 -1
package/dist/index.es.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.es.js","sources":["../src/strings.ts","../src/util.ts","../src/builders/string_property_builder.ts","../src/builders/validation_builder.ts","../src/builders/reference_property_builder.ts","../src/collection_builder.ts"],"sourcesContent":["import { ValuesCountEntry } from \"./types\";\nimport { DocumentReference } from \"firebase/firestore\";\n\nexport function findCommonInitialStringInPath(valuesCount?: ValuesCountEntry) {\n\n if (!valuesCount) return undefined;\n\n function getPath(value: any) {\n if (typeof value === \"string\") return value;\n else if (value instanceof DocumentReference) return value.path;\n else return undefined;\n }\n\n const strings: string[] = valuesCount.values.map((v) => getPath(v)).filter(v => !!v) as string[];\n const pathWithSlash = strings.find((s) => s.includes(\"/\"));\n if (!pathWithSlash)\n return undefined;\n\n const searchedPath = pathWithSlash.substr(0, pathWithSlash.lastIndexOf(\"/\"));\n\n const yep = valuesCount.values\n .filter((value) => {\n const path = getPath(value);\n if (!path) return false;\n return path.startsWith(searchedPath)\n }).length > valuesCount.values.length / 3 * 2;\n\n return yep ? searchedPath : undefined;\n\n}\n\nexport function removeInitialAndTrailingSlashes(s: string): string {\n return removeInitialSlash(removeTrailingSlash(s));\n}\n\nexport function removeInitialSlash(s: string) {\n if (s.startsWith(\"/\"))\n return s.slice(1);\n else return s;\n}\n\nexport function removeTrailingSlash(s: string) {\n if (s.endsWith(\"/\"))\n return s.slice(0, -1);\n else return s;\n}\n","import { unslugify } from \"@firecms/core\";\n\nexport function extractEnumFromValues(values: unknown[]) {\n if (!Array.isArray(values)) {\n return [];\n }\n const enumValues = values\n .map((value) => {\n if (typeof value === \"string\") {\n return ({ id: value, label: unslugify(value) });\n } else\n return null;\n }).filter(Boolean) as Array<{ id: string, label: string }>;\n enumValues.sort((a, b) => a.label.localeCompare(b.label));\n return enumValues;\n}\n","import { FileType, Property, StringProperty } from \"@firecms/core\";\nimport { InferencePropertyBuilderProps, ValuesCountEntry } from \"../types\";\nimport { findCommonInitialStringInPath } from \"../strings\";\nimport { extractEnumFromValues } from \"../util\";\n\nconst IMAGE_EXTENSIONS = [\".jpg\", \".png\", \".webp\", \".gif\"];\nconst AUDIO_EXTENSIONS = [\".mp3\", \".ogg\", \".opus\", \".aac\"];\nconst VIDEO_EXTENSIONS = [\".avi\", \".mp4\"];\n\nconst emailRegEx = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;\n\n\nexport function buildStringProperty({\n totalDocsCount,\n valuesResult\n }: InferencePropertyBuilderProps): Property {\n\n let stringProperty: Property = {\n dataType: \"string\",\n\n };\n\n if (valuesResult) {\n\n const totalEntriesCount = valuesResult.values.length;\n const totalValues = Array.from(valuesResult.valuesCount.keys()).length;\n\n const config: Partial<StringProperty> = {};\n\n const probablyAURL = valuesResult.values\n .filter((value) => typeof value === \"string\" &&\n value.toString().startsWith(\"http\")).length > totalDocsCount / 3 * 2;\n if (probablyAURL) {\n config.url = true;\n }\n\n const probablyAnEmail = valuesResult.values\n .filter((value) => typeof value === \"string\" &&\n emailRegEx.test(value)).length > totalDocsCount / 3 * 2;\n if (probablyAnEmail) {\n config.email = true;\n }\n\n const probablyUserIds = valuesResult.values\n .filter((value) => typeof value === \"string\" && value.length === 28 && !value.includes(\" \"))\n .length > totalDocsCount / 3 * 2;\n if (probablyUserIds)\n config.readOnly = true;\n\n if (!probablyAnEmail &&\n !probablyAURL &&\n !probablyUserIds &&\n !probablyAURL &&\n totalValues < totalEntriesCount / 3\n ) {\n const enumValues = extractEnumFromValues(Array.from(valuesResult.valuesCount.keys()));\n\n if (Object.keys(enumValues).length > 1)\n config.enumValues = enumValues;\n }\n\n // regular string\n if (!probablyAnEmail &&\n !probablyAURL &&\n !probablyUserIds &&\n !probablyAURL &&\n !config.enumValues) {\n const fileType = probableFileType(valuesResult, totalDocsCount);\n if (fileType) {\n config.storage = {\n acceptedFiles: [fileType as FileType],\n storagePath: findCommonInitialStringInPath(valuesResult) ?? \"/\"\n };\n }\n }\n\n if (Object.keys(config).length > 0)\n stringProperty = {\n ...stringProperty,\n ...config,\n editable: true\n };\n }\n\n return stringProperty;\n}\n\n// TODO: support returning multiple types\nfunction probableFileType(valuesCount: ValuesCountEntry, totalDocsCount: number): boolean | FileType {\n const probablyAnImage = valuesCount.values\n .filter((value) => typeof value === \"string\" &&\n IMAGE_EXTENSIONS.some((extension) => value.toString().endsWith(extension))).length > totalDocsCount / 3 * 2;\n\n const probablyAudio = valuesCount.values\n .filter((value) => typeof value === \"string\" &&\n AUDIO_EXTENSIONS.some((extension) => value.toString().endsWith(extension))).length > totalDocsCount / 3 * 2;\n\n const probablyVideo = valuesCount.values\n .filter((value) => typeof value === \"string\" &&\n VIDEO_EXTENSIONS.some((extension) => value.toString().endsWith(extension))).length > totalDocsCount / 3 * 2;\n\n const fileType: boolean | FileType = probablyAnImage\n ? \"image/*\"\n : probablyAudio\n ? \"audio/*\"\n : probablyVideo ? \"video/*\" : false;\n return fileType;\n}\n","import { PropertyValidationSchema } from \"@firecms/core\";\nimport { InferencePropertyBuilderProps } from \"../types\";\n\nexport function buildValidation({\n totalDocsCount,\n valuesResult\n }: InferencePropertyBuilderProps): PropertyValidationSchema | undefined {\n\n if (valuesResult) {\n const totalEntriesCount = valuesResult.values.length;\n if (totalDocsCount === totalEntriesCount)\n return {\n required: true\n }\n }\n\n return undefined;\n}\n","import { InferencePropertyBuilderProps } from \"../types\";\nimport { findCommonInitialStringInPath } from \"../strings\";\nimport { Property } from \"@firecms/core\";\n\nexport function buildReferenceProperty({\n totalDocsCount,\n valuesResult\n }: InferencePropertyBuilderProps): Property {\n\n const property: Property = {\n dataType: \"reference\",\n path: findCommonInitialStringInPath(valuesResult) ?? \"!!!FIX_ME!!!\",\n editable: true\n };\n\n return property;\n}\n","import {\n DataType,\n EnumValues,\n mergeDeep,\n Properties,\n Property,\n resolveEnumValues,\n StringProperty,\n unslugify\n} from \"@firecms/core\";\nimport {\n InferencePropertyBuilderProps,\n TypesCount,\n TypesCountRecord,\n ValuesCountEntry,\n ValuesCountRecord\n} from \"./types\";\nimport { buildStringProperty } from \"./builders/string_property_builder\";\nimport { buildValidation } from \"./builders/validation_builder\";\nimport { buildReferenceProperty } from \"./builders/reference_property_builder\";\nimport { extractEnumFromValues } from \"./util\";\n\nexport type InferenceTypeBuilder = (value: any) => DataType;\n\nexport async function buildEntityPropertiesFromData(data: object[], getType: InferenceTypeBuilder): Promise<Properties> {\n const typesCount: TypesCountRecord = {};\n const valuesCount: ValuesCountRecord = {};\n if (data) {\n data.forEach((entry) => {\n if (entry) {\n Object.entries(entry).forEach(([key, value]) => {\n increaseMapTypeCount(typesCount, key, value, getType);\n increaseValuesCount(valuesCount, key, value, getType);\n })\n }\n });\n }\n // console.log(util.inspect({ typesCount }, { showHidden: false, depth: null, colors: true }));\n return buildPropertiesFromCount(data.length, typesCount, valuesCount);\n}\n\nexport function buildPropertyFromData(data: any[], property: Property, getType: InferenceTypeBuilder): Property {\n const typesCount = {};\n const valuesCount: ValuesCountRecord = {};\n if (data) {\n data.forEach((entry) => {\n increaseTypeCount(property.dataType, typesCount, entry, getType);\n increaseValuesCount(valuesCount, \"inferred_prop\", entry, getType);\n });\n }\n const enumValues = \"enumValues\" in property ? resolveEnumValues(property[\"enumValues\"] as EnumValues) : undefined;\n if (enumValues) {\n const newEnumValues = extractEnumFromValues(Array.from(valuesCount[\"inferred_prop\"].valuesCount.keys()));\n return {\n ...property,\n enumValues: [...newEnumValues, ...enumValues]\n } as StringProperty;\n }\n const generatedProperty = buildPropertyFromCount(\"inferred_prop\", data.length, property.dataType, typesCount, valuesCount[\"inferred_prop\"]);\n return mergeDeep(generatedProperty, property);\n}\n\nexport function buildPropertiesOrder(properties: Properties<any>): string [] {\n function propOrder(s: string) {\n const k = s.toLowerCase();\n if (k === \"title\" || k === \"name\") return 3;\n if (k.includes(\"title\") || k.includes(\"name\")) return 2;\n if (k.includes(\"image\") || k.includes(\"picture\")) return 1;\n return 0;\n }\n\n const keys = Object.keys(properties);\n keys.sort(); // alphabetically\n keys.sort((a, b) => {\n return propOrder(b) - propOrder(a);\n });\n return keys;\n}\n\n/**\n * @param type\n * @param typesCount\n * @param fieldValue\n * @param getType\n */\nfunction increaseTypeCount(type: DataType, typesCount: TypesCount, fieldValue: any, getType: InferenceTypeBuilder) {\n if (type === \"map\") {\n if (fieldValue) {\n let mapTypesCount = typesCount[type];\n if (!mapTypesCount) {\n mapTypesCount = {};\n typesCount[type] = mapTypesCount;\n }\n Object.entries(fieldValue).forEach(([key, value]) => {\n increaseMapTypeCount(mapTypesCount as TypesCountRecord, key, value, getType);\n })\n }\n } else if (type === \"array\") {\n let arrayTypesCount = typesCount[type];\n if (!arrayTypesCount) {\n arrayTypesCount = {};\n typesCount[type] = arrayTypesCount;\n }\n if (fieldValue && Array.isArray(fieldValue) && fieldValue.length > 0) {\n const arrayType = getMostProbableTypeInArray(fieldValue, getType); // get type of first element\n if (!arrayTypesCount[arrayType]) (arrayTypesCount[arrayType] as number) = 1;\n else (arrayTypesCount[arrayType] as number)++;\n }\n } else {\n if (!typesCount[type]) typesCount[type] = 1;\n else (typesCount[type] as number)++;\n }\n}\n\nfunction increaseMapTypeCount(\n typesCountRecord: TypesCountRecord,\n key: string,\n fieldValue: any,\n getType: InferenceTypeBuilder\n) {\n let typesCount: TypesCount = typesCountRecord[key];\n if (!typesCount) {\n typesCount = {};\n typesCountRecord[key] = typesCount;\n }\n\n if (fieldValue != null) { // Check that fieldValue is not null or undefined before proceeding\n const type = getType(fieldValue);\n increaseTypeCount(type, typesCount, fieldValue, getType);\n }\n}\n\nfunction increaseValuesCount(\n typeValuesRecord: ValuesCountRecord,\n key: string,\n fieldValue: any,\n getType: InferenceTypeBuilder\n) {\n\n const dataType = getType(fieldValue);\n\n let valuesRecord: {\n values: any[];\n valuesCount: Map<any, number>;\n map?: ValuesCountRecord;\n } = typeValuesRecord[key];\n\n if (!valuesRecord) {\n valuesRecord = {\n values: [],\n valuesCount: new Map()\n };\n typeValuesRecord[key] = valuesRecord;\n }\n\n if (dataType === \"map\") {\n let mapValuesRecord: ValuesCountRecord | undefined = valuesRecord.map;\n if (!mapValuesRecord) {\n mapValuesRecord = {};\n valuesRecord.map = mapValuesRecord;\n }\n if (fieldValue)\n Object.entries(fieldValue).forEach(([key, value]) => increaseValuesCount(mapValuesRecord as ValuesCountRecord, key, value, getType))\n } else if (dataType === \"array\") {\n if (Array.isArray(fieldValue)) {\n fieldValue.forEach((value) => {\n valuesRecord.values.push(value);\n valuesRecord.valuesCount.set(value, (valuesRecord.valuesCount.get(value) ?? 0) + 1);\n })\n }\n } else {\n if (fieldValue) {\n valuesRecord.values.push(fieldValue);\n valuesRecord.valuesCount.set(fieldValue, (valuesRecord.valuesCount.get(fieldValue) ?? 0) + 1);\n }\n }\n\n}\n\nfunction getHighestTypesCount(typesCount: TypesCount): number {\n let highestCount = 0;\n Object.entries(typesCount).forEach(([type, count]) => {\n let countValue = 0;\n if (type === \"map\") {\n countValue = getHighestRecordCount(count as TypesCountRecord);\n } else if (type === \"array\") {\n countValue = getHighestTypesCount(count as TypesCount);\n } else {\n countValue = count as number;\n }\n if (countValue > highestCount) {\n highestCount = countValue;\n }\n });\n\n return highestCount;\n}\n\nfunction getHighestRecordCount(record: TypesCountRecord): number {\n return Object.entries(record)\n .map(([key, typesCount]) => getHighestTypesCount(typesCount))\n .reduce((a, b) => Math.max(a, b), 0);\n}\n\nfunction getMostProbableType(typesCount: TypesCount): DataType {\n let highestCount = -1;\n let probableType: DataType = \"string\"; //default\n Object.entries(typesCount).forEach(([type, count]) => {\n let countValue;\n if (type === \"map\") {\n countValue = getHighestRecordCount(count as TypesCountRecord);\n } else if (type === \"array\") {\n countValue = getHighestTypesCount(count as TypesCount);\n } else {\n countValue = count as number;\n }\n if (countValue > highestCount) {\n highestCount = countValue;\n probableType = type as DataType;\n }\n });\n return probableType;\n}\n\nfunction buildPropertyFromCount(key: string, totalDocsCount: number, mostProbableType: DataType, typesCount: TypesCount, valuesResult?: ValuesCountEntry): Property {\n let title: string | undefined;\n\n if (key) {\n title = unslugify(key);\n }\n\n let result: Property | undefined = undefined;\n if (mostProbableType === \"map\") {\n\n const highVariability = checkTypesCountHighVariability(typesCount);\n if (highVariability) {\n result = {\n dataType: \"map\",\n name: title,\n keyValue: true,\n properties: {}\n };\n }\n const properties = buildPropertiesFromCount(totalDocsCount, typesCount.map as TypesCountRecord, valuesResult ? valuesResult.mapValues : undefined);\n result = {\n dataType: \"map\",\n name: title,\n properties\n };\n } else if (mostProbableType === \"array\") {\n const arrayTypesCount = typesCount.array as TypesCount;\n const arrayMostProbableType = getMostProbableType(arrayTypesCount);\n const of = buildPropertyFromCount(key, totalDocsCount, arrayMostProbableType, arrayTypesCount, valuesResult);\n result = {\n dataType: \"array\",\n name: title,\n of\n };\n }\n if (!result) {\n const propertyProps: InferencePropertyBuilderProps = {\n name: key,\n totalDocsCount,\n valuesResult\n };\n if (mostProbableType === \"string\") {\n result = buildStringProperty(propertyProps);\n } else if (mostProbableType === \"reference\") {\n result = buildReferenceProperty(propertyProps);\n } else {\n result = {\n dataType: mostProbableType\n } as Property;\n }\n\n if (title) {\n result.name = title;\n }\n\n const validation = buildValidation(propertyProps);\n if (validation) {\n result.validation = validation;\n }\n }\n\n return {\n ...result,\n editable: true\n };\n}\n\nfunction buildPropertiesFromCount(totalDocsCount: number, typesCountRecord: TypesCountRecord, valuesCountRecord?: ValuesCountRecord): Properties {\n const res: Properties = {};\n Object.entries(typesCountRecord).forEach(([key, typesCount]) => {\n const mostProbableType = getMostProbableType(typesCount);\n res[key] = buildPropertyFromCount(key, totalDocsCount, mostProbableType, typesCount, valuesCountRecord ? valuesCountRecord[key] : undefined);\n })\n return res;\n}\n\nfunction countMaxDocumentsUnder(typesCount: TypesCount) {\n let count = 0;\n Object.entries(typesCount).forEach(([type, value]) => {\n // console.log(util.inspect({ type, value }, { showHidden: false, depth: null, colors: true }));\n if (typeof value === \"object\") {\n count = Math.max(count, countMaxDocumentsUnder(value as TypesCountRecord));\n } else {\n count = Math.max(count, value as number);\n }\n });\n return count;\n}\n\nfunction getMostProbableTypeInArray(array: any[], getType: InferenceTypeBuilder): DataType {\n let typesCount: TypesCount = {};\n array.forEach((value) => {\n increaseTypeCount(getType(value), typesCount, value, getType);\n });\n return getMostProbableType(typesCount);\n}\n\nfunction checkTypesCountHighVariability(typesCount: TypesCount) {\n const maxCount = countMaxDocumentsUnder(typesCount);\n let keysWithFewValues = 0;\n Object.entries(typesCount.map ?? {})\n .forEach(([key, value]) => {\n const count = countMaxDocumentsUnder(value);\n if (count < maxCount / 3) {\n keysWithFewValues++;\n }\n });\n return keysWithFewValues / Object.entries(typesCount.map ?? {}).length > 0.5;\n}\n\n"],"names":["findCommonInitialStringInPath","valuesCount","getPath","value","DocumentReference","pathWithSlash","v","s","searchedPath","path","extractEnumFromValues","values","enumValues","unslugify","a","b","IMAGE_EXTENSIONS","AUDIO_EXTENSIONS","VIDEO_EXTENSIONS","emailRegEx","buildStringProperty","totalDocsCount","valuesResult","stringProperty","totalEntriesCount","totalValues","config","probablyAURL","probablyAnEmail","probablyUserIds","fileType","probableFileType","probablyAnImage","extension","probablyAudio","probablyVideo","buildValidation","buildReferenceProperty","buildEntityPropertiesFromData","data","getType","typesCount","entry","key","increaseMapTypeCount","increaseValuesCount","buildPropertiesFromCount","buildPropertyFromData","property","increaseTypeCount","resolveEnumValues","newEnumValues","generatedProperty","buildPropertyFromCount","mergeDeep","buildPropertiesOrder","properties","propOrder","k","keys","type","fieldValue","mapTypesCount","arrayTypesCount","arrayType","getMostProbableTypeInArray","typesCountRecord","typeValuesRecord","dataType","valuesRecord","mapValuesRecord","getHighestTypesCount","highestCount","count","countValue","getHighestRecordCount","record","getMostProbableType","probableType","mostProbableType","title","result","checkTypesCountHighVariability","arrayMostProbableType","of","propertyProps","validation","valuesCountRecord","res","countMaxDocumentsUnder","array","maxCount","keysWithFewValues"],"mappings":";;AAGO,SAASA,EAA8BC,GAAgC;AAE1E,MAAI,CAACA;AAAoB;AAEzB,WAASC,EAAQC,GAAY;AACzB,WAAI,OAAOA,KAAU,WAAiBA,IAC7BA,aAAiBC,IAA0BD,EAAM,OAC9C;AAAA,EAChB;AAGM,QAAAE,IADoBJ,EAAY,OAAO,IAAI,CAACK,MAAMJ,EAAQI,CAAC,CAAC,EAAE,OAAO,CAAKA,MAAA,CAAC,CAACA,CAAC,EACrD,KAAK,CAACC,MAAMA,EAAE,SAAS,GAAG,CAAC;AACzD,MAAI,CAACF;AACM;AAEX,QAAMG,IAAeH,EAAc,OAAO,GAAGA,EAAc,YAAY,GAAG,CAAC;AAS3E,SAPYJ,EAAY,OACnB,OAAO,CAACE,MAAU;AACT,UAAAM,IAAOP,EAAQC,CAAK;AAC1B,WAAKM,IACEA,EAAK,WAAWD,CAAY,IADjB;AAAA,EACiB,CACtC,EAAE,SAASP,EAAY,OAAO,SAAS,IAAI,IAEnCO,IAAe;AAEhC;AC3BO,SAASE,EAAsBC,GAAmB;AACrD,MAAI,CAAC,MAAM,QAAQA,CAAM;AACrB,WAAO;AAEX,QAAMC,IAAaD,EACd,IAAI,CAACR,MACE,OAAOA,KAAU,WACT,EAAE,IAAIA,GAAO,OAAOU,EAAUV,CAAK,MAE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GAAyC;AACvF,MAAIC,IAAyB,CAAA;AACvB,SAAAiD,EAAA,QAAQ,CAACvF,MAAU;AACrB,IAAA8C,EAAkBT,EAAQrC,CAAK,GAAGsC,GAAYtC,GAAOqC,CAAO;AAAA,EAAA,CAC/D,GACMqC,EAAoBpC,CAAU;AACzC;AAEA,SAASyC,EAA+BzC,GAAwB;AACtD,QAAAkD,IAAWF,EAAuBhD,CAAU;AAClD,MAAImD,IAAoB;AACjB,gBAAA,QAAQnD,EAAW,OAAO,CAAA,CAAE,EAC9B,QAAQ,CAAC,CAACE,GAAKxC,CAAK,MAAM;AAEnB,IADUsF,EAAuBtF,CAAK,IAC9BwF,IAAW,KACnBC;AAAA,EACJ,CACH,GACEA,IAAoB,OAAO,QAAQnD,EAAW,OAAO,CAAE,CAAA,EAAE,SAAS;AAC7E;"}
|
1
|
+
{"version":3,"file":"index.es.js","sources":["../src/strings.ts","../src/util.ts","../src/builders/string_property_builder.ts","../src/builders/validation_builder.ts","../src/builders/reference_property_builder.ts","../src/collection_builder.ts"],"sourcesContent":["import { ValuesCountEntry } from \"./types\";\nimport { DocumentReference } from \"@firebase/firestore\";\n\nexport function findCommonInitialStringInPath(valuesCount?: ValuesCountEntry) {\n\n if (!valuesCount) return undefined;\n\n function getPath(value: any) {\n if (typeof value === \"string\") return value;\n else if (value instanceof DocumentReference) return value.path;\n else return undefined;\n }\n\n const strings: string[] = valuesCount.values.map((v) => getPath(v)).filter(v => !!v) as string[];\n const pathWithSlash = strings.find((s) => s.includes(\"/\"));\n if (!pathWithSlash)\n return undefined;\n\n const searchedPath = pathWithSlash.substr(0, pathWithSlash.lastIndexOf(\"/\"));\n\n const yep = valuesCount.values\n .filter((value) => {\n const path = getPath(value);\n if (!path) return false;\n return path.startsWith(searchedPath)\n }).length > valuesCount.values.length / 3 * 2;\n\n return yep ? searchedPath : undefined;\n\n}\n\nexport function removeInitialAndTrailingSlashes(s: string): string {\n return removeInitialSlash(removeTrailingSlash(s));\n}\n\nexport function removeInitialSlash(s: string) {\n if (s.startsWith(\"/\"))\n return s.slice(1);\n else return s;\n}\n\nexport function removeTrailingSlash(s: string) {\n if (s.endsWith(\"/\"))\n return s.slice(0, -1);\n else return s;\n}\n","import { unslugify } from \"@firecms/core\";\n\nexport function extractEnumFromValues(values: unknown[]) {\n if (!Array.isArray(values)) {\n return [];\n }\n const enumValues = values\n .map((value) => {\n if (typeof value === \"string\") {\n return ({ id: value, label: unslugify(value) });\n } else\n return null;\n }).filter(Boolean) as Array<{ id: string, label: string }>;\n enumValues.sort((a, b) => a.label.localeCompare(b.label));\n return enumValues;\n}\n","import { FileType, Property, StringProperty } from \"@firecms/core\";\nimport { InferencePropertyBuilderProps, ValuesCountEntry } from \"../types\";\nimport { findCommonInitialStringInPath } from \"../strings\";\nimport { extractEnumFromValues } from \"../util\";\n\nconst IMAGE_EXTENSIONS = [\".jpg\", \".jpeg\", \".png\", \".webp\", \".gif\", \".avif\"];\nconst AUDIO_EXTENSIONS = [\".mp3\", \".ogg\", \".opus\", \".aac\"];\nconst VIDEO_EXTENSIONS = [\".avi\", \".mp4\"];\n\nconst emailRegEx = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;\n\nexport function buildStringProperty({\n totalDocsCount,\n valuesResult\n }: InferencePropertyBuilderProps): Property {\n\n let stringProperty: Property = {\n dataType: \"string\",\n\n };\n\n if (valuesResult) {\n\n const totalEntriesCount = valuesResult.values.length;\n const totalValues = Array.from(valuesResult.valuesCount.keys()).length;\n\n const config: Partial<StringProperty> = {};\n\n const probablyAURL = valuesResult.values\n .filter((value) => typeof value === \"string\" &&\n value.toString().startsWith(\"http\")).length > totalDocsCount / 3 * 2;\n if (probablyAURL) {\n config.url = true;\n }\n\n const probablyAnEmail = valuesResult.values\n .filter((value) => typeof value === \"string\" &&\n emailRegEx.test(value)).length > totalDocsCount / 3 * 2;\n if (probablyAnEmail) {\n config.email = true;\n }\n\n const probablyUserIds = valuesResult.values\n .filter((value) => typeof value === \"string\" && value.length === 28 && !value.includes(\" \"))\n .length > totalDocsCount / 3 * 2;\n if (probablyUserIds)\n config.readOnly = true;\n\n if (!probablyAnEmail &&\n !probablyAURL &&\n !probablyUserIds &&\n !probablyAURL &&\n totalValues < totalEntriesCount / 3\n ) {\n const enumValues = extractEnumFromValues(Array.from(valuesResult.valuesCount.keys()));\n\n if (Object.keys(enumValues).length > 1)\n config.enumValues = enumValues;\n }\n\n // regular string\n if (!probablyAnEmail &&\n !probablyAURL &&\n !probablyUserIds &&\n !probablyAURL &&\n !config.enumValues) {\n const fileType = probableFileType(valuesResult, totalDocsCount);\n if (fileType) {\n config.storage = {\n acceptedFiles: [fileType as FileType],\n storagePath: findCommonInitialStringInPath(valuesResult) ?? \"/\"\n };\n }\n }\n\n if (Object.keys(config).length > 0)\n stringProperty = {\n ...stringProperty,\n ...config,\n editable: true\n };\n }\n\n return stringProperty;\n}\n\n// TODO: support returning multiple types\nfunction probableFileType(valuesCount: ValuesCountEntry, totalDocsCount: number): boolean | FileType {\n const probablyAnImage = valuesCount.values\n .filter((value) => typeof value === \"string\" &&\n IMAGE_EXTENSIONS.some((extension) => value.toString().endsWith(extension))).length > totalDocsCount / 3 * 2;\n\n const probablyAudio = valuesCount.values\n .filter((value) => typeof value === \"string\" &&\n AUDIO_EXTENSIONS.some((extension) => value.toString().endsWith(extension))).length > totalDocsCount / 3 * 2;\n\n const probablyVideo = valuesCount.values\n .filter((value) => typeof value === \"string\" &&\n VIDEO_EXTENSIONS.some((extension) => value.toString().endsWith(extension))).length > totalDocsCount / 3 * 2;\n\n const fileType: boolean | FileType = probablyAnImage\n ? \"image/*\"\n : probablyAudio\n ? \"audio/*\"\n : probablyVideo ? \"video/*\" : false;\n return fileType;\n}\n","import { PropertyValidationSchema } from \"@firecms/core\";\nimport { InferencePropertyBuilderProps } from \"../types\";\n\nexport function buildValidation({\n totalDocsCount,\n valuesResult\n }: InferencePropertyBuilderProps): PropertyValidationSchema | undefined {\n\n if (valuesResult) {\n const totalEntriesCount = valuesResult.values.length;\n if (totalDocsCount === totalEntriesCount)\n return {\n required: true\n }\n }\n\n return undefined;\n}\n","import { InferencePropertyBuilderProps } from \"../types\";\nimport { findCommonInitialStringInPath } from \"../strings\";\nimport { Property } from \"@firecms/core\";\n\nexport function buildReferenceProperty({\n totalDocsCount,\n valuesResult\n }: InferencePropertyBuilderProps): Property {\n\n const property: Property = {\n dataType: \"reference\",\n path: findCommonInitialStringInPath(valuesResult) ?? \"!!!FIX_ME!!!\",\n editable: true\n };\n\n return property;\n}\n","import {\n DataType,\n EnumValues,\n mergeDeep,\n Properties, PropertiesOrBuilders,\n Property,\n resolveEnumValues,\n StringProperty\n} from \"@firecms/core\";\nimport {\n InferencePropertyBuilderProps,\n TypesCount,\n TypesCountRecord,\n ValuesCountEntry,\n ValuesCountRecord\n} from \"./types\";\nimport { buildStringProperty } from \"./builders/string_property_builder\";\nimport { buildValidation } from \"./builders/validation_builder\";\nimport { buildReferenceProperty } from \"./builders/reference_property_builder\";\nimport { extractEnumFromValues } from \"./util\";\n\nexport type InferenceTypeBuilder = (value: any) => DataType;\n\nexport async function buildEntityPropertiesFromData(\n data: object[],\n getType: InferenceTypeBuilder\n): Promise<Properties> {\n const typesCount: TypesCountRecord = {};\n const valuesCount: ValuesCountRecord = {};\n if (data) {\n data.forEach((entry) => {\n if (entry) {\n Object.entries(entry).forEach(([key, value]) => {\n increaseMapTypeCount(typesCount, key, value, getType);\n increaseValuesCount(valuesCount, key, value, getType);\n });\n }\n });\n }\n return buildPropertiesFromCount(data.length, typesCount, valuesCount);\n}\n\nexport function buildPropertyFromData(\n data: any[],\n property: Property,\n getType: InferenceTypeBuilder\n): Property {\n const typesCount = {};\n const valuesCount: ValuesCountRecord = {};\n if (data) {\n data.forEach((entry) => {\n increaseTypeCount(property.dataType, typesCount, entry, getType);\n increaseValuesCount(valuesCount, \"inferred_prop\", entry, getType);\n });\n }\n const enumValues = \"enumValues\" in property ? resolveEnumValues(property[\"enumValues\"] as EnumValues) : undefined;\n if (enumValues) {\n const newEnumValues = extractEnumFromValues(Array.from(valuesCount[\"inferred_prop\"].valuesCount.keys()));\n return {\n ...property,\n enumValues: [...newEnumValues, ...enumValues]\n } as StringProperty;\n }\n const generatedProperty = buildPropertyFromCount(\n \"inferred_prop\",\n data.length,\n property.dataType,\n typesCount,\n valuesCount[\"inferred_prop\"]\n );\n return mergeDeep(generatedProperty, property);\n}\n\nexport function buildPropertiesOrder(\n properties: PropertiesOrBuilders,\n propertiesOrder?: string[],\n priorityKeys?: string[]\n): string[] {\n const lowerCasePriorityKeys = (priorityKeys ?? []).map((key) => key.toLowerCase());\n\n function propOrder(s: string) {\n const k = s.toLowerCase();\n if (lowerCasePriorityKeys.includes(k)) return 4;\n if (k === \"title\" || k === \"name\") return 3;\n if (k.includes(\"title\") || k.includes(\"name\")) return 2;\n if (k.includes(\"image\") || k.includes(\"picture\")) return 1;\n return 0;\n }\n\n const keys = propertiesOrder ?? Object.keys(properties);\n keys.sort(); // alphabetically\n keys.sort((a, b) => {\n return propOrder(b) - propOrder(a);\n });\n return keys;\n}\n\n/**\n * @param type\n * @param typesCount\n * @param fieldValue\n * @param getType\n */\nfunction increaseTypeCount(\n type: DataType,\n typesCount: TypesCount,\n fieldValue: any,\n getType: InferenceTypeBuilder\n) {\n if (type === \"map\") {\n if (fieldValue) {\n let mapTypesCount = typesCount[type];\n if (!mapTypesCount) {\n mapTypesCount = {};\n typesCount[type] = mapTypesCount;\n }\n Object.entries(fieldValue).forEach(([key, value]) => {\n increaseMapTypeCount(mapTypesCount as TypesCountRecord, key, value, getType);\n });\n }\n } else if (type === \"array\") {\n let arrayTypesCount = typesCount[type];\n if (!arrayTypesCount) {\n arrayTypesCount = {};\n typesCount[type] = arrayTypesCount;\n }\n if (fieldValue && Array.isArray(fieldValue) && fieldValue.length > 0) {\n const arrayType = getMostProbableTypeInArray(fieldValue, getType);\n if (arrayType === \"map\") {\n let mapTypesCount = arrayTypesCount[arrayType];\n if (!mapTypesCount) {\n mapTypesCount = {};\n }\n fieldValue.forEach((value) => {\n Object.entries(value).forEach(([key, v]) =>\n increaseMapTypeCount(mapTypesCount, key, v, getType)\n );\n });\n arrayTypesCount[arrayType] = mapTypesCount;\n } else {\n if (!arrayTypesCount[arrayType]) arrayTypesCount[arrayType] = 1;\n else (arrayTypesCount[arrayType] as number)++;\n }\n }\n } else {\n if (!typesCount[type]) typesCount[type] = 1;\n else (typesCount[type] as number)++;\n }\n}\n\nfunction increaseMapTypeCount(\n typesCountRecord: TypesCountRecord,\n key: string,\n fieldValue: any,\n getType: InferenceTypeBuilder\n) {\n let typesCount: TypesCount = typesCountRecord[key];\n if (!typesCount) {\n typesCount = {};\n typesCountRecord[key] = typesCount;\n }\n\n if (fieldValue != null) {\n // Check that fieldValue is not null or undefined before proceeding\n const type = getType(fieldValue);\n increaseTypeCount(type, typesCount, fieldValue, getType);\n }\n}\n\nfunction increaseValuesCount(\n typeValuesRecord: ValuesCountRecord,\n key: string,\n fieldValue: any,\n getType: InferenceTypeBuilder\n) {\n const dataType = getType(fieldValue);\n\n let valuesRecord: {\n values: any[];\n valuesCount: Map<any, number>;\n map?: ValuesCountRecord;\n } = typeValuesRecord[key];\n\n if (!valuesRecord) {\n valuesRecord = {\n values: [],\n valuesCount: new Map()\n };\n typeValuesRecord[key] = valuesRecord;\n }\n\n if (dataType === \"map\") {\n let mapValuesRecord: ValuesCountRecord | undefined = valuesRecord.map;\n if (!mapValuesRecord) {\n mapValuesRecord = {};\n valuesRecord.map = mapValuesRecord;\n }\n if (fieldValue)\n Object.entries(fieldValue).forEach(([key, value]) =>\n increaseValuesCount(mapValuesRecord as ValuesCountRecord, key, value, getType)\n );\n } else if (dataType === \"array\") {\n if (Array.isArray(fieldValue)) {\n fieldValue.forEach((value) => {\n valuesRecord.values.push(value);\n valuesRecord.valuesCount.set(value, (valuesRecord.valuesCount.get(value) ?? 0) + 1);\n });\n }\n } else {\n if (fieldValue) {\n valuesRecord.values.push(fieldValue);\n valuesRecord.valuesCount.set(fieldValue, (valuesRecord.valuesCount.get(fieldValue) ?? 0) + 1);\n }\n }\n}\n\nfunction getHighestTypesCount(typesCount: TypesCount): number {\n let highestCount = 0;\n Object.entries(typesCount).forEach(([type, count]) => {\n let countValue = 0;\n if (type === \"map\") {\n countValue = getHighestRecordCount(count as TypesCountRecord);\n } else if (type === \"array\") {\n countValue = getHighestTypesCount(count as TypesCount);\n } else {\n countValue = count as number;\n }\n if (countValue > highestCount) {\n highestCount = countValue;\n }\n });\n\n return highestCount;\n}\n\nfunction getHighestRecordCount(record: TypesCountRecord): number {\n return Object.entries(record)\n .map(([key, typesCount]) => getHighestTypesCount(typesCount))\n .reduce((a, b) => Math.max(a, b), 0);\n}\n\nfunction getMostProbableType(typesCount: TypesCount): DataType {\n let highestCount = -1;\n let probableType: DataType = \"string\"; // default\n Object.entries(typesCount).forEach(([type, count]) => {\n let countValue;\n if (type === \"map\") {\n countValue = getHighestRecordCount(count as TypesCountRecord);\n } else if (type === \"array\") {\n countValue = getHighestTypesCount(count as TypesCount);\n } else {\n countValue = count as number;\n }\n if (countValue > highestCount) {\n highestCount = countValue;\n probableType = type as DataType;\n }\n });\n return probableType;\n}\n\nfunction buildPropertyFromCount(\n key: string,\n totalDocsCount: number,\n mostProbableType: DataType,\n typesCount: TypesCount,\n valuesResult?: ValuesCountEntry\n): Property {\n let title: string | undefined;\n\n if (key) {\n title = formatString(key.toLowerCase());\n }\n\n let result: Property | undefined = undefined;\n if (mostProbableType === \"map\") {\n const highVariability = checkTypesCountHighVariability(typesCount);\n if (highVariability) {\n result = {\n dataType: \"map\",\n name: title,\n keyValue: true,\n properties: {}\n };\n }\n const properties = buildPropertiesFromCount(\n totalDocsCount,\n typesCount.map as TypesCountRecord,\n valuesResult ? valuesResult.mapValues : undefined\n );\n result = {\n dataType: \"map\",\n name: title,\n properties\n };\n } else if (mostProbableType === \"array\") {\n const arrayTypesCount = typesCount.array as TypesCount;\n const arrayMostProbableType = getMostProbableType(arrayTypesCount);\n const of = buildPropertyFromCount(\n key,\n totalDocsCount,\n arrayMostProbableType,\n arrayTypesCount,\n valuesResult\n );\n result = {\n dataType: \"array\",\n name: title,\n of\n };\n }\n\n if (!result) {\n const propertyProps: InferencePropertyBuilderProps = {\n name: key,\n totalDocsCount,\n valuesResult\n };\n if (mostProbableType === \"string\") {\n result = buildStringProperty(propertyProps);\n } else if (mostProbableType === \"reference\") {\n result = buildReferenceProperty(propertyProps);\n } else {\n result = {\n dataType: mostProbableType\n } as Property;\n }\n\n if (title) {\n result.name = title;\n }\n\n const validation = buildValidation(propertyProps);\n if (validation) {\n result.validation = validation;\n }\n }\n\n return {\n ...result,\n editable: true\n };\n}\n\nfunction buildPropertiesFromCount(\n totalDocsCount: number,\n typesCountRecord: TypesCountRecord,\n valuesCountRecord?: ValuesCountRecord\n): Properties {\n const res: Properties = {};\n Object.entries(typesCountRecord).forEach(([key, typesCount]) => {\n const mostProbableType = getMostProbableType(typesCount);\n res[key] = buildPropertyFromCount(\n key,\n totalDocsCount,\n mostProbableType,\n typesCount,\n valuesCountRecord ? valuesCountRecord[key] : undefined\n );\n });\n return res;\n}\n\nfunction countMaxDocumentsUnder(typesCount: TypesCount) {\n let count = 0;\n Object.entries(typesCount).forEach(([type, value]) => {\n if (typeof value === \"object\") {\n count = Math.max(count, countMaxDocumentsUnder(value as TypesCountRecord));\n } else {\n count = Math.max(count, value as number);\n }\n });\n return count;\n}\n\nfunction getMostProbableTypeInArray(\n array: any[],\n getType: InferenceTypeBuilder\n): DataType {\n let typesCount: TypesCount = {};\n array.forEach((value) => {\n increaseTypeCount(getType(value), typesCount, value, getType);\n });\n return getMostProbableType(typesCount);\n}\n\nfunction checkTypesCountHighVariability(typesCount: TypesCount) {\n const maxCount = countMaxDocumentsUnder(typesCount);\n let keysWithFewValues = 0;\n Object.entries(typesCount.map ?? {}).forEach(([key, value]) => {\n const count = countMaxDocumentsUnder(value);\n if (count < maxCount / 3) {\n keysWithFewValues++;\n }\n });\n return keysWithFewValues / Object.entries(typesCount.map ?? {}).length > 0.5;\n}\n\nfunction formatString(input: string): string {\n const normalized = input\n .replace(/[_\\-]+/g, \" \")\n .replace(/([a-z])([A-Z])/g, \"$1 $2\")\n .toLowerCase();\n\n // Split the normalized string into words\n const words = normalized.split(\" \");\n\n // Capitalize the first letter of each word and join them with a space\n const formatted = words\n .map((word) => word.charAt(0).toUpperCase() + word.slice(1))\n .join(\" \");\n\n return formatted;\n}\n\nexport function inferTypeFromValue(value: any): DataType {\n if (typeof value === \"string\") return \"string\";\n if (typeof value === \"number\") return \"number\";\n if (typeof value === \"boolean\") return \"boolean\";\n if (Array.isArray(value)) return \"array\";\n if (typeof value === \"object\") return \"map\";\n return \"string\";\n}\n"],"names":["key"],"mappings":";;AAGO,SAAS,8BAA8B,aAAgC;AAEtE,MAAA,CAAC,YAAoB,QAAA;AAEzB,WAAS,QAAQ,OAAY;AACrB,QAAA,OAAO,UAAU,SAAiB,QAAA;AAAA,aAC7B,iBAAiB,kBAAmB,QAAO,MAAM;AAAA,QAC9C,QAAA;AAAA,EAAA;AAGhB,QAAM,UAAoB,YAAY,OAAO,IAAI,CAAC,MAAM,QAAQ,CAAC,CAAC,EAAE,OAAO,CAAK,MAAA,CAAC,CAAC,CAAC;AAC7E,QAAA,gBAAgB,QAAQ,KAAK,CAAC,MAAM,EAAE,SAAS,GAAG,CAAC;AACzD,MAAI,CAAC;AACM,WAAA;AAEX,QAAM,eAAe,cAAc,OAAO,GAAG,cAAc,YAAY,GAAG,CAAC;AAE3E,QAAM,MAAM,YAAY,OACnB,OAAO,CAAC,UAAU;AACT,UAAA,OAAO,QAAQ,KAAK;AACtB,QAAA,CAAC,KAAa,QAAA;AACX,WAAA,KAAK,WAAW,YAAY;AAAA,EAAA,CACtC,EAAE,SAAS,YAAY,OAAO,SA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O,OAAO;AAAA,EAAA,CAC/D;AACD,SAAO,oBAAoB,UAAU;AACzC;AAEA,SAAS,+BAA+B,YAAwB;AACtD,QAAA,WAAW,uBAAuB,UAAU;AAClD,MAAI,oBAAoB;AACjB,SAAA,QAAQ,WAAW,OAAO,CAAA,CAAE,EAAE,QAAQ,CAAC,CAAC,KAAK,KAAK,MAAM;AACrD,UAAA,QAAQ,uBAAuB,KAAK;AACtC,QAAA,QAAQ,WAAW,GAAG;AACtB;AAAA,IAAA;AAAA,EACJ,CACH;AACM,SAAA,oBAAoB,OAAO,QAAQ,WAAW,OAAO,CAAE,CAAA,EAAE,SAAS;AAC7E;AAEA,SAAS,aAAa,OAAuB;AACnC,QAAA,aAAa,MACd,QAAQ,WAAW,GAAG,EACtB,QAAQ,mBAAmB,OAAO,EAClC,YAAY;AAGX,QAAA,QAAQ,WAAW,MAAM,GAAG;AAGlC,QAAM,YAAY,MACb,IAAI,CAAC,SAAS,KAAK,OAAO,CAAC,EAAE,YAAA,IAAgB,KAAK,MAAM,CAAC,CAAC,EAC1D,KAAK,GAAG;AAEN,SAAA;AACX;AAEO,SAAS,mBAAmB,OAAsB;AACjD,MAAA,OAAO,UAAU,SAAiB,QAAA;AAClC,MAAA,OAAO,UAAU,SAAiB,QAAA;AAClC,MAAA,OAAO,UAAU,UAAkB,QAAA;AACvC,MAAI,MAAM,QAAQ,KAAK,EAAU,QAAA;AAC7B,MAAA,OAAO,UAAU,SAAiB,QAAA;AAC/B,SAAA;AACX;"}
|
package/dist/index.umd.js
CHANGED
@@ -1,2 +1,434 @@
|
|
1
|
-
(function(
|
1
|
+
(function(global, factory) {
|
2
|
+
typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("@firecms/core"), require("@firebase/firestore")) : typeof define === "function" && define.amd ? define(["exports", "@firecms/core", "@firebase/firestore"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global["FireCMS schema inference"] = {}, global.core, global.firestore));
|
3
|
+
})(this, function(exports2, core, firestore) {
|
4
|
+
"use strict";
|
5
|
+
function findCommonInitialStringInPath(valuesCount) {
|
6
|
+
if (!valuesCount) return void 0;
|
7
|
+
function getPath(value) {
|
8
|
+
if (typeof value === "string") return value;
|
9
|
+
else if (value instanceof firestore.DocumentReference) return value.path;
|
10
|
+
else return void 0;
|
11
|
+
}
|
12
|
+
const strings = valuesCount.values.map((v) => getPath(v)).filter((v) => !!v);
|
13
|
+
const pathWithSlash = strings.find((s) => s.includes("/"));
|
14
|
+
if (!pathWithSlash)
|
15
|
+
return void 0;
|
16
|
+
const searchedPath = pathWithSlash.substr(0, pathWithSlash.lastIndexOf("/"));
|
17
|
+
const yep = valuesCount.values.filter((value) => {
|
18
|
+
const path = getPath(value);
|
19
|
+
if (!path) return false;
|
20
|
+
return path.startsWith(searchedPath);
|
21
|
+
}).length > valuesCount.values.length / 3 * 2;
|
22
|
+
return yep ? searchedPath : void 0;
|
23
|
+
}
|
24
|
+
function extractEnumFromValues(values) {
|
25
|
+
if (!Array.isArray(values)) {
|
26
|
+
return [];
|
27
|
+
}
|
28
|
+
const enumValues = values.map((value) => {
|
29
|
+
if (typeof value === "string") {
|
30
|
+
return { id: value, label: core.unslugify(value) };
|
31
|
+
} else
|
32
|
+
return null;
|
33
|
+
}).filter(Boolean);
|
34
|
+
enumValues.sort((a, b) => a.label.localeCompare(b.label));
|
35
|
+
return enumValues;
|
36
|
+
}
|
37
|
+
const IMAGE_EXTENSIONS = [".jpg", ".jpeg", ".png", ".webp", ".gif", ".avif"];
|
38
|
+
const AUDIO_EXTENSIONS = [".mp3", ".ogg", ".opus", ".aac"];
|
39
|
+
const VIDEO_EXTENSIONS = [".avi", ".mp4"];
|
40
|
+
const emailRegEx = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
41
|
+
function buildStringProperty({
|
42
|
+
totalDocsCount,
|
43
|
+
valuesResult
|
44
|
+
}) {
|
45
|
+
let stringProperty = {
|
46
|
+
dataType: "string"
|
47
|
+
};
|
48
|
+
if (valuesResult) {
|
49
|
+
const totalEntriesCount = valuesResult.values.length;
|
50
|
+
const totalValues = Array.from(valuesResult.valuesCount.keys()).length;
|
51
|
+
const config = {};
|
52
|
+
const probablyAURL = valuesResult.values.filter((value) => typeof value === "string" && value.toString().startsWith("http")).length > totalDocsCount / 3 * 2;
|
53
|
+
if (probablyAURL) {
|
54
|
+
config.url = true;
|
55
|
+
}
|
56
|
+
const probablyAnEmail = valuesResult.values.filter((value) => typeof value === "string" && emailRegEx.test(value)).length > totalDocsCount / 3 * 2;
|
57
|
+
if (probablyAnEmail) {
|
58
|
+
config.email = true;
|
59
|
+
}
|
60
|
+
const probablyUserIds = valuesResult.values.filter((value) => typeof value === "string" && value.length === 28 && !value.includes(" ")).length > totalDocsCount / 3 * 2;
|
61
|
+
if (probablyUserIds)
|
62
|
+
config.readOnly = true;
|
63
|
+
if (!probablyAnEmail && !probablyAURL && !probablyUserIds && !probablyAURL && totalValues < totalEntriesCount / 3) {
|
64
|
+
const enumValues = extractEnumFromValues(Array.from(valuesResult.valuesCount.keys()));
|
65
|
+
if (Object.keys(enumValues).length > 1)
|
66
|
+
config.enumValues = enumValues;
|
67
|
+
}
|
68
|
+
if (!probablyAnEmail && !probablyAURL && !probablyUserIds && !probablyAURL && !config.enumValues) {
|
69
|
+
const fileType = probableFileType(valuesResult, totalDocsCount);
|
70
|
+
if (fileType) {
|
71
|
+
config.storage = {
|
72
|
+
acceptedFiles: [fileType],
|
73
|
+
storagePath: findCommonInitialStringInPath(valuesResult) ?? "/"
|
74
|
+
};
|
75
|
+
}
|
76
|
+
}
|
77
|
+
if (Object.keys(config).length > 0)
|
78
|
+
stringProperty = {
|
79
|
+
...stringProperty,
|
80
|
+
...config,
|
81
|
+
editable: true
|
82
|
+
};
|
83
|
+
}
|
84
|
+
return stringProperty;
|
85
|
+
}
|
86
|
+
function probableFileType(valuesCount, totalDocsCount) {
|
87
|
+
const probablyAnImage = valuesCount.values.filter((value) => typeof value === "string" && IMAGE_EXTENSIONS.some((extension) => value.toString().endsWith(extension))).length > totalDocsCount / 3 * 2;
|
88
|
+
const probablyAudio = valuesCount.values.filter((value) => typeof value === "string" && AUDIO_EXTENSIONS.some((extension) => value.toString().endsWith(extension))).length > totalDocsCount / 3 * 2;
|
89
|
+
const probablyVideo = valuesCount.values.filter((value) => typeof value === "string" && VIDEO_EXTENSIONS.some((extension) => value.toString().endsWith(extension))).length > totalDocsCount / 3 * 2;
|
90
|
+
const fileType = probablyAnImage ? "image/*" : probablyAudio ? "audio/*" : probablyVideo ? "video/*" : false;
|
91
|
+
return fileType;
|
92
|
+
}
|
93
|
+
function buildValidation({
|
94
|
+
totalDocsCount,
|
95
|
+
valuesResult
|
96
|
+
}) {
|
97
|
+
if (valuesResult) {
|
98
|
+
const totalEntriesCount = valuesResult.values.length;
|
99
|
+
if (totalDocsCount === totalEntriesCount)
|
100
|
+
return {
|
101
|
+
required: true
|
102
|
+
};
|
103
|
+
}
|
104
|
+
return void 0;
|
105
|
+
}
|
106
|
+
function buildReferenceProperty({
|
107
|
+
totalDocsCount,
|
108
|
+
valuesResult
|
109
|
+
}) {
|
110
|
+
const property = {
|
111
|
+
dataType: "reference",
|
112
|
+
path: findCommonInitialStringInPath(valuesResult) ?? "!!!FIX_ME!!!",
|
113
|
+
editable: true
|
114
|
+
};
|
115
|
+
return property;
|
116
|
+
}
|
117
|
+
async function buildEntityPropertiesFromData(data, getType) {
|
118
|
+
const typesCount = {};
|
119
|
+
const valuesCount = {};
|
120
|
+
if (data) {
|
121
|
+
data.forEach((entry) => {
|
122
|
+
if (entry) {
|
123
|
+
Object.entries(entry).forEach(([key, value]) => {
|
124
|
+
increaseMapTypeCount(typesCount, key, value, getType);
|
125
|
+
increaseValuesCount(valuesCount, key, value, getType);
|
126
|
+
});
|
127
|
+
}
|
128
|
+
});
|
129
|
+
}
|
130
|
+
return buildPropertiesFromCount(data.length, typesCount, valuesCount);
|
131
|
+
}
|
132
|
+
function buildPropertyFromData(data, property, getType) {
|
133
|
+
const typesCount = {};
|
134
|
+
const valuesCount = {};
|
135
|
+
if (data) {
|
136
|
+
data.forEach((entry) => {
|
137
|
+
increaseTypeCount(property.dataType, typesCount, entry, getType);
|
138
|
+
increaseValuesCount(valuesCount, "inferred_prop", entry, getType);
|
139
|
+
});
|
140
|
+
}
|
141
|
+
const enumValues = "enumValues" in property ? core.resolveEnumValues(property["enumValues"]) : void 0;
|
142
|
+
if (enumValues) {
|
143
|
+
const newEnumValues = extractEnumFromValues(Array.from(valuesCount["inferred_prop"].valuesCount.keys()));
|
144
|
+
return {
|
145
|
+
...property,
|
146
|
+
enumValues: [...newEnumValues, ...enumValues]
|
147
|
+
};
|
148
|
+
}
|
149
|
+
const generatedProperty = buildPropertyFromCount(
|
150
|
+
"inferred_prop",
|
151
|
+
data.length,
|
152
|
+
property.dataType,
|
153
|
+
typesCount,
|
154
|
+
valuesCount["inferred_prop"]
|
155
|
+
);
|
156
|
+
return core.mergeDeep(generatedProperty, property);
|
157
|
+
}
|
158
|
+
function buildPropertiesOrder(properties, propertiesOrder, priorityKeys) {
|
159
|
+
const lowerCasePriorityKeys = (priorityKeys ?? []).map((key) => key.toLowerCase());
|
160
|
+
function propOrder(s) {
|
161
|
+
const k = s.toLowerCase();
|
162
|
+
if (lowerCasePriorityKeys.includes(k)) return 4;
|
163
|
+
if (k === "title" || k === "name") return 3;
|
164
|
+
if (k.includes("title") || k.includes("name")) return 2;
|
165
|
+
if (k.includes("image") || k.includes("picture")) return 1;
|
166
|
+
return 0;
|
167
|
+
}
|
168
|
+
const keys = propertiesOrder ?? Object.keys(properties);
|
169
|
+
keys.sort();
|
170
|
+
keys.sort((a, b) => {
|
171
|
+
return propOrder(b) - propOrder(a);
|
172
|
+
});
|
173
|
+
return keys;
|
174
|
+
}
|
175
|
+
function increaseTypeCount(type, typesCount, fieldValue, getType) {
|
176
|
+
if (type === "map") {
|
177
|
+
if (fieldValue) {
|
178
|
+
let mapTypesCount = typesCount[type];
|
179
|
+
if (!mapTypesCount) {
|
180
|
+
mapTypesCount = {};
|
181
|
+
typesCount[type] = mapTypesCount;
|
182
|
+
}
|
183
|
+
Object.entries(fieldValue).forEach(([key, value]) => {
|
184
|
+
increaseMapTypeCount(mapTypesCount, key, value, getType);
|
185
|
+
});
|
186
|
+
}
|
187
|
+
} else if (type === "array") {
|
188
|
+
let arrayTypesCount = typesCount[type];
|
189
|
+
if (!arrayTypesCount) {
|
190
|
+
arrayTypesCount = {};
|
191
|
+
typesCount[type] = arrayTypesCount;
|
192
|
+
}
|
193
|
+
if (fieldValue && Array.isArray(fieldValue) && fieldValue.length > 0) {
|
194
|
+
const arrayType = getMostProbableTypeInArray(fieldValue, getType);
|
195
|
+
if (arrayType === "map") {
|
196
|
+
let mapTypesCount = arrayTypesCount[arrayType];
|
197
|
+
if (!mapTypesCount) {
|
198
|
+
mapTypesCount = {};
|
199
|
+
}
|
200
|
+
fieldValue.forEach((value) => {
|
201
|
+
Object.entries(value).forEach(
|
202
|
+
([key, v]) => increaseMapTypeCount(mapTypesCount, key, v, getType)
|
203
|
+
);
|
204
|
+
});
|
205
|
+
arrayTypesCount[arrayType] = mapTypesCount;
|
206
|
+
} else {
|
207
|
+
if (!arrayTypesCount[arrayType]) arrayTypesCount[arrayType] = 1;
|
208
|
+
else arrayTypesCount[arrayType]++;
|
209
|
+
}
|
210
|
+
}
|
211
|
+
} else {
|
212
|
+
if (!typesCount[type]) typesCount[type] = 1;
|
213
|
+
else typesCount[type]++;
|
214
|
+
}
|
215
|
+
}
|
216
|
+
function increaseMapTypeCount(typesCountRecord, key, fieldValue, getType) {
|
217
|
+
let typesCount = typesCountRecord[key];
|
218
|
+
if (!typesCount) {
|
219
|
+
typesCount = {};
|
220
|
+
typesCountRecord[key] = typesCount;
|
221
|
+
}
|
222
|
+
if (fieldValue != null) {
|
223
|
+
const type = getType(fieldValue);
|
224
|
+
increaseTypeCount(type, typesCount, fieldValue, getType);
|
225
|
+
}
|
226
|
+
}
|
227
|
+
function increaseValuesCount(typeValuesRecord, key, fieldValue, getType) {
|
228
|
+
const dataType = getType(fieldValue);
|
229
|
+
let valuesRecord = typeValuesRecord[key];
|
230
|
+
if (!valuesRecord) {
|
231
|
+
valuesRecord = {
|
232
|
+
values: [],
|
233
|
+
valuesCount: /* @__PURE__ */ new Map()
|
234
|
+
};
|
235
|
+
typeValuesRecord[key] = valuesRecord;
|
236
|
+
}
|
237
|
+
if (dataType === "map") {
|
238
|
+
let mapValuesRecord = valuesRecord.map;
|
239
|
+
if (!mapValuesRecord) {
|
240
|
+
mapValuesRecord = {};
|
241
|
+
valuesRecord.map = mapValuesRecord;
|
242
|
+
}
|
243
|
+
if (fieldValue)
|
244
|
+
Object.entries(fieldValue).forEach(
|
245
|
+
([key2, value]) => increaseValuesCount(mapValuesRecord, key2, value, getType)
|
246
|
+
);
|
247
|
+
} else if (dataType === "array") {
|
248
|
+
if (Array.isArray(fieldValue)) {
|
249
|
+
fieldValue.forEach((value) => {
|
250
|
+
valuesRecord.values.push(value);
|
251
|
+
valuesRecord.valuesCount.set(value, (valuesRecord.valuesCount.get(value) ?? 0) + 1);
|
252
|
+
});
|
253
|
+
}
|
254
|
+
} else {
|
255
|
+
if (fieldValue) {
|
256
|
+
valuesRecord.values.push(fieldValue);
|
257
|
+
valuesRecord.valuesCount.set(fieldValue, (valuesRecord.valuesCount.get(fieldValue) ?? 0) + 1);
|
258
|
+
}
|
259
|
+
}
|
260
|
+
}
|
261
|
+
function getHighestTypesCount(typesCount) {
|
262
|
+
let highestCount = 0;
|
263
|
+
Object.entries(typesCount).forEach(([type, count]) => {
|
264
|
+
let countValue = 0;
|
265
|
+
if (type === "map") {
|
266
|
+
countValue = getHighestRecordCount(count);
|
267
|
+
} else if (type === "array") {
|
268
|
+
countValue = getHighestTypesCount(count);
|
269
|
+
} else {
|
270
|
+
countValue = count;
|
271
|
+
}
|
272
|
+
if (countValue > highestCount) {
|
273
|
+
highestCount = countValue;
|
274
|
+
}
|
275
|
+
});
|
276
|
+
return highestCount;
|
277
|
+
}
|
278
|
+
function getHighestRecordCount(record) {
|
279
|
+
return Object.entries(record).map(([key, typesCount]) => getHighestTypesCount(typesCount)).reduce((a, b) => Math.max(a, b), 0);
|
280
|
+
}
|
281
|
+
function getMostProbableType(typesCount) {
|
282
|
+
let highestCount = -1;
|
283
|
+
let probableType = "string";
|
284
|
+
Object.entries(typesCount).forEach(([type, count]) => {
|
285
|
+
let countValue;
|
286
|
+
if (type === "map") {
|
287
|
+
countValue = getHighestRecordCount(count);
|
288
|
+
} else if (type === "array") {
|
289
|
+
countValue = getHighestTypesCount(count);
|
290
|
+
} else {
|
291
|
+
countValue = count;
|
292
|
+
}
|
293
|
+
if (countValue > highestCount) {
|
294
|
+
highestCount = countValue;
|
295
|
+
probableType = type;
|
296
|
+
}
|
297
|
+
});
|
298
|
+
return probableType;
|
299
|
+
}
|
300
|
+
function buildPropertyFromCount(key, totalDocsCount, mostProbableType, typesCount, valuesResult) {
|
301
|
+
let title;
|
302
|
+
if (key) {
|
303
|
+
title = formatString(key.toLowerCase());
|
304
|
+
}
|
305
|
+
let result = void 0;
|
306
|
+
if (mostProbableType === "map") {
|
307
|
+
const highVariability = checkTypesCountHighVariability(typesCount);
|
308
|
+
if (highVariability) {
|
309
|
+
result = {
|
310
|
+
dataType: "map",
|
311
|
+
name: title,
|
312
|
+
keyValue: true,
|
313
|
+
properties: {}
|
314
|
+
};
|
315
|
+
}
|
316
|
+
const properties = buildPropertiesFromCount(
|
317
|
+
totalDocsCount,
|
318
|
+
typesCount.map,
|
319
|
+
valuesResult ? valuesResult.mapValues : void 0
|
320
|
+
);
|
321
|
+
result = {
|
322
|
+
dataType: "map",
|
323
|
+
name: title,
|
324
|
+
properties
|
325
|
+
};
|
326
|
+
} else if (mostProbableType === "array") {
|
327
|
+
const arrayTypesCount = typesCount.array;
|
328
|
+
const arrayMostProbableType = getMostProbableType(arrayTypesCount);
|
329
|
+
const of = buildPropertyFromCount(
|
330
|
+
key,
|
331
|
+
totalDocsCount,
|
332
|
+
arrayMostProbableType,
|
333
|
+
arrayTypesCount,
|
334
|
+
valuesResult
|
335
|
+
);
|
336
|
+
result = {
|
337
|
+
dataType: "array",
|
338
|
+
name: title,
|
339
|
+
of
|
340
|
+
};
|
341
|
+
}
|
342
|
+
if (!result) {
|
343
|
+
const propertyProps = {
|
344
|
+
name: key,
|
345
|
+
totalDocsCount,
|
346
|
+
valuesResult
|
347
|
+
};
|
348
|
+
if (mostProbableType === "string") {
|
349
|
+
result = buildStringProperty(propertyProps);
|
350
|
+
} else if (mostProbableType === "reference") {
|
351
|
+
result = buildReferenceProperty(propertyProps);
|
352
|
+
} else {
|
353
|
+
result = {
|
354
|
+
dataType: mostProbableType
|
355
|
+
};
|
356
|
+
}
|
357
|
+
if (title) {
|
358
|
+
result.name = title;
|
359
|
+
}
|
360
|
+
const validation = buildValidation(propertyProps);
|
361
|
+
if (validation) {
|
362
|
+
result.validation = validation;
|
363
|
+
}
|
364
|
+
}
|
365
|
+
return {
|
366
|
+
...result,
|
367
|
+
editable: true
|
368
|
+
};
|
369
|
+
}
|
370
|
+
function buildPropertiesFromCount(totalDocsCount, typesCountRecord, valuesCountRecord) {
|
371
|
+
const res = {};
|
372
|
+
Object.entries(typesCountRecord).forEach(([key, typesCount]) => {
|
373
|
+
const mostProbableType = getMostProbableType(typesCount);
|
374
|
+
res[key] = buildPropertyFromCount(
|
375
|
+
key,
|
376
|
+
totalDocsCount,
|
377
|
+
mostProbableType,
|
378
|
+
typesCount,
|
379
|
+
valuesCountRecord ? valuesCountRecord[key] : void 0
|
380
|
+
);
|
381
|
+
});
|
382
|
+
return res;
|
383
|
+
}
|
384
|
+
function countMaxDocumentsUnder(typesCount) {
|
385
|
+
let count = 0;
|
386
|
+
Object.entries(typesCount).forEach(([type, value]) => {
|
387
|
+
if (typeof value === "object") {
|
388
|
+
count = Math.max(count, countMaxDocumentsUnder(value));
|
389
|
+
} else {
|
390
|
+
count = Math.max(count, value);
|
391
|
+
}
|
392
|
+
});
|
393
|
+
return count;
|
394
|
+
}
|
395
|
+
function getMostProbableTypeInArray(array, getType) {
|
396
|
+
let typesCount = {};
|
397
|
+
array.forEach((value) => {
|
398
|
+
increaseTypeCount(getType(value), typesCount, value, getType);
|
399
|
+
});
|
400
|
+
return getMostProbableType(typesCount);
|
401
|
+
}
|
402
|
+
function checkTypesCountHighVariability(typesCount) {
|
403
|
+
const maxCount = countMaxDocumentsUnder(typesCount);
|
404
|
+
let keysWithFewValues = 0;
|
405
|
+
Object.entries(typesCount.map ?? {}).forEach(([key, value]) => {
|
406
|
+
const count = countMaxDocumentsUnder(value);
|
407
|
+
if (count < maxCount / 3) {
|
408
|
+
keysWithFewValues++;
|
409
|
+
}
|
410
|
+
});
|
411
|
+
return keysWithFewValues / Object.entries(typesCount.map ?? {}).length > 0.5;
|
412
|
+
}
|
413
|
+
function formatString(input) {
|
414
|
+
const normalized = input.replace(/[_\-]+/g, " ").replace(/([a-z])([A-Z])/g, "$1 $2").toLowerCase();
|
415
|
+
const words = normalized.split(" ");
|
416
|
+
const formatted = words.map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
|
417
|
+
return formatted;
|
418
|
+
}
|
419
|
+
function inferTypeFromValue(value) {
|
420
|
+
if (typeof value === "string") return "string";
|
421
|
+
if (typeof value === "number") return "number";
|
422
|
+
if (typeof value === "boolean") return "boolean";
|
423
|
+
if (Array.isArray(value)) return "array";
|
424
|
+
if (typeof value === "object") return "map";
|
425
|
+
return "string";
|
426
|
+
}
|
427
|
+
exports2.buildEntityPropertiesFromData = buildEntityPropertiesFromData;
|
428
|
+
exports2.buildPropertiesOrder = buildPropertiesOrder;
|
429
|
+
exports2.buildPropertyFromData = buildPropertyFromData;
|
430
|
+
exports2.extractEnumFromValues = extractEnumFromValues;
|
431
|
+
exports2.inferTypeFromValue = inferTypeFromValue;
|
432
|
+
Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
|
433
|
+
});
|
2
434
|
//# sourceMappingURL=index.umd.js.map
|