@firecms/schema_inference 3.0.0-canary.43 → 3.0.0-canary.45

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.
@@ -2,5 +2,5 @@ import { DataType, Properties, Property } from "@firecms/core";
2
2
  export type InferenceTypeBuilder = (value: any) => DataType;
3
3
  export declare function buildEntityPropertiesFromData(data: object[], getType: InferenceTypeBuilder): Promise<Properties>;
4
4
  export declare function buildPropertyFromData(data: any[], property: Property, getType: InferenceTypeBuilder): Property;
5
- export declare function buildPropertiesOrder(properties: Properties<any>): string[];
5
+ export declare function buildPropertiesOrder(properties: Properties<any>, priorityKeys?: string[]): string[];
6
6
  export declare function inferTypeFromValue(value: any): DataType;
package/dist/index.es.js CHANGED
@@ -3,17 +3,17 @@ import { DocumentReference as P } from "firebase/firestore";
3
3
  function y(r) {
4
4
  if (!r)
5
5
  return;
6
- function e(i) {
7
- return typeof i == "string" ? i : i instanceof P ? i.path : void 0;
6
+ function e(n) {
7
+ return typeof n == "string" ? n : n instanceof P ? n.path : void 0;
8
8
  }
9
- const s = r.values.map((i) => e(i)).filter((i) => !!i).find((i) => i.includes("/"));
9
+ const s = r.values.map((n) => e(n)).filter((n) => !!n).find((n) => n.includes("/"));
10
10
  if (!s)
11
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;
12
+ const i = s.substr(0, s.lastIndexOf("/"));
13
+ return r.values.filter((n) => {
14
+ const a = e(n);
15
+ return a ? a.startsWith(i) : !1;
16
+ }).length > r.values.length / 3 * 2 ? i : void 0;
17
17
  }
18
18
  function b(r) {
19
19
  if (!Array.isArray(r))
@@ -21,8 +21,8 @@ function b(r) {
21
21
  const e = r.map((t) => typeof t == "string" ? { id: t, label: O(t) } : null).filter(Boolean);
22
22
  return e.sort((t, s) => t.label.localeCompare(s.label)), e;
23
23
  }
24
- const S = [".jpg", ".png", ".webp", ".gif"], I = [".mp3", ".ogg", ".opus", ".aac"], C = [".avi", ".mp4"], M = /^[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 z({
24
+ const C = [".jpg", ".jpeg", ".png", ".webp", ".gif", ".avif"], S = [".mp3", ".ogg", ".opus", ".aac"], I = [".avi", ".mp4"], M = /^[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 w({
26
26
  totalDocsCount: r,
27
27
  valuesResult: e
28
28
  }) {
@@ -30,17 +30,17 @@ function z({
30
30
  dataType: "string"
31
31
  };
32
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);
33
+ const s = e.values.length, i = Array.from(e.valuesCount.keys()).length, o = {}, n = e.values.filter((f) => typeof f == "string" && f.toString().startsWith("http")).length > r / 3 * 2;
34
+ n && (o.url = !0);
35
35
  const a = e.values.filter((f) => typeof f == "string" && M.test(f)).length > r / 3 * 2;
36
36
  a && (o.email = !0);
37
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) {
38
+ if (c && (o.readOnly = !0), !a && !n && !c && !n && i < s / 3) {
39
39
  const f = b(Array.from(e.valuesCount.keys()));
40
40
  Object.keys(f).length > 1 && (o.enumValues = f);
41
41
  }
42
- if (!a && !i && !c && !i && !o.enumValues) {
43
- const f = F(e, r);
42
+ if (!a && !n && !c && !n && !o.enumValues) {
43
+ const f = z(e, r);
44
44
  f && (o.storage = {
45
45
  acceptedFiles: [f],
46
46
  storagePath: y(e) ?? "/"
@@ -54,11 +54,11 @@ function z({
54
54
  }
55
55
  return t;
56
56
  }
57
- function F(r, e) {
58
- const t = r.values.filter((i) => typeof i == "string" && S.some((a) => i.toString().endsWith(a))).length > e / 3 * 2, s = r.values.filter((i) => typeof i == "string" && I.some((a) => i.toString().endsWith(a))).length > e / 3 * 2, n = r.values.filter((i) => typeof i == "string" && C.some((a) => i.toString().endsWith(a))).length > e / 3 * 2;
59
- return t ? "image/*" : s ? "audio/*" : n ? "video/*" : !1;
57
+ function z(r, e) {
58
+ const t = r.values.filter((n) => typeof n == "string" && C.some((a) => n.toString().endsWith(a))).length > e / 3 * 2, s = r.values.filter((n) => typeof n == "string" && S.some((a) => n.toString().endsWith(a))).length > e / 3 * 2, i = r.values.filter((n) => typeof n == "string" && I.some((a) => n.toString().endsWith(a))).length > e / 3 * 2;
59
+ return t ? "image/*" : s ? "audio/*" : i ? "video/*" : !1;
60
60
  }
61
- function V({
61
+ function F({
62
62
  totalDocsCount: r,
63
63
  valuesResult: e
64
64
  }) {
@@ -70,7 +70,7 @@ function V({
70
70
  };
71
71
  }
72
72
  }
73
- function _({
73
+ function V({
74
74
  totalDocsCount: r,
75
75
  valuesResult: e
76
76
  }) {
@@ -82,79 +82,80 @@ function _({
82
82
  }
83
83
  async function k(r, e) {
84
84
  const t = {}, s = {};
85
- return r && r.forEach((n) => {
86
- n && Object.entries(n).forEach(([o, i]) => {
87
- d(t, o, i, e), p(s, o, i, e);
85
+ return r && r.forEach((i) => {
86
+ i && Object.entries(i).forEach(([o, n]) => {
87
+ d(t, o, n, e), p(s, o, n, e);
88
88
  });
89
89
  }), A(r.length, t, s);
90
90
  }
91
- function U(r, e, t) {
92
- const s = {}, n = {};
91
+ function L(r, e, t) {
92
+ const s = {}, i = {};
93
93
  r && r.forEach((a) => {
94
- l(e.dataType, s, a, t), p(n, "inferred_prop", a, t);
94
+ l(e.dataType, s, a, t), p(i, "inferred_prop", a, t);
95
95
  });
96
96
  const o = "enumValues" in e ? T(e.enumValues) : void 0;
97
97
  if (o) {
98
- const a = b(Array.from(n.inferred_prop.valuesCount.keys()));
98
+ const a = b(Array.from(i.inferred_prop.valuesCount.keys()));
99
99
  return {
100
100
  ...e,
101
101
  enumValues: [...a, ...o]
102
102
  };
103
103
  }
104
- const i = h("inferred_prop", r.length, e.dataType, s, n.inferred_prop);
105
- return j(i, e);
106
- }
107
- function L(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;
104
+ const n = h("inferred_prop", r.length, e.dataType, s, i.inferred_prop);
105
+ return j(n, e);
106
+ }
107
+ function U(r, e) {
108
+ const t = (e ?? []).map((o) => o.toLowerCase());
109
+ function s(o) {
110
+ const n = o.toLowerCase();
111
+ return t.includes(n) ? 4 : n === "title" || n === "name" || n === "title" || n === "name" ? 3 : n.includes("title") || n.includes("name") ? 2 : n.includes("image") || n.includes("picture") ? 1 : 0;
111
112
  }
112
- const t = Object.keys(r);
113
- return t.sort(), t.sort((s, n) => e(n) - e(s)), t;
113
+ const i = Object.keys(r);
114
+ return i.sort(), i.sort((o, n) => s(n) - s(o)), i;
114
115
  }
115
116
  function l(r, e, t, s) {
116
117
  if (r === "map") {
117
118
  if (t) {
118
- let n = e[r];
119
- n || (n = {}, e[r] = n), Object.entries(t).forEach(([o, i]) => {
120
- d(n, o, i, s);
119
+ let i = e[r];
120
+ i || (i = {}, e[r] = i), Object.entries(t).forEach(([o, n]) => {
121
+ d(i, o, n, s);
121
122
  });
122
123
  }
123
124
  } 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 = x(t, s);
127
- n[o] ? n[o]++ : n[o] = 1;
125
+ let i = e[r];
126
+ if (i || (i = {}, e[r] = i), t && Array.isArray(t) && t.length > 0) {
127
+ const o = _(t, s);
128
+ i[o] ? i[o]++ : i[o] = 1;
128
129
  }
129
130
  } else
130
131
  e[r] ? e[r]++ : e[r] = 1;
131
132
  }
132
133
  function d(r, e, t, s) {
133
- let n = r[e];
134
- if (n || (n = {}, r[e] = n), t != null) {
134
+ let i = r[e];
135
+ if (i || (i = {}, r[e] = i), t != null) {
135
136
  const o = s(t);
136
- l(o, n, t, s);
137
+ l(o, i, t, s);
137
138
  }
138
139
  }
139
140
  function p(r, e, t, s) {
140
- const n = s(t);
141
+ const i = s(t);
141
142
  let o = r[e];
142
143
  if (o || (o = {
143
144
  values: [],
144
145
  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));
146
+ }, r[e] = o), i === "map") {
147
+ let n = o.map;
148
+ n || (n = {}, o.map = n), t && Object.entries(t).forEach(([a, c]) => p(n, a, c, s));
148
149
  } 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);
150
+ i === "array" ? Array.isArray(t) && t.forEach((n) => {
151
+ o.values.push(n), o.valuesCount.set(n, (o.valuesCount.get(n) ?? 0) + 1);
151
152
  }) : t && (o.values.push(t), o.valuesCount.set(t, (o.valuesCount.get(t) ?? 0) + 1));
152
153
  }
153
154
  function m(r) {
154
155
  let e = 0;
155
156
  return Object.entries(r).forEach(([t, s]) => {
156
- let n = 0;
157
- t === "map" ? n = E(s) : t === "array" ? n = m(s) : n = s, n > e && (e = n);
157
+ let i = 0;
158
+ t === "map" ? i = E(s) : t === "array" ? i = m(s) : i = s, i > e && (e = i);
158
159
  }), e;
159
160
  }
160
161
  function E(r) {
@@ -162,58 +163,58 @@ function E(r) {
162
163
  }
163
164
  function g(r) {
164
165
  let e = -1, t = "string";
165
- return Object.entries(r).forEach(([s, n]) => {
166
+ return Object.entries(r).forEach(([s, i]) => {
166
167
  let o;
167
- s === "map" ? o = E(n) : s === "array" ? o = m(n) : o = n, o > e && (e = o, t = s);
168
+ s === "map" ? o = E(i) : s === "array" ? o = m(i) : o = i, o > e && (e = o, t = s);
168
169
  }), t;
169
170
  }
170
- function h(r, e, t, s, n) {
171
+ function h(r, e, t, s, i) {
171
172
  let o;
172
173
  r && (o = Z(r.toLowerCase()));
173
- let i;
174
+ let n;
174
175
  if (t === "map") {
175
- w(s) && (i = {
176
+ x(s) && (n = {
176
177
  dataType: "map",
177
178
  name: o,
178
179
  keyValue: !0,
179
180
  properties: {}
180
181
  });
181
- const c = A(e, s.map, n ? n.mapValues : void 0);
182
- i = {
182
+ const c = A(e, s.map, i ? i.mapValues : void 0);
183
+ n = {
183
184
  dataType: "map",
184
185
  name: o,
185
186
  properties: c
186
187
  };
187
188
  } else if (t === "array") {
188
- const a = s.array, c = g(a), f = h(r, e, c, a, n);
189
- i = {
189
+ const a = s.array, c = g(a), f = h(r, e, c, a, i);
190
+ n = {
190
191
  dataType: "array",
191
192
  name: o,
192
193
  of: f
193
194
  };
194
195
  }
195
- if (!i) {
196
+ if (!n) {
196
197
  const a = {
197
198
  name: r,
198
199
  totalDocsCount: e,
199
- valuesResult: n
200
+ valuesResult: i
200
201
  };
201
- t === "string" ? i = z(a) : t === "reference" ? i = _(a) : i = {
202
+ t === "string" ? n = w(a) : t === "reference" ? n = V(a) : n = {
202
203
  dataType: t
203
- }, o && (i.name = o);
204
- const c = V(a);
205
- c && (i.validation = c);
204
+ }, o && (n.name = o);
205
+ const c = F(a);
206
+ c && (n.validation = c);
206
207
  }
207
208
  return {
208
- ...i,
209
+ ...n,
209
210
  editable: !0
210
211
  };
211
212
  }
212
213
  function A(r, e, t) {
213
214
  const s = {};
214
- return Object.entries(e).forEach(([n, o]) => {
215
- const i = g(o);
216
- s[n] = h(n, r, i, o, t ? t[n] : void 0);
215
+ return Object.entries(e).forEach(([i, o]) => {
216
+ const n = g(o);
217
+ s[i] = h(i, r, n, o, t ? t[i] : void 0);
217
218
  }), s;
218
219
  }
219
220
  function u(r) {
@@ -222,29 +223,29 @@ function u(r) {
222
223
  typeof s == "object" ? e = Math.max(e, u(s)) : e = Math.max(e, s);
223
224
  }), e;
224
225
  }
225
- function x(r, e) {
226
+ function _(r, e) {
226
227
  let t = {};
227
228
  return r.forEach((s) => {
228
229
  l(e(s), t, s, e);
229
230
  }), g(t);
230
231
  }
231
- function w(r) {
232
+ function x(r) {
232
233
  const e = u(r);
233
234
  let t = 0;
234
- return Object.entries(r.map ?? {}).forEach(([s, n]) => {
235
- u(n) < e / 3 && t++;
235
+ return Object.entries(r.map ?? {}).forEach(([s, i]) => {
236
+ u(i) < e / 3 && t++;
236
237
  }), t / Object.entries(r.map ?? {}).length > 0.5;
237
238
  }
238
239
  function Z(r) {
239
- return r.replace(/[_\-]+/g, " ").replace(/([a-z])([A-Z])/g, "$1 $2").toLowerCase().split(" ").map((n) => n.charAt(0).toUpperCase() + n.slice(1)).join(" ");
240
+ return r.replace(/[_\-]+/g, " ").replace(/([a-z])([A-Z])/g, "$1 $2").toLowerCase().split(" ").map((i) => i.charAt(0).toUpperCase() + i.slice(1)).join(" ");
240
241
  }
241
242
  function X(r) {
242
243
  return typeof r == "string" ? "string" : typeof r == "number" ? "number" : typeof r == "boolean" ? "boolean" : Array.isArray(r) ? "array" : typeof r == "object" ? "map" : "string";
243
244
  }
244
245
  export {
245
246
  k as buildEntityPropertiesFromData,
246
- L as buildPropertiesOrder,
247
- U as buildPropertyFromData,
247
+ U as buildPropertiesOrder,
248
+ L as buildPropertyFromData,
248
249
  b as extractEnumFromValues,
249
250
  X as inferTypeFromValue
250
251
  };
@@ -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} 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 = formatString(key.toLowerCase());\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\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\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","formatString","result","checkTypesCountHighVariability","arrayMostProbableType","of","propertyProps","validation","valuesCountRecord","res","countMaxDocumentsUnder","array","maxCount","keysWithFewValues","input","word","inferTypeFromValue"],"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,MAEpC,IACd,EAAE,OAAO,OAAO;AACV,SAAAS,EAAA,KAAK,CAACE,GAAGC,MAAMD,EAAE,MAAM,cAAcC,EAAE,KAAK,CAAC,GACjDH;AACX;ACVA,MAAMI,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AYtC,GAAOqC,CAAO;AAAA,EAAA,CAC/D,GACMqC,EAAoBpC,CAAU;AACzC;AAEA,SAAS0C,EAA+B1C,GAAwB;AACtD,QAAAmD,IAAWF,EAAuBjD,CAAU;AAClD,MAAIoD,IAAoB;AACjB,gBAAA,QAAQpD,EAAW,OAAO,CAAA,CAAE,EAC9B,QAAQ,CAAC,CAACE,GAAKxC,CAAK,MAAM;AAEnB,IADUuF,EAAuBvF,CAAK,IAC9ByF,IAAW,KACnBC;AAAA,EACJ,CACH,GACEA,IAAoB,OAAO,QAAQpD,EAAW,OAAO,CAAE,CAAA,EAAE,SAAS;AAC7E;AAEA,SAASwC,EAAaa,GAAuB;AAclC,SAbYA,EACd,QAAQ,WAAW,GAAG,EACtB,QAAQ,mBAAmB,OAAO,EAClC,YAAY,EAGQ,MAAM,GAAG,EAI7B,IAAI,CAAQC,MAAAA,EAAK,OAAO,CAAC,EAAE,YAAY,IAAIA,EAAK,MAAM,CAAC,CAAC,EACxD,KAAK,GAAG;AAGjB;AAEO,SAASC,EAAmB7F,GAAsB;AACrD,SAAI,OAAOA,KAAU,WAAiB,WAClC,OAAOA,KAAU,WAAiB,WAClC,OAAOA,KAAU,YAAkB,YACnC,MAAM,QAAQA,CAAK,IAAU,UAC7B,OAAOA,KAAU,WAAiB,QAC/B;AACX;"}
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,\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(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>, priorityKeys?: string[]): 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 === \"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 = formatString(key.toLowerCase());\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\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\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","priorityKeys","lowerCasePriorityKeys","propOrder","k","keys","type","fieldValue","mapTypesCount","arrayTypesCount","arrayType","getMostProbableTypeInArray","typesCountRecord","typeValuesRecord","dataType","valuesRecord","mapValuesRecord","getHighestTypesCount","highestCount","count","countValue","getHighestRecordCount","record","getMostProbableType","probableType","mostProbableType","title","formatString","result","checkTypesCountHighVariability","arrayMostProbableType","of","propertyProps","validation","valuesCountRecord","res","countMaxDocumentsUnder","array","maxCount","keysWithFewValues","input","word","inferTypeFromValue"],"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,MAEpC,IACd,EAAE,OAAO,OAAO;AACV,SAAAS,EAAA,KAAK,CAACE,GAAGC,MAAMD,EAAE,MAAM,cAAcC,EAAE,KAAK,CAAC,GACjDH;AACX;ACVA,MAAMI,IAAmB,CAAC,QAAQ,SAAS,QAAQ,SAAS,QAAQ,OAAO,GACrEC,IAAmB,CAAC,QAAQ,QAAQ,SAAS,MAAM,GACnDC,IAAmB,CAAC,QAAQ,MA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AAA8C,EAAkBT,EAAQrC,CAAK,GAAGsC,GAAYtC,GAAOqC,CAAO;AAAA,EAAA,CAC/D,GACMuC,EAAoBtC,CAAU;AACzC;AAEA,SAAS4C,EAA+B5C,GAAwB;AACtD,QAAAqD,IAAWF,EAAuBnD,CAAU;AAClD,MAAIsD,IAAoB;AACjB,gBAAA,QAAQtD,EAAW,OAAO,CAAA,CAAE,EAC9B,QAAQ,CAAC,CAACE,GAAKxC,CAAK,MAAM;AAEnB,IADUyF,EAAuBzF,CAAK,IAC9B2F,IAAW,KACnBC;AAAA,EACJ,CACH,GACEA,IAAoB,OAAO,QAAQtD,EAAW,OAAO,CAAE,CAAA,EAAE,SAAS;AAC7E;AAEA,SAAS0C,EAAaa,GAAuB;AAclC,SAbYA,EACd,QAAQ,WAAW,GAAG,EACtB,QAAQ,mBAAmB,OAAO,EAClC,YAAY,EAGQ,MAAM,GAAG,EAI7B,IAAI,CAAQC,MAAAA,EAAK,OAAO,CAAC,EAAE,YAAY,IAAIA,EAAK,MAAM,CAAC,CAAC,EACxD,KAAK,GAAG;AAGjB;AAEO,SAASC,EAAmB/F,GAAsB;AACrD,SAAI,OAAOA,KAAU,WAAiB,WAClC,OAAOA,KAAU,WAAiB,WAClC,OAAOA,KAAU,YAAkB,YACnC,MAAM,QAAQA,CAAK,IAAU,UAC7B,OAAOA,KAAU,WAAiB,QAC/B;AACX;"}
package/dist/index.umd.js CHANGED
@@ -1,2 +1,2 @@
1
- (function(c,l){typeof exports=="object"&&typeof module<"u"?l(exports,require("@firecms/core"),require("firebase/firestore")):typeof define=="function"&&define.amd?define(["exports","@firecms/core","firebase/firestore"],l):(c=typeof globalThis<"u"?globalThis:c||self,l(c["FireCMS schema inference"]={},c.core,c.firestore))})(this,function(c,l,j){"use strict";function E(r){if(!r)return;function e(i){return typeof i=="string"?i:i instanceof j.DocumentReference?i.path:void 0}const s=r.values.map(i=>e(i)).filter(i=>!!i).find(i=>i.includes("/"));if(!s)return;const n=s.substr(0,s.lastIndexOf("/"));return r.values.filter(i=>{const a=e(i);return a?a.startsWith(n):!1}).length>r.values.length/3*2?n:void 0}function p(r){if(!Array.isArray(r))return[];const e=r.map(t=>typeof t=="string"?{id:t,label:l.unslugify(t)}:null).filter(Boolean);return e.sort((t,s)=>t.label.localeCompare(s.label)),e}const P=[".jpg",".png",".webp",".gif"],S=[".mp3",".ogg",".opus",".aac"],F=[".avi",".mp4"],C=/^[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])?)*$/;function I({totalDocsCount:r,valuesResult:e}){let t={dataType:"string"};if(e){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;i&&(o.url=!0);const a=e.values.filter(f=>typeof f=="string"&&C.test(f)).length>r/3*2;a&&(o.email=!0);const u=e.values.filter(f=>typeof f=="string"&&f.length===28&&!f.includes(" ")).length>r/3*2;if(u&&(o.readOnly=!0),!a&&!i&&!u&&!i&&n<s/3){const f=p(Array.from(e.valuesCount.keys()));Object.keys(f).length>1&&(o.enumValues=f)}if(!a&&!i&&!u&&!i&&!o.enumValues){const f=M(e,r);f&&(o.storage={acceptedFiles:[f],storagePath:E(e)??"/"})}Object.keys(o).length>0&&(t={...t,...o,editable:!0})}return t}function M(r,e){const t=r.values.filter(i=>typeof i=="string"&&P.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"&&F.some(a=>i.toString().endsWith(a))).length>e/3*2;return t?"image/*":s?"audio/*":n?"video/*":!1}function V({totalDocsCount:r,valuesResult:e}){if(e){const t=e.values.length;if(r===t)return{required:!0}}}function z({totalDocsCount:r,valuesResult:e}){return{dataType:"reference",path:E(e)??"!!!FIX_ME!!!",editable:!0}}async function _(r,e){const t={},s={};return r&&r.forEach(n=>{n&&Object.entries(n).forEach(([o,i])=>{A(t,o,i,e),h(s,o,i,e)})}),T(r.length,t,s)}function w(r,e,t){const s={},n={};r&&r.forEach(a=>{m(e.dataType,s,a,t),h(n,"inferred_prop",a,t)});const o="enumValues"in e?l.resolveEnumValues(e.enumValues):void 0;if(o){const a=p(Array.from(n.inferred_prop.valuesCount.keys()));return{...e,enumValues:[...a,...o]}}const i=y("inferred_prop",r.length,e.dataType,s,n.inferred_prop);return l.mergeDeep(i,e)}function Z(r){function e(s){const n=s.toLowerCase();return n==="title"||n==="name"?3:n.includes("title")||n.includes("name")?2:n.includes("image")||n.includes("picture")?1:0}const t=Object.keys(r);return t.sort(),t.sort((s,n)=>e(n)-e(s)),t}function m(r,e,t,s){if(r==="map"){if(t){let n=e[r];n||(n={},e[r]=n),Object.entries(t).forEach(([o,i])=>{A(n,o,i,s)})}}else if(r==="array"){let n=e[r];if(n||(n={},e[r]=n),t&&Array.isArray(t)&&t.length>0){const o=x(t,s);n[o]?n[o]++:n[o]=1}}else e[r]?e[r]++:e[r]=1}function A(r,e,t,s){let n=r[e];if(n||(n={},r[e]=n),t!=null){const o=s(t);m(o,n,t,s)}}function h(r,e,t,s){const n=s(t);let o=r[e];if(o||(o={values:[],valuesCount:new Map},r[e]=o),n==="map"){let i=o.map;i||(i={},o.map=i),t&&Object.entries(t).forEach(([a,u])=>h(i,a,u,s))}else n==="array"?Array.isArray(t)&&t.forEach(i=>{o.values.push(i),o.valuesCount.set(i,(o.valuesCount.get(i)??0)+1)}):t&&(o.values.push(t),o.valuesCount.set(t,(o.valuesCount.get(t)??0)+1))}function g(r){let e=0;return Object.entries(r).forEach(([t,s])=>{let n=0;t==="map"?n=O(s):t==="array"?n=g(s):n=s,n>e&&(e=n)}),e}function O(r){return Object.entries(r).map(([e,t])=>g(t)).reduce((e,t)=>Math.max(e,t),0)}function d(r){let e=-1,t="string";return Object.entries(r).forEach(([s,n])=>{let o;s==="map"?o=O(n):s==="array"?o=g(n):o=n,o>e&&(e=o,t=s)}),t}function y(r,e,t,s,n){let o;r&&(o=N(r.toLowerCase()));let i;if(t==="map"){W(s)&&(i={dataType:"map",name:o,keyValue:!0,properties:{}});const u=T(e,s.map,n?n.mapValues:void 0);i={dataType:"map",name:o,properties:u}}else if(t==="array"){const a=s.array,u=d(a),f=y(r,e,u,a,n);i={dataType:"array",name:o,of:f}}if(!i){const a={name:r,totalDocsCount:e,valuesResult:n};t==="string"?i=I(a):t==="reference"?i=z(a):i={dataType:t},o&&(i.name=o);const u=V(a);u&&(i.validation=u)}return{...i,editable:!0}}function T(r,e,t){const s={};return Object.entries(e).forEach(([n,o])=>{const i=d(o);s[n]=y(n,r,i,o,t?t[n]:void 0)}),s}function b(r){let e=0;return Object.entries(r).forEach(([t,s])=>{typeof s=="object"?e=Math.max(e,b(s)):e=Math.max(e,s)}),e}function x(r,e){let t={};return r.forEach(s=>{m(e(s),t,s,e)}),d(t)}function W(r){const e=b(r);let t=0;return Object.entries(r.map??{}).forEach(([s,n])=>{b(n)<e/3&&t++}),t/Object.entries(r.map??{}).length>.5}function N(r){return r.replace(/[_\-]+/g," ").replace(/([a-z])([A-Z])/g,"$1 $2").toLowerCase().split(" ").map(n=>n.charAt(0).toUpperCase()+n.slice(1)).join(" ")}function k(r){return typeof r=="string"?"string":typeof r=="number"?"number":typeof r=="boolean"?"boolean":Array.isArray(r)?"array":typeof r=="object"?"map":"string"}c.buildEntityPropertiesFromData=_,c.buildPropertiesOrder=Z,c.buildPropertyFromData=w,c.extractEnumFromValues=p,c.inferTypeFromValue=k,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
1
+ (function(c,l){typeof exports=="object"&&typeof module<"u"?l(exports,require("@firecms/core"),require("firebase/firestore")):typeof define=="function"&&define.amd?define(["exports","@firecms/core","firebase/firestore"],l):(c=typeof globalThis<"u"?globalThis:c||self,l(c["FireCMS schema inference"]={},c.core,c.firestore))})(this,function(c,l,j){"use strict";function E(r){if(!r)return;function e(n){return typeof n=="string"?n:n instanceof j.DocumentReference?n.path:void 0}const s=r.values.map(n=>e(n)).filter(n=>!!n).find(n=>n.includes("/"));if(!s)return;const i=s.substr(0,s.lastIndexOf("/"));return r.values.filter(n=>{const a=e(n);return a?a.startsWith(i):!1}).length>r.values.length/3*2?i:void 0}function p(r){if(!Array.isArray(r))return[];const e=r.map(t=>typeof t=="string"?{id:t,label:l.unslugify(t)}:null).filter(Boolean);return e.sort((t,s)=>t.label.localeCompare(s.label)),e}const P=[".jpg",".jpeg",".png",".webp",".gif",".avif"],S=[".mp3",".ogg",".opus",".aac"],C=[".avi",".mp4"],F=/^[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])?)*$/;function I({totalDocsCount:r,valuesResult:e}){let t={dataType:"string"};if(e){const s=e.values.length,i=Array.from(e.valuesCount.keys()).length,o={},n=e.values.filter(f=>typeof f=="string"&&f.toString().startsWith("http")).length>r/3*2;n&&(o.url=!0);const a=e.values.filter(f=>typeof f=="string"&&F.test(f)).length>r/3*2;a&&(o.email=!0);const u=e.values.filter(f=>typeof f=="string"&&f.length===28&&!f.includes(" ")).length>r/3*2;if(u&&(o.readOnly=!0),!a&&!n&&!u&&!n&&i<s/3){const f=p(Array.from(e.valuesCount.keys()));Object.keys(f).length>1&&(o.enumValues=f)}if(!a&&!n&&!u&&!n&&!o.enumValues){const f=M(e,r);f&&(o.storage={acceptedFiles:[f],storagePath:E(e)??"/"})}Object.keys(o).length>0&&(t={...t,...o,editable:!0})}return t}function M(r,e){const t=r.values.filter(n=>typeof n=="string"&&P.some(a=>n.toString().endsWith(a))).length>e/3*2,s=r.values.filter(n=>typeof n=="string"&&S.some(a=>n.toString().endsWith(a))).length>e/3*2,i=r.values.filter(n=>typeof n=="string"&&C.some(a=>n.toString().endsWith(a))).length>e/3*2;return t?"image/*":s?"audio/*":i?"video/*":!1}function V({totalDocsCount:r,valuesResult:e}){if(e){const t=e.values.length;if(r===t)return{required:!0}}}function w({totalDocsCount:r,valuesResult:e}){return{dataType:"reference",path:E(e)??"!!!FIX_ME!!!",editable:!0}}async function z(r,e){const t={},s={};return r&&r.forEach(i=>{i&&Object.entries(i).forEach(([o,n])=>{A(t,o,n,e),h(s,o,n,e)})}),T(r.length,t,s)}function _(r,e,t){const s={},i={};r&&r.forEach(a=>{m(e.dataType,s,a,t),h(i,"inferred_prop",a,t)});const o="enumValues"in e?l.resolveEnumValues(e.enumValues):void 0;if(o){const a=p(Array.from(i.inferred_prop.valuesCount.keys()));return{...e,enumValues:[...a,...o]}}const n=y("inferred_prop",r.length,e.dataType,s,i.inferred_prop);return l.mergeDeep(n,e)}function Z(r,e){const t=(e??[]).map(o=>o.toLowerCase());function s(o){const n=o.toLowerCase();return t.includes(n)?4:n==="title"||n==="name"||n==="title"||n==="name"?3:n.includes("title")||n.includes("name")?2:n.includes("image")||n.includes("picture")?1:0}const i=Object.keys(r);return i.sort(),i.sort((o,n)=>s(n)-s(o)),i}function m(r,e,t,s){if(r==="map"){if(t){let i=e[r];i||(i={},e[r]=i),Object.entries(t).forEach(([o,n])=>{A(i,o,n,s)})}}else if(r==="array"){let i=e[r];if(i||(i={},e[r]=i),t&&Array.isArray(t)&&t.length>0){const o=x(t,s);i[o]?i[o]++:i[o]=1}}else e[r]?e[r]++:e[r]=1}function A(r,e,t,s){let i=r[e];if(i||(i={},r[e]=i),t!=null){const o=s(t);m(o,i,t,s)}}function h(r,e,t,s){const i=s(t);let o=r[e];if(o||(o={values:[],valuesCount:new Map},r[e]=o),i==="map"){let n=o.map;n||(n={},o.map=n),t&&Object.entries(t).forEach(([a,u])=>h(n,a,u,s))}else i==="array"?Array.isArray(t)&&t.forEach(n=>{o.values.push(n),o.valuesCount.set(n,(o.valuesCount.get(n)??0)+1)}):t&&(o.values.push(t),o.valuesCount.set(t,(o.valuesCount.get(t)??0)+1))}function g(r){let e=0;return Object.entries(r).forEach(([t,s])=>{let i=0;t==="map"?i=O(s):t==="array"?i=g(s):i=s,i>e&&(e=i)}),e}function O(r){return Object.entries(r).map(([e,t])=>g(t)).reduce((e,t)=>Math.max(e,t),0)}function d(r){let e=-1,t="string";return Object.entries(r).forEach(([s,i])=>{let o;s==="map"?o=O(i):s==="array"?o=g(i):o=i,o>e&&(e=o,t=s)}),t}function y(r,e,t,s,i){let o;r&&(o=N(r.toLowerCase()));let n;if(t==="map"){W(s)&&(n={dataType:"map",name:o,keyValue:!0,properties:{}});const u=T(e,s.map,i?i.mapValues:void 0);n={dataType:"map",name:o,properties:u}}else if(t==="array"){const a=s.array,u=d(a),f=y(r,e,u,a,i);n={dataType:"array",name:o,of:f}}if(!n){const a={name:r,totalDocsCount:e,valuesResult:i};t==="string"?n=I(a):t==="reference"?n=w(a):n={dataType:t},o&&(n.name=o);const u=V(a);u&&(n.validation=u)}return{...n,editable:!0}}function T(r,e,t){const s={};return Object.entries(e).forEach(([i,o])=>{const n=d(o);s[i]=y(i,r,n,o,t?t[i]:void 0)}),s}function b(r){let e=0;return Object.entries(r).forEach(([t,s])=>{typeof s=="object"?e=Math.max(e,b(s)):e=Math.max(e,s)}),e}function x(r,e){let t={};return r.forEach(s=>{m(e(s),t,s,e)}),d(t)}function W(r){const e=b(r);let t=0;return Object.entries(r.map??{}).forEach(([s,i])=>{b(i)<e/3&&t++}),t/Object.entries(r.map??{}).length>.5}function N(r){return r.replace(/[_\-]+/g," ").replace(/([a-z])([A-Z])/g,"$1 $2").toLowerCase().split(" ").map(i=>i.charAt(0).toUpperCase()+i.slice(1)).join(" ")}function k(r){return typeof r=="string"?"string":typeof r=="number"?"number":typeof r=="boolean"?"boolean":Array.isArray(r)?"array":typeof r=="object"?"map":"string"}c.buildEntityPropertiesFromData=z,c.buildPropertiesOrder=Z,c.buildPropertyFromData=_,c.extractEnumFromValues=p,c.inferTypeFromValue=k,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
2
2
  //# sourceMappingURL=index.umd.js.map
@@ -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} 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 = formatString(key.toLowerCase());\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\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\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","formatString","result","checkTypesCountHighVariability","arrayMostProbableType","of","propertyProps","validation","valuesCountRecord","res","countMaxDocumentsUnder","array","maxCount","keysWithFewValues","input","word","inferTypeFromValue"],"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,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AGQ,MAAM,GAAG,EAI7B,IAAYC,GAAAA,EAAK,OAAO,CAAC,EAAE,YAAY,EAAIA,EAAK,MAAM,CAAC,CAAC,EACxD,KAAK,GAAG,CAGjB,CAEO,SAASC,EAAmB7F,EAAsB,CACrD,OAAI,OAAOA,GAAU,SAAiB,SAClC,OAAOA,GAAU,SAAiB,SAClC,OAAOA,GAAU,UAAkB,UACnC,MAAM,QAAQA,CAAK,EAAU,QAC7B,OAAOA,GAAU,SAAiB,MAC/B,QACX"}
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,\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(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>, priorityKeys?: string[]): 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 === \"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 = formatString(key.toLowerCase());\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\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\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","priorityKeys","lowerCasePriorityKeys","propOrder","k","keys","type","fieldValue","mapTypesCount","arrayTypesCount","arrayType","getMostProbableTypeInArray","typesCountRecord","typeValuesRecord","dataType","valuesRecord","mapValuesRecord","getHighestTypesCount","highestCount","count","countValue","getHighestRecordCount","record","getMostProbableType","probableType","mostProbableType","title","formatString","result","checkTypesCountHighVariability","arrayMostProbableType","of","propertyProps","validation","valuesCountRecord","res","countMaxDocumentsUnder","array","maxCount","keysWithFewValues","input","word","inferTypeFromValue"],"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,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QAAS1F,GAAU,CACrB8C,EAAkBT,EAAQrC,CAAK,EAAGsC,EAAYtC,EAAOqC,CAAO,CAAA,CAC/D,EACMuC,EAAoBtC,CAAU,CACzC,CAEA,SAAS4C,EAA+B5C,EAAwB,CACtD,MAAAqD,EAAWF,EAAuBnD,CAAU,EAClD,IAAIsD,EAAoB,EACjB,cAAA,QAAQtD,EAAW,KAAO,CAAA,CAAE,EAC9B,QAAQ,CAAC,CAACE,EAAKxC,CAAK,IAAM,CACTyF,EAAuBzF,CAAK,EAC9B2F,EAAW,GACnBC,GACJ,CACH,EACEA,EAAoB,OAAO,QAAQtD,EAAW,KAAO,CAAE,CAAA,EAAE,OAAS,EAC7E,CAEA,SAAS0C,EAAaa,EAAuB,CAclC,OAbYA,EACd,QAAQ,UAAW,GAAG,EACtB,QAAQ,kBAAmB,OAAO,EAClC,YAAY,EAGQ,MAAM,GAAG,EAI7B,IAAYC,GAAAA,EAAK,OAAO,CAAC,EAAE,YAAY,EAAIA,EAAK,MAAM,CAAC,CAAC,EACxD,KAAK,GAAG,CAGjB,CAEO,SAASC,EAAmB/F,EAAsB,CACrD,OAAI,OAAOA,GAAU,SAAiB,SAClC,OAAOA,GAAU,SAAiB,SAClC,OAAOA,GAAU,UAAkB,UACnC,MAAM,QAAQA,CAAK,EAAU,QAC7B,OAAOA,GAAU,SAAiB,MAC/B,QACX"}
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "3.0.0-canary.43",
2
+ "version": "3.0.0-canary.45",
3
3
  "type": "module",
4
4
  "name": "@firecms/schema_inference",
5
5
  "access": "public",
@@ -8,7 +8,7 @@
8
8
  "types": "dist/index.d.ts",
9
9
  "source": "src/index.ts",
10
10
  "devDependencies": {
11
- "@firecms/core": "^3.0.0-canary.43",
11
+ "@firecms/core": "^3.0.0-canary.45",
12
12
  "@types/node": "^20.12.7",
13
13
  "typescript": "^5.4.5",
14
14
  "vite": "^5.2.9"
@@ -30,5 +30,5 @@
30
30
  "build": "vite build && tsc --emitDeclarationOnly -p tsconfig.prod.json",
31
31
  "clean": "rm -rf dist && find ./src -name '*.js' -type f | xargs rm -f"
32
32
  },
33
- "gitHead": "fedcb0d43c504245dd76b702e4ab4479fa8592df"
33
+ "gitHead": "7f22bfa3bb20479ad23becd9c10f78cac1a3bc32"
34
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
@@ -59,9 +59,13 @@ export function buildPropertyFromData(data: any[], property: Property, getType:
59
59
  return mergeDeep(generatedProperty, property);
60
60
  }
61
61
 
62
- export function buildPropertiesOrder(properties: Properties<any>): string [] {
62
+ export function buildPropertiesOrder(properties: Properties<any>, priorityKeys?: string[]): string [] {
63
+ const lowerCasePriorityKeys = (priorityKeys ?? []).map((key) => key.toLowerCase());
64
+
63
65
  function propOrder(s: string) {
64
66
  const k = s.toLowerCase();
67
+ if (lowerCasePriorityKeys.includes(k)) return 4;
68
+ if (k === "title" || k === "name") return 3;
65
69
  if (k === "title" || k === "name") return 3;
66
70
  if (k.includes("title") || k.includes("name")) return 2;
67
71
  if (k.includes("image") || k.includes("picture")) return 1;