@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.umd.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.umd.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":"sWAGO,SAASA,EAA8BC,EAAgC,CAE1E,GAAI,CAACA,EAAoB,OAEzB,SAASC,EAAQC,EAAY,CACzB,OAAI,OAAOA,GAAU,SAAiBA,EAC7BA,aAAiBC,EAAA,kBAA0BD,EAAM,KAC9C,MAChB,CAGM,MAAAE,EADoBJ,EAAY,OAAO,IAAKK,GAAMJ,EAAQI,CAAC,CAAC,EAAE,OAAYA,GAAA,CAAC,CAACA,CAAC,EACrD,KAAMC,GAAMA,EAAE,SAAS,GAAG,CAAC,EACzD,GAAI,CAACF,EACM,OAEX,MAAMG,EAAeH,EAAc,OAAO,EAAGA,EAAc,YAAY,GAAG,CAAC,EAS3E,OAPYJ,EAAY,OACnB,OAAQE,GAAU,CACT,MAAAM,EAAOP,EAAQC,CAAK,EAC1B,OAAKM,EACEA,EAAK,WAAWD,CAAY,EADjB,EACiB,CACtC,EAAE,OAASP,EAAY,OAAO,OAAS,EAAI,EAEnCO,EAAe,MAEhC,CC3BO,SAASE,EAAsBC,EAAmB,CACrD,GAAI,CAAC,MAAM,QAAQA,CAAM,EACrB,MAAO,GAEX,MAAMC,EAAaD,EACd,IAAKR,GACE,OAAOA,GAAU,SACT,CAAE,GAAIA,EAAO,MAAOU,EAAA,UAAUV,CAAK,GAEpC,IACd,EAAE,OAAO,O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EACjB,cAAA,QAAQtB,CAAgB,EAAE,QAAQ,CAAC,CAACvB,EAAKF,CAAU,IAAM,CACtD,MAAAsC,EAAmBF,EAAoBpC,CAAU,EACnD+C,EAAA7C,CAAG,EAAIU,EAAuBV,EAAKtB,EAAgB0D,EAAkBtC,EAAY8C,EAAoBA,EAAkB5C,CAAG,EAAI,MAAS,CAAA,CAC9I,EACM6C,CACX,CAEA,SAASC,EAAuBhD,EAAwB,CACpD,IAAIgC,EAAQ,EACL,cAAA,QAAQhC,CAAU,EAAE,QAAQ,CAAC,CAACmB,EAAMzD,CAAK,IAAM,CAE9C,OAAOA,GAAU,SACjBsE,EAAQ,KAAK,IAAIA,EAAOgB,EAAuBtF,CAAyB,CAAC,EAEjEsE,EAAA,KAAK,IAAIA,EAAOtE,CAAe,CAC3C,CACH,EACMsE,CACX,CAEA,SAASR,EAA2ByB,EAAclD,EAAyC,CACvF,IAAIC,EAAyB,CAAA,EACvB,OAAAiD,EAAA,QAASvF,GAAU,CACrB8C,EAAkBT,EAAQrC,CAAK,EAAGsC,EAAYtC,EAAOqC,CAAO,CAAA,CAC/D,EACMqC,EAAoBpC,CAAU,CACzC,CAEA,SAASyC,EAA+BzC,EAAwB,CACtD,MAAAkD,EAAWF,EAAuBhD,CAAU,EAClD,IAAImD,EAAoB,EACjB,cAAA,QAAQnD,EAAW,KAAO,CAAA,CAAE,EAC9B,QAAQ,CAAC,CAACE,EAAKxC,CAAK,IAAM,CACTsF,EAAuBtF,CAAK,EAC9BwF,EAAW,GACnBC,GACJ,CACH,EACEA,EAAoB,OAAO,QAAQnD,EAAW,KAAO,CAAE,CAAA,EAAE,OAAS,EAC7E"}
|
1
|
+
{"version":3,"file":"index.umd.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":["DocumentReference","unslugify","resolveEnumValues","mergeDeep","key"],"mappings":";;;;AAGO,WAAS,8BAA8B,aAAgC;AAEtE,QAAA,CAAC,YAAoB,QAAA;AAEzB,aAAS,QAAQ,OAAY;AACrB,UAAA,OAA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;AAGX,UAAA,QAAQ,WAAW,MAAM,GAAG;AAGlC,UAAM,YAAY,MACb,IAAI,CAAC,SAAS,KAAK,OAAO,CAAC,EAAE,YAAA,IAAgB,KAAK,MAAM,CAAC,CAAC,EAC1D,KAAK,GAAG;AAEN,WAAA;AAAA,EACX;AAEO,WAAS,mBAAmB,OAAsB;AACjD,QAAA,OAAO,UAAU,SAAiB,QAAA;AAClC,QAAA,OAAO,UAAU,SAAiB,QAAA;AAClC,QAAA,OAAO,UAAU,UAAkB,QAAA;AACvC,QAAI,MAAM,QAAQ,KAAK,EAAU,QAAA;AAC7B,QAAA,OAAO,UAAU,SAAiB,QAAA;AAC/B,WAAA;AAAA,EACX;;;;;;;;"}
|
package/package.json
CHANGED
@@ -1,18 +1,17 @@
|
|
1
1
|
{
|
2
|
-
"version": "3.0.0-canary.
|
2
|
+
"version": "3.0.0-canary.191",
|
3
3
|
"type": "module",
|
4
4
|
"name": "@firecms/schema_inference",
|
5
5
|
"access": "public",
|
6
|
-
"packageManager": "yarn@4.1.0",
|
7
6
|
"main": "./dist/index.umd.js",
|
8
7
|
"module": "./dist/index.es.js",
|
9
8
|
"types": "dist/index.d.ts",
|
10
9
|
"source": "src/index.ts",
|
11
10
|
"devDependencies": {
|
12
|
-
"@firecms/core": "^3.0.0-canary.
|
13
|
-
"@types/node": "^20.
|
14
|
-
"typescript": "^5.
|
15
|
-
"vite": "^5.
|
11
|
+
"@firecms/core": "^3.0.0-canary.191",
|
12
|
+
"@types/node": "^20.17.14",
|
13
|
+
"typescript": "^5.7.3",
|
14
|
+
"vite": "^5.4.14"
|
16
15
|
},
|
17
16
|
"exports": {
|
18
17
|
".": {
|
@@ -31,5 +30,5 @@
|
|
31
30
|
"build": "vite build && tsc --emitDeclarationOnly -p tsconfig.prod.json",
|
32
31
|
"clean": "rm -rf dist && find ./src -name '*.js' -type f | xargs rm -f"
|
33
32
|
},
|
34
|
-
"gitHead": "
|
33
|
+
"gitHead": "eec920b0db61ede54c9845a4e245a1f9b91d6298"
|
35
34
|
}
|
@@ -3,13 +3,12 @@ import { InferencePropertyBuilderProps, ValuesCountEntry } from "../types";
|
|
3
3
|
import { findCommonInitialStringInPath } from "../strings";
|
4
4
|
import { extractEnumFromValues } from "../util";
|
5
5
|
|
6
|
-
const IMAGE_EXTENSIONS = [".jpg", ".png", ".webp", ".gif"];
|
6
|
+
const IMAGE_EXTENSIONS = [".jpg", ".jpeg", ".png", ".webp", ".gif", ".avif"];
|
7
7
|
const AUDIO_EXTENSIONS = [".mp3", ".ogg", ".opus", ".aac"];
|
8
8
|
const VIDEO_EXTENSIONS = [".avi", ".mp4"];
|
9
9
|
|
10
10
|
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])?)*$/;
|
11
11
|
|
12
|
-
|
13
12
|
export function buildStringProperty({
|
14
13
|
totalDocsCount,
|
15
14
|
valuesResult
|
@@ -2,11 +2,10 @@ import {
|
|
2
2
|
DataType,
|
3
3
|
EnumValues,
|
4
4
|
mergeDeep,
|
5
|
-
Properties,
|
5
|
+
Properties, PropertiesOrBuilders,
|
6
6
|
Property,
|
7
7
|
resolveEnumValues,
|
8
|
-
StringProperty
|
9
|
-
unslugify
|
8
|
+
StringProperty
|
10
9
|
} from "@firecms/core";
|
11
10
|
import {
|
12
11
|
InferencePropertyBuilderProps,
|
@@ -22,7 +21,10 @@ import { extractEnumFromValues } from "./util";
|
|
22
21
|
|
23
22
|
export type InferenceTypeBuilder = (value: any) => DataType;
|
24
23
|
|
25
|
-
export async function buildEntityPropertiesFromData(
|
24
|
+
export async function buildEntityPropertiesFromData(
|
25
|
+
data: object[],
|
26
|
+
getType: InferenceTypeBuilder
|
27
|
+
): Promise<Properties> {
|
26
28
|
const typesCount: TypesCountRecord = {};
|
27
29
|
const valuesCount: ValuesCountRecord = {};
|
28
30
|
if (data) {
|
@@ -31,15 +33,18 @@ export async function buildEntityPropertiesFromData(data: object[], getType: Inf
|
|
31
33
|
Object.entries(entry).forEach(([key, value]) => {
|
32
34
|
increaseMapTypeCount(typesCount, key, value, getType);
|
33
35
|
increaseValuesCount(valuesCount, key, value, getType);
|
34
|
-
})
|
36
|
+
});
|
35
37
|
}
|
36
38
|
});
|
37
39
|
}
|
38
|
-
// console.log(util.inspect({ typesCount }, { showHidden: false, depth: null, colors: true }));
|
39
40
|
return buildPropertiesFromCount(data.length, typesCount, valuesCount);
|
40
41
|
}
|
41
42
|
|
42
|
-
export function buildPropertyFromData(
|
43
|
+
export function buildPropertyFromData(
|
44
|
+
data: any[],
|
45
|
+
property: Property,
|
46
|
+
getType: InferenceTypeBuilder
|
47
|
+
): Property {
|
43
48
|
const typesCount = {};
|
44
49
|
const valuesCount: ValuesCountRecord = {};
|
45
50
|
if (data) {
|
@@ -56,20 +61,33 @@ export function buildPropertyFromData(data: any[], property: Property, getType:
|
|
56
61
|
enumValues: [...newEnumValues, ...enumValues]
|
57
62
|
} as StringProperty;
|
58
63
|
}
|
59
|
-
const generatedProperty = buildPropertyFromCount(
|
64
|
+
const generatedProperty = buildPropertyFromCount(
|
65
|
+
"inferred_prop",
|
66
|
+
data.length,
|
67
|
+
property.dataType,
|
68
|
+
typesCount,
|
69
|
+
valuesCount["inferred_prop"]
|
70
|
+
);
|
60
71
|
return mergeDeep(generatedProperty, property);
|
61
72
|
}
|
62
73
|
|
63
|
-
export function buildPropertiesOrder(
|
74
|
+
export function buildPropertiesOrder(
|
75
|
+
properties: PropertiesOrBuilders,
|
76
|
+
propertiesOrder?: string[],
|
77
|
+
priorityKeys?: string[]
|
78
|
+
): string[] {
|
79
|
+
const lowerCasePriorityKeys = (priorityKeys ?? []).map((key) => key.toLowerCase());
|
80
|
+
|
64
81
|
function propOrder(s: string) {
|
65
82
|
const k = s.toLowerCase();
|
83
|
+
if (lowerCasePriorityKeys.includes(k)) return 4;
|
66
84
|
if (k === "title" || k === "name") return 3;
|
67
85
|
if (k.includes("title") || k.includes("name")) return 2;
|
68
86
|
if (k.includes("image") || k.includes("picture")) return 1;
|
69
87
|
return 0;
|
70
88
|
}
|
71
89
|
|
72
|
-
const keys = Object.keys(properties);
|
90
|
+
const keys = propertiesOrder ?? Object.keys(properties);
|
73
91
|
keys.sort(); // alphabetically
|
74
92
|
keys.sort((a, b) => {
|
75
93
|
return propOrder(b) - propOrder(a);
|
@@ -83,7 +101,12 @@ export function buildPropertiesOrder(properties: Properties<any>): string [] {
|
|
83
101
|
* @param fieldValue
|
84
102
|
* @param getType
|
85
103
|
*/
|
86
|
-
function increaseTypeCount(
|
104
|
+
function increaseTypeCount(
|
105
|
+
type: DataType,
|
106
|
+
typesCount: TypesCount,
|
107
|
+
fieldValue: any,
|
108
|
+
getType: InferenceTypeBuilder
|
109
|
+
) {
|
87
110
|
if (type === "map") {
|
88
111
|
if (fieldValue) {
|
89
112
|
let mapTypesCount = typesCount[type];
|
@@ -93,7 +116,7 @@ function increaseTypeCount(type: DataType, typesCount: TypesCount, fieldValue: a
|
|
93
116
|
}
|
94
117
|
Object.entries(fieldValue).forEach(([key, value]) => {
|
95
118
|
increaseMapTypeCount(mapTypesCount as TypesCountRecord, key, value, getType);
|
96
|
-
})
|
119
|
+
});
|
97
120
|
}
|
98
121
|
} else if (type === "array") {
|
99
122
|
let arrayTypesCount = typesCount[type];
|
@@ -102,9 +125,22 @@ function increaseTypeCount(type: DataType, typesCount: TypesCount, fieldValue: a
|
|
102
125
|
typesCount[type] = arrayTypesCount;
|
103
126
|
}
|
104
127
|
if (fieldValue && Array.isArray(fieldValue) && fieldValue.length > 0) {
|
105
|
-
const arrayType = getMostProbableTypeInArray(fieldValue, getType);
|
106
|
-
if (
|
107
|
-
|
128
|
+
const arrayType = getMostProbableTypeInArray(fieldValue, getType);
|
129
|
+
if (arrayType === "map") {
|
130
|
+
let mapTypesCount = arrayTypesCount[arrayType];
|
131
|
+
if (!mapTypesCount) {
|
132
|
+
mapTypesCount = {};
|
133
|
+
}
|
134
|
+
fieldValue.forEach((value) => {
|
135
|
+
Object.entries(value).forEach(([key, v]) =>
|
136
|
+
increaseMapTypeCount(mapTypesCount, key, v, getType)
|
137
|
+
);
|
138
|
+
});
|
139
|
+
arrayTypesCount[arrayType] = mapTypesCount;
|
140
|
+
} else {
|
141
|
+
if (!arrayTypesCount[arrayType]) arrayTypesCount[arrayType] = 1;
|
142
|
+
else (arrayTypesCount[arrayType] as number)++;
|
143
|
+
}
|
108
144
|
}
|
109
145
|
} else {
|
110
146
|
if (!typesCount[type]) typesCount[type] = 1;
|
@@ -124,7 +160,8 @@ function increaseMapTypeCount(
|
|
124
160
|
typesCountRecord[key] = typesCount;
|
125
161
|
}
|
126
162
|
|
127
|
-
if (fieldValue != null) {
|
163
|
+
if (fieldValue != null) {
|
164
|
+
// Check that fieldValue is not null or undefined before proceeding
|
128
165
|
const type = getType(fieldValue);
|
129
166
|
increaseTypeCount(type, typesCount, fieldValue, getType);
|
130
167
|
}
|
@@ -136,7 +173,6 @@ function increaseValuesCount(
|
|
136
173
|
fieldValue: any,
|
137
174
|
getType: InferenceTypeBuilder
|
138
175
|
) {
|
139
|
-
|
140
176
|
const dataType = getType(fieldValue);
|
141
177
|
|
142
178
|
let valuesRecord: {
|
@@ -160,13 +196,15 @@ function increaseValuesCount(
|
|
160
196
|
valuesRecord.map = mapValuesRecord;
|
161
197
|
}
|
162
198
|
if (fieldValue)
|
163
|
-
Object.entries(fieldValue).forEach(([key, value]) =>
|
199
|
+
Object.entries(fieldValue).forEach(([key, value]) =>
|
200
|
+
increaseValuesCount(mapValuesRecord as ValuesCountRecord, key, value, getType)
|
201
|
+
);
|
164
202
|
} else if (dataType === "array") {
|
165
203
|
if (Array.isArray(fieldValue)) {
|
166
204
|
fieldValue.forEach((value) => {
|
167
205
|
valuesRecord.values.push(value);
|
168
206
|
valuesRecord.valuesCount.set(value, (valuesRecord.valuesCount.get(value) ?? 0) + 1);
|
169
|
-
})
|
207
|
+
});
|
170
208
|
}
|
171
209
|
} else {
|
172
210
|
if (fieldValue) {
|
@@ -174,7 +212,6 @@ function increaseValuesCount(
|
|
174
212
|
valuesRecord.valuesCount.set(fieldValue, (valuesRecord.valuesCount.get(fieldValue) ?? 0) + 1);
|
175
213
|
}
|
176
214
|
}
|
177
|
-
|
178
215
|
}
|
179
216
|
|
180
217
|
function getHighestTypesCount(typesCount: TypesCount): number {
|
@@ -204,7 +241,7 @@ function getHighestRecordCount(record: TypesCountRecord): number {
|
|
204
241
|
|
205
242
|
function getMostProbableType(typesCount: TypesCount): DataType {
|
206
243
|
let highestCount = -1;
|
207
|
-
let probableType: DataType = "string"; //default
|
244
|
+
let probableType: DataType = "string"; // default
|
208
245
|
Object.entries(typesCount).forEach(([type, count]) => {
|
209
246
|
let countValue;
|
210
247
|
if (type === "map") {
|
@@ -222,16 +259,21 @@ function getMostProbableType(typesCount: TypesCount): DataType {
|
|
222
259
|
return probableType;
|
223
260
|
}
|
224
261
|
|
225
|
-
function buildPropertyFromCount(
|
262
|
+
function buildPropertyFromCount(
|
263
|
+
key: string,
|
264
|
+
totalDocsCount: number,
|
265
|
+
mostProbableType: DataType,
|
266
|
+
typesCount: TypesCount,
|
267
|
+
valuesResult?: ValuesCountEntry
|
268
|
+
): Property {
|
226
269
|
let title: string | undefined;
|
227
270
|
|
228
271
|
if (key) {
|
229
|
-
title =
|
272
|
+
title = formatString(key.toLowerCase());
|
230
273
|
}
|
231
274
|
|
232
275
|
let result: Property | undefined = undefined;
|
233
276
|
if (mostProbableType === "map") {
|
234
|
-
|
235
277
|
const highVariability = checkTypesCountHighVariability(typesCount);
|
236
278
|
if (highVariability) {
|
237
279
|
result = {
|
@@ -241,7 +283,11 @@ function buildPropertyFromCount(key: string, totalDocsCount: number, mostProbabl
|
|
241
283
|
properties: {}
|
242
284
|
};
|
243
285
|
}
|
244
|
-
const properties = buildPropertiesFromCount(
|
286
|
+
const properties = buildPropertiesFromCount(
|
287
|
+
totalDocsCount,
|
288
|
+
typesCount.map as TypesCountRecord,
|
289
|
+
valuesResult ? valuesResult.mapValues : undefined
|
290
|
+
);
|
245
291
|
result = {
|
246
292
|
dataType: "map",
|
247
293
|
name: title,
|
@@ -250,13 +296,20 @@ function buildPropertyFromCount(key: string, totalDocsCount: number, mostProbabl
|
|
250
296
|
} else if (mostProbableType === "array") {
|
251
297
|
const arrayTypesCount = typesCount.array as TypesCount;
|
252
298
|
const arrayMostProbableType = getMostProbableType(arrayTypesCount);
|
253
|
-
const of = buildPropertyFromCount(
|
299
|
+
const of = buildPropertyFromCount(
|
300
|
+
key,
|
301
|
+
totalDocsCount,
|
302
|
+
arrayMostProbableType,
|
303
|
+
arrayTypesCount,
|
304
|
+
valuesResult
|
305
|
+
);
|
254
306
|
result = {
|
255
307
|
dataType: "array",
|
256
308
|
name: title,
|
257
309
|
of
|
258
310
|
};
|
259
311
|
}
|
312
|
+
|
260
313
|
if (!result) {
|
261
314
|
const propertyProps: InferencePropertyBuilderProps = {
|
262
315
|
name: key,
|
@@ -289,19 +342,28 @@ function buildPropertyFromCount(key: string, totalDocsCount: number, mostProbabl
|
|
289
342
|
};
|
290
343
|
}
|
291
344
|
|
292
|
-
function buildPropertiesFromCount(
|
345
|
+
function buildPropertiesFromCount(
|
346
|
+
totalDocsCount: number,
|
347
|
+
typesCountRecord: TypesCountRecord,
|
348
|
+
valuesCountRecord?: ValuesCountRecord
|
349
|
+
): Properties {
|
293
350
|
const res: Properties = {};
|
294
351
|
Object.entries(typesCountRecord).forEach(([key, typesCount]) => {
|
295
352
|
const mostProbableType = getMostProbableType(typesCount);
|
296
|
-
res[key] = buildPropertyFromCount(
|
297
|
-
|
353
|
+
res[key] = buildPropertyFromCount(
|
354
|
+
key,
|
355
|
+
totalDocsCount,
|
356
|
+
mostProbableType,
|
357
|
+
typesCount,
|
358
|
+
valuesCountRecord ? valuesCountRecord[key] : undefined
|
359
|
+
);
|
360
|
+
});
|
298
361
|
return res;
|
299
362
|
}
|
300
363
|
|
301
364
|
function countMaxDocumentsUnder(typesCount: TypesCount) {
|
302
365
|
let count = 0;
|
303
366
|
Object.entries(typesCount).forEach(([type, value]) => {
|
304
|
-
// console.log(util.inspect({ type, value }, { showHidden: false, depth: null, colors: true }));
|
305
367
|
if (typeof value === "object") {
|
306
368
|
count = Math.max(count, countMaxDocumentsUnder(value as TypesCountRecord));
|
307
369
|
} else {
|
@@ -311,7 +373,10 @@ function countMaxDocumentsUnder(typesCount: TypesCount) {
|
|
311
373
|
return count;
|
312
374
|
}
|
313
375
|
|
314
|
-
function getMostProbableTypeInArray(
|
376
|
+
function getMostProbableTypeInArray(
|
377
|
+
array: any[],
|
378
|
+
getType: InferenceTypeBuilder
|
379
|
+
): DataType {
|
315
380
|
let typesCount: TypesCount = {};
|
316
381
|
array.forEach((value) => {
|
317
382
|
increaseTypeCount(getType(value), typesCount, value, getType);
|
@@ -322,13 +387,37 @@ function getMostProbableTypeInArray(array: any[], getType: InferenceTypeBuilder)
|
|
322
387
|
function checkTypesCountHighVariability(typesCount: TypesCount) {
|
323
388
|
const maxCount = countMaxDocumentsUnder(typesCount);
|
324
389
|
let keysWithFewValues = 0;
|
325
|
-
Object.entries(typesCount.map ?? {})
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
});
|
390
|
+
Object.entries(typesCount.map ?? {}).forEach(([key, value]) => {
|
391
|
+
const count = countMaxDocumentsUnder(value);
|
392
|
+
if (count < maxCount / 3) {
|
393
|
+
keysWithFewValues++;
|
394
|
+
}
|
395
|
+
});
|
332
396
|
return keysWithFewValues / Object.entries(typesCount.map ?? {}).length > 0.5;
|
333
397
|
}
|
334
398
|
|
399
|
+
function formatString(input: string): string {
|
400
|
+
const normalized = input
|
401
|
+
.replace(/[_\-]+/g, " ")
|
402
|
+
.replace(/([a-z])([A-Z])/g, "$1 $2")
|
403
|
+
.toLowerCase();
|
404
|
+
|
405
|
+
// Split the normalized string into words
|
406
|
+
const words = normalized.split(" ");
|
407
|
+
|
408
|
+
// Capitalize the first letter of each word and join them with a space
|
409
|
+
const formatted = words
|
410
|
+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
411
|
+
.join(" ");
|
412
|
+
|
413
|
+
return formatted;
|
414
|
+
}
|
415
|
+
|
416
|
+
export function inferTypeFromValue(value: any): DataType {
|
417
|
+
if (typeof value === "string") return "string";
|
418
|
+
if (typeof value === "number") return "number";
|
419
|
+
if (typeof value === "boolean") return "boolean";
|
420
|
+
if (Array.isArray(value)) return "array";
|
421
|
+
if (typeof value === "object") return "map";
|
422
|
+
return "string";
|
423
|
+
}
|
package/src/strings.ts
CHANGED