@firecms/schema_inference 3.0.0-canary.231 → 3.0.0-canary.233

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.
@@ -1,12 +1,3 @@
1
- import {
2
- DataType,
3
- EnumValues,
4
- mergeDeep,
5
- Properties, PropertiesOrBuilders,
6
- Property,
7
- resolveEnumValues,
8
- StringProperty
9
- } from "@firecms/core";
10
1
  import {
11
2
  InferencePropertyBuilderProps,
12
3
  TypesCount,
@@ -17,7 +8,8 @@ import {
17
8
  import { buildStringProperty } from "./builders/string_property_builder";
18
9
  import { buildValidation } from "./builders/validation_builder";
19
10
  import { buildReferenceProperty } from "./builders/reference_property_builder";
20
- import { extractEnumFromValues } from "./util";
11
+ import { extractEnumFromValues, mergeDeep, resolveEnumValues } from "./util";
12
+ import { DataType, EnumValues, Properties, PropertiesOrBuilders, Property, StringProperty } from "./cms_types";
21
13
 
22
14
  export type InferenceTypeBuilder = (value: any) => DataType;
23
15
 
@@ -377,7 +369,7 @@ function getMostProbableTypeInArray(
377
369
  array: any[],
378
370
  getType: InferenceTypeBuilder
379
371
  ): DataType {
380
- let typesCount: TypesCount = {};
372
+ const typesCount: TypesCount = {};
381
373
  array.forEach((value) => {
382
374
  increaseTypeCount(getType(value), typesCount, value, getType);
383
375
  });
@@ -398,7 +390,7 @@ function checkTypesCountHighVariability(typesCount: TypesCount) {
398
390
 
399
391
  function formatString(input: string): string {
400
392
  const normalized = input
401
- .replace(/[_\-]+/g, " ")
393
+ .replace(/[_-]+/g, " ")
402
394
  .replace(/([a-z])([A-Z])/g, "$1 $2")
403
395
  .toLowerCase();
404
396
 
@@ -1,5 +1,4 @@
1
1
  import { buildEntityPropertiesFromData } from "../collection_builder";
2
- import { DataType } from "@firecms/core";
3
2
 
4
3
  // import usage from "./usage.json" assert {
5
4
  // type: "json",
@@ -10,6 +9,7 @@ import usage from "./pop_products.json" assert {
10
9
  integrity: "sha384-ABC123"
11
10
  };
12
11
  import * as util from "util";
12
+ import { DataType } from "../cms_types";
13
13
 
14
14
  buildEntityPropertiesFromData(usage, getType)
15
15
  .then((res) => console.log(util.inspect(res, { showHidden: false, depth: null, colors: true })));
package/src/types.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { DataType } from "@firecms/core";
1
+ import { DataType } from "./cms_types";
2
2
 
3
3
  export type TypesCount = {
4
4
  number?: number,
package/src/util.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { unslugify } from "@firecms/core";
1
+ import { EnumValueConfig, EnumValues } from "./cms_types";
2
2
 
3
3
  export function extractEnumFromValues(values: unknown[]) {
4
4
  if (!Array.isArray(values)) {
@@ -14,3 +14,63 @@ export function extractEnumFromValues(values: unknown[]) {
14
14
  enumValues.sort((a, b) => a.label.localeCompare(b.label));
15
15
  return enumValues;
16
16
  }
17
+
18
+ export function unslugify(slug?: string): string {
19
+ if (!slug) return "";
20
+ if (slug.includes("-") || slug.includes("_") || !slug.includes(" ")) {
21
+ const result = slug.replace(/[-_]/g, " ");
22
+ return result.replace(/\w\S*/g, function (txt) {
23
+ return txt.charAt(0).toUpperCase() + txt.substr(1);
24
+ }).trim();
25
+ } else {
26
+ return slug.trim();
27
+ }
28
+ }
29
+
30
+ export function resolveEnumValues(input: EnumValues): EnumValueConfig[] | undefined {
31
+ if (typeof input === "object") {
32
+ return Object.entries(input).map(([id, value]) =>
33
+ (typeof value === "string"
34
+ ? {
35
+ id,
36
+ label: value
37
+ }
38
+ : value));
39
+ } else if (Array.isArray(input)) {
40
+ return input as EnumValueConfig[];
41
+ } else {
42
+ return undefined;
43
+ }
44
+ }
45
+
46
+ export function mergeDeep<T extends Record<any, any>, U extends Record<any, any>>(target: T, source: U, ignoreUndefined: boolean = false): T & U {
47
+ const targetIsObject = isObject(target);
48
+ const output = targetIsObject ? { ...target } : target;
49
+ if (targetIsObject && isObject(source)) {
50
+ Object.keys(source).forEach(key => {
51
+ const sourceElement = source[key];
52
+ // Skip undefined values when ignoreUndefined is true
53
+ if (ignoreUndefined && sourceElement === undefined) {
54
+ return;
55
+ }
56
+ if (sourceElement instanceof Date) {
57
+ // Assign a new Date instance with the same time value
58
+ Object.assign(output, { [key]: new Date(sourceElement.getTime()) });
59
+ } else if (isObject(sourceElement)) {
60
+ if (!(key in target))
61
+ Object.assign(output, { [key]: sourceElement });
62
+ else
63
+ (output as any)[key] = mergeDeep((target as any)[key], sourceElement);
64
+ } else {
65
+ Object.assign(output, { [key]: sourceElement });
66
+ }
67
+ });
68
+ }
69
+ return output as T;
70
+ }
71
+
72
+ export function isObject(item: any) {
73
+ return item && typeof item === "object" && !Array.isArray(item);
74
+ }
75
+
76
+