@rebasepro/common 0.0.1-canary.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.
Files changed (73) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +174 -0
  3. package/dist/collections/CollectionRegistry.d.ts +48 -0
  4. package/dist/collections/index.d.ts +1 -0
  5. package/dist/index.d.ts +2 -0
  6. package/dist/index.es.js +2380 -0
  7. package/dist/index.es.js.map +1 -0
  8. package/dist/index.umd.js +2379 -0
  9. package/dist/index.umd.js.map +1 -0
  10. package/dist/util/arrays.d.ts +1 -0
  11. package/dist/util/builders.d.ts +64 -0
  12. package/dist/util/callbacks.d.ts +6 -0
  13. package/dist/util/collections.d.ts +11 -0
  14. package/dist/util/common.d.ts +2 -0
  15. package/dist/util/conditions.d.ts +26 -0
  16. package/dist/util/dates.d.ts +1 -0
  17. package/dist/util/entities.d.ts +28 -0
  18. package/dist/util/entity_actions.d.ts +2 -0
  19. package/dist/util/enums.d.ts +3 -0
  20. package/dist/util/fields.d.ts +2 -0
  21. package/dist/util/flatten_object.d.ts +5 -0
  22. package/dist/util/hash.d.ts +1 -0
  23. package/dist/util/index.d.ts +26 -0
  24. package/dist/util/names.d.ts +22 -0
  25. package/dist/util/navigation_from_path.d.ts +29 -0
  26. package/dist/util/navigation_utils.d.ts +31 -0
  27. package/dist/util/objects.d.ts +26 -0
  28. package/dist/util/os.d.ts +2 -0
  29. package/dist/util/parent_references_from_path.d.ts +6 -0
  30. package/dist/util/paths.d.ts +14 -0
  31. package/dist/util/permissions.d.ts +5 -0
  32. package/dist/util/permissions.test.d.ts +1 -0
  33. package/dist/util/plurals.d.ts +16 -0
  34. package/dist/util/references.d.ts +2 -0
  35. package/dist/util/regexp.d.ts +7 -0
  36. package/dist/util/relations.d.ts +12 -0
  37. package/dist/util/resolutions.d.ts +74 -0
  38. package/dist/util/storage.d.ts +24 -0
  39. package/dist/util/strings.d.ts +7 -0
  40. package/package.json +118 -0
  41. package/src/collections/CollectionRegistry.ts +319 -0
  42. package/src/collections/index.ts +1 -0
  43. package/src/index.ts +2 -0
  44. package/src/util/arrays.ts +3 -0
  45. package/src/util/builders.ts +138 -0
  46. package/src/util/callbacks.ts +115 -0
  47. package/src/util/collections.ts +126 -0
  48. package/src/util/common.ts +2 -0
  49. package/src/util/conditions.ts +348 -0
  50. package/src/util/dates.ts +1 -0
  51. package/src/util/entities.ts +212 -0
  52. package/src/util/entity_actions.ts +28 -0
  53. package/src/util/enums.ts +26 -0
  54. package/src/util/fields.ts +28 -0
  55. package/src/util/flatten_object.ts +45 -0
  56. package/src/util/hash.ts +11 -0
  57. package/src/util/index.ts +26 -0
  58. package/src/util/names.ts +30 -0
  59. package/src/util/navigation_from_path.ts +121 -0
  60. package/src/util/navigation_utils.ts +222 -0
  61. package/src/util/objects.ts +376 -0
  62. package/src/util/os.ts +13 -0
  63. package/src/util/parent_references_from_path.ts +57 -0
  64. package/src/util/paths.ts +27 -0
  65. package/src/util/permissions.test.ts +716 -0
  66. package/src/util/permissions.ts +235 -0
  67. package/src/util/plurals.ts +188 -0
  68. package/src/util/references.ts +34 -0
  69. package/src/util/regexp.ts +32 -0
  70. package/src/util/relations.ts +211 -0
  71. package/src/util/resolutions.ts +383 -0
  72. package/src/util/storage.ts +144 -0
  73. package/src/util/strings.ts +84 -0
@@ -0,0 +1,144 @@
1
+ import { ArrayProperty, EntityValues, StorageConfig, StringProperty, UploadedFileContext } from "@rebasepro/types";
2
+ import { randomString } from "./strings";
3
+
4
+ interface ResolveFilenameStringParams<M extends object> {
5
+ input: string | ((context: UploadedFileContext) => (Promise<string> | string));
6
+ storage: StorageConfig;
7
+ values: EntityValues<M>;
8
+ entityId?: string | number;
9
+ path?: string;
10
+ property: StringProperty | ArrayProperty,
11
+ file: File;
12
+ propertyKey: string;
13
+ }
14
+
15
+ export async function resolveStorageFilenameString<M extends object>(
16
+ {
17
+ input,
18
+ storage,
19
+ values,
20
+ entityId,
21
+ path,
22
+ property,
23
+ file,
24
+ propertyKey
25
+ }: ResolveFilenameStringParams<M>): Promise<string> {
26
+ let result;
27
+
28
+ if (typeof input === "function") {
29
+ result = await input({
30
+ path,
31
+ entityId,
32
+ values,
33
+ property,
34
+ file,
35
+ storage,
36
+ propertyKey
37
+ });
38
+ if (!result)
39
+ console.warn("Storage callback returned empty result. Using default name value")
40
+ } else {
41
+ result = replacePlaceholders({
42
+ file,
43
+ input,
44
+ entityId,
45
+ propertyKey,
46
+ path
47
+ });
48
+ }
49
+
50
+ if (!result)
51
+ result = randomString() + "_" + file.name;
52
+
53
+ return result;
54
+ }
55
+
56
+ interface ResolveStoragePathStringParams<M extends object> {
57
+ input: string | ((context: UploadedFileContext) => string);
58
+ storage: StorageConfig;
59
+ values: EntityValues<M>;
60
+ entityId?: string | number;
61
+ path?: string;
62
+ property: StringProperty | ArrayProperty;
63
+ file: File;
64
+ propertyKey: string;
65
+ }
66
+
67
+ export function resolveStoragePathString<M extends object>(
68
+ {
69
+ input,
70
+ storage,
71
+ values,
72
+ entityId,
73
+ path,
74
+ property,
75
+ file,
76
+ propertyKey
77
+ }: ResolveStoragePathStringParams<M>): string {
78
+ let result;
79
+ if (typeof input === "function") {
80
+ result = input({
81
+ path,
82
+ entityId,
83
+ values,
84
+ property,
85
+ file,
86
+ storage,
87
+ propertyKey
88
+ });
89
+ if (!result)
90
+ console.warn("Storage callback returned empty result. Using default name value")
91
+ } else {
92
+ result = replacePlaceholders({
93
+ file,
94
+ input,
95
+ entityId,
96
+ propertyKey,
97
+ path
98
+ });
99
+ }
100
+
101
+ if (!result)
102
+ result = randomString() + "_" + file.name;
103
+
104
+ return result;
105
+ }
106
+
107
+ interface Placeholders {
108
+ file: File;
109
+ input: string;
110
+ entityId?: string | number;
111
+ propertyKey: string;
112
+ path?: string;
113
+ }
114
+
115
+ function replacePlaceholders({
116
+ file,
117
+ input,
118
+ entityId,
119
+ propertyKey,
120
+ path
121
+ }: Placeholders) {
122
+ const ext = file.name.split(".").pop();
123
+ let result = input
124
+ .replace("{propertyKey}", propertyKey)
125
+ .replace("{rand}", randomString())
126
+ .replace("{file}", file.name)
127
+ .replace("{file.type}", file.type);
128
+ if (entityId) {
129
+ result = result.replace("{entityId}", String(entityId));
130
+ }
131
+ if (path) {
132
+ result = result.replace("{path}", path);
133
+ }
134
+ if (ext) {
135
+ result = result.replace("{file.ext}", ext);
136
+ const name = file.name.replace(`.${ext}`, "");
137
+ result = result.replace("{file.name}", name)
138
+ }
139
+
140
+ if (!result)
141
+ result = randomString() + "_" + file.name;
142
+
143
+ return result;
144
+ }
@@ -0,0 +1,84 @@
1
+ const kebabCaseRegex = /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g;
2
+
3
+ export const toKebabCase = (str: string) => {
4
+ const regExpMatchArray = str.match(kebabCaseRegex);
5
+ if (!regExpMatchArray) return "";
6
+ return regExpMatchArray
7
+ .map(x => x.toLowerCase())
8
+ .join("-");
9
+ };
10
+
11
+ const snakeCaseRegex = /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g;
12
+
13
+ export const toSnakeCase = (str: string) => {
14
+ const regExpMatchArray = str.match(snakeCaseRegex);
15
+ if (!regExpMatchArray) return "";
16
+ return regExpMatchArray
17
+ .map(x => x.toLowerCase())
18
+ .join("_");
19
+ };
20
+
21
+ export function randomString(strLength = 5) {
22
+ return Math.random().toString(36).slice(2, 2 + strLength);
23
+ }
24
+
25
+ export function randomColor() {
26
+ return Math.floor(Math.random() * 16777215).toString(16);
27
+ }
28
+
29
+ export function slugify(text?: string, separator = "_", lowercase = true) {
30
+ if (!text) return "";
31
+ const from = "ãàáäâẽèéëêìíïîõòóöôùúüûñç·/_,:;-"
32
+ const to = `aaaaaeeeeeiiiiooooouuuunc${separator}${separator}${separator}${separator}${separator}${separator}${separator}`;
33
+
34
+ for (let i = 0, l = from.length; i < l; i++) {
35
+ text = text.replace(new RegExp(from.charAt(i), "g"), to.charAt(i));
36
+ }
37
+
38
+ text = text
39
+ .toString() // Cast to string
40
+ .replace(/\s+/g, separator) // Replace spaces with separator
41
+ .replace(/&/g, separator) // Replace & with separator
42
+ .replace(/[^\w\\-]+/g, "") // Remove all non-word chars
43
+ .replace(new RegExp("\\" + separator + "\\" + separator + "+", "g"),
44
+ separator) // Replace multiple separators with single one
45
+ .trim() // Remove whitespace from both sides of a string
46
+ .replace(/^\s+|\s+$/g, "");
47
+
48
+ return lowercase
49
+ ? text.toLowerCase() // Convert the string to lowercase letters
50
+ : text;
51
+ }
52
+
53
+ export function unslugify(slug?: string): string {
54
+ if (!slug) return "";
55
+ if (slug.includes("-") || slug.includes("_") || !slug.includes(" ")) {
56
+ const result = slug.replace(/[-_]/g, " ");
57
+ return result.replace(/\w\S*/g, function (txt) {
58
+ return txt.charAt(0).toUpperCase() + txt.substring(1);
59
+ }).trim();
60
+ } else {
61
+ return slug.trim();
62
+ }
63
+ }
64
+
65
+ export function prettifyIdentifier(input: string) {
66
+ if (!input) return "";
67
+
68
+ let text = input;
69
+
70
+ // 1. Handle camelCase and Acronyms
71
+ // Group 1 ($1 $2): Lowercase followed by Uppercase (e.g., imageURL -> image URL)
72
+ // Group 2 ($3 $4): Uppercase followed by Uppercase+lowercase (e.g., XMLParser -> XML Parser)
73
+ text = text.replace(/([a-z])([A-Z])|([A-Z])([A-Z][a-z])/g, "$1$3 $2$4");
74
+
75
+ // 2. Replace hyphens/underscores with spaces
76
+ text = text.replace(/[_-]+/g, " ");
77
+
78
+ // 3. Capitalize first letter of each word (Title Case)
79
+ const s = text
80
+ .trim()
81
+ .replace(/\b\w/g, (char) => char.toUpperCase());
82
+ console.log("Prettified identifier:", { input,s });
83
+ return s;
84
+ }