@rebasepro/schema-inference 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.es.js +370 -473
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.cjs +469 -540
- package/dist/index.umd.cjs.map +1 -1
- package/package.json +9 -9
- package/src/builders/string_property_builder.ts +2 -1
package/dist/index.umd.cjs
CHANGED
|
@@ -1,541 +1,470 @@
|
|
|
1
1
|
(function(global, factory) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
if (typeof value === "object") {
|
|
472
|
-
count = Math.max(count, countMaxDocumentsUnder(value));
|
|
473
|
-
} else {
|
|
474
|
-
count = Math.max(count, Number(value));
|
|
475
|
-
}
|
|
476
|
-
});
|
|
477
|
-
return count;
|
|
478
|
-
}
|
|
479
|
-
function getMostProbableTypeInArray(array, getType) {
|
|
480
|
-
const typesCount = {};
|
|
481
|
-
array.forEach((value) => {
|
|
482
|
-
increaseTypeCount(getType(value), typesCount, value, getType);
|
|
483
|
-
});
|
|
484
|
-
return getMostProbableType(typesCount);
|
|
485
|
-
}
|
|
486
|
-
function checkTypesCountHighVariability(typesCount) {
|
|
487
|
-
const maxCount = countMaxDocumentsUnder(typesCount);
|
|
488
|
-
let keysWithFewValues = 0;
|
|
489
|
-
Object.entries(typesCount.map ?? {}).forEach(([key, value]) => {
|
|
490
|
-
const count = countMaxDocumentsUnder(value);
|
|
491
|
-
if (count < maxCount / 3) {
|
|
492
|
-
keysWithFewValues++;
|
|
493
|
-
}
|
|
494
|
-
});
|
|
495
|
-
return keysWithFewValues / Object.entries(typesCount.map ?? {}).length > 0.5;
|
|
496
|
-
}
|
|
497
|
-
function inferTypeFromValue(value) {
|
|
498
|
-
if (value === null || value === void 0) return "string";
|
|
499
|
-
if (value instanceof types.Vector || value && typeof value === "object" && "__type" in value && value.__type === "Vector") return "vector";
|
|
500
|
-
if (typeof value === "string") return "string";
|
|
501
|
-
if (typeof value === "number") return "number";
|
|
502
|
-
if (typeof value === "boolean") return "boolean";
|
|
503
|
-
if (Array.isArray(value)) return "array";
|
|
504
|
-
if (typeof value === "object") return "map";
|
|
505
|
-
return "string";
|
|
506
|
-
}
|
|
507
|
-
Object.defineProperty(exports2, "isObject", {
|
|
508
|
-
enumerable: true,
|
|
509
|
-
get: () => utils.isObject
|
|
510
|
-
});
|
|
511
|
-
Object.defineProperty(exports2, "isPlainObject", {
|
|
512
|
-
enumerable: true,
|
|
513
|
-
get: () => utils.isPlainObject
|
|
514
|
-
});
|
|
515
|
-
Object.defineProperty(exports2, "mergeDeep", {
|
|
516
|
-
enumerable: true,
|
|
517
|
-
get: () => utils.mergeDeep
|
|
518
|
-
});
|
|
519
|
-
Object.defineProperty(exports2, "prettifyIdentifier", {
|
|
520
|
-
enumerable: true,
|
|
521
|
-
get: () => utils.prettifyIdentifier
|
|
522
|
-
});
|
|
523
|
-
Object.defineProperty(exports2, "unslugify", {
|
|
524
|
-
enumerable: true,
|
|
525
|
-
get: () => utils.unslugify
|
|
526
|
-
});
|
|
527
|
-
exports2.buildEntityPropertiesFromData = buildEntityPropertiesFromData;
|
|
528
|
-
exports2.buildPropertiesOrder = buildPropertiesOrder;
|
|
529
|
-
exports2.buildPropertyFromData = buildPropertyFromData;
|
|
530
|
-
exports2.extractEnumFromValues = extractEnumFromValues;
|
|
531
|
-
exports2.findCommonInitialStringInPath = findCommonInitialStringInPath;
|
|
532
|
-
exports2.inferTypeFromValue = inferTypeFromValue;
|
|
533
|
-
exports2.looksLikeReference = looksLikeReference;
|
|
534
|
-
exports2.parseReferenceString = parseReferenceString;
|
|
535
|
-
exports2.removeInitialAndTrailingSlashes = removeInitialAndTrailingSlashes;
|
|
536
|
-
exports2.removeInitialSlash = removeInitialSlash;
|
|
537
|
-
exports2.removeTrailingSlash = removeTrailingSlash;
|
|
538
|
-
exports2.resolveEnumValues = resolveEnumValues;
|
|
539
|
-
Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
|
|
540
|
-
}));
|
|
541
|
-
//# sourceMappingURL=index.umd.cjs.map
|
|
2
|
+
typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("@rebasepro/utils"), require("@rebasepro/types")) : typeof define === "function" && define.amd ? define([
|
|
3
|
+
"exports",
|
|
4
|
+
"@rebasepro/utils",
|
|
5
|
+
"@rebasepro/types"
|
|
6
|
+
], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global["Rebase schema inference"] = {}, global._rebasepro_utils, global._rebasepro_types));
|
|
7
|
+
})(this, function(exports, _rebasepro_utils, _rebasepro_types) {
|
|
8
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
9
|
+
//#region src/strings.ts
|
|
10
|
+
/**
|
|
11
|
+
* Parse a reference string value which can be in the format:
|
|
12
|
+
* - Simple: "path/entityId"
|
|
13
|
+
* - With database: "database_name:::path/entityId"
|
|
14
|
+
* Returns the path and database (undefined if not specified or if "(default)")
|
|
15
|
+
*/
|
|
16
|
+
function parseReferenceString(value) {
|
|
17
|
+
if (!value) return null;
|
|
18
|
+
let database = void 0;
|
|
19
|
+
let fullPath = value;
|
|
20
|
+
if (value.includes(":::")) {
|
|
21
|
+
const [dbName, pathPart] = value.split(":::");
|
|
22
|
+
if (dbName && dbName !== "(default)") database = dbName;
|
|
23
|
+
fullPath = pathPart;
|
|
24
|
+
}
|
|
25
|
+
if (!fullPath || !fullPath.includes("/")) return null;
|
|
26
|
+
return {
|
|
27
|
+
path: fullPath.substring(0, fullPath.lastIndexOf("/")),
|
|
28
|
+
database
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Check if a string value looks like a reference
|
|
33
|
+
*/
|
|
34
|
+
function looksLikeReference(value) {
|
|
35
|
+
if (typeof value !== "string") return false;
|
|
36
|
+
return parseReferenceString(value) !== null;
|
|
37
|
+
}
|
|
38
|
+
function findCommonInitialStringInPath(valuesCount) {
|
|
39
|
+
if (!valuesCount) return void 0;
|
|
40
|
+
function getPath(value) {
|
|
41
|
+
let pathString;
|
|
42
|
+
if (typeof value === "string") pathString = value;
|
|
43
|
+
else if (value && typeof value === "object" && "slug" in value && typeof value.slug === "string") pathString = value.slug;
|
|
44
|
+
else {
|
|
45
|
+
console.warn("findCommonInitialStringInPath: value is not a string or document with path", value);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (!pathString) return void 0;
|
|
49
|
+
if (pathString.includes(":::")) {
|
|
50
|
+
const [, pathPart] = pathString.split(":::");
|
|
51
|
+
pathString = pathPart;
|
|
52
|
+
}
|
|
53
|
+
return pathString;
|
|
54
|
+
}
|
|
55
|
+
const pathWithSlash = valuesCount.values.map((v) => getPath(v)).filter((v) => !!v).find((s) => s.includes("/"));
|
|
56
|
+
if (!pathWithSlash) return void 0;
|
|
57
|
+
const searchedPath = pathWithSlash.substring(0, pathWithSlash.lastIndexOf("/"));
|
|
58
|
+
return valuesCount.values.filter((value) => {
|
|
59
|
+
const path = getPath(value);
|
|
60
|
+
if (!path) return false;
|
|
61
|
+
return path.startsWith(searchedPath);
|
|
62
|
+
}).length > valuesCount.values.length / 3 * 2 ? searchedPath : void 0;
|
|
63
|
+
}
|
|
64
|
+
function removeInitialAndTrailingSlashes(s) {
|
|
65
|
+
return removeInitialSlash(removeTrailingSlash(s));
|
|
66
|
+
}
|
|
67
|
+
function removeInitialSlash(s) {
|
|
68
|
+
if (s.startsWith("/")) return s.slice(1);
|
|
69
|
+
else return s;
|
|
70
|
+
}
|
|
71
|
+
function removeTrailingSlash(s) {
|
|
72
|
+
if (s.endsWith("/")) return s.slice(0, -1);
|
|
73
|
+
else return s;
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region src/util.ts
|
|
77
|
+
/**
|
|
78
|
+
* Extract enum values from a list of sample values.
|
|
79
|
+
* This is schema-inference-specific logic (not a general utility).
|
|
80
|
+
*/
|
|
81
|
+
function extractEnumFromValues(values) {
|
|
82
|
+
if (!Array.isArray(values)) return [];
|
|
83
|
+
const enumValues = values.map((value) => {
|
|
84
|
+
if (typeof value === "string") return {
|
|
85
|
+
id: value,
|
|
86
|
+
label: (0, _rebasepro_utils.unslugify)(value)
|
|
87
|
+
};
|
|
88
|
+
else return null;
|
|
89
|
+
}).filter(Boolean);
|
|
90
|
+
enumValues.sort((a, b) => a.label.localeCompare(b.label));
|
|
91
|
+
return enumValues;
|
|
92
|
+
}
|
|
93
|
+
function resolveEnumValues(input) {
|
|
94
|
+
if (Array.isArray(input)) return input;
|
|
95
|
+
else if (typeof input === "object" && input !== null) return Object.entries(input).map(([id, value]) => typeof value === "string" ? {
|
|
96
|
+
id,
|
|
97
|
+
label: value
|
|
98
|
+
} : value);
|
|
99
|
+
else return;
|
|
100
|
+
}
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region src/builders/string_property_builder.ts
|
|
103
|
+
var IMAGE_EXTENSIONS = [
|
|
104
|
+
".jpg",
|
|
105
|
+
".jpeg",
|
|
106
|
+
".png",
|
|
107
|
+
".webp",
|
|
108
|
+
".gif",
|
|
109
|
+
".avif"
|
|
110
|
+
];
|
|
111
|
+
var AUDIO_EXTENSIONS = [
|
|
112
|
+
".mp3",
|
|
113
|
+
".ogg",
|
|
114
|
+
".opus",
|
|
115
|
+
".aac"
|
|
116
|
+
];
|
|
117
|
+
var VIDEO_EXTENSIONS = [".avi", ".mp4"];
|
|
118
|
+
var 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])?)*$/;
|
|
119
|
+
function buildStringProperty({ name, totalDocsCount, valuesResult }) {
|
|
120
|
+
let stringProperty = {
|
|
121
|
+
name: name ?? "",
|
|
122
|
+
type: "string"
|
|
123
|
+
};
|
|
124
|
+
if (valuesResult) {
|
|
125
|
+
const totalEntriesCount = valuesResult.values.length;
|
|
126
|
+
const totalValues = Array.from(valuesResult.valuesCount.keys()).length;
|
|
127
|
+
const config = {};
|
|
128
|
+
const probablyAURL = valuesResult.values.filter((value) => typeof value === "string" && value.toString().startsWith("http")).length > totalDocsCount / 3 * 2;
|
|
129
|
+
if (probablyAURL) config.ui = { url: true };
|
|
130
|
+
const probablyAnEmail = valuesResult.values.filter((value) => typeof value === "string" && emailRegEx.test(value)).length > totalDocsCount / 3 * 2;
|
|
131
|
+
if (probablyAnEmail) config.email = true;
|
|
132
|
+
const probablyUserIds = valuesResult.values.filter((value) => typeof value === "string" && value.length === 28 && !value.includes(" ")).length > totalDocsCount / 3 * 2;
|
|
133
|
+
if (probablyUserIds) config.ui = {
|
|
134
|
+
...config.ui,
|
|
135
|
+
readOnly: true
|
|
136
|
+
};
|
|
137
|
+
if (!probablyAnEmail && !probablyAURL && !probablyUserIds && !probablyAURL && totalValues < totalEntriesCount / 3) {
|
|
138
|
+
const enumValues = extractEnumFromValues(Array.from(valuesResult.valuesCount.keys()));
|
|
139
|
+
if (Object.keys(enumValues).length > 1) config.enum = enumValues;
|
|
140
|
+
}
|
|
141
|
+
if (!probablyAnEmail && !probablyAURL && !probablyUserIds && !probablyAURL && !config.enum) {
|
|
142
|
+
const fileType = probableFileType(valuesResult, totalDocsCount);
|
|
143
|
+
if (fileType) config.storage = {
|
|
144
|
+
acceptedFiles: fileType,
|
|
145
|
+
storagePath: findCommonInitialStringInPath(valuesResult) ?? "/"
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
if (Object.keys(config).length > 0) stringProperty = {
|
|
149
|
+
...stringProperty,
|
|
150
|
+
...config
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
return stringProperty;
|
|
154
|
+
}
|
|
155
|
+
function probableFileType(valuesCount, totalDocsCount) {
|
|
156
|
+
const isImage = (value) => IMAGE_EXTENSIONS.some((extension) => value.toString().endsWith(extension));
|
|
157
|
+
const isAudio = (value) => AUDIO_EXTENSIONS.some((extension) => value.toString().endsWith(extension));
|
|
158
|
+
const isVideo = (value) => VIDEO_EXTENSIONS.some((extension) => value.toString().endsWith(extension));
|
|
159
|
+
const stringValues = valuesCount.values.filter((v) => typeof v === "string");
|
|
160
|
+
let imageCount = 0;
|
|
161
|
+
let audioCount = 0;
|
|
162
|
+
let videoCount = 0;
|
|
163
|
+
for (const value of stringValues) if (isImage(value)) imageCount++;
|
|
164
|
+
else if (isAudio(value)) audioCount++;
|
|
165
|
+
else if (isVideo(value)) videoCount++;
|
|
166
|
+
if (imageCount + audioCount + videoCount > totalDocsCount * 2 / 3) {
|
|
167
|
+
const fileTypes = [];
|
|
168
|
+
if (imageCount > 0) fileTypes.push("image/*");
|
|
169
|
+
if (audioCount > 0) fileTypes.push("audio/*");
|
|
170
|
+
if (videoCount > 0) fileTypes.push("video/*");
|
|
171
|
+
return fileTypes.length > 0 ? fileTypes : false;
|
|
172
|
+
}
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
//#endregion
|
|
176
|
+
//#region src/builders/validation_builder.ts
|
|
177
|
+
function buildValidation({ totalDocsCount, valuesResult }) {
|
|
178
|
+
if (valuesResult) {
|
|
179
|
+
if (totalDocsCount === valuesResult.values.length) return { required: true };
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
//#endregion
|
|
183
|
+
//#region src/builders/reference_property_builder.ts
|
|
184
|
+
function buildReferenceProperty({ name, totalDocsCount, valuesResult }) {
|
|
185
|
+
return {
|
|
186
|
+
name: name ?? "",
|
|
187
|
+
type: "reference",
|
|
188
|
+
path: findCommonInitialStringInPath(valuesResult) ?? "!!!FIX_ME!!!"
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
//#endregion
|
|
192
|
+
//#region src/collection_builder.ts
|
|
193
|
+
async function buildEntityPropertiesFromData(data, getType) {
|
|
194
|
+
const typesCount = {};
|
|
195
|
+
const valuesCount = {};
|
|
196
|
+
if (data) data.forEach((entry) => {
|
|
197
|
+
if (entry) Object.entries(entry).forEach(([key, value]) => {
|
|
198
|
+
if (key.startsWith("_")) return;
|
|
199
|
+
increaseMapTypeCount(typesCount, key, value, getType);
|
|
200
|
+
increaseValuesCount(valuesCount, key, value, getType);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
return buildPropertiesFromCount(data.length, typesCount, valuesCount);
|
|
204
|
+
}
|
|
205
|
+
function buildPropertyFromData(data, property, getType) {
|
|
206
|
+
const typesCount = {};
|
|
207
|
+
const valuesCount = {};
|
|
208
|
+
if (data) data.forEach((entry) => {
|
|
209
|
+
increaseTypeCount(property.type, typesCount, entry, getType);
|
|
210
|
+
increaseValuesCount(valuesCount, "inferred_prop", entry, getType);
|
|
211
|
+
});
|
|
212
|
+
const enumValues = "enum" in property ? resolveEnumValues(property["enum"]) : void 0;
|
|
213
|
+
if (enumValues) {
|
|
214
|
+
const newEnumValues = extractEnumFromValues(Array.from(valuesCount["inferred_prop"].valuesCount.keys()));
|
|
215
|
+
return {
|
|
216
|
+
...property,
|
|
217
|
+
enum: [...newEnumValues, ...enumValues]
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
return (0, _rebasepro_utils.mergeDeep)(buildPropertyFromCount("inferred_prop", data.length, property.type, typesCount, valuesCount["inferred_prop"]), property);
|
|
221
|
+
}
|
|
222
|
+
function buildPropertiesOrder(properties, propertiesOrder, priorityKeys) {
|
|
223
|
+
const lowerCasePriorityKeys = (priorityKeys ?? []).map((key) => key.toLowerCase());
|
|
224
|
+
function propOrder(s) {
|
|
225
|
+
const k = s.toLowerCase();
|
|
226
|
+
if (lowerCasePriorityKeys.includes(k)) return 4;
|
|
227
|
+
if (k === "title" || k === "name") return 3;
|
|
228
|
+
if (k.includes("title") || k.includes("name")) return 2;
|
|
229
|
+
if (k.includes("image") || k.includes("picture")) return 1;
|
|
230
|
+
return 0;
|
|
231
|
+
}
|
|
232
|
+
const keys = propertiesOrder ?? Object.keys(properties);
|
|
233
|
+
keys.sort();
|
|
234
|
+
keys.sort((a, b) => {
|
|
235
|
+
return propOrder(b) - propOrder(a);
|
|
236
|
+
});
|
|
237
|
+
return keys;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* @param type
|
|
241
|
+
* @param typesCount
|
|
242
|
+
* @param fieldValue
|
|
243
|
+
* @param getType
|
|
244
|
+
*/
|
|
245
|
+
function increaseTypeCount(type, typesCount, fieldValue, getType) {
|
|
246
|
+
if (type === "map") {
|
|
247
|
+
if (fieldValue) {
|
|
248
|
+
let mapTypesCount = typesCount[type];
|
|
249
|
+
if (!mapTypesCount) {
|
|
250
|
+
mapTypesCount = {};
|
|
251
|
+
typesCount[type] = mapTypesCount;
|
|
252
|
+
}
|
|
253
|
+
Object.entries(fieldValue).forEach(([key, value]) => {
|
|
254
|
+
increaseMapTypeCount(mapTypesCount, key, value, getType);
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
} else if (type === "array") {
|
|
258
|
+
let arrayTypesCount = typesCount[type];
|
|
259
|
+
if (!arrayTypesCount) {
|
|
260
|
+
arrayTypesCount = {};
|
|
261
|
+
typesCount[type] = arrayTypesCount;
|
|
262
|
+
}
|
|
263
|
+
if (fieldValue && Array.isArray(fieldValue) && fieldValue.length > 0) {
|
|
264
|
+
const arrayType = getMostProbableTypeInArray(fieldValue, getType);
|
|
265
|
+
if (arrayType === "map") {
|
|
266
|
+
let mapTypesCount = arrayTypesCount[arrayType];
|
|
267
|
+
if (!mapTypesCount) mapTypesCount = {};
|
|
268
|
+
fieldValue.forEach((value) => {
|
|
269
|
+
if (value && typeof value === "object" && !Array.isArray(value)) Object.entries(value).forEach(([key, v]) => increaseMapTypeCount(mapTypesCount, key, v, getType));
|
|
270
|
+
});
|
|
271
|
+
arrayTypesCount[arrayType] = mapTypesCount;
|
|
272
|
+
} else if (!arrayTypesCount[arrayType]) arrayTypesCount[arrayType] = 1;
|
|
273
|
+
else arrayTypesCount[arrayType] = Number(arrayTypesCount[arrayType]) + 1;
|
|
274
|
+
}
|
|
275
|
+
} else if (!typesCount[type]) typesCount[type] = 1;
|
|
276
|
+
else typesCount[type] = Number(typesCount[type]) + 1;
|
|
277
|
+
}
|
|
278
|
+
function increaseMapTypeCount(typesCountRecord, key, fieldValue, getType) {
|
|
279
|
+
if (key.startsWith("_")) return;
|
|
280
|
+
let typesCount = typesCountRecord[key];
|
|
281
|
+
if (!typesCount) {
|
|
282
|
+
typesCount = {};
|
|
283
|
+
typesCountRecord[key] = typesCount;
|
|
284
|
+
}
|
|
285
|
+
if (fieldValue != null) increaseTypeCount(getType(fieldValue), typesCount, fieldValue, getType);
|
|
286
|
+
}
|
|
287
|
+
function increaseValuesCount(typeValuesRecord, key, fieldValue, getType) {
|
|
288
|
+
if (key.startsWith("_")) return;
|
|
289
|
+
const type = getType(fieldValue);
|
|
290
|
+
let valuesRecord = typeValuesRecord[key];
|
|
291
|
+
if (!valuesRecord) {
|
|
292
|
+
valuesRecord = {
|
|
293
|
+
values: [],
|
|
294
|
+
valuesCount: /* @__PURE__ */ new Map()
|
|
295
|
+
};
|
|
296
|
+
typeValuesRecord[key] = valuesRecord;
|
|
297
|
+
}
|
|
298
|
+
if (type === "map") {
|
|
299
|
+
let mapValuesRecord = valuesRecord.map;
|
|
300
|
+
if (!mapValuesRecord) {
|
|
301
|
+
mapValuesRecord = {};
|
|
302
|
+
valuesRecord.map = mapValuesRecord;
|
|
303
|
+
}
|
|
304
|
+
if (fieldValue) Object.entries(fieldValue).forEach(([subKey, value]) => increaseValuesCount(mapValuesRecord, subKey, value, getType));
|
|
305
|
+
} else if (type === "array") {
|
|
306
|
+
if (Array.isArray(fieldValue)) fieldValue.forEach((value) => {
|
|
307
|
+
valuesRecord.values.push(value);
|
|
308
|
+
valuesRecord.valuesCount.set(value, (valuesRecord.valuesCount.get(value) ?? 0) + 1);
|
|
309
|
+
});
|
|
310
|
+
} else if (fieldValue !== null && fieldValue !== void 0) {
|
|
311
|
+
valuesRecord.values.push(fieldValue);
|
|
312
|
+
valuesRecord.valuesCount.set(fieldValue, (valuesRecord.valuesCount.get(fieldValue) ?? 0) + 1);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
function getHighestTypesCount(typesCount) {
|
|
316
|
+
let highestCount = 0;
|
|
317
|
+
Object.entries(typesCount).forEach(([type, count]) => {
|
|
318
|
+
let countValue = 0;
|
|
319
|
+
if (type === "map") countValue = getHighestRecordCount(count);
|
|
320
|
+
else if (type === "array") countValue = getHighestTypesCount(count);
|
|
321
|
+
else countValue = Number(count);
|
|
322
|
+
if (countValue > highestCount) highestCount = countValue;
|
|
323
|
+
});
|
|
324
|
+
return highestCount;
|
|
325
|
+
}
|
|
326
|
+
function getHighestRecordCount(record) {
|
|
327
|
+
return Object.entries(record).map(([key, typesCount]) => getHighestTypesCount(typesCount)).reduce((a, b) => Math.max(a, b), 0);
|
|
328
|
+
}
|
|
329
|
+
function getMostProbableType(typesCount) {
|
|
330
|
+
let highestCount = -1;
|
|
331
|
+
let probableType = "string";
|
|
332
|
+
Object.entries(typesCount).forEach(([type, count]) => {
|
|
333
|
+
let countValue;
|
|
334
|
+
if (type === "map") countValue = getHighestRecordCount(count);
|
|
335
|
+
else if (type === "array") countValue = getHighestTypesCount(count);
|
|
336
|
+
else countValue = Number(count);
|
|
337
|
+
if (countValue > highestCount) {
|
|
338
|
+
highestCount = countValue;
|
|
339
|
+
probableType = type;
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
return probableType;
|
|
343
|
+
}
|
|
344
|
+
function buildPropertyFromCount(key, totalDocsCount, mostProbableType, typesCount, valuesResult) {
|
|
345
|
+
let title;
|
|
346
|
+
if (key) title = (0, _rebasepro_utils.prettifyIdentifier)(key);
|
|
347
|
+
let result = void 0;
|
|
348
|
+
if (mostProbableType === "map") {
|
|
349
|
+
if (checkTypesCountHighVariability(typesCount)) result = {
|
|
350
|
+
type: "map",
|
|
351
|
+
name: title ?? key ?? "",
|
|
352
|
+
keyValue: true,
|
|
353
|
+
properties: {}
|
|
354
|
+
};
|
|
355
|
+
const properties = buildPropertiesFromCount(totalDocsCount, typesCount.map, valuesResult ? valuesResult.mapValues : void 0);
|
|
356
|
+
result = {
|
|
357
|
+
type: "map",
|
|
358
|
+
name: title ?? key ?? "",
|
|
359
|
+
properties
|
|
360
|
+
};
|
|
361
|
+
} else if (mostProbableType === "array") {
|
|
362
|
+
const arrayTypesCount = typesCount.array;
|
|
363
|
+
const of = buildPropertyFromCount(key, totalDocsCount, getMostProbableType(arrayTypesCount), arrayTypesCount, valuesResult);
|
|
364
|
+
result = {
|
|
365
|
+
type: "array",
|
|
366
|
+
name: title ?? key ?? "",
|
|
367
|
+
of
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
if (!result) {
|
|
371
|
+
const propertyProps = {
|
|
372
|
+
name: key,
|
|
373
|
+
totalDocsCount,
|
|
374
|
+
valuesResult
|
|
375
|
+
};
|
|
376
|
+
if (mostProbableType === "string") result = buildStringProperty(propertyProps);
|
|
377
|
+
else if (mostProbableType === "reference") result = buildReferenceProperty(propertyProps);
|
|
378
|
+
else result = { type: mostProbableType };
|
|
379
|
+
if (title) result.name = title;
|
|
380
|
+
const validation = buildValidation(propertyProps);
|
|
381
|
+
if (validation) result.validation = validation;
|
|
382
|
+
}
|
|
383
|
+
return result;
|
|
384
|
+
}
|
|
385
|
+
function buildPropertiesFromCount(totalDocsCount, typesCountRecord, valuesCountRecord) {
|
|
386
|
+
const res = {};
|
|
387
|
+
Object.entries(typesCountRecord).forEach(([key, typesCount]) => {
|
|
388
|
+
res[key] = buildPropertyFromCount(key, totalDocsCount, getMostProbableType(typesCount), typesCount, valuesCountRecord ? valuesCountRecord[key] : void 0);
|
|
389
|
+
});
|
|
390
|
+
return res;
|
|
391
|
+
}
|
|
392
|
+
function countMaxDocumentsUnder(typesCount) {
|
|
393
|
+
let count = 0;
|
|
394
|
+
Object.entries(typesCount).forEach(([type, value]) => {
|
|
395
|
+
if (typeof value === "object") count = Math.max(count, countMaxDocumentsUnder(value));
|
|
396
|
+
else count = Math.max(count, Number(value));
|
|
397
|
+
});
|
|
398
|
+
return count;
|
|
399
|
+
}
|
|
400
|
+
function getMostProbableTypeInArray(array, getType) {
|
|
401
|
+
const typesCount = {};
|
|
402
|
+
array.forEach((value) => {
|
|
403
|
+
increaseTypeCount(getType(value), typesCount, value, getType);
|
|
404
|
+
});
|
|
405
|
+
return getMostProbableType(typesCount);
|
|
406
|
+
}
|
|
407
|
+
function checkTypesCountHighVariability(typesCount) {
|
|
408
|
+
const maxCount = countMaxDocumentsUnder(typesCount);
|
|
409
|
+
let keysWithFewValues = 0;
|
|
410
|
+
Object.entries(typesCount.map ?? {}).forEach(([key, value]) => {
|
|
411
|
+
if (countMaxDocumentsUnder(value) < maxCount / 3) keysWithFewValues++;
|
|
412
|
+
});
|
|
413
|
+
return keysWithFewValues / Object.entries(typesCount.map ?? {}).length > .5;
|
|
414
|
+
}
|
|
415
|
+
function inferTypeFromValue(value) {
|
|
416
|
+
if (value === null || value === void 0) return "string";
|
|
417
|
+
if (value instanceof _rebasepro_types.Vector || value && typeof value === "object" && "__type" in value && value.__type === "Vector") return "vector";
|
|
418
|
+
if (typeof value === "string") return "string";
|
|
419
|
+
if (typeof value === "number") return "number";
|
|
420
|
+
if (typeof value === "boolean") return "boolean";
|
|
421
|
+
if (Array.isArray(value)) return "array";
|
|
422
|
+
if (typeof value === "object") return "map";
|
|
423
|
+
return "string";
|
|
424
|
+
}
|
|
425
|
+
//#endregion
|
|
426
|
+
exports.buildEntityPropertiesFromData = buildEntityPropertiesFromData;
|
|
427
|
+
exports.buildPropertiesOrder = buildPropertiesOrder;
|
|
428
|
+
exports.buildPropertyFromData = buildPropertyFromData;
|
|
429
|
+
exports.extractEnumFromValues = extractEnumFromValues;
|
|
430
|
+
exports.findCommonInitialStringInPath = findCommonInitialStringInPath;
|
|
431
|
+
exports.inferTypeFromValue = inferTypeFromValue;
|
|
432
|
+
Object.defineProperty(exports, "isObject", {
|
|
433
|
+
enumerable: true,
|
|
434
|
+
get: function() {
|
|
435
|
+
return _rebasepro_utils.isObject;
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
Object.defineProperty(exports, "isPlainObject", {
|
|
439
|
+
enumerable: true,
|
|
440
|
+
get: function() {
|
|
441
|
+
return _rebasepro_utils.isPlainObject;
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
exports.looksLikeReference = looksLikeReference;
|
|
445
|
+
Object.defineProperty(exports, "mergeDeep", {
|
|
446
|
+
enumerable: true,
|
|
447
|
+
get: function() {
|
|
448
|
+
return _rebasepro_utils.mergeDeep;
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
exports.parseReferenceString = parseReferenceString;
|
|
452
|
+
Object.defineProperty(exports, "prettifyIdentifier", {
|
|
453
|
+
enumerable: true,
|
|
454
|
+
get: function() {
|
|
455
|
+
return _rebasepro_utils.prettifyIdentifier;
|
|
456
|
+
}
|
|
457
|
+
});
|
|
458
|
+
exports.removeInitialAndTrailingSlashes = removeInitialAndTrailingSlashes;
|
|
459
|
+
exports.removeInitialSlash = removeInitialSlash;
|
|
460
|
+
exports.removeTrailingSlash = removeTrailingSlash;
|
|
461
|
+
exports.resolveEnumValues = resolveEnumValues;
|
|
462
|
+
Object.defineProperty(exports, "unslugify", {
|
|
463
|
+
enumerable: true,
|
|
464
|
+
get: function() {
|
|
465
|
+
return _rebasepro_utils.unslugify;
|
|
466
|
+
}
|
|
467
|
+
});
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
//# sourceMappingURL=index.umd.cjs.map
|