@firecms/schema_inference 3.0.0-canary.24 → 3.0.0-canary.241

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/dist/index.es.js CHANGED
@@ -1,244 +1,489 @@
1
- import { unslugify as b, resolveEnumValues as T, mergeDeep as j } from "@firecms/core";
2
- import { DocumentReference as P } from "firebase/firestore";
3
- function y(r) {
4
- if (!r)
5
- return;
6
- function e(i) {
7
- return typeof i == "string" ? i : i instanceof P ? i.path : void 0;
8
- }
9
- const s = r.values.map((i) => e(i)).filter((i) => !!i).find((i) => i.includes("/"));
10
- if (!s)
11
- return;
12
- const n = s.substr(0, s.lastIndexOf("/"));
13
- return r.values.filter((i) => {
14
- const a = e(i);
15
- return a ? a.startsWith(n) : !1;
16
- }).length > r.values.length / 3 * 2 ? n : void 0;
17
- }
18
- function d(r) {
19
- if (!Array.isArray(r))
1
+ function findCommonInitialStringInPath(valuesCount) {
2
+ if (!valuesCount) return void 0;
3
+ function getPath(value) {
4
+ if (typeof value === "string") return value;
5
+ else if (value?.type === "document" && value.path) return value.path;
6
+ else return void 0;
7
+ }
8
+ const strings = valuesCount.values.map((v) => getPath(v)).filter((v) => !!v);
9
+ const pathWithSlash = strings.find((s) => s.includes("/"));
10
+ if (!pathWithSlash)
11
+ return void 0;
12
+ const searchedPath = pathWithSlash.substr(0, pathWithSlash.lastIndexOf("/"));
13
+ const yep = valuesCount.values.filter((value) => {
14
+ const path = getPath(value);
15
+ if (!path) return false;
16
+ return path.startsWith(searchedPath);
17
+ }).length > valuesCount.values.length / 3 * 2;
18
+ return yep ? searchedPath : void 0;
19
+ }
20
+ function extractEnumFromValues(values) {
21
+ if (!Array.isArray(values)) {
20
22
  return [];
21
- const e = r.map((t) => typeof t == "string" ? { id: t, label: b(t) } : null).filter(Boolean);
22
- return e.sort((t, s) => t.label.localeCompare(s.label)), e;
23
+ }
24
+ const enumValues = values.map((value) => {
25
+ if (typeof value === "string") {
26
+ return { id: value, label: unslugify(value) };
27
+ } else
28
+ return null;
29
+ }).filter(Boolean);
30
+ enumValues.sort((a, b) => a.label.localeCompare(b.label));
31
+ return enumValues;
32
+ }
33
+ function unslugify(slug) {
34
+ if (!slug) return "";
35
+ if (slug.includes("-") || slug.includes("_") || !slug.includes(" ")) {
36
+ const result = slug.replace(/[-_]/g, " ");
37
+ return result.replace(/\w\S*/g, function(txt) {
38
+ return txt.charAt(0).toUpperCase() + txt.substr(1);
39
+ }).trim();
40
+ } else {
41
+ return slug.trim();
42
+ }
23
43
  }
24
- const I = [".jpg", ".png", ".webp", ".gif"], S = [".mp3", ".ogg", ".opus", ".aac"], M = [".avi", ".mp4"], x = /^[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])?)*$/;
25
- function C({
26
- totalDocsCount: r,
27
- valuesResult: e
44
+ function resolveEnumValues(input) {
45
+ if (typeof input === "object") {
46
+ return Object.entries(input).map(([id, value]) => typeof value === "string" ? {
47
+ id,
48
+ label: value
49
+ } : value);
50
+ } else if (Array.isArray(input)) {
51
+ return input;
52
+ } else {
53
+ return void 0;
54
+ }
55
+ }
56
+ function mergeDeep(target, source, ignoreUndefined = false) {
57
+ const targetIsObject = isObject(target);
58
+ const output = targetIsObject ? { ...target } : target;
59
+ if (targetIsObject && isObject(source)) {
60
+ Object.keys(source).forEach((key) => {
61
+ const sourceElement = source[key];
62
+ if (ignoreUndefined && sourceElement === void 0) {
63
+ return;
64
+ }
65
+ if (sourceElement instanceof Date) {
66
+ Object.assign(output, { [key]: new Date(sourceElement.getTime()) });
67
+ } else if (isObject(sourceElement)) {
68
+ if (!(key in target))
69
+ Object.assign(output, { [key]: sourceElement });
70
+ else
71
+ output[key] = mergeDeep(target[key], sourceElement);
72
+ } else {
73
+ Object.assign(output, { [key]: sourceElement });
74
+ }
75
+ });
76
+ }
77
+ return output;
78
+ }
79
+ function isObject(item) {
80
+ return item && typeof item === "object" && !Array.isArray(item);
81
+ }
82
+ const IMAGE_EXTENSIONS = [".jpg", ".jpeg", ".png", ".webp", ".gif", ".avif"];
83
+ const AUDIO_EXTENSIONS = [".mp3", ".ogg", ".opus", ".aac"];
84
+ const VIDEO_EXTENSIONS = [".avi", ".mp4"];
85
+ 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])?)*$/;
86
+ function buildStringProperty({
87
+ totalDocsCount,
88
+ valuesResult
28
89
  }) {
29
- let t = {
90
+ let stringProperty = {
30
91
  dataType: "string"
31
92
  };
32
- if (e) {
33
- const s = e.values.length, n = Array.from(e.valuesCount.keys()).length, o = {}, i = e.values.filter((f) => typeof f == "string" && f.toString().startsWith("http")).length > r / 3 * 2;
34
- i && (o.url = !0);
35
- const a = e.values.filter((f) => typeof f == "string" && x.test(f)).length > r / 3 * 2;
36
- a && (o.email = !0);
37
- const c = e.values.filter((f) => typeof f == "string" && f.length === 28 && !f.includes(" ")).length > r / 3 * 2;
38
- if (c && (o.readOnly = !0), !a && !i && !c && !i && n < s / 3) {
39
- const f = d(Array.from(e.valuesCount.keys()));
40
- Object.keys(f).length > 1 && (o.enumValues = f);
41
- }
42
- if (!a && !i && !c && !i && !o.enumValues) {
43
- const f = F(e, r);
44
- f && (o.storage = {
45
- acceptedFiles: [f],
46
- storagePath: y(e) ?? "/"
47
- });
93
+ if (valuesResult) {
94
+ const totalEntriesCount = valuesResult.values.length;
95
+ const totalValues = Array.from(valuesResult.valuesCount.keys()).length;
96
+ const config = {};
97
+ const probablyAURL = valuesResult.values.filter((value) => typeof value === "string" && value.toString().startsWith("http")).length > totalDocsCount / 3 * 2;
98
+ if (probablyAURL) {
99
+ config.url = true;
48
100
  }
49
- Object.keys(o).length > 0 && (t = {
50
- ...t,
51
- ...o,
52
- editable: !0
53
- });
101
+ const probablyAnEmail = valuesResult.values.filter((value) => typeof value === "string" && emailRegEx.test(value)).length > totalDocsCount / 3 * 2;
102
+ if (probablyAnEmail) {
103
+ config.email = true;
104
+ }
105
+ const probablyUserIds = valuesResult.values.filter((value) => typeof value === "string" && value.length === 28 && !value.includes(" ")).length > totalDocsCount / 3 * 2;
106
+ if (probablyUserIds)
107
+ config.readOnly = true;
108
+ if (!probablyAnEmail && !probablyAURL && !probablyUserIds && !probablyAURL && totalValues < totalEntriesCount / 3) {
109
+ const enumValues = extractEnumFromValues(Array.from(valuesResult.valuesCount.keys()));
110
+ if (Object.keys(enumValues).length > 1)
111
+ config.enumValues = enumValues;
112
+ }
113
+ if (!probablyAnEmail && !probablyAURL && !probablyUserIds && !probablyAURL && !config.enumValues) {
114
+ const fileType = probableFileType(valuesResult, totalDocsCount);
115
+ if (fileType) {
116
+ config.storage = {
117
+ acceptedFiles: [fileType],
118
+ storagePath: findCommonInitialStringInPath(valuesResult) ?? "/"
119
+ };
120
+ }
121
+ }
122
+ if (Object.keys(config).length > 0)
123
+ stringProperty = {
124
+ ...stringProperty,
125
+ ...config,
126
+ editable: true
127
+ };
54
128
  }
55
- return t;
129
+ return stringProperty;
56
130
  }
57
- function F(r, e) {
58
- const t = r.values.filter((i) => typeof i == "string" && I.some((a) => i.toString().endsWith(a))).length > e / 3 * 2, s = r.values.filter((i) => typeof i == "string" && S.some((a) => i.toString().endsWith(a))).length > e / 3 * 2, n = r.values.filter((i) => typeof i == "string" && M.some((a) => i.toString().endsWith(a))).length > e / 3 * 2;
59
- return t ? "image/*" : s ? "audio/*" : n ? "video/*" : !1;
131
+ function probableFileType(valuesCount, totalDocsCount) {
132
+ const probablyAnImage = valuesCount.values.filter((value) => typeof value === "string" && IMAGE_EXTENSIONS.some((extension) => value.toString().endsWith(extension))).length > totalDocsCount / 3 * 2;
133
+ const probablyAudio = valuesCount.values.filter((value) => typeof value === "string" && AUDIO_EXTENSIONS.some((extension) => value.toString().endsWith(extension))).length > totalDocsCount / 3 * 2;
134
+ const probablyVideo = valuesCount.values.filter((value) => typeof value === "string" && VIDEO_EXTENSIONS.some((extension) => value.toString().endsWith(extension))).length > totalDocsCount / 3 * 2;
135
+ const fileType = probablyAnImage ? "image/*" : probablyAudio ? "audio/*" : probablyVideo ? "video/*" : false;
136
+ return fileType;
60
137
  }
61
- function V({
62
- totalDocsCount: r,
63
- valuesResult: e
138
+ function buildValidation({
139
+ totalDocsCount,
140
+ valuesResult
64
141
  }) {
65
- if (e) {
66
- const t = e.values.length;
67
- if (r === t)
142
+ if (valuesResult) {
143
+ const totalEntriesCount = valuesResult.values.length;
144
+ if (totalDocsCount === totalEntriesCount)
68
145
  return {
69
- required: !0
146
+ required: true
70
147
  };
71
148
  }
149
+ return void 0;
72
150
  }
73
- function _({
74
- totalDocsCount: r,
75
- valuesResult: e
151
+ function buildReferenceProperty({
152
+ totalDocsCount,
153
+ valuesResult
76
154
  }) {
77
- return {
155
+ const property = {
78
156
  dataType: "reference",
79
- path: y(e) ?? "!!!FIX_ME!!!",
80
- editable: !0
157
+ path: findCommonInitialStringInPath(valuesResult) ?? "!!!FIX_ME!!!",
158
+ editable: true
81
159
  };
160
+ return property;
82
161
  }
83
- async function k(r, e) {
84
- const t = {}, s = {};
85
- return r && r.forEach((n) => {
86
- n && Object.entries(n).forEach(([o, i]) => {
87
- E(t, o, i, e), p(s, o, i, e);
162
+ async function buildEntityPropertiesFromData(data, getType) {
163
+ const typesCount = {};
164
+ const valuesCount = {};
165
+ if (data) {
166
+ data.forEach((entry) => {
167
+ if (entry) {
168
+ Object.entries(entry).forEach(([key, value]) => {
169
+ if (key.startsWith("_")) return;
170
+ increaseMapTypeCount(typesCount, key, value, getType);
171
+ increaseValuesCount(valuesCount, key, value, getType);
172
+ });
173
+ }
88
174
  });
89
- }), O(r.length, t, s);
175
+ }
176
+ return buildPropertiesFromCount(data.length, typesCount, valuesCount);
90
177
  }
91
- function w(r, e, t) {
92
- const s = {}, n = {};
93
- r && r.forEach((a) => {
94
- l(e.dataType, s, a, t), p(n, "inferred_prop", a, t);
95
- });
96
- const o = "enumValues" in e ? T(e.enumValues) : void 0;
97
- if (o) {
98
- const a = d(Array.from(n.inferred_prop.valuesCount.keys()));
178
+ function buildPropertyFromData(data, property, getType) {
179
+ const typesCount = {};
180
+ const valuesCount = {};
181
+ if (data) {
182
+ data.forEach((entry) => {
183
+ increaseTypeCount(property.dataType, typesCount, entry, getType);
184
+ increaseValuesCount(valuesCount, "inferred_prop", entry, getType);
185
+ });
186
+ }
187
+ const enumValues = "enumValues" in property ? resolveEnumValues(property["enumValues"]) : void 0;
188
+ if (enumValues) {
189
+ const newEnumValues = extractEnumFromValues(Array.from(valuesCount["inferred_prop"].valuesCount.keys()));
99
190
  return {
100
- ...e,
101
- enumValues: [...a, ...o]
191
+ ...property,
192
+ enumValues: [...newEnumValues, ...enumValues]
102
193
  };
103
194
  }
104
- const i = g("inferred_prop", r.length, e.dataType, s, n.inferred_prop);
105
- return j(i, e);
195
+ const generatedProperty = buildPropertyFromCount(
196
+ "inferred_prop",
197
+ data.length,
198
+ property.dataType,
199
+ typesCount,
200
+ valuesCount["inferred_prop"]
201
+ );
202
+ return mergeDeep(generatedProperty, property);
106
203
  }
107
- function U(r) {
108
- function e(s) {
109
- const n = s.toLowerCase();
110
- return n === "title" || n === "name" ? 3 : n.includes("title") || n.includes("name") ? 2 : n.includes("image") || n.includes("picture") ? 1 : 0;
204
+ function buildPropertiesOrder(properties, propertiesOrder, priorityKeys) {
205
+ const lowerCasePriorityKeys = (priorityKeys ?? []).map((key) => key.toLowerCase());
206
+ function propOrder(s) {
207
+ const k = s.toLowerCase();
208
+ if (lowerCasePriorityKeys.includes(k)) return 4;
209
+ if (k === "title" || k === "name") return 3;
210
+ if (k.includes("title") || k.includes("name")) return 2;
211
+ if (k.includes("image") || k.includes("picture")) return 1;
212
+ return 0;
111
213
  }
112
- const t = Object.keys(r);
113
- return t.sort(), t.sort((s, n) => e(n) - e(s)), t;
214
+ const keys = propertiesOrder ?? Object.keys(properties);
215
+ keys.sort();
216
+ keys.sort((a, b) => {
217
+ return propOrder(b) - propOrder(a);
218
+ });
219
+ return keys;
114
220
  }
115
- function l(r, e, t, s) {
116
- if (r === "map") {
117
- if (t) {
118
- let n = e[r];
119
- n || (n = {}, e[r] = n), Object.entries(t).forEach(([o, i]) => {
120
- E(n, o, i, s);
221
+ function increaseTypeCount(type, typesCount, fieldValue, getType) {
222
+ if (type === "map") {
223
+ if (fieldValue) {
224
+ let mapTypesCount = typesCount[type];
225
+ if (!mapTypesCount) {
226
+ mapTypesCount = {};
227
+ typesCount[type] = mapTypesCount;
228
+ }
229
+ Object.entries(fieldValue).forEach(([key, value]) => {
230
+ increaseMapTypeCount(mapTypesCount, key, value, getType);
121
231
  });
122
232
  }
123
- } else if (r === "array") {
124
- let n = e[r];
125
- if (n || (n = {}, e[r] = n), t && Array.isArray(t) && t.length > 0) {
126
- const o = z(t, s);
127
- n[o] ? n[o]++ : n[o] = 1;
128
- }
129
- } else
130
- e[r] ? e[r]++ : e[r] = 1;
131
- }
132
- function E(r, e, t, s) {
133
- let n = r[e];
134
- if (n || (n = {}, r[e] = n), t != null) {
135
- const o = s(t);
136
- l(o, n, t, s);
137
- }
138
- }
139
- function p(r, e, t, s) {
140
- const n = s(t);
141
- let o = r[e];
142
- if (o || (o = {
143
- values: [],
144
- valuesCount: /* @__PURE__ */ new Map()
145
- }, r[e] = o), n === "map") {
146
- let i = o.map;
147
- i || (i = {}, o.map = i), t && Object.entries(t).forEach(([a, c]) => p(i, a, c, s));
148
- } else
149
- n === "array" ? Array.isArray(t) && t.forEach((i) => {
150
- o.values.push(i), o.valuesCount.set(i, (o.valuesCount.get(i) ?? 0) + 1);
151
- }) : t && (o.values.push(t), o.valuesCount.set(t, (o.valuesCount.get(t) ?? 0) + 1));
152
- }
153
- function m(r) {
154
- let e = 0;
155
- return Object.entries(r).forEach(([t, s]) => {
156
- let n = 0;
157
- t === "map" ? n = A(s) : t === "array" ? n = m(s) : n = s, n > e && (e = n);
158
- }), e;
159
- }
160
- function A(r) {
161
- return Object.entries(r).map(([e, t]) => m(t)).reduce((e, t) => Math.max(e, t), 0);
162
- }
163
- function h(r) {
164
- let e = -1, t = "string";
165
- return Object.entries(r).forEach(([s, n]) => {
166
- let o;
167
- s === "map" ? o = A(n) : s === "array" ? o = m(n) : o = n, o > e && (e = o, t = s);
168
- }), t;
169
- }
170
- function g(r, e, t, s, n) {
171
- let o;
172
- r && (o = b(r));
173
- let i;
174
- if (t === "map") {
175
- W(s) && (i = {
176
- dataType: "map",
177
- name: o,
178
- keyValue: !0,
179
- properties: {}
180
- });
181
- const c = O(e, s.map, n ? n.mapValues : void 0);
182
- i = {
233
+ } else if (type === "array") {
234
+ let arrayTypesCount = typesCount[type];
235
+ if (!arrayTypesCount) {
236
+ arrayTypesCount = {};
237
+ typesCount[type] = arrayTypesCount;
238
+ }
239
+ if (fieldValue && Array.isArray(fieldValue) && fieldValue.length > 0) {
240
+ const arrayType = getMostProbableTypeInArray(fieldValue, getType);
241
+ if (arrayType === "map") {
242
+ let mapTypesCount = arrayTypesCount[arrayType];
243
+ if (!mapTypesCount) {
244
+ mapTypesCount = {};
245
+ }
246
+ fieldValue.forEach((value) => {
247
+ if (value && typeof value === "object" && !Array.isArray(value)) {
248
+ Object.entries(value).forEach(
249
+ ([key, v]) => increaseMapTypeCount(mapTypesCount, key, v, getType)
250
+ );
251
+ }
252
+ });
253
+ arrayTypesCount[arrayType] = mapTypesCount;
254
+ } else {
255
+ if (!arrayTypesCount[arrayType]) arrayTypesCount[arrayType] = 1;
256
+ else arrayTypesCount[arrayType]++;
257
+ }
258
+ }
259
+ } else {
260
+ if (!typesCount[type]) typesCount[type] = 1;
261
+ else typesCount[type]++;
262
+ }
263
+ }
264
+ function increaseMapTypeCount(typesCountRecord, key, fieldValue, getType) {
265
+ if (key.startsWith("_")) return;
266
+ let typesCount = typesCountRecord[key];
267
+ if (!typesCount) {
268
+ typesCount = {};
269
+ typesCountRecord[key] = typesCount;
270
+ }
271
+ if (fieldValue != null) {
272
+ const type = getType(fieldValue);
273
+ increaseTypeCount(type, typesCount, fieldValue, getType);
274
+ }
275
+ }
276
+ function increaseValuesCount(typeValuesRecord, key, fieldValue, getType) {
277
+ if (key.startsWith("_")) return;
278
+ const dataType = getType(fieldValue);
279
+ let valuesRecord = typeValuesRecord[key];
280
+ if (!valuesRecord) {
281
+ valuesRecord = {
282
+ values: [],
283
+ valuesCount: /* @__PURE__ */ new Map()
284
+ };
285
+ typeValuesRecord[key] = valuesRecord;
286
+ }
287
+ if (dataType === "map") {
288
+ let mapValuesRecord = valuesRecord.map;
289
+ if (!mapValuesRecord) {
290
+ mapValuesRecord = {};
291
+ valuesRecord.map = mapValuesRecord;
292
+ }
293
+ if (fieldValue)
294
+ Object.entries(fieldValue).forEach(
295
+ ([subKey, value]) => increaseValuesCount(mapValuesRecord, subKey, value, getType)
296
+ );
297
+ } else if (dataType === "array") {
298
+ if (Array.isArray(fieldValue)) {
299
+ fieldValue.forEach((value) => {
300
+ valuesRecord.values.push(value);
301
+ valuesRecord.valuesCount.set(value, (valuesRecord.valuesCount.get(value) ?? 0) + 1);
302
+ });
303
+ }
304
+ } else {
305
+ if (fieldValue !== null && fieldValue !== void 0) {
306
+ valuesRecord.values.push(fieldValue);
307
+ valuesRecord.valuesCount.set(fieldValue, (valuesRecord.valuesCount.get(fieldValue) ?? 0) + 1);
308
+ }
309
+ }
310
+ }
311
+ function getHighestTypesCount(typesCount) {
312
+ let highestCount = 0;
313
+ Object.entries(typesCount).forEach(([type, count]) => {
314
+ let countValue = 0;
315
+ if (type === "map") {
316
+ countValue = getHighestRecordCount(count);
317
+ } else if (type === "array") {
318
+ countValue = getHighestTypesCount(count);
319
+ } else {
320
+ countValue = count;
321
+ }
322
+ if (countValue > highestCount) {
323
+ highestCount = countValue;
324
+ }
325
+ });
326
+ return highestCount;
327
+ }
328
+ function getHighestRecordCount(record) {
329
+ return Object.entries(record).map(([key, typesCount]) => getHighestTypesCount(typesCount)).reduce((a, b) => Math.max(a, b), 0);
330
+ }
331
+ function getMostProbableType(typesCount) {
332
+ let highestCount = -1;
333
+ let probableType = "string";
334
+ Object.entries(typesCount).forEach(([type, count]) => {
335
+ let countValue;
336
+ if (type === "map") {
337
+ countValue = getHighestRecordCount(count);
338
+ } else if (type === "array") {
339
+ countValue = getHighestTypesCount(count);
340
+ } else {
341
+ countValue = count;
342
+ }
343
+ if (countValue > highestCount) {
344
+ highestCount = countValue;
345
+ probableType = type;
346
+ }
347
+ });
348
+ return probableType;
349
+ }
350
+ function buildPropertyFromCount(key, totalDocsCount, mostProbableType, typesCount, valuesResult) {
351
+ let title;
352
+ if (key) {
353
+ title = formatString(key.toLowerCase());
354
+ }
355
+ let result = void 0;
356
+ if (mostProbableType === "map") {
357
+ const highVariability = checkTypesCountHighVariability(typesCount);
358
+ if (highVariability) {
359
+ result = {
360
+ dataType: "map",
361
+ name: title,
362
+ keyValue: true,
363
+ properties: {}
364
+ };
365
+ }
366
+ const properties = buildPropertiesFromCount(
367
+ totalDocsCount,
368
+ typesCount.map,
369
+ valuesResult ? valuesResult.mapValues : void 0
370
+ );
371
+ result = {
183
372
  dataType: "map",
184
- name: o,
185
- properties: c
373
+ name: title,
374
+ properties
186
375
  };
187
- } else if (t === "array") {
188
- const a = s.array, c = h(a), f = g(r, e, c, a, n);
189
- i = {
376
+ } else if (mostProbableType === "array") {
377
+ const arrayTypesCount = typesCount.array;
378
+ const arrayMostProbableType = getMostProbableType(arrayTypesCount);
379
+ const of = buildPropertyFromCount(
380
+ key,
381
+ totalDocsCount,
382
+ arrayMostProbableType,
383
+ arrayTypesCount,
384
+ valuesResult
385
+ );
386
+ result = {
190
387
  dataType: "array",
191
- name: o,
192
- of: f
388
+ name: title,
389
+ of
193
390
  };
194
391
  }
195
- if (!i) {
196
- const a = {
197
- name: r,
198
- totalDocsCount: e,
199
- valuesResult: n
392
+ if (!result) {
393
+ const propertyProps = {
394
+ name: key,
395
+ totalDocsCount,
396
+ valuesResult
200
397
  };
201
- t === "string" ? i = C(a) : t === "reference" ? i = _(a) : i = {
202
- dataType: t
203
- }, o && (i.name = o);
204
- const c = V(a);
205
- c && (i.validation = c);
398
+ if (mostProbableType === "string") {
399
+ result = buildStringProperty(propertyProps);
400
+ } else if (mostProbableType === "reference") {
401
+ result = buildReferenceProperty(propertyProps);
402
+ } else {
403
+ result = {
404
+ dataType: mostProbableType
405
+ };
406
+ }
407
+ if (title) {
408
+ result.name = title;
409
+ }
410
+ const validation = buildValidation(propertyProps);
411
+ if (validation) {
412
+ result.validation = validation;
413
+ }
206
414
  }
207
415
  return {
208
- ...i,
209
- editable: !0
416
+ ...result,
417
+ editable: true
210
418
  };
211
419
  }
212
- function O(r, e, t) {
213
- const s = {};
214
- return Object.entries(e).forEach(([n, o]) => {
215
- const i = h(o);
216
- s[n] = g(n, r, i, o, t ? t[n] : void 0);
217
- }), s;
218
- }
219
- function u(r) {
220
- let e = 0;
221
- return Object.entries(r).forEach(([t, s]) => {
222
- typeof s == "object" ? e = Math.max(e, u(s)) : e = Math.max(e, s);
223
- }), e;
224
- }
225
- function z(r, e) {
226
- let t = {};
227
- return r.forEach((s) => {
228
- l(e(s), t, s, e);
229
- }), h(t);
230
- }
231
- function W(r) {
232
- const e = u(r);
233
- let t = 0;
234
- return Object.entries(r.map ?? {}).forEach(([s, n]) => {
235
- u(n) < e / 3 && t++;
236
- }), t / Object.entries(r.map ?? {}).length > 0.5;
420
+ function buildPropertiesFromCount(totalDocsCount, typesCountRecord, valuesCountRecord) {
421
+ const res = {};
422
+ Object.entries(typesCountRecord).forEach(([key, typesCount]) => {
423
+ const mostProbableType = getMostProbableType(typesCount);
424
+ res[key] = buildPropertyFromCount(
425
+ key,
426
+ totalDocsCount,
427
+ mostProbableType,
428
+ typesCount,
429
+ valuesCountRecord ? valuesCountRecord[key] : void 0
430
+ );
431
+ });
432
+ return res;
433
+ }
434
+ function countMaxDocumentsUnder(typesCount) {
435
+ let count = 0;
436
+ Object.entries(typesCount).forEach(([type, value]) => {
437
+ if (typeof value === "object") {
438
+ count = Math.max(count, countMaxDocumentsUnder(value));
439
+ } else {
440
+ count = Math.max(count, value);
441
+ }
442
+ });
443
+ return count;
444
+ }
445
+ function getMostProbableTypeInArray(array, getType) {
446
+ const typesCount = {};
447
+ array.forEach((value) => {
448
+ increaseTypeCount(getType(value), typesCount, value, getType);
449
+ });
450
+ return getMostProbableType(typesCount);
451
+ }
452
+ function checkTypesCountHighVariability(typesCount) {
453
+ const maxCount = countMaxDocumentsUnder(typesCount);
454
+ let keysWithFewValues = 0;
455
+ Object.entries(typesCount.map ?? {}).forEach(([key, value]) => {
456
+ const count = countMaxDocumentsUnder(value);
457
+ if (count < maxCount / 3) {
458
+ keysWithFewValues++;
459
+ }
460
+ });
461
+ return keysWithFewValues / Object.entries(typesCount.map ?? {}).length > 0.5;
462
+ }
463
+ function formatString(input) {
464
+ const normalized = input.replace(/[_-]+/g, " ").replace(/([a-z])([A-Z])/g, "$1 $2").toLowerCase();
465
+ const words = normalized.split(" ");
466
+ const formatted = words.map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
467
+ return formatted;
468
+ }
469
+ function inferTypeFromValue(value) {
470
+ if (value === null || value === void 0) return "string";
471
+ if (typeof value === "string") return "string";
472
+ if (typeof value === "number") return "number";
473
+ if (typeof value === "boolean") return "boolean";
474
+ if (Array.isArray(value)) return "array";
475
+ if (typeof value === "object") return "map";
476
+ return "string";
237
477
  }
238
478
  export {
239
- k as buildEntityPropertiesFromData,
240
- U as buildPropertiesOrder,
241
- w as buildPropertyFromData,
242
- d as extractEnumFromValues
479
+ buildEntityPropertiesFromData,
480
+ buildPropertiesOrder,
481
+ buildPropertyFromData,
482
+ extractEnumFromValues,
483
+ inferTypeFromValue,
484
+ isObject,
485
+ mergeDeep,
486
+ resolveEnumValues,
487
+ unslugify
243
488
  };
244
489
  //# sourceMappingURL=index.es.js.map