@rebasepro/utils 0.0.1-canary.eae7889 → 0.0.1-canary.eb08332

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 CHANGED
@@ -1,588 +1,564 @@
1
1
  import hash from "object-hash";
2
2
  import { GeoPoint } from "@rebasepro/types";
3
- const kebabCaseRegex = /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g;
4
- const toKebabCase = (str) => {
5
- const regExpMatchArray = str.match(kebabCaseRegex);
6
- if (!regExpMatchArray) return "";
7
- return regExpMatchArray.map((x) => x.toLowerCase()).join("-");
3
+ //#region src/strings.ts
4
+ var tokenizeRegex = /[A-Z]{2,}(?=[A-Z][a-z]|\b)|[A-Z]?[a-z]+|[0-9]+(?:[a-z](?![a-z]))?|[A-Z]/g;
5
+ var toKebabCase = (str) => {
6
+ if (!str || typeof str !== "string") return "";
7
+ const regExpMatchArray = str.match(tokenizeRegex);
8
+ if (!regExpMatchArray) return "";
9
+ return regExpMatchArray.map((x) => x.toLowerCase()).join("-");
8
10
  };
9
- const snakeCaseRegex = /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g;
10
- const toSnakeCase = (str) => {
11
- const regExpMatchArray = str.match(snakeCaseRegex);
12
- if (!regExpMatchArray) return "";
13
- return regExpMatchArray.map((x) => x.toLowerCase()).join("_");
11
+ var snakeCaseRegex = tokenizeRegex;
12
+ var toSnakeCase = (str) => {
13
+ if (!str || typeof str !== "string") return "";
14
+ const regExpMatchArray = str.match(snakeCaseRegex);
15
+ if (!regExpMatchArray) return "";
16
+ return regExpMatchArray.map((x) => x.toLowerCase()).join("_");
14
17
  };
15
18
  function camelCase(str) {
16
- if (!str) return "";
17
- if (str.length === 1) return str.toLowerCase();
18
- const parts = str.split(/[-_ ]+/).filter(Boolean);
19
- if (parts.length === 0) return "";
20
- return parts[0].toLowerCase() + // Transform remaining parts to have first letter uppercase
21
- parts.slice(1).map((part) => part.charAt(0).toUpperCase() + part.substring(1).toLowerCase()).join("");
19
+ if (!str) return "";
20
+ if (str.length === 1) return str.toLowerCase();
21
+ const parts = str.split(/[-_ ]+/).filter(Boolean);
22
+ if (parts.length === 0) return "";
23
+ return parts[0].toLowerCase() + parts.slice(1).map((part) => part.charAt(0).toUpperCase() + part.substring(1).toLowerCase()).join("");
22
24
  }
23
25
  function randomString(strLength = 5) {
24
- return Math.random().toString(36).slice(2, 2 + strLength);
26
+ return Math.random().toString(36).slice(2, 2 + strLength);
25
27
  }
26
28
  function randomColor() {
27
- return Math.floor(Math.random() * 16777215).toString(16);
29
+ return Math.floor(Math.random() * 16777215).toString(16);
28
30
  }
29
31
  function slugify(text, separator = "_", lowercase = true) {
30
- if (!text) return "";
31
- const from = "ãàáäâẽèéëêìíïîõòóöôùúüûñç·/_,:;-";
32
- const to = `aaaaaeeeeeiiiiooooouuuunc${separator}${separator}${separator}${separator}${separator}${separator}${separator}`;
33
- for (let i = 0, l = from.length; i < l; i++) {
34
- text = text.replace(new RegExp(from.charAt(i), "g"), to.charAt(i));
35
- }
36
- text = text.toString().trim().replace(/^\s+|\s+$/g, "").replace(/\s+/g, separator).replace(/&/g, separator).replace(/[^\w\\-]+/g, "").replace(new RegExp("\\" + separator + "\\" + separator + "+", "g"), separator);
37
- return lowercase ? text.toLowerCase() : text;
32
+ if (!text) return "";
33
+ const from = "ãàáäâẽèéëêìíïîõòóöôùúüûñç·/_,:;-";
34
+ const to = `aaaaaeeeeeiiiiooooouuuunc${separator}${separator}${separator}${separator}${separator}${separator}${separator}`;
35
+ for (let i = 0, l = 32; i < l; i++) text = text.replace(new RegExp(from.charAt(i), "g"), to.charAt(i));
36
+ text = text.toString().trim().replace(/^\s+|\s+$/g, "").replace(/\s+/g, separator).replace(/&/g, separator).replace(/[^\w\\-]+/g, "").replace(new RegExp("\\" + separator + "\\" + separator + "+", "g"), separator);
37
+ return lowercase ? text.toLowerCase() : text;
38
38
  }
39
39
  function unslugify(slug) {
40
- if (!slug) return "";
41
- if (slug.includes("-") || slug.includes("_") || !slug.includes(" ")) {
42
- const result = slug.replace(/[-_]/g, " ");
43
- return result.replace(/\w\S*/g, function(txt) {
44
- return txt.charAt(0).toUpperCase() + txt.substring(1);
45
- }).trim();
46
- } else {
47
- return slug.trim();
48
- }
40
+ if (!slug) return "";
41
+ if (slug.includes("-") || slug.includes("_") || !slug.includes(" ")) return slug.replace(/[-_]/g, " ").replace(/\w\S*/g, function(txt) {
42
+ return txt.charAt(0).toUpperCase() + txt.substring(1);
43
+ }).trim();
44
+ else return slug.trim();
49
45
  }
50
46
  function prettifyIdentifier(input) {
51
- if (!input) return "";
52
- let text = input;
53
- text = text.replace(/([a-z])([A-Z])|([A-Z])([A-Z][a-z])/g, "$1$3 $2$4");
54
- text = text.replace(/[_-]+/g, " ");
55
- const s = text.trim().replace(/\b\w/g, (char) => char.toUpperCase());
56
- return s;
57
- }
58
- const isEmptyArray = (value) => Array.isArray(value) && value.length === 0;
59
- const isFunction = (obj) => typeof obj === "function";
60
- const isInteger = (obj) => String(Math.floor(Number(obj))) === String(obj);
61
- const isNaN = (obj) => obj !== obj;
47
+ if (!input) return "";
48
+ let text = input;
49
+ text = text.replace(/([a-z])([A-Z])|([A-Z])([A-Z][a-z])/g, "$1$3 $2$4");
50
+ text = text.replace(/[_-]+/g, " ");
51
+ return text.trim().replace(/\b\w/g, (char) => char.toUpperCase());
52
+ }
53
+ //#endregion
54
+ //#region src/objects.ts
55
+ /** @private is the value an empty array? */
56
+ var isEmptyArray = (value) => Array.isArray(value) && value.length === 0;
57
+ /** @private is the given object a Function? */
58
+ var isFunction = (obj) => typeof obj === "function";
59
+ /** @private is the given object an integer? */
60
+ var isInteger = (obj) => String(Math.floor(Number(obj))) === String(obj);
61
+ /** @private is the given object a NaN? */
62
+ var isNaN = (obj) => obj !== obj;
63
+ /**
64
+ * Deeply get a value from an object via its path.
65
+ */
62
66
  function getIn(obj, key, def, p = 0) {
63
- const path = toPath(key);
64
- while (obj && p < path.length) {
65
- obj = obj[path[p++]];
66
- }
67
- if (p !== path.length && !obj) {
68
- return def;
69
- }
70
- return obj === void 0 ? def : obj;
67
+ const path = toPath(key);
68
+ while (obj && p < path.length) obj = obj[path[p++]];
69
+ if (p !== path.length && !obj) return def;
70
+ return obj === void 0 ? def : obj;
71
71
  }
72
72
  function setIn(obj, path, value) {
73
- const res = clone(obj);
74
- let resVal = res;
75
- let i = 0;
76
- const pathArray = toPath(path);
77
- for (; i < pathArray.length - 1; i++) {
78
- const currentPath = pathArray[i];
79
- const currentObj = getIn(obj, pathArray.slice(0, i + 1));
80
- if (currentObj && (isObject(currentObj) || Array.isArray(currentObj))) {
81
- resVal = resVal[currentPath] = clone(currentObj);
82
- } else {
83
- const nextPath = pathArray[i + 1];
84
- resVal = resVal[currentPath] = isInteger(nextPath) && Number(nextPath) >= 0 ? [] : {};
85
- }
86
- }
87
- if ((i === 0 ? obj : resVal)[pathArray[i]] === value) {
88
- return obj;
89
- }
90
- if (value === void 0) {
91
- delete resVal[pathArray[i]];
92
- } else {
93
- resVal[pathArray[i]] = value;
94
- }
95
- if (i === 0 && value === void 0) {
96
- delete res[pathArray[i]];
97
- }
98
- return res;
73
+ const res = clone(obj);
74
+ let resVal = res;
75
+ let i = 0;
76
+ const pathArray = toPath(path);
77
+ for (; i < pathArray.length - 1; i++) {
78
+ const currentPath = pathArray[i];
79
+ const currentObj = getIn(obj, pathArray.slice(0, i + 1));
80
+ if (currentObj && (isObject(currentObj) || Array.isArray(currentObj))) resVal = resVal[currentPath] = clone(currentObj);
81
+ else {
82
+ const nextPath = pathArray[i + 1];
83
+ resVal = resVal[currentPath] = isInteger(nextPath) && Number(nextPath) >= 0 ? [] : {};
84
+ }
85
+ }
86
+ if ((i === 0 ? obj : resVal)[pathArray[i]] === value) return obj;
87
+ if (value === void 0) delete resVal[pathArray[i]];
88
+ else resVal[pathArray[i]] = value;
89
+ if (i === 0 && value === void 0) delete res[pathArray[i]];
90
+ return res;
99
91
  }
100
92
  function clone(value) {
101
- if (Array.isArray(value)) {
102
- return [...value];
103
- } else if (typeof value === "object" && value !== null) {
104
- return {
105
- ...value
106
- };
107
- } else {
108
- return value;
109
- }
93
+ if (Array.isArray(value)) return [...value];
94
+ else if (typeof value === "object" && value !== null) return { ...value };
95
+ else return value;
96
+ }
97
+ /**
98
+ * Deep clone a value, preserving function references and class instances.
99
+ * Unlike structuredClone, this handles objects that contain functions
100
+ * (e.g. CollectionConfig with target(), childCollections(), callbacks).
101
+ */
102
+ function deepClone(value) {
103
+ if (value === null || value === void 0) return value;
104
+ if (typeof value === "function") return value;
105
+ if (typeof value !== "object") return value;
106
+ if (Array.isArray(value)) return value.map((item) => deepClone(item));
107
+ if (Object.getPrototypeOf(value) !== Object.prototype) return value;
108
+ const result = {};
109
+ for (const key of Object.keys(value)) result[key] = deepClone(value[key]);
110
+ return result;
110
111
  }
111
112
  function toPath(value) {
112
- if (Array.isArray(value)) return value;
113
- return value.replace(/\[(\d+)]/g, ".$1").replace(/^\./, "").replace(/\.$/, "").split(".");
114
- }
115
- const pick = (obj, ...args) => ({
116
- ...args.reduce((res, key) => ({
117
- ...res,
118
- [key]: obj[key]
119
- }), {})
120
- });
113
+ if (Array.isArray(value)) return value;
114
+ return value.replace(/\[(\d+)]/g, ".$1").replace(/^\./, "").replace(/\.$/, "").split(".");
115
+ }
116
+ var pick = (obj, ...args) => ({ ...args.reduce((res, key) => ({
117
+ ...res,
118
+ [key]: obj[key]
119
+ }), {}) });
121
120
  function isObject(item) {
122
- return !!item && typeof item === "object" && !Array.isArray(item);
121
+ return !!item && typeof item === "object" && !Array.isArray(item);
123
122
  }
124
123
  function isPlainObject(obj) {
125
- if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
126
- return false;
127
- }
128
- const proto = Object.getPrototypeOf(obj);
129
- return proto === Object.prototype;
124
+ if (typeof obj !== "object" || obj === null || Array.isArray(obj)) return false;
125
+ return Object.getPrototypeOf(obj) === Object.prototype;
130
126
  }
131
127
  function mergeDeep(target, source, ignoreUndefined = false) {
132
- if (!isObject(target)) {
133
- return target;
134
- }
135
- const output = {
136
- ...target
137
- };
138
- if (!isObject(source)) {
139
- return output;
140
- }
141
- for (const key in source) {
142
- if (Object.prototype.hasOwnProperty.call(source, key)) {
143
- const sourceValue = source[key];
144
- const outputValue = output[key];
145
- if (ignoreUndefined && sourceValue === void 0) {
146
- continue;
147
- }
148
- if (sourceValue instanceof Date) {
149
- output[key] = new Date(sourceValue.getTime());
150
- } else if (Array.isArray(sourceValue)) {
151
- if (Array.isArray(outputValue)) {
152
- const newArray = [];
153
- const maxLength = Math.max(outputValue.length, sourceValue.length);
154
- for (let i = 0; i < maxLength; i++) {
155
- const sourceItem = sourceValue[i];
156
- const targetItem = outputValue[i];
157
- if (i >= sourceValue.length) {
158
- newArray[i] = targetItem;
159
- } else if (i >= outputValue.length) {
160
- newArray[i] = sourceItem;
161
- } else if (sourceItem === null) {
162
- newArray[i] = targetItem;
163
- } else if (isPlainObject(sourceItem) && isPlainObject(targetItem)) {
164
- newArray[i] = mergeDeep(targetItem, sourceItem, ignoreUndefined);
165
- } else {
166
- newArray[i] = sourceItem;
167
- }
168
- }
169
- output[key] = newArray;
170
- } else {
171
- output[key] = [...sourceValue];
172
- }
173
- } else if (isPlainObject(sourceValue)) {
174
- if (isPlainObject(outputValue)) {
175
- output[key] = mergeDeep(outputValue, sourceValue, ignoreUndefined);
176
- } else {
177
- output[key] = sourceValue;
178
- }
179
- } else if (isObject(sourceValue)) {
180
- output[key] = sourceValue;
181
- } else {
182
- output[key] = sourceValue;
183
- }
184
- }
185
- }
186
- return output;
128
+ if (!isObject(target)) return target;
129
+ const output = { ...target };
130
+ if (!isObject(source)) return output;
131
+ for (const key in source) {
132
+ if (key === "__proto__" || key === "constructor" || key === "prototype") continue;
133
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
134
+ const sourceValue = source[key];
135
+ const outputValue = output[key];
136
+ if (ignoreUndefined && sourceValue === void 0) continue;
137
+ if (sourceValue instanceof Date) output[key] = new Date(sourceValue.getTime());
138
+ else if (Array.isArray(sourceValue)) if (Array.isArray(outputValue)) if (!(sourceValue.some(isPlainObject) || outputValue.some(isPlainObject))) output[key] = [...sourceValue];
139
+ else {
140
+ const newArray = [];
141
+ const maxLength = Math.max(outputValue.length, sourceValue.length);
142
+ for (let i = 0; i < maxLength; i++) {
143
+ const sourceItem = sourceValue[i];
144
+ const targetItem = outputValue[i];
145
+ if (i >= sourceValue.length) newArray[i] = targetItem;
146
+ else if (i >= outputValue.length) newArray[i] = sourceItem;
147
+ else if (sourceItem === null) newArray[i] = targetItem;
148
+ else if (isPlainObject(sourceItem) && isPlainObject(targetItem)) newArray[i] = mergeDeep(targetItem, sourceItem, ignoreUndefined);
149
+ else newArray[i] = sourceItem;
150
+ }
151
+ output[key] = newArray;
152
+ }
153
+ else output[key] = [...sourceValue];
154
+ else if (isPlainObject(sourceValue)) if (isPlainObject(outputValue)) output[key] = mergeDeep(outputValue, sourceValue, ignoreUndefined);
155
+ else output[key] = sourceValue;
156
+ else if (isObject(sourceValue)) output[key] = sourceValue;
157
+ else output[key] = sourceValue;
158
+ }
159
+ }
160
+ return output;
187
161
  }
188
162
  function getValueInPath(o, path) {
189
- if (!o) return void 0;
190
- if (typeof o === "object") {
191
- if (path in o) {
192
- return o[path];
193
- }
194
- if (path.includes(".") || path.includes("[")) {
195
- let pathSegments = path.split(/[.[]/);
196
- if (path.includes("[")) {
197
- pathSegments = pathSegments.map((segment) => segment.replace("]", ""));
198
- }
199
- const firstSegment = pathSegments[0];
200
- const isArrayAndIndexExists = Array.isArray(o[firstSegment]) && !isNaN(parseInt(pathSegments[1]));
201
- const nextObject = isArrayAndIndexExists ? o[firstSegment][parseInt(pathSegments[1])] : o[firstSegment];
202
- const nextPath = pathSegments.slice(isArrayAndIndexExists ? 2 : 1).join(".");
203
- if (nextPath === "") return nextObject;
204
- return getValueInPath(nextObject, nextPath);
205
- }
206
- }
207
- return void 0;
163
+ if (!o) return void 0;
164
+ if (typeof o === "object") {
165
+ if (path in o) return o[path];
166
+ if (path.includes(".") || path.includes("[")) {
167
+ let pathSegments = path.split(/[.[]/);
168
+ if (path.includes("[")) pathSegments = pathSegments.map((segment) => segment.replace("]", ""));
169
+ const firstSegment = pathSegments[0];
170
+ const isArrayAndIndexExists = Array.isArray(o[firstSegment]) && !isNaN(parseInt(pathSegments[1]));
171
+ const nextObject = isArrayAndIndexExists ? o[firstSegment][parseInt(pathSegments[1])] : o[firstSegment];
172
+ const nextPath = pathSegments.slice(isArrayAndIndexExists ? 2 : 1).join(".");
173
+ if (nextPath === "") return nextObject;
174
+ return getValueInPath(nextObject, nextPath);
175
+ }
176
+ }
208
177
  }
209
178
  function removeInPath(o, path) {
210
- let currentObject = {
211
- ...o
212
- };
213
- const parts = path.split(".");
214
- const last = parts.pop();
215
- for (const part of parts) {
216
- currentObject = currentObject[part];
217
- }
218
- if (last) delete currentObject[last];
219
- return currentObject;
179
+ const res = clone(o);
180
+ let current = res;
181
+ const parts = path.split(".");
182
+ const last = parts.pop();
183
+ for (const part of parts) if (part in current && current[part] !== null && typeof current[part] === "object") {
184
+ current[part] = clone(current[part]);
185
+ current = current[part];
186
+ } else return res;
187
+ if (last && current && typeof current === "object") delete current[last];
188
+ return res;
220
189
  }
221
190
  function removeFunctions(o) {
222
- if (o === void 0) return void 0;
223
- if (o === null) return null;
224
- if (typeof o === "object") {
225
- if (Array.isArray(o)) {
226
- return o.map((v) => removeFunctions(v));
227
- }
228
- if (!isPlainObject(o)) {
229
- return o;
230
- }
231
- return Object.entries(o).filter(([_, value]) => typeof value !== "function").map(([key, value]) => {
232
- if (Array.isArray(value)) {
233
- return {
234
- [key]: value.map((v) => removeFunctions(v))
235
- };
236
- } else if (typeof value === "object") {
237
- return {
238
- [key]: removeFunctions(value)
239
- };
240
- } else return {
241
- [key]: value
242
- };
243
- }).reduce((a, b) => ({
244
- ...a,
245
- ...b
246
- }), {});
247
- }
248
- return o;
191
+ if (o === void 0) return void 0;
192
+ if (o === null) return null;
193
+ if (typeof o === "object") {
194
+ if (Array.isArray(o)) return o.map((v) => removeFunctions(v));
195
+ if (!isPlainObject(o)) return o;
196
+ return Object.entries(o).filter(([_, value]) => typeof value !== "function").map(([key, value]) => {
197
+ if (Array.isArray(value)) return { [key]: value.map((v) => removeFunctions(v)) };
198
+ else if (typeof value === "object") return { [key]: removeFunctions(value) };
199
+ else return { [key]: value };
200
+ }).reduce((a, b) => ({
201
+ ...a,
202
+ ...b
203
+ }), {});
204
+ }
205
+ return o;
249
206
  }
250
207
  function getHashValue(v) {
251
- if (!v) return null;
252
- if (typeof v === "object" && v !== null) {
253
- if ("id" in v) return String(v.id);
254
- else if (v instanceof Date) return v.toLocaleString();
255
- else if (v instanceof GeoPoint) return hash(v);
256
- }
257
- return hash(v, {
258
- ignoreUnknown: true
259
- });
208
+ if (!v) return null;
209
+ if (typeof v === "object" && v !== null) {
210
+ if ("id" in v) return String(v.id);
211
+ else if (v instanceof Date) return v.toLocaleString();
212
+ else if (v instanceof GeoPoint) return hash(v);
213
+ }
214
+ return hash(v, { ignoreUnknown: true });
260
215
  }
261
216
  function removeUndefined(value, removeEmptyStrings) {
262
- if (typeof value === "function") {
263
- return value;
264
- }
265
- if (Array.isArray(value)) {
266
- return value.map((v) => removeUndefined(v, removeEmptyStrings));
267
- }
268
- if (typeof value === "object") {
269
- if (value === null) return value;
270
- if (!isPlainObject(value)) {
271
- return value;
272
- }
273
- const res = {};
274
- Object.keys(value).forEach((key) => {
275
- if (!isEmptyObject(value)) {
276
- const childRes = removeUndefined(value[key], removeEmptyStrings);
277
- const isString = typeof childRes === "string";
278
- const shouldKeepIfString = !removeEmptyStrings || removeEmptyStrings && !isString || removeEmptyStrings && isString && childRes !== "";
279
- if (childRes !== void 0 && !isEmptyObject(childRes) && shouldKeepIfString) res[key] = childRes;
280
- }
281
- });
282
- return res;
283
- }
284
- return value;
217
+ if (typeof value === "function") return value;
218
+ if (Array.isArray(value)) return value.map((v) => removeUndefined(v, removeEmptyStrings));
219
+ if (typeof value === "object") {
220
+ if (value === null) return value;
221
+ if (!isPlainObject(value)) return value;
222
+ const res = {};
223
+ Object.keys(value).forEach((key) => {
224
+ if (!isEmptyObject(value)) {
225
+ const childRes = removeUndefined(value[key], removeEmptyStrings);
226
+ const isString = typeof childRes === "string";
227
+ const shouldKeepIfString = !removeEmptyStrings || removeEmptyStrings && !isString || removeEmptyStrings && isString && childRes !== "";
228
+ if (childRes !== void 0 && !isEmptyObject(childRes) && shouldKeepIfString) res[key] = childRes;
229
+ }
230
+ });
231
+ return res;
232
+ }
233
+ return value;
285
234
  }
286
235
  function removeNulls(value) {
287
- if (typeof value === "function") {
288
- return value;
289
- }
290
- if (Array.isArray(value)) {
291
- return value.map((v) => removeNulls(v));
292
- }
293
- if (typeof value === "object") {
294
- if (value === null) return value;
295
- if (!isPlainObject(value)) {
296
- return value;
297
- }
298
- const res = {};
299
- const obj = value;
300
- Object.keys(obj).forEach((key) => {
301
- if (obj[key] !== null) res[key] = removeNulls(obj[key]);
302
- });
303
- return res;
304
- }
305
- return value;
236
+ if (typeof value === "function") return value;
237
+ if (Array.isArray(value)) return value.map((v) => removeNulls(v));
238
+ if (typeof value === "object") {
239
+ if (value === null) return value;
240
+ if (!isPlainObject(value)) return value;
241
+ const res = {};
242
+ const obj = value;
243
+ Object.keys(obj).forEach((key) => {
244
+ if (obj[key] !== null) res[key] = removeNulls(obj[key]);
245
+ });
246
+ return res;
247
+ }
248
+ return value;
306
249
  }
307
250
  function isEmptyObject(obj) {
308
- return obj && Object.getPrototypeOf(obj) === Object.prototype && Object.keys(obj).length === 0;
251
+ return obj && Object.getPrototypeOf(obj) === Object.prototype && Object.keys(obj).length === 0;
309
252
  }
310
253
  function removePropsIfExisting(source, comparison) {
311
- const isObject2 = (val) => typeof val === "object" && val !== null;
312
- const isArray = (val) => Array.isArray(val);
313
- if (!isObject2(source) || !isObject2(comparison)) {
314
- return source;
315
- }
316
- const res = isArray(source) ? [...source] : {
317
- ...source
318
- };
319
- if (isArray(res)) {
320
- for (let i = res.length - 1; i >= 0; i--) {
321
- if (res[i] === comparison[i]) {
322
- res.splice(i, 1);
323
- } else if (isObject2(res[i]) && isObject2(comparison[i])) {
324
- res[i] = removePropsIfExisting(res[i], comparison[i]);
325
- }
326
- }
327
- } else {
328
- Object.keys(comparison).forEach((key) => {
329
- if (key in res) {
330
- if (isObject2(res[key]) && isObject2(comparison[key])) {
331
- res[key] = removePropsIfExisting(res[key], comparison[key]);
332
- } else if (res[key] === comparison[key]) {
333
- delete res[key];
334
- }
335
- }
336
- });
337
- }
338
- return res;
339
- }
254
+ const isObject = (val) => typeof val === "object" && val !== null;
255
+ const isArray = (val) => Array.isArray(val);
256
+ if (!isObject(source) || !isObject(comparison)) return source;
257
+ const res = isArray(source) ? [...source] : { ...source };
258
+ if (isArray(res)) {
259
+ for (let i = res.length - 1; i >= 0; i--) if (res[i] === comparison[i]) res.splice(i, 1);
260
+ else if (isObject(res[i]) && isObject(comparison[i])) res[i] = removePropsIfExisting(res[i], comparison[i]);
261
+ } else Object.keys(comparison).forEach((key) => {
262
+ if (key in res) {
263
+ if (isObject(res[key]) && isObject(comparison[key])) res[key] = removePropsIfExisting(res[key], comparison[key]);
264
+ else if (res[key] === comparison[key]) delete res[key];
265
+ }
266
+ });
267
+ return res;
268
+ }
269
+ //#endregion
270
+ //#region src/arrays.ts
340
271
  function toArray(input) {
341
- return Array.isArray(input) ? input : input ? [input] : [];
272
+ return Array.isArray(input) ? input : input ? [input] : [];
342
273
  }
343
- const defaultDateFormat = "MMMM dd, yyyy, HH:mm:ss";
274
+ //#endregion
275
+ //#region src/dates.ts
276
+ var defaultDateFormat = "MMMM dd, yyyy, HH:mm:ss";
277
+ //#endregion
278
+ //#region src/hash.ts
344
279
  function hashString(str) {
345
- if (!str) return 0;
346
- let hash2 = 0;
347
- let i;
348
- let chr;
349
- for (i = 0; i < str.length; i++) {
350
- chr = str.charCodeAt(i);
351
- hash2 = (hash2 << 5) - hash2 + chr;
352
- hash2 |= 0;
353
- }
354
- return Math.abs(hash2);
355
- }
280
+ if (!str) return 0;
281
+ let hash = 0;
282
+ let i;
283
+ let chr;
284
+ for (i = 0; i < str.length; i++) {
285
+ chr = str.charCodeAt(i);
286
+ hash = (hash << 5) - hash + chr;
287
+ hash |= 0;
288
+ }
289
+ return Math.abs(hash);
290
+ }
291
+ //#endregion
292
+ //#region src/regexp.ts
356
293
  function serializeRegExp(input) {
357
- if (!input) return "";
358
- return input.toString();
294
+ if (!input) return "";
295
+ return input.toString();
359
296
  }
297
+ /**
298
+ * Get a RegExp out of a serialized string
299
+ * @param input
300
+ */
360
301
  function hydrateRegExp(input) {
361
- if (!input) return void 0;
362
- const fragments = input.match(/\/(.*?)\/([a-z]*)?$/i);
363
- if (fragments) {
364
- return new RegExp(fragments[1], fragments[2] || "");
365
- } else {
366
- return new RegExp(input, "");
367
- }
302
+ if (!input) return void 0;
303
+ const fragments = input.match(/\/(.*?)\/([a-z]*)?$/i);
304
+ if (fragments) return new RegExp(fragments[1], fragments[2] || "");
305
+ else return new RegExp(input, "");
368
306
  }
369
307
  function isValidRegExp(input) {
370
- const fullRegexp = input.match(/\/((?![*+?])(?:[^\r\n[/\\]|\\.|\[(?:[^\r\n\]\\]|\\.)*])+)\/((?:g(?:im?|mi?)?|i(?:gm?|mg?)?|m(?:gi?|ig?)?)?)/);
371
- if (fullRegexp) return true;
372
- const simpleRegexp = input.match(/((?![*+?])(?:[^\r\n[/\\]|\\.|\[(?:[^\r\n\]\\]|\\.)*])+)/);
373
- return !!simpleRegexp;
308
+ if (input.match(/\/((?![*+?])(?:[^\r\n[/\\]|\\.|\[(?:[^\r\n\]\\]|\\.)*])+)\/((?:g(?:im?|mi?)?|i(?:gm?|mg?)?|m(?:gi?|ig?)?)?)/)) return true;
309
+ return !!input.match(/((?![*+?])(?:[^\r\n[/\\]|\\.|\[(?:[^\r\n\]\\]|\\.)*])+)/);
374
310
  }
311
+ //#endregion
312
+ //#region src/flatten_object.ts
375
313
  function flattenObject(obj, parentKey = "") {
376
- if (!obj) return obj;
377
- return Object.keys(obj).reduce((flatObj, key) => {
378
- const newKey = parentKey ? `${parentKey}.${key}` : key;
379
- if (typeof obj[key] === "object" && obj[key] !== null) {
380
- if (Array.isArray(obj[key])) {
381
- obj[key].forEach((item, index) => {
382
- Object.assign(flatObj, flattenObject(item, `${newKey}[${index}]`));
383
- });
384
- } else {
385
- Object.assign(flatObj, flattenObject(obj[key], newKey));
386
- }
387
- } else {
388
- flatObj[newKey] = obj[key];
389
- }
390
- return flatObj;
391
- }, {});
314
+ if (!obj) return obj;
315
+ return Object.keys(obj).reduce((flatObj, key) => {
316
+ const newKey = parentKey ? `${parentKey}.${key}` : key;
317
+ if (typeof obj[key] === "object" && obj[key] !== null) if (Array.isArray(obj[key])) obj[key].forEach((item, index) => {
318
+ if (typeof item === "object" && item !== null) Object.assign(flatObj, flattenObject(item, `${newKey}[${index}]`));
319
+ else flatObj[`${newKey}[${index}]`] = item;
320
+ });
321
+ else Object.assign(flatObj, flattenObject(obj[key], newKey));
322
+ else flatObj[newKey] = obj[key];
323
+ return flatObj;
324
+ }, {});
392
325
  }
393
326
  function getArrayValuesCount(array) {
394
- return array.reduce((acc, obj) => {
395
- Object.entries(obj).forEach(([key, value]) => {
396
- if (Array.isArray(value)) {
397
- acc[key] = Math.max(acc[key] || 0, value.length);
398
- }
399
- if (typeof value === "object" && value !== null) {
400
- const nested = getArrayValuesCount([value]);
401
- Object.entries(nested).forEach(([nestedKey, nestedCount]) => {
402
- const compoundKey = `${key}.${nestedKey}`;
403
- acc[compoundKey] = Math.max(acc[compoundKey] || 0, nestedCount);
404
- });
405
- }
406
- });
407
- return acc;
408
- }, {});
409
- }
327
+ return array.reduce((acc, obj) => {
328
+ Object.entries(obj).forEach(([key, value]) => {
329
+ if (Array.isArray(value)) acc[key] = Math.max(acc[key] || 0, value.length);
330
+ if (typeof value === "object" && value !== null) {
331
+ const nested = getArrayValuesCount([value]);
332
+ Object.entries(nested).forEach(([nestedKey, nestedCount]) => {
333
+ const compoundKey = `${key}.${nestedKey}`;
334
+ acc[compoundKey] = Math.max(acc[compoundKey] || 0, nestedCount);
335
+ });
336
+ }
337
+ });
338
+ return acc;
339
+ }, {});
340
+ }
341
+ //#endregion
342
+ //#region src/plurals.ts
343
+ /**
344
+ * Returns the plural of an English word.
345
+ *
346
+ * @param {string} word
347
+ * @param {number} [amount]
348
+ * @returns {string}
349
+ */
410
350
  function plural(word, amount) {
411
- if (amount !== void 0 && amount === 1) {
412
- return word;
413
- }
414
- const plurals = {
415
- "(quiz)$": "$1zes",
416
- "^(ox)$": "$1en",
417
- "([m|l])ouse$": "$1ice",
418
- "(matr|vert|ind)ix|ex$": "$1ices",
419
- "(x|ch|ss|sh)$": "$1es",
420
- "([^aeiouy]|qu)y$": "$1ies",
421
- "(hive)$": "$1s",
422
- "(?:([^f])fe|([lr])f)$": "$1$2ves",
423
- "(shea|lea|loa|thie)f$": "$1ves",
424
- sis$: "ses",
425
- "([ti])um$": "$1a",
426
- "(tomat|potat|ech|her|vet)o$": "$1oes",
427
- "(bu)s$": "$1ses",
428
- "(alias)$": "$1es",
429
- "(octop)us$": "$1i",
430
- "(ax|test)is$": "$1es",
431
- "(us)$": "$1es",
432
- "([^s]+)$": "$1s"
433
- };
434
- const irregular = {
435
- move: "moves",
436
- foot: "feet",
437
- goose: "geese",
438
- sex: "sexes",
439
- child: "children",
440
- man: "men",
441
- tooth: "teeth",
442
- person: "people"
443
- };
444
- const uncountable = ["sheep", "fish", "deer", "moose", "series", "species", "money", "rice", "information", "equipment", "bison", "cod", "offspring", "pike", "salmon", "shrimp", "swine", "trout", "aircraft", "hovercraft", "spacecraft", "sugar", "tuna", "you", "wood"];
445
- if (uncountable.indexOf(word.toLowerCase()) >= 0) {
446
- return word;
447
- }
448
- for (const w in irregular) {
449
- const pattern = new RegExp(`${w}$`, "i");
450
- const replace = irregular[w];
451
- if (pattern.test(word)) {
452
- return word.replace(pattern, replace);
453
- }
454
- }
455
- for (const reg in plurals) {
456
- const pattern = new RegExp(reg, "i");
457
- if (pattern.test(word)) {
458
- return word.replace(pattern, plurals[reg]);
459
- }
460
- }
461
- return word;
462
- }
351
+ if (amount !== void 0 && amount === 1) return word;
352
+ const plurals = {
353
+ "(quiz)$": "$1zes",
354
+ "^(ox)$": "$1en",
355
+ "([m|l])ouse$": "$1ice",
356
+ "(matr|vert|ind)ix|ex$": "$1ices",
357
+ "(x|ch|ss|sh)$": "$1es",
358
+ "([^aeiouy]|qu)y$": "$1ies",
359
+ "(hive)$": "$1s",
360
+ "(?:([^f])fe|([lr])f)$": "$1$2ves",
361
+ "(shea|lea|loa|thie)f$": "$1ves",
362
+ sis$: "ses",
363
+ "([ti])um$": "$1a",
364
+ "(tomat|potat|ech|her|vet)o$": "$1oes",
365
+ "(bu)s$": "$1ses",
366
+ "(alias)$": "$1es",
367
+ "(octop)us$": "$1i",
368
+ "(ax|test)is$": "$1es",
369
+ "(us)$": "$1es",
370
+ "([^s]+)$": "$1s"
371
+ };
372
+ const irregular = {
373
+ move: "moves",
374
+ foot: "feet",
375
+ goose: "geese",
376
+ sex: "sexes",
377
+ child: "children",
378
+ man: "men",
379
+ tooth: "teeth",
380
+ person: "people"
381
+ };
382
+ if ([
383
+ "sheep",
384
+ "fish",
385
+ "deer",
386
+ "moose",
387
+ "series",
388
+ "species",
389
+ "money",
390
+ "rice",
391
+ "information",
392
+ "equipment",
393
+ "bison",
394
+ "cod",
395
+ "offspring",
396
+ "pike",
397
+ "salmon",
398
+ "shrimp",
399
+ "swine",
400
+ "trout",
401
+ "aircraft",
402
+ "hovercraft",
403
+ "spacecraft",
404
+ "sugar",
405
+ "tuna",
406
+ "you",
407
+ "wood"
408
+ ].indexOf(word.toLowerCase()) >= 0) return word;
409
+ for (const w in irregular) {
410
+ const pattern = new RegExp(`${w}$`, "i");
411
+ const replace = irregular[w];
412
+ if (pattern.test(word)) return word.replace(pattern, replace);
413
+ }
414
+ for (const reg in plurals) {
415
+ const pattern = new RegExp(reg, "i");
416
+ if (pattern.test(word)) return word.replace(pattern, plurals[reg]);
417
+ }
418
+ return word;
419
+ }
420
+ /**
421
+ * Returns the singular of an English word.
422
+ *
423
+ * @param {string} word
424
+ * @param {number} [amount]
425
+ * @returns {string}
426
+ */
463
427
  function singular(word, amount) {
464
- if (amount !== void 0 && amount !== 1) {
465
- return word;
466
- }
467
- const singulars = {
468
- "(quiz)zes$": "$1",
469
- "(matr)ices$": "$1ix",
470
- "(vert|ind)ices$": "$1ex",
471
- "^(ox)en$": "$1",
472
- "(alias)es$": "$1",
473
- "(octop|vir)i$": "$1us",
474
- "(cris|ax|test)es$": "$1is",
475
- "(shoe)s$": "$1",
476
- "(o)es$": "$1",
477
- "(bus)es$": "$1",
478
- "([m|l])ice$": "$1ouse",
479
- "(x|ch|ss|sh)es$": "$1",
480
- "(m)ovies$": "$1ovie",
481
- "(s)eries$": "$1eries",
482
- "([^aeiouy]|qu)ies$": "$1y",
483
- "([lr])ves$": "$1f",
484
- "(tive)s$": "$1",
485
- "(hive)s$": "$1",
486
- "(li|wi|kni)ves$": "$1fe",
487
- "(shea|loa|lea|thie)ves$": "$1f",
488
- "(^analy)ses$": "$1sis",
489
- "((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$": "$1$2sis",
490
- "([ti])a$": "$1um",
491
- "(n)ews$": "$1ews",
492
- "(h|bl)ouses$": "$1ouse",
493
- "(corpse)s$": "$1",
494
- "(us)es$": "$1",
495
- s$: ""
496
- };
497
- const irregular = {
498
- move: "moves",
499
- foot: "feet",
500
- goose: "geese",
501
- sex: "sexes",
502
- child: "children",
503
- man: "men",
504
- tooth: "teeth",
505
- person: "people"
506
- };
507
- const uncountable = ["sheep", "fish", "deer", "moose", "series", "species", "money", "rice", "information", "equipment", "bison", "cod", "offspring", "pike", "salmon", "shrimp", "swine", "trout", "aircraft", "hovercraft", "spacecraft", "sugar", "tuna", "you", "wood"];
508
- if (uncountable.indexOf(word.toLowerCase()) >= 0) {
509
- return word;
510
- }
511
- for (const w in irregular) {
512
- const pattern = new RegExp(`${irregular[w]}$`, "i");
513
- if (pattern.test(word)) {
514
- return word.replace(pattern, w);
515
- }
516
- }
517
- for (const reg in singulars) {
518
- const pattern = new RegExp(reg, "i");
519
- if (pattern.test(word)) {
520
- return word.replace(pattern, singulars[reg]);
521
- }
522
- }
523
- return word;
524
- }
525
- function getOS() {
526
- let OS = "Unknown";
527
- if (navigator.userAgent.indexOf("Win") !== -1) OS = "Windows";
528
- if (navigator.userAgent.indexOf("Mac") !== -1) OS = "MacOS";
529
- if (navigator.userAgent.indexOf("X11") !== -1) OS = "UNIX";
530
- if (navigator.userAgent.indexOf("Linux") !== -1) OS = "Linux";
531
- return OS;
532
- }
533
- function getAltSymbol() {
534
- if (getOS() === "MacOS") return "⌥";
535
- return "Alt";
536
- }
428
+ if (amount !== void 0 && amount !== 1) return word;
429
+ const singulars = {
430
+ "(quiz)zes$": "$1",
431
+ "(matr)ices$": "$1ix",
432
+ "(vert|ind)ices$": "$1ex",
433
+ "^(ox)en$": "$1",
434
+ "(alias)es$": "$1",
435
+ "(octop|vir)i$": "$1us",
436
+ "(cris|ax|test)es$": "$1is",
437
+ "(shoe)s$": "$1",
438
+ "(o)es$": "$1",
439
+ "(bus)es$": "$1",
440
+ "([m|l])ice$": "$1ouse",
441
+ "(x|ch|ss|sh)es$": "$1",
442
+ "(m)ovies$": "$1ovie",
443
+ "(s)eries$": "$1eries",
444
+ "([^aeiouy]|qu)ies$": "$1y",
445
+ "([lr])ves$": "$1f",
446
+ "(tive)s$": "$1",
447
+ "(hive)s$": "$1",
448
+ "(li|wi|kni)ves$": "$1fe",
449
+ "(shea|loa|lea|thie)ves$": "$1f",
450
+ "(^analy)ses$": "$1sis",
451
+ "((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$": "$1$2sis",
452
+ "([ti])a$": "$1um",
453
+ "(n)ews$": "$1ews",
454
+ "(h|bl)ouses$": "$1ouse",
455
+ "(corpse)s$": "$1",
456
+ "(us)es$": "$1",
457
+ s$: ""
458
+ };
459
+ const irregular = {
460
+ move: "moves",
461
+ foot: "feet",
462
+ goose: "geese",
463
+ sex: "sexes",
464
+ child: "children",
465
+ man: "men",
466
+ tooth: "teeth",
467
+ person: "people"
468
+ };
469
+ if ([
470
+ "sheep",
471
+ "fish",
472
+ "deer",
473
+ "moose",
474
+ "series",
475
+ "species",
476
+ "money",
477
+ "rice",
478
+ "information",
479
+ "equipment",
480
+ "bison",
481
+ "cod",
482
+ "offspring",
483
+ "pike",
484
+ "salmon",
485
+ "shrimp",
486
+ "swine",
487
+ "trout",
488
+ "aircraft",
489
+ "hovercraft",
490
+ "spacecraft",
491
+ "sugar",
492
+ "tuna",
493
+ "you",
494
+ "wood"
495
+ ].indexOf(word.toLowerCase()) >= 0) return word;
496
+ for (const w in irregular) {
497
+ const pattern = new RegExp(`${irregular[w]}$`, "i");
498
+ if (pattern.test(word)) return word.replace(pattern, w);
499
+ }
500
+ for (const reg in singulars) {
501
+ const pattern = new RegExp(reg, "i");
502
+ if (pattern.test(word)) return word.replace(pattern, singulars[reg]);
503
+ }
504
+ return word;
505
+ }
506
+ //#endregion
507
+ //#region src/names.ts
508
+ /**
509
+ * Generates a foreign key column name from a given string, typically a collection slug or name.
510
+ * It converts the name to snake_case, attempts to singularize it by removing a trailing 's'
511
+ * (a common convention for collection names), and appends '_id'.
512
+ *
513
+ * @param name The base name to convert to a foreign key.
514
+ * @returns A foreign key name in the format 'singular_name_id'.
515
+ *
516
+ * @example
517
+ * // returns "user_id"
518
+ * generateForeignKeyName("users")
519
+ *
520
+ * @example
521
+ * // returns "post_id"
522
+ * generateForeignKeyName("posts")
523
+ *
524
+ * @example
525
+ * // returns "product_id"
526
+ * generateForeignKeyName("Product")
527
+ *
528
+ */
537
529
  function generateForeignKeyName(name) {
538
- const snakeCaseName = toSnakeCase(name);
539
- const singularName = snakeCaseName.endsWith("s") ? snakeCaseName.slice(0, -1) : snakeCaseName;
540
- return `${singularName}_id`;
530
+ const snakeCaseName = toSnakeCase(name);
531
+ return `${snakeCaseName.endsWith("s") ? snakeCaseName.slice(0, -1) : snakeCaseName}_id`;
541
532
  }
533
+ //#endregion
534
+ //#region src/fields.ts
542
535
  function isDefaultFieldConfigId(id) {
543
- return ["text_field", "multiline", "markdown", "url", "email", "switch", "select", "multi_select", "number_input", "number_select", "multi_number_select", "file_upload", "multi_file_upload", "reference_as_string", "reference", "multi_references", "relation", "date_time", "group", "key_value", "repeat", "custom_array", "block"].includes(id);
544
- }
545
- export {
546
- camelCase,
547
- clone,
548
- defaultDateFormat,
549
- flattenObject,
550
- generateForeignKeyName,
551
- getAltSymbol,
552
- getArrayValuesCount,
553
- getHashValue,
554
- getIn,
555
- getOS,
556
- getValueInPath,
557
- hashString,
558
- hydrateRegExp,
559
- isDefaultFieldConfigId,
560
- isEmptyArray,
561
- isEmptyObject,
562
- isFunction,
563
- isInteger,
564
- isNaN,
565
- isObject,
566
- isPlainObject,
567
- isValidRegExp,
568
- mergeDeep,
569
- pick,
570
- plural,
571
- prettifyIdentifier,
572
- randomColor,
573
- randomString,
574
- removeFunctions,
575
- removeInPath,
576
- removeNulls,
577
- removePropsIfExisting,
578
- removeUndefined,
579
- serializeRegExp,
580
- setIn,
581
- singular,
582
- slugify,
583
- toArray,
584
- toKebabCase,
585
- toSnakeCase,
586
- unslugify
587
- };
588
- //# sourceMappingURL=index.es.js.map
536
+ return [
537
+ "text_field",
538
+ "multiline",
539
+ "markdown",
540
+ "url",
541
+ "email",
542
+ "switch",
543
+ "select",
544
+ "multi_select",
545
+ "number_input",
546
+ "number_select",
547
+ "multi_number_select",
548
+ "file_upload",
549
+ "multi_file_upload",
550
+ "reference",
551
+ "multi_references",
552
+ "relation",
553
+ "date_time",
554
+ "group",
555
+ "key_value",
556
+ "repeat",
557
+ "custom_array",
558
+ "block"
559
+ ].includes(id);
560
+ }
561
+ //#endregion
562
+ export { camelCase, clone, deepClone, defaultDateFormat, flattenObject, generateForeignKeyName, getArrayValuesCount, getHashValue, getIn, getValueInPath, hashString, hydrateRegExp, isDefaultFieldConfigId, isEmptyArray, isEmptyObject, isFunction, isInteger, isNaN, isObject, isPlainObject, isValidRegExp, mergeDeep, pick, plural, prettifyIdentifier, randomColor, randomString, removeFunctions, removeInPath, removeNulls, removePropsIfExisting, removeUndefined, serializeRegExp, setIn, singular, slugify, toArray, toKebabCase, toSnakeCase, unslugify };
563
+
564
+ //# sourceMappingURL=index.es.js.map