@jsb188/app 1.0.4 → 1.1.2
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/package.json +57 -37
- package/src/constants/agent.ts +74 -0
- package/src/constants/app.ts +389 -0
- package/src/constants/emoji.ts +9153 -0
- package/src/constants/other.ts +13 -0
- package/src/constants/user.ts +27 -0
- package/src/constants/wsActions.ts +49 -0
- package/src/i18n/account.ts +139 -0
- package/src/i18n/agent.ts +397 -0
- package/src/i18n/agentToolBusiness.ts +196 -0
- package/src/i18n/agentToolGrid.ts +137 -0
- package/src/i18n/agentToolWorkflow.ts +101 -0
- package/src/i18n/agent_note.ts +109 -0
- package/src/i18n/app.ts +124 -0
- package/src/i18n/auth.ts +128 -0
- package/src/i18n/billing.ts +7 -0
- package/src/i18n/c.ts +907 -0
- package/src/i18n/campaign.ts +919 -0
- package/src/i18n/country.ts +249 -0
- package/src/i18n/datetime.ts +66 -0
- package/src/i18n/editor.ts +58 -0
- package/src/i18n/error.ts +579 -0
- package/src/i18n/field_doc_editor.ts +21 -0
- package/src/i18n/field_doc_programs.ts +52 -0
- package/src/i18n/field_docs.ts +190 -0
- package/src/i18n/index.ts +64 -0
- package/src/i18n/log.ts +566 -0
- package/src/i18n/memory.ts +59 -0
- package/src/i18n/message.ts +13 -0
- package/src/i18n/note.ts +19 -0
- package/src/i18n/org.ts +306 -0
- package/src/i18n/schedule.ts +209 -0
- package/src/i18n/sheet.ts +339 -0
- package/src/i18n/sheet_formula.ts +165 -0
- package/src/i18n/workflow.ts +111 -0
- package/src/main.ts +89 -0
- package/src/platform.ts +166 -0
- package/src/types/ai.d.ts +63 -0
- package/src/types/app.d.ts +127 -0
- package/src/types/auth.d.ts +94 -0
- package/src/types/console.d.ts +3 -0
- package/src/types/node-polyglot.d.ts +23 -0
- package/src/types/other.d.ts +66 -0
- package/src/types.d.ts +29 -0
- package/src/utils/account.ts +97 -0
- package/src/utils/api.ts +358 -0
- package/src/utils/authSession.ts +24 -0
- package/src/utils/clientIP.ts +213 -0
- package/src/utils/color.ts +279 -0
- package/src/utils/cron.ts +310 -0
- package/src/utils/csv.ts +19 -0
- package/src/utils/datetime.ts +1394 -0
- package/src/utils/deferredSave.ts +89 -0
- package/src/utils/email.ts +39 -0
- package/src/utils/emoji.ts +174 -0
- package/src/utils/expression.helpers.ts +470 -0
- package/src/utils/expression.test.ts +793 -0
- package/src/utils/expression.ts +2261 -0
- package/src/utils/interval.ts +80 -0
- package/src/utils/language.ts +9 -0
- package/src/utils/logic.ts +299 -0
- package/src/utils/managedSession.ts +34 -0
- package/src/utils/managedSessionBff.test.ts +60 -0
- package/src/utils/managedSessionBff.ts +117 -0
- package/src/utils/number.ts +338 -0
- package/src/utils/object.ts +916 -0
- package/src/utils/promise.ts +98 -0
- package/src/utils/queryReset.ts +266 -0
- package/src/utils/recurringTask.ts +60 -0
- package/src/utils/schema.ts +293 -0
- package/src/utils/string.test.ts +278 -0
- package/src/utils/string.ts +709 -0
- package/src/utils/timeZone.ts +2048 -0
- package/src/utils/url_client.ts +206 -0
- package/tsconfig.json +12 -0
- package/constants/app.ts +0 -47
- package/constants/billing.ts +0 -146
- package/constants/emoji.ts +0 -8945
- package/constants/user.ts +0 -36
- package/constants/ws.ts +0 -47
- package/i18n/app.ts +0 -99
- package/i18n/auth.ts +0 -57
- package/i18n/billing.ts +0 -94
- package/i18n/datetime.ts +0 -22
- package/i18n/error.ts +0 -138
- package/i18n/form.ts +0 -73
- package/i18n/index.ts +0 -6
- package/i18n/user.ts +0 -98
- package/main.ts +0 -62
- package/types/app.d.ts +0 -46
- package/types/datetime.d.ts +0 -10
- package/types/form.d.ts +0 -56
- package/types/user.d.ts +0 -52
- package/types.d.ts +0 -23
- package/utils/api.ts +0 -99
- package/utils/billing.ts +0 -134
- package/utils/datetime.ts +0 -358
- package/utils/emoji.ts +0 -151
- package/utils/logic.ts +0 -115
- package/utils/number.ts +0 -115
- package/utils/object.ts +0 -367
- package/utils/string.ts +0 -211
- package/utils/url_client.ts +0 -138
- package/utils/user.ts +0 -273
|
@@ -0,0 +1,916 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Filter array and return only unique values
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export function uniq(
|
|
6
|
+
arr: Array<any>,
|
|
7
|
+
fn?: (value: any, index: number, self: Array<any>) => boolean,
|
|
8
|
+
) {
|
|
9
|
+
return arr.filter((value, index, self) => {
|
|
10
|
+
return self.indexOf(value) === index && (!fn || fn(value, index, self));
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Freeze object
|
|
16
|
+
* NOTE: This is useful for preventing Array from turning into Array-like Object
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// export function freezeObject(obj: object) {
|
|
20
|
+
// if (Array.isArray(obj)) {
|
|
21
|
+
// Object.freeze(obj);
|
|
22
|
+
// }
|
|
23
|
+
|
|
24
|
+
// for (const key in obj) {
|
|
25
|
+
// if (Object.prototype.hasOwnProperty.call(obj, key) && typeof obj[key] === 'object') {
|
|
26
|
+
// freezeObject(obj[key]);
|
|
27
|
+
// }
|
|
28
|
+
// }
|
|
29
|
+
// }
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Remove undefined (and/or null) values from an object
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
export function removeUndefined<T>(
|
|
36
|
+
obj: T,
|
|
37
|
+
removeNull?: boolean,
|
|
38
|
+
removeFalsy?: boolean,
|
|
39
|
+
): T {
|
|
40
|
+
for (const key in obj) {
|
|
41
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
42
|
+
if (
|
|
43
|
+
obj[key] === undefined ||
|
|
44
|
+
(removeNull && obj[key] === null) ||
|
|
45
|
+
(removeFalsy && !obj[key])
|
|
46
|
+
) {
|
|
47
|
+
delete obj[key];
|
|
48
|
+
} else if (obj[key] && typeof obj[key] === 'object') {
|
|
49
|
+
removeUndefined(obj[key], removeNull, removeFalsy);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return obj;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Force undefined values to be null instead
|
|
58
|
+
* NOTE: This is useful for database bulkInserts where undefined values in Postgres ORM causes inconsistent queries
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
export function setUndefinedToNull<T>(obj: T, objMap?: Record<string, any>): T {
|
|
62
|
+
for (const key in obj) {
|
|
63
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
64
|
+
if (obj[key] === undefined) {
|
|
65
|
+
obj[key] = objMap?.[key] ?? null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return obj;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Try/catch parse JSON object
|
|
74
|
+
*/
|
|
75
|
+
|
|
76
|
+
export function parseJSON(jsonStr?: string) {
|
|
77
|
+
if (jsonStr === undefined || jsonStr === null) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
return JSON.parse(jsonStr);
|
|
83
|
+
} catch (err) {
|
|
84
|
+
console.warn(err);
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Parse a Postgres string-array literal into plain string values.
|
|
91
|
+
*/
|
|
92
|
+
|
|
93
|
+
export function parsePostgresStringArray(value: string) {
|
|
94
|
+
const trimmedValue = value.trim();
|
|
95
|
+
if (!trimmedValue.startsWith('{') || !trimmedValue.endsWith('}')) {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const body = trimmedValue.slice(1, -1);
|
|
100
|
+
if (!body) {
|
|
101
|
+
return [];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const values: string[] = [];
|
|
105
|
+
let currentValue = '';
|
|
106
|
+
let isInQuotes = false;
|
|
107
|
+
let isEscaped = false;
|
|
108
|
+
|
|
109
|
+
for (const character of body) {
|
|
110
|
+
if (isEscaped) {
|
|
111
|
+
currentValue += character;
|
|
112
|
+
isEscaped = false;
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (character === '\\' && isInQuotes) {
|
|
117
|
+
isEscaped = true;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (character === '"') {
|
|
122
|
+
isInQuotes = !isInQuotes;
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (character === ',' && !isInQuotes) {
|
|
127
|
+
values.push(currentValue);
|
|
128
|
+
currentValue = '';
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
currentValue += character;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
values.push(currentValue);
|
|
136
|
+
return values;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Normalize arrays, array-like objects, JSON arrays, and Postgres array strings.
|
|
141
|
+
*/
|
|
142
|
+
|
|
143
|
+
export function normalizeArrayLikeValue<T = any>(value: unknown): T[] | null {
|
|
144
|
+
let arrayValue = value;
|
|
145
|
+
|
|
146
|
+
if (!arrayValue) {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (typeof arrayValue === 'string') {
|
|
151
|
+
const trimmedValue = arrayValue.trim();
|
|
152
|
+
|
|
153
|
+
if (!trimmedValue || trimmedValue.toUpperCase() === 'NULL') {
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (trimmedValue.startsWith('[')) {
|
|
158
|
+
arrayValue = parseJSON(trimmedValue);
|
|
159
|
+
} else if (trimmedValue.startsWith('{')) {
|
|
160
|
+
arrayValue = parsePostgresStringArray(trimmedValue);
|
|
161
|
+
} else {
|
|
162
|
+
arrayValue = [trimmedValue];
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (
|
|
167
|
+
arrayValue &&
|
|
168
|
+
typeof arrayValue === 'object' &&
|
|
169
|
+
!Array.isArray(arrayValue)
|
|
170
|
+
) {
|
|
171
|
+
arrayValue = mapArrayLikeObjects(arrayValue);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return Array.isArray(arrayValue) ? (arrayValue as T[]) : null;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Stringify JSON objects with recursive BigInt support. Precision-safe string
|
|
179
|
+
* conversion is the default; callers must opt into lossy number conversion.
|
|
180
|
+
*/
|
|
181
|
+
|
|
182
|
+
export function stringifyJSON(
|
|
183
|
+
obj: any,
|
|
184
|
+
preserveBigIntPrecision: boolean = true,
|
|
185
|
+
): string {
|
|
186
|
+
return JSON.stringify(obj, (_key, value) =>
|
|
187
|
+
typeof value === 'bigint'
|
|
188
|
+
? preserveBigIntPrecision
|
|
189
|
+
? value.toString()
|
|
190
|
+
: Number(value)
|
|
191
|
+
: value,
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Normalize a JSON-like value for order-insensitive serialization: object keys
|
|
197
|
+
* sorted by UTF-16 code unit (never by locale), array order kept, and BigInt
|
|
198
|
+
* and Date values converted to their JSON text forms.
|
|
199
|
+
*/
|
|
200
|
+
|
|
201
|
+
export function canonicalJSONValue(value: unknown): unknown {
|
|
202
|
+
if (typeof value === 'bigint') return String(value);
|
|
203
|
+
if (value instanceof Date) return value.toISOString();
|
|
204
|
+
if (Array.isArray(value)) return value.map(canonicalJSONValue);
|
|
205
|
+
if (value && typeof value === 'object') {
|
|
206
|
+
return Object.fromEntries(
|
|
207
|
+
Object.entries(value)
|
|
208
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
209
|
+
.map(([key, item]) => [key, canonicalJSONValue(item)]),
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
return value;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Serialize JSON with sorted object keys (see canonicalJSONValue), so equal
|
|
217
|
+
* content always produces the same text regardless of property order.
|
|
218
|
+
*/
|
|
219
|
+
|
|
220
|
+
export function stableJSONText(value: unknown): string {
|
|
221
|
+
return JSON.stringify(canonicalJSONValue(value));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Merge nested objects together
|
|
226
|
+
*/
|
|
227
|
+
|
|
228
|
+
export function mergeNestedObjects(obj1: any, obj2: any) {
|
|
229
|
+
if (!obj1) {
|
|
230
|
+
return { ...obj2 };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const newObj = { ...obj1 };
|
|
234
|
+
for (const key in obj2) {
|
|
235
|
+
if (Object.prototype.hasOwnProperty.call(obj2, key)) {
|
|
236
|
+
if (typeof obj2[key] === 'object' && obj2[key] !== null) {
|
|
237
|
+
newObj[key] = mergeNestedObjects(obj1[key], obj2[key]);
|
|
238
|
+
} else {
|
|
239
|
+
newObj[key] = obj2[key];
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return newObj;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Check if objects are equal
|
|
249
|
+
*/
|
|
250
|
+
|
|
251
|
+
export function areObjectsEqual(
|
|
252
|
+
obj1: any,
|
|
253
|
+
obj2: any,
|
|
254
|
+
isNested: boolean = false,
|
|
255
|
+
) {
|
|
256
|
+
if (isNested) {
|
|
257
|
+
return JSON.stringify(obj1) === JSON.stringify(obj2);
|
|
258
|
+
} else if (obj1 && obj2) {
|
|
259
|
+
return Object.keys(obj1).every(
|
|
260
|
+
(key: string) =>
|
|
261
|
+
Object.prototype.hasOwnProperty.call(obj2, key) &&
|
|
262
|
+
obj1[key] === obj2[key],
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
return obj1 === obj2;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Order an array of objects by a key
|
|
270
|
+
*/
|
|
271
|
+
|
|
272
|
+
export function orderBy(
|
|
273
|
+
arr: Array<any>,
|
|
274
|
+
key: ((obj: any) => any) | string,
|
|
275
|
+
order: 'asc' | 'desc' = 'asc',
|
|
276
|
+
) {
|
|
277
|
+
const compareFn = (a: any, b: any) => {
|
|
278
|
+
const valueA = typeof key === 'function' ? key(a) : a[key];
|
|
279
|
+
const valueB = typeof key === 'function' ? key(b) : b[key];
|
|
280
|
+
|
|
281
|
+
if (valueA < valueB) {
|
|
282
|
+
return order === 'asc' ? -1 : 1;
|
|
283
|
+
}
|
|
284
|
+
if (valueA > valueB) {
|
|
285
|
+
return order === 'asc' ? 1 : -1;
|
|
286
|
+
}
|
|
287
|
+
return 0;
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
return arr.sort(compareFn);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Group array by something
|
|
295
|
+
*/
|
|
296
|
+
|
|
297
|
+
export function groupBy(arr: Array<any>, key: string | ((obj: any) => string)) {
|
|
298
|
+
return arr.reduce((acc, obj) => {
|
|
299
|
+
const group = typeof key === 'function' ? key(obj) : obj[key];
|
|
300
|
+
acc[group] = acc[group] || [];
|
|
301
|
+
acc[group].push(obj);
|
|
302
|
+
return acc;
|
|
303
|
+
}, {});
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Make sure each array item is unique by a key check
|
|
308
|
+
*/
|
|
309
|
+
|
|
310
|
+
export function uniqBy(arr: any[], keys: ((obj: any) => any) | string) {
|
|
311
|
+
let keyGetter;
|
|
312
|
+
if (typeof keys === 'function') {
|
|
313
|
+
keyGetter = keys;
|
|
314
|
+
} else if (Array.isArray(keys)) {
|
|
315
|
+
keyGetter = (o: any) => keys.map((k) => get(o, k)).join('');
|
|
316
|
+
} else {
|
|
317
|
+
keyGetter = (o: any) => get(o, keys);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return arr.filter((obj, index, self) => {
|
|
321
|
+
const key = keyGetter(obj);
|
|
322
|
+
return (
|
|
323
|
+
self.findIndex((obj2) => {
|
|
324
|
+
const key2 = keyGetter(obj2);
|
|
325
|
+
return key2 === key;
|
|
326
|
+
}) === index
|
|
327
|
+
);
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Pick a random item from an array
|
|
333
|
+
*/
|
|
334
|
+
|
|
335
|
+
export function randomItem(arr: Array<any>) {
|
|
336
|
+
return arr[Math.floor(Math.random() * arr.length)];
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Rearrange an array left or right
|
|
341
|
+
*/
|
|
342
|
+
|
|
343
|
+
export function rearrangeArray(
|
|
344
|
+
arr: Array<any>,
|
|
345
|
+
loop: number = 1,
|
|
346
|
+
shiftRight?: boolean,
|
|
347
|
+
) {
|
|
348
|
+
if (shiftRight) {
|
|
349
|
+
for (let i = 0; i < loop; i++) {
|
|
350
|
+
arr.unshift(arr.pop());
|
|
351
|
+
}
|
|
352
|
+
} else {
|
|
353
|
+
for (let i = 0; i < loop; i++) {
|
|
354
|
+
arr.push(arr.shift());
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Get the value in an object at path separted by dots
|
|
361
|
+
*/
|
|
362
|
+
|
|
363
|
+
export function get(object: any, path: string) {
|
|
364
|
+
return path.split('.').reduce((o, key) => o && o[key], object);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Zip objects together from a list of alternating keys and values
|
|
369
|
+
* NOTE: Use this after Redis.hgetall()
|
|
370
|
+
*/
|
|
371
|
+
|
|
372
|
+
export function hgetZipObject(
|
|
373
|
+
arr: Array<string>,
|
|
374
|
+
mapperFn?: (value: string, index: number) => any,
|
|
375
|
+
) {
|
|
376
|
+
const obj: Record<string, any> = {};
|
|
377
|
+
for (let i = 0; i < arr.length; i += 2) {
|
|
378
|
+
if (mapperFn) {
|
|
379
|
+
obj[arr[i]] = mapperFn(arr[i + 1], i);
|
|
380
|
+
} else {
|
|
381
|
+
obj[arr[i]] = arr[i + 1];
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return obj;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Zip objects from keys and values
|
|
389
|
+
*/
|
|
390
|
+
|
|
391
|
+
export function zipObject(keys: Array<string>, values: Array<any>) {
|
|
392
|
+
const obj: Record<string, any> = {};
|
|
393
|
+
for (let i = 0; i < keys.length; i++) {
|
|
394
|
+
obj[keys[i]] = values[i];
|
|
395
|
+
}
|
|
396
|
+
return obj;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Find last index of an item in an array
|
|
401
|
+
* NOTE: Use this for best NodeJS and browser compatibility
|
|
402
|
+
*/
|
|
403
|
+
|
|
404
|
+
export function findLastIndex(
|
|
405
|
+
arr: Array<any>,
|
|
406
|
+
fn: (value: any, index: number, array: any[]) => boolean,
|
|
407
|
+
) {
|
|
408
|
+
if (typeof arr.findLastIndex === 'function') {
|
|
409
|
+
return arr.findLastIndex(fn);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
for (let i = arr.length - 1; i >= 0; i--) {
|
|
413
|
+
if (fn(arr[i], i, arr)) {
|
|
414
|
+
return i;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
return -1;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Array intersection
|
|
422
|
+
*/
|
|
423
|
+
|
|
424
|
+
export function intersection(arr1: unknown, arr2: unknown) {
|
|
425
|
+
const normalizedArr1 = normalizeArrayLikeValue(arr1) || [];
|
|
426
|
+
const normalizedArr2 = normalizeArrayLikeValue(arr2) || [];
|
|
427
|
+
|
|
428
|
+
return normalizedArr1.filter((value) => normalizedArr2.includes(value));
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Toggle one value in an array; removes the value when present, appends it otherwise
|
|
433
|
+
*/
|
|
434
|
+
|
|
435
|
+
export function toggleArrayValue<T>(arr: Array<T>, value: T): Array<T> {
|
|
436
|
+
return arr.includes(value)
|
|
437
|
+
? arr.filter((currentValue) => currentValue !== value)
|
|
438
|
+
: [...arr, value];
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Group a collection and merge array fields
|
|
443
|
+
* NOTE: Use this after a findAll() query with inner collections
|
|
444
|
+
*
|
|
445
|
+
* @param collections
|
|
446
|
+
* @param innerCollectionNames
|
|
447
|
+
* @param innerCollectionDefaultValues - Use this to define Array or Object
|
|
448
|
+
* @param primaryKeyName
|
|
449
|
+
* @returns
|
|
450
|
+
*/
|
|
451
|
+
|
|
452
|
+
export function groupCollections(
|
|
453
|
+
collections_: Array<any>,
|
|
454
|
+
innerCollectionNames: string[],
|
|
455
|
+
innerCollectionDefaultValues: Record<string, any> = {},
|
|
456
|
+
primaryKey: string | ((o: any) => string) = 'id',
|
|
457
|
+
innerCollectionPrimaryKeys?: Record<string, (obj: any) => string>,
|
|
458
|
+
isTest?: boolean,
|
|
459
|
+
): any[] {
|
|
460
|
+
/**
|
|
461
|
+
* Check if object has a meaningful (non-null) value, including nested objects.
|
|
462
|
+
*/
|
|
463
|
+
|
|
464
|
+
function hasMeaningfulValue(obj: any): boolean {
|
|
465
|
+
if (obj === null || typeof obj === 'undefined') {
|
|
466
|
+
return false;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
if (typeof obj !== 'object') {
|
|
470
|
+
return true;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
if (Array.isArray(obj)) {
|
|
474
|
+
return obj.some((item) => hasMeaningfulValue(item));
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
const proto = Object.getPrototypeOf(obj);
|
|
478
|
+
if (proto && proto !== Object.prototype) {
|
|
479
|
+
return true;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
for (const key in obj) {
|
|
483
|
+
if (
|
|
484
|
+
!Object.prototype.hasOwnProperty.call(obj, key) ||
|
|
485
|
+
key.startsWith('__')
|
|
486
|
+
) {
|
|
487
|
+
continue;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
if (hasMeaningfulValue(obj[key])) {
|
|
491
|
+
return true;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
return false;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Build a stable key for dedupe checks.
|
|
500
|
+
*/
|
|
501
|
+
|
|
502
|
+
function makeMergeKey(
|
|
503
|
+
obj: any,
|
|
504
|
+
pkFn?: (obj: any) => string,
|
|
505
|
+
pkName?: string,
|
|
506
|
+
): string | number | undefined {
|
|
507
|
+
if (!obj) {
|
|
508
|
+
return undefined;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
if (pkFn) {
|
|
512
|
+
return pkFn(obj);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
if (obj.id !== null && typeof obj.id !== 'undefined') {
|
|
516
|
+
return obj.id;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
if (pkName && obj[pkName] !== null && typeof obj[pkName] !== 'undefined') {
|
|
520
|
+
return obj[pkName];
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
return Object.entries(obj).flat().join('|');
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
let hasHash = false;
|
|
527
|
+
let primaryKeyName;
|
|
528
|
+
let collections = collections_;
|
|
529
|
+
|
|
530
|
+
if (typeof primaryKey === 'function') {
|
|
531
|
+
primaryKeyName = '__primaryKey';
|
|
532
|
+
collections = collections_.map((obj) => ({
|
|
533
|
+
...obj,
|
|
534
|
+
__primaryKey: primaryKey(obj),
|
|
535
|
+
}));
|
|
536
|
+
} else {
|
|
537
|
+
primaryKeyName = primaryKey;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
const groupedCollections = collections.reduce((acc, obj) => {
|
|
541
|
+
const primaryKey = obj[primaryKeyName];
|
|
542
|
+
const i = acc.findIndex((o: any) => o[primaryKeyName] === primaryKey);
|
|
543
|
+
|
|
544
|
+
if (i === -1) {
|
|
545
|
+
// Map everything without hash first (inner array collections)
|
|
546
|
+
innerCollectionNames.forEach((name: string) => {
|
|
547
|
+
const defaultValue = innerCollectionDefaultValues[name] || [];
|
|
548
|
+
const [, afterHash] = name.split('.#.');
|
|
549
|
+
|
|
550
|
+
if (afterHash) {
|
|
551
|
+
// This is a merged array collection
|
|
552
|
+
// This mapping is handled separately after first merge
|
|
553
|
+
// const beforeHashObj = getObject(obj, beforeHash);
|
|
554
|
+
// console.log('beforeHashObj:', beforeHash, beforeHashObj);
|
|
555
|
+
// name = `${beforeHash}.1.${afterHash ? `${afterHash}` : ''}`;
|
|
556
|
+
hasHash = true;
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
const ref = getObject(obj, name);
|
|
561
|
+
|
|
562
|
+
// if (isTest) {
|
|
563
|
+
// console.log('name', name);
|
|
564
|
+
// console.log('ref', ref);
|
|
565
|
+
// console.log('ref', obj);
|
|
566
|
+
// }
|
|
567
|
+
|
|
568
|
+
if (ref) {
|
|
569
|
+
if (hasMeaningfulValue(ref)) {
|
|
570
|
+
setObject(obj, name, Array.isArray(defaultValue) ? [ref] : ref);
|
|
571
|
+
} else {
|
|
572
|
+
setObject(obj, name, defaultValue);
|
|
573
|
+
}
|
|
574
|
+
} else {
|
|
575
|
+
setObject(obj, name, defaultValue);
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
// Old version, but this doesn't support merged array collections
|
|
579
|
+
// if (!obj[name]) {
|
|
580
|
+
// obj[name] = [];
|
|
581
|
+
// } else {
|
|
582
|
+
// obj[name] = [obj[name]];
|
|
583
|
+
// }
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
acc.push(obj);
|
|
587
|
+
} else {
|
|
588
|
+
innerCollectionNames.forEach((name: string) => {
|
|
589
|
+
const defaultValue = innerCollectionDefaultValues[name] || [];
|
|
590
|
+
const [, afterHash] = name.split('.#.');
|
|
591
|
+
|
|
592
|
+
if (afterHash) {
|
|
593
|
+
// This is a merged array collection
|
|
594
|
+
// This mapping is handled separately after first merge
|
|
595
|
+
// const beforeHashObj = getObject(obj, beforeHash);
|
|
596
|
+
// console.log('beforeHashObj:', beforeHash, beforeHashObj);
|
|
597
|
+
// name = `${beforeHash}.1.${afterHash ? `${afterHash}` : ''}`;
|
|
598
|
+
hasHash = true;
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
const innerObj = getObject(obj, name);
|
|
603
|
+
const ref = getObject(acc[i], name);
|
|
604
|
+
|
|
605
|
+
const hasInnerObj = hasMeaningfulValue(innerObj);
|
|
606
|
+
|
|
607
|
+
if (!ref) {
|
|
608
|
+
setObject(acc[i], name, hasInnerObj ? [innerObj] : defaultValue);
|
|
609
|
+
} else if (
|
|
610
|
+
hasInnerObj &&
|
|
611
|
+
!ref.find((o: any) => {
|
|
612
|
+
const innerPKFn = innerCollectionPrimaryKeys?.[name];
|
|
613
|
+
if (innerPKFn) {
|
|
614
|
+
return innerPKFn(o) === innerPKFn(innerObj);
|
|
615
|
+
}
|
|
616
|
+
return (
|
|
617
|
+
(o.id && o.id === innerObj.id) ||
|
|
618
|
+
(!o.id &&
|
|
619
|
+
Object.entries(o).flat().join('|') ===
|
|
620
|
+
Object.entries(innerObj).flat().join('|'))
|
|
621
|
+
);
|
|
622
|
+
})
|
|
623
|
+
) {
|
|
624
|
+
setObject(
|
|
625
|
+
acc[i],
|
|
626
|
+
name,
|
|
627
|
+
Array.isArray(ref) ? ref.concat(innerObj) : innerObj,
|
|
628
|
+
);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// Old version, but this doesn't support merged array collections
|
|
632
|
+
// const innerObj = obj[name];
|
|
633
|
+
// if (!acc[i][name]) {
|
|
634
|
+
// acc[i][name] = innerObj ? [innerObj] : [];
|
|
635
|
+
// } else if (innerObj && !acc[i][name].find((o: any) => o.id === innerObj.id)) {
|
|
636
|
+
// acc[i][name].push(obj[name]);
|
|
637
|
+
// }
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
return acc;
|
|
642
|
+
}, []);
|
|
643
|
+
|
|
644
|
+
if (!hasHash) {
|
|
645
|
+
return groupedCollections;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
innerCollectionNames.forEach((name: string) => {
|
|
649
|
+
const [beforeHash, afterHash] = name.split('.#.');
|
|
650
|
+
if (!afterHash) {
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
const flatName = name.replace('.#.', '.');
|
|
655
|
+
const parentPKFn = innerCollectionPrimaryKeys?.[beforeHash];
|
|
656
|
+
const childPKFn = innerCollectionPrimaryKeys?.[flatName];
|
|
657
|
+
|
|
658
|
+
collections.forEach((obj) => {
|
|
659
|
+
const innerObj = getObject(obj, flatName);
|
|
660
|
+
const primaryKey = obj[primaryKeyName];
|
|
661
|
+
const i = groupedCollections.findIndex(
|
|
662
|
+
(o: any) => o[primaryKeyName] === primaryKey,
|
|
663
|
+
);
|
|
664
|
+
const innerParent = getObject(obj, beforeHash);
|
|
665
|
+
const innerParentId = makeMergeKey(
|
|
666
|
+
innerParent,
|
|
667
|
+
parentPKFn,
|
|
668
|
+
primaryKeyName,
|
|
669
|
+
);
|
|
670
|
+
|
|
671
|
+
if (!innerObj || !hasMeaningfulValue(innerObj)) {
|
|
672
|
+
return;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
if (innerParentId && i >= 0) {
|
|
676
|
+
const innerArr = getObject(groupedCollections[i], beforeHash) || [];
|
|
677
|
+
const j = innerArr.findIndex(
|
|
678
|
+
(o: any) =>
|
|
679
|
+
makeMergeKey(o, parentPKFn, primaryKeyName) === innerParentId,
|
|
680
|
+
);
|
|
681
|
+
|
|
682
|
+
if (j >= 0) {
|
|
683
|
+
const currentValue = innerArr[j][afterHash];
|
|
684
|
+
const currentValueIsArray = Array.isArray(currentValue);
|
|
685
|
+
const actualPathName = `${beforeHash}.${j}.${afterHash}`;
|
|
686
|
+
const nextInnerObjKey = makeMergeKey(innerObj, childPKFn);
|
|
687
|
+
|
|
688
|
+
let newValue;
|
|
689
|
+
if (currentValueIsArray) {
|
|
690
|
+
if (
|
|
691
|
+
!currentValue.find(
|
|
692
|
+
(o) => makeMergeKey(o, childPKFn) === nextInnerObjKey,
|
|
693
|
+
)
|
|
694
|
+
) {
|
|
695
|
+
newValue = currentValue.concat(innerObj);
|
|
696
|
+
}
|
|
697
|
+
} else if (
|
|
698
|
+
currentValue &&
|
|
699
|
+
makeMergeKey(currentValue, childPKFn) !== nextInnerObjKey
|
|
700
|
+
) {
|
|
701
|
+
newValue = [currentValue].concat(innerObj);
|
|
702
|
+
} else if (!currentValue) {
|
|
703
|
+
newValue = [innerObj];
|
|
704
|
+
} else if (!currentValueIsArray) {
|
|
705
|
+
newValue = [currentValue];
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
if (newValue) {
|
|
709
|
+
setObject(groupedCollections[i], actualPathName, newValue);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
});
|
|
714
|
+
|
|
715
|
+
collections.forEach((obj) => {
|
|
716
|
+
const innerParent = getObject(obj, beforeHash);
|
|
717
|
+
|
|
718
|
+
if (Array.isArray(innerParent)) {
|
|
719
|
+
innerParent.forEach((obj: any) => {
|
|
720
|
+
const innerParentObj = getObject(obj, afterHash);
|
|
721
|
+
if (innerParentObj && hasMeaningfulValue(innerParentObj)) {
|
|
722
|
+
if (Array.isArray(innerParentObj)) {
|
|
723
|
+
if (childPKFn) {
|
|
724
|
+
setObject(obj, afterHash, uniqBy(innerParentObj, childPKFn));
|
|
725
|
+
} else {
|
|
726
|
+
setObject(
|
|
727
|
+
obj,
|
|
728
|
+
afterHash,
|
|
729
|
+
uniqBy(innerParentObj, (iObj: any) => makeMergeKey(iObj)),
|
|
730
|
+
);
|
|
731
|
+
}
|
|
732
|
+
} else {
|
|
733
|
+
setObject(obj, afterHash, [innerParentObj]);
|
|
734
|
+
}
|
|
735
|
+
} else {
|
|
736
|
+
setObject(obj, afterHash, []);
|
|
737
|
+
}
|
|
738
|
+
});
|
|
739
|
+
}
|
|
740
|
+
});
|
|
741
|
+
});
|
|
742
|
+
|
|
743
|
+
return groupedCollections;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
/**
|
|
747
|
+
* Check if Object is array-like, then convert to plain Object.
|
|
748
|
+
* NOTE: This happens a lot when using JS Map()
|
|
749
|
+
* NOTE: This assumes that the keys are ordered numbers
|
|
750
|
+
* NOTE: This function always clones the object.
|
|
751
|
+
*/
|
|
752
|
+
|
|
753
|
+
export function cloneArrayLike(obj: any) {
|
|
754
|
+
if (
|
|
755
|
+
!Array.isArray(obj) &&
|
|
756
|
+
Object.keys(obj).every((key, i) => key === i.toString())
|
|
757
|
+
) {
|
|
758
|
+
return Object.values(obj);
|
|
759
|
+
}
|
|
760
|
+
return { ...obj };
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
/**
|
|
764
|
+
* There's a lot of cases where Array[] are automatically converted to Array-like Objects by Javasript itself.
|
|
765
|
+
* For example, when using JS Map() or Set() with a medium to larget amount of data.
|
|
766
|
+
* This function checks if the object is an array-like Object, and returns a plain Object.
|
|
767
|
+
* @param obj - The object to check
|
|
768
|
+
* @returns - Returns a plain Object if the input is an array-like Object, otherwise returns the original object.
|
|
769
|
+
*/
|
|
770
|
+
|
|
771
|
+
export function mapArrayLikeObjects(obj: any) {
|
|
772
|
+
if (
|
|
773
|
+
!Array.isArray(obj) &&
|
|
774
|
+
obj !== null &&
|
|
775
|
+
typeof obj === 'object' &&
|
|
776
|
+
Object.keys(obj).every((key, i) => key === i.toString())
|
|
777
|
+
) {
|
|
778
|
+
return Object.values(obj);
|
|
779
|
+
}
|
|
780
|
+
return obj;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
/**
|
|
784
|
+
* Check if Object is iterable
|
|
785
|
+
* @param obj - The object to check
|
|
786
|
+
* @returns - Returns true if the object is iterable, otherwise false
|
|
787
|
+
*/
|
|
788
|
+
|
|
789
|
+
export function isIterable(obj: any) {
|
|
790
|
+
return obj != null && typeof obj[Symbol.iterator] === 'function';
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
/**
|
|
794
|
+
* Get the last key from a JS Map
|
|
795
|
+
* @param map - The Map to get the last key from
|
|
796
|
+
* @returns - Returns the last key from the Map, or undefined if the Map is empty
|
|
797
|
+
*/
|
|
798
|
+
|
|
799
|
+
export function getLastMapKey(map: Map<any, any>) {
|
|
800
|
+
if (!map || map.size === 0) {
|
|
801
|
+
return undefined;
|
|
802
|
+
}
|
|
803
|
+
return Array.from(map.keys()).pop();
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
/**
|
|
807
|
+
* Get the first key from a JS Map
|
|
808
|
+
* @param map - The Map to get the first key from
|
|
809
|
+
* @returns - Returns the first key from the Map, or undefined if the Map is empty
|
|
810
|
+
*/
|
|
811
|
+
|
|
812
|
+
export function getFirstMapKey(map: Map<any, any>) {
|
|
813
|
+
if (!map || map.size === 0) {
|
|
814
|
+
return undefined;
|
|
815
|
+
}
|
|
816
|
+
return Array.from(map.keys()).shift();
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
/**
|
|
820
|
+
* Get value at path in an object
|
|
821
|
+
* @param obj - The object to get the value from
|
|
822
|
+
* @param path - The path to the value, e.g. 'a.b.c'
|
|
823
|
+
* @returns - Returns the value at the path, or undefined if the path does not exist
|
|
824
|
+
*/
|
|
825
|
+
|
|
826
|
+
export function getObject(obj: any, path: string): any {
|
|
827
|
+
if (!obj || !path) {
|
|
828
|
+
return undefined;
|
|
829
|
+
}
|
|
830
|
+
return path.split('.').reduce((o, key) => {
|
|
831
|
+
if (o && typeof o === 'object' && key in o) {
|
|
832
|
+
return o[key];
|
|
833
|
+
}
|
|
834
|
+
return undefined;
|
|
835
|
+
}, obj);
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
/**
|
|
839
|
+
* Set value at path in an object
|
|
840
|
+
* @param obj - The object to set the value in
|
|
841
|
+
* @param path - The path to the value, e.g. 'a.b.c'
|
|
842
|
+
* @param value - The value to set
|
|
843
|
+
* @returns - Returns the modified object
|
|
844
|
+
*/
|
|
845
|
+
|
|
846
|
+
export function setObject(obj: any, path: string, value: any): any {
|
|
847
|
+
if (!obj || !path) {
|
|
848
|
+
return obj;
|
|
849
|
+
}
|
|
850
|
+
const keys = path.split('.');
|
|
851
|
+
if (
|
|
852
|
+
keys.some((key) => ['__proto__', 'constructor', 'prototype'].includes(key))
|
|
853
|
+
) {
|
|
854
|
+
return obj;
|
|
855
|
+
}
|
|
856
|
+
const lastKey = keys.pop();
|
|
857
|
+
const nestedObj = keys.reduce((o, key) => {
|
|
858
|
+
if (!o[key]) {
|
|
859
|
+
o[key] = {};
|
|
860
|
+
}
|
|
861
|
+
return o[key];
|
|
862
|
+
}, obj);
|
|
863
|
+
|
|
864
|
+
if (lastKey) {
|
|
865
|
+
if (typeof nestedObj === 'object' && nestedObj !== null) {
|
|
866
|
+
nestedObj[lastKey] = value;
|
|
867
|
+
} else {
|
|
868
|
+
console.warn(
|
|
869
|
+
`(!1) Cannot assign value to ${lastKey} because its reference is not an object.`,
|
|
870
|
+
);
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
return obj;
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
/**
|
|
878
|
+
* Check if namespace exists in an object
|
|
879
|
+
* @param obj - The object to check
|
|
880
|
+
* @param namespace - The namespace to check, e.g. 'a.b.c'
|
|
881
|
+
* @returns - Returns true if the namespace exists, otherwise false
|
|
882
|
+
*/
|
|
883
|
+
|
|
884
|
+
export function hasObject(obj: any, namespace: string): boolean {
|
|
885
|
+
if (!obj || !namespace) {
|
|
886
|
+
return false;
|
|
887
|
+
}
|
|
888
|
+
return namespace.split('.').every((key) => {
|
|
889
|
+
if (obj && typeof obj === 'object' && key in obj) {
|
|
890
|
+
obj = obj[key];
|
|
891
|
+
return true;
|
|
892
|
+
}
|
|
893
|
+
return false;
|
|
894
|
+
});
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
/**
|
|
898
|
+
* Sort an Object by keys alphabetically
|
|
899
|
+
* @param obj - The object to sort
|
|
900
|
+
* @returns - Returns a new object with keys sorted alphabetically
|
|
901
|
+
*/
|
|
902
|
+
|
|
903
|
+
export function sortObjectByKeys(
|
|
904
|
+
obj: Record<string, any>,
|
|
905
|
+
): Record<string, any> {
|
|
906
|
+
if (!obj || typeof obj !== 'object') {
|
|
907
|
+
return obj;
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
const sortedKeys = Object.keys(obj).sort();
|
|
911
|
+
const sortedObj: Record<string, any> = {};
|
|
912
|
+
for (const key of sortedKeys) {
|
|
913
|
+
sortedObj[key] = obj[key];
|
|
914
|
+
}
|
|
915
|
+
return sortedObj;
|
|
916
|
+
}
|