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