@rebasepro/schema-inference 0.0.1-canary.4d4fb3e

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/src/strings.ts ADDED
@@ -0,0 +1,103 @@
1
+ import { ValuesCountEntry } from "./types";
2
+
3
+ /**
4
+ * Parse a reference string value which can be in the format:
5
+ * - Simple: "path/entityId"
6
+ * - With database: "database_name:::path/entityId"
7
+ * Returns the path and database (undefined if not specified or if "(default)")
8
+ */
9
+ export function parseReferenceString(value: string): { path: string; database?: string } | null {
10
+ if (!value) return null;
11
+
12
+ let database: string | undefined = undefined;
13
+ let fullPath = value;
14
+
15
+ // Parse the new format: database_name:::path/entityId
16
+ if (value.includes(":::")) {
17
+ const [dbName, pathPart] = value.split(":::");
18
+ if (dbName && dbName !== "(default)") {
19
+ database = dbName;
20
+ }
21
+ fullPath = pathPart;
22
+ }
23
+
24
+ // Check if it looks like a path (contains at least one slash)
25
+ if (!fullPath || !fullPath.includes("/")) {
26
+ return null;
27
+ }
28
+
29
+ // Extract the collection path (everything before the last slash)
30
+ const path = fullPath.substring(0, fullPath.lastIndexOf("/"));
31
+
32
+ return { path, database };
33
+ }
34
+
35
+ /**
36
+ * Check if a string value looks like a reference
37
+ */
38
+ export function looksLikeReference(value: any): boolean {
39
+ if (typeof value !== "string") return false;
40
+ return parseReferenceString(value) !== null;
41
+ }
42
+
43
+ export function findCommonInitialStringInPath(valuesCount?: ValuesCountEntry) {
44
+
45
+ if (!valuesCount) return undefined;
46
+
47
+ function getPath(value: any): string | undefined {
48
+ let pathString: string | undefined;
49
+
50
+ if (typeof value === "string") {
51
+ pathString = value;
52
+ } else if (value.slug) {
53
+ pathString = value.slug;
54
+ } else {
55
+ console.warn("findCommonInitialStringInPath: value is not a string or document with path", value);
56
+ return undefined;
57
+ }
58
+
59
+ if (!pathString) return undefined;
60
+
61
+ // Parse the new format: database_name:::path/entityId
62
+ // Extract just the path portion for comparison
63
+ if (pathString.includes(":::")) {
64
+ const [, pathPart] = pathString.split(":::");
65
+ pathString = pathPart;
66
+ }
67
+
68
+ return pathString;
69
+ }
70
+
71
+ const strings: string[] = valuesCount.values.map((v) => getPath(v)).filter(v => !!v) as string[];
72
+ const pathWithSlash = strings.find((s) => s.includes("/"));
73
+ if (!pathWithSlash)
74
+ return undefined;
75
+
76
+ const searchedPath = pathWithSlash.substring(0, pathWithSlash.lastIndexOf("/"));
77
+
78
+ const yep = valuesCount.values
79
+ .filter((value) => {
80
+ const path = getPath(value);
81
+ if (!path) return false;
82
+ return path.startsWith(searchedPath)
83
+ }).length > valuesCount.values.length / 3 * 2;
84
+
85
+ return yep ? searchedPath : undefined;
86
+
87
+ }
88
+
89
+ export function removeInitialAndTrailingSlashes(s: string): string {
90
+ return removeInitialSlash(removeTrailingSlash(s));
91
+ }
92
+
93
+ export function removeInitialSlash(s: string) {
94
+ if (s.startsWith("/"))
95
+ return s.slice(1);
96
+ else return s;
97
+ }
98
+
99
+ export function removeTrailingSlash(s: string) {
100
+ if (s.endsWith("/"))
101
+ return s.slice(0, -1);
102
+ else return s;
103
+ }