@jsb188/app 1.1.4 → 1.1.6
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/constants/app.ts +340 -24
- package/constants/other.ts +17 -0
- package/constants/wsActions.ts +21 -0
- package/i18n/{user.ts → account.ts} +21 -38
- package/i18n/app.ts +4 -18
- package/i18n/auth.ts +33 -9
- package/i18n/billing.ts +1 -1
- package/i18n/datetime.ts +57 -18
- package/i18n/error.ts +32 -18
- package/i18n/form.ts +192 -3
- package/main.ts +2 -2
- package/package.json +6 -4
- package/types/account.d.ts +63 -0
- package/types/ai.d.ts +3 -0
- package/types/app.d.ts +26 -1
- package/types/auth.d.ts +6 -1
- package/types/form.d.ts +9 -3
- package/types/other.d.ts +61 -0
- package/utils/action.ts +28 -0
- package/utils/api.ts +1 -1
- package/utils/datetime.ts +417 -56
- package/utils/logic.ts +60 -9
- package/utils/number.ts +160 -8
- package/utils/object.ts +291 -71
- package/utils/string.ts +183 -21
- package/utils/timeZone.ts +612 -436
- package/utils/url_client.ts +17 -22
- package/utils/user.ts +1 -126
- package/constants/organization.ts +0 -8
- package/constants/ws.ts +0 -47
- package/types/organization.d.ts +0 -40
- package/types/user.d.ts +0 -52
package/utils/object.ts
CHANGED
|
@@ -29,16 +29,17 @@ export function uniq(arr: Array<any>, fn?: (value: any, index: number, self: Arr
|
|
|
29
29
|
* Remove undefined (and/or null) values from an object
|
|
30
30
|
*/
|
|
31
31
|
|
|
32
|
-
export function removeUndefined<T>(obj: T, removeNull?: boolean) {
|
|
32
|
+
export function removeUndefined<T>(obj: T, removeNull?: boolean, removeFalsy?: boolean): T {
|
|
33
33
|
for (const key in obj) {
|
|
34
34
|
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
35
35
|
if (
|
|
36
36
|
obj[key] === undefined ||
|
|
37
|
-
(removeNull && obj[key] === null)
|
|
37
|
+
(removeNull && obj[key] === null) ||
|
|
38
|
+
(removeFalsy && !obj[key])
|
|
38
39
|
) {
|
|
39
40
|
delete obj[key];
|
|
40
|
-
} else if (typeof obj[key] === 'object') {
|
|
41
|
-
removeUndefined(obj[key], removeNull);
|
|
41
|
+
} else if (obj[key] && typeof obj[key] === 'object') {
|
|
42
|
+
removeUndefined(obj[key], removeNull, removeFalsy);
|
|
42
43
|
}
|
|
43
44
|
}
|
|
44
45
|
}
|
|
@@ -78,6 +79,19 @@ export function parseJSON(jsonStr?: string) {
|
|
|
78
79
|
return null;
|
|
79
80
|
}
|
|
80
81
|
|
|
82
|
+
/**
|
|
83
|
+
* Stringify JSON objects with BigInt support
|
|
84
|
+
* NOTE: This will work recursively for nested objects as well.
|
|
85
|
+
*/
|
|
86
|
+
|
|
87
|
+
export function stringifyJSON(obj: any, convertBigIntToNumber: boolean = false): string {
|
|
88
|
+
if (!convertBigIntToNumber) {
|
|
89
|
+
return JSON.stringify(obj, (_key, value) => typeof value === 'bigint' ? Number(value) : value);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return JSON.stringify(obj, (_key, value) => typeof value === 'bigint' ? value.toString() : value);
|
|
93
|
+
}
|
|
94
|
+
|
|
81
95
|
/**
|
|
82
96
|
* Merge nested objects together
|
|
83
97
|
*/
|
|
@@ -113,9 +127,7 @@ export function areObjectsEqual(
|
|
|
113
127
|
if (isNested) {
|
|
114
128
|
return JSON.stringify(obj1) === JSON.stringify(obj2);
|
|
115
129
|
} else if (obj1 && obj2) {
|
|
116
|
-
return Object.keys(obj1).every((key: string) =>
|
|
117
|
-
Object.prototype.hasOwnProperty.call(obj2, key) && obj1[key] === obj2[key]
|
|
118
|
-
);
|
|
130
|
+
return Object.keys(obj1).every((key: string) => Object.prototype.hasOwnProperty.call(obj2, key) && obj1[key] === obj2[key]);
|
|
119
131
|
}
|
|
120
132
|
return obj1 === obj2;
|
|
121
133
|
}
|
|
@@ -149,9 +161,9 @@ export function orderBy(
|
|
|
149
161
|
* Group array by something
|
|
150
162
|
*/
|
|
151
163
|
|
|
152
|
-
export function groupBy(arr: Array<any>, key: string) {
|
|
164
|
+
export function groupBy(arr: Array<any>, key: string | ((obj: any) => string)) {
|
|
153
165
|
return arr.reduce((acc, obj) => {
|
|
154
|
-
const group = obj[key];
|
|
166
|
+
const group = typeof key === 'function' ? key(obj) : obj[key];
|
|
155
167
|
acc[group] = acc[group] || [];
|
|
156
168
|
acc[group].push(obj);
|
|
157
169
|
return acc;
|
|
@@ -268,79 +280,82 @@ export function intersection(arr1: Array<any>, arr2: Array<any>) {
|
|
|
268
280
|
return arr1.filter((value) => arr2.includes(value));
|
|
269
281
|
}
|
|
270
282
|
|
|
271
|
-
/**
|
|
272
|
-
* Get a nested value of an object at a property path
|
|
273
|
-
*
|
|
274
|
-
* @param obj - The object to get the value from
|
|
275
|
-
* @param path - The property path as a string, e.g. 'a.b.c'
|
|
276
|
-
*/
|
|
277
|
-
|
|
278
|
-
export function getNestedValue(obj: any, path: string): any {
|
|
279
|
-
if (!obj || !path) {
|
|
280
|
-
return undefined;
|
|
281
|
-
}
|
|
282
|
-
return path.split('.').reduce((o, key) => {
|
|
283
|
-
if (o && typeof o === 'object' && key in o) {
|
|
284
|
-
return o[key];
|
|
285
|
-
}
|
|
286
|
-
return undefined;
|
|
287
|
-
}, obj);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Append value to a nested object
|
|
292
|
-
*
|
|
293
|
-
* @param obj - The object to append to
|
|
294
|
-
* @param path - The property path as a string, e.g. 'a.b.c'
|
|
295
|
-
* @param value - The value to append
|
|
296
|
-
*/
|
|
297
|
-
|
|
298
|
-
export function assignToNestedObject(obj: any, path: string, value: any) {
|
|
299
|
-
if (!obj || !path) {
|
|
300
|
-
return;
|
|
301
|
-
}
|
|
302
|
-
const keys = path.split('.');
|
|
303
|
-
const lastKey = keys.pop();
|
|
304
|
-
const nestedObj = keys.reduce((o, key) => {
|
|
305
|
-
if (!o[key]) {
|
|
306
|
-
o[key] = {};
|
|
307
|
-
}
|
|
308
|
-
return o[key];
|
|
309
|
-
}, obj);
|
|
310
|
-
|
|
311
|
-
if (lastKey) {
|
|
312
|
-
if (typeof nestedObj === 'object' && nestedObj !== null) {
|
|
313
|
-
nestedObj[lastKey] = value;
|
|
314
|
-
} else {
|
|
315
|
-
console.warn(`(!1) Cannot assign value to ${lastKey} because its reference is not an object.`);
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
return obj;
|
|
320
|
-
}
|
|
321
|
-
|
|
322
283
|
/**
|
|
323
284
|
* Group a collection and merge array fields
|
|
324
285
|
* NOTE: Use this after a findAll() query with inner collections
|
|
325
286
|
*
|
|
326
287
|
* @param collections
|
|
327
288
|
* @param innerCollectionNames
|
|
289
|
+
* @param innerCollectionDefaultValues - Use this to define Array or Object
|
|
328
290
|
* @param primaryKeyName
|
|
329
291
|
* @returns
|
|
330
292
|
*/
|
|
331
293
|
|
|
332
|
-
export function groupCollections(
|
|
333
|
-
|
|
294
|
+
export function groupCollections(
|
|
295
|
+
collections_: Array<any>,
|
|
296
|
+
innerCollectionNames: string[],
|
|
297
|
+
innerCollectionDefaultValues: Record<string, any> = {},
|
|
298
|
+
primaryKey: string | ((o: any) => string) = 'id',
|
|
299
|
+
isTest?: boolean,
|
|
300
|
+
): any[] {
|
|
301
|
+
let hasHash = false;
|
|
302
|
+
let primaryKeyName;
|
|
303
|
+
let collections = collections_;
|
|
304
|
+
|
|
305
|
+
if (typeof primaryKey === 'function') {
|
|
306
|
+
primaryKeyName = '__primaryKey';
|
|
307
|
+
collections = collections_.map((obj) => ({
|
|
308
|
+
...obj,
|
|
309
|
+
__primaryKey: primaryKey(obj),
|
|
310
|
+
}));
|
|
311
|
+
} else {
|
|
312
|
+
primaryKeyName = primaryKey;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const groupedCollections = collections.reduce((acc, obj) => {
|
|
334
316
|
const primaryKey = obj[primaryKeyName];
|
|
335
317
|
const i = acc.findIndex((o: any) => o[primaryKeyName] === primaryKey);
|
|
336
318
|
|
|
337
319
|
if (i === -1) {
|
|
320
|
+
// Map everything without hash first (inner array collections)
|
|
338
321
|
innerCollectionNames.forEach((name: string) => {
|
|
339
|
-
const
|
|
340
|
-
|
|
341
|
-
|
|
322
|
+
const defaultValue = innerCollectionDefaultValues[name] || [];
|
|
323
|
+
const [, afterHash] = name.split('.#.');
|
|
324
|
+
|
|
325
|
+
if (afterHash) {
|
|
326
|
+
// This is a merged array collection
|
|
327
|
+
// This mapping is handled separately after first merge
|
|
328
|
+
// const beforeHashObj = getObject(obj, beforeHash);
|
|
329
|
+
// console.log('beforeHashObj:', beforeHash, beforeHashObj);
|
|
330
|
+
// name = `${beforeHash}.1.${afterHash ? `${afterHash}` : ''}`;
|
|
331
|
+
hasHash = true;
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const ref = getObject(obj, name);
|
|
336
|
+
|
|
337
|
+
// if (isTest) {
|
|
338
|
+
// console.log('name', name);
|
|
339
|
+
// console.log('ref', ref);
|
|
340
|
+
// console.log('ref', obj);
|
|
341
|
+
// }
|
|
342
|
+
|
|
343
|
+
if (ref) {
|
|
344
|
+
let isNull = true;
|
|
345
|
+
for (const key in ref) {
|
|
346
|
+
if (!key.startsWith('__') && ref[key] !== null) {
|
|
347
|
+
isNull = false;
|
|
348
|
+
break;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (!isNull) {
|
|
353
|
+
setObject(obj, name, Array.isArray(defaultValue) ? [ref] : ref);
|
|
354
|
+
} else {
|
|
355
|
+
setObject(obj, name, defaultValue);
|
|
356
|
+
}
|
|
342
357
|
} else {
|
|
343
|
-
|
|
358
|
+
setObject(obj, name, defaultValue);
|
|
344
359
|
}
|
|
345
360
|
|
|
346
361
|
// Old version, but this doesn't support merged array collections
|
|
@@ -354,13 +369,32 @@ export function groupCollections(collections: Array<any>, innerCollectionNames:
|
|
|
354
369
|
acc.push(obj);
|
|
355
370
|
} else {
|
|
356
371
|
innerCollectionNames.forEach((name: string) => {
|
|
357
|
-
const
|
|
358
|
-
const
|
|
372
|
+
const defaultValue = innerCollectionDefaultValues[name] || [];
|
|
373
|
+
const [, afterHash] = name.split('.#.');
|
|
374
|
+
|
|
375
|
+
if (afterHash) {
|
|
376
|
+
// This is a merged array collection
|
|
377
|
+
// This mapping is handled separately after first merge
|
|
378
|
+
// const beforeHashObj = getObject(obj, beforeHash);
|
|
379
|
+
// console.log('beforeHashObj:', beforeHash, beforeHashObj);
|
|
380
|
+
// name = `${beforeHash}.1.${afterHash ? `${afterHash}` : ''}`;
|
|
381
|
+
hasHash = true;
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const innerObj = getObject(obj, name);
|
|
386
|
+
const ref = getObject(acc[i], name);
|
|
359
387
|
|
|
360
388
|
if (!ref) {
|
|
361
|
-
|
|
362
|
-
} else if (
|
|
363
|
-
|
|
389
|
+
setObject(acc[i], name, innerObj && Array.isArray(innerObj) ? [innerObj] : (innerObj || defaultValue));
|
|
390
|
+
} else if (
|
|
391
|
+
innerObj &&
|
|
392
|
+
!ref.find((o: any) =>
|
|
393
|
+
(o.id && o.id === innerObj.id) ||
|
|
394
|
+
(!o.id && Object.entries(o).flat().join('|') === Object.entries(innerObj).flat().join('|'))
|
|
395
|
+
)
|
|
396
|
+
) {
|
|
397
|
+
setObject(acc[i], name, Array.isArray(ref) ? ref.concat(innerObj) : innerObj);
|
|
364
398
|
}
|
|
365
399
|
|
|
366
400
|
// Old version, but this doesn't support merged array collections
|
|
@@ -375,6 +409,78 @@ export function groupCollections(collections: Array<any>, innerCollectionNames:
|
|
|
375
409
|
|
|
376
410
|
return acc;
|
|
377
411
|
}, []);
|
|
412
|
+
|
|
413
|
+
if (!hasHash) {
|
|
414
|
+
return groupedCollections;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
innerCollectionNames.forEach((name: string) => {
|
|
418
|
+
const [beforeHash, afterHash] = name.split('.#.');
|
|
419
|
+
if (!afterHash) {
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
const flatName = name.replace('.#.', '.');
|
|
424
|
+
|
|
425
|
+
collections.forEach((obj) => {
|
|
426
|
+
const innerObj = getObject(obj, flatName);
|
|
427
|
+
const primaryKey = obj[primaryKeyName];
|
|
428
|
+
const i = groupedCollections.findIndex((o: any) => o[primaryKeyName] === primaryKey);
|
|
429
|
+
const innerParent = getObject(obj, beforeHash);
|
|
430
|
+
const innerParentId = innerParent?.[primaryKeyName];
|
|
431
|
+
|
|
432
|
+
if (!innerObj) {
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
if (innerParentId && i >= 0) {
|
|
437
|
+
const innerArr = getObject(groupedCollections[i], beforeHash) || [];
|
|
438
|
+
const j = innerArr.findIndex((o: any) => o[primaryKeyName] === innerParentId);
|
|
439
|
+
|
|
440
|
+
if (j >= 0) {
|
|
441
|
+
const currentValue = innerArr[j][afterHash];
|
|
442
|
+
const currentValueIsArray = Array.isArray(currentValue);
|
|
443
|
+
const actualPathName = `${beforeHash}.${j}.${afterHash}`;
|
|
444
|
+
|
|
445
|
+
let newValue;
|
|
446
|
+
if (currentValueIsArray) {
|
|
447
|
+
if (!currentValue.find((o) => o.id === innerObj.id)) {
|
|
448
|
+
newValue = currentValue.concat(innerObj);
|
|
449
|
+
}
|
|
450
|
+
} else if (currentValue && currentValue.id !== innerObj.id) {
|
|
451
|
+
newValue = [currentValue].concat(innerObj);
|
|
452
|
+
} else if (!currentValue) {
|
|
453
|
+
newValue = [innerObj];
|
|
454
|
+
} else if (!currentValueIsArray) {
|
|
455
|
+
newValue = [currentValue];
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
if (newValue) {
|
|
459
|
+
setObject(groupedCollections[i], actualPathName, newValue);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
collections.forEach((obj) => {
|
|
466
|
+
const innerParent = getObject(obj, beforeHash);
|
|
467
|
+
|
|
468
|
+
if (Array.isArray(innerParent)) {
|
|
469
|
+
innerParent.forEach((obj: any) => {
|
|
470
|
+
const innerParentObj = getObject(obj, afterHash);
|
|
471
|
+
if (innerParentObj) {
|
|
472
|
+
if (Array.isArray(innerParentObj)) {
|
|
473
|
+
setObject(obj, afterHash, uniqBy(innerParentObj, primaryKeyName));
|
|
474
|
+
} else {
|
|
475
|
+
setObject(obj, afterHash, [innerParentObj]);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
return groupedCollections;
|
|
378
484
|
}
|
|
379
485
|
|
|
380
486
|
/**
|
|
@@ -394,8 +500,28 @@ export function cloneArrayLike(obj: any) {
|
|
|
394
500
|
return { ...obj };
|
|
395
501
|
}
|
|
396
502
|
|
|
503
|
+
/**
|
|
504
|
+
* There's a lot of cases where Array[] are automatically converted to Array-like Objects by Javasript itself.
|
|
505
|
+
* For example, when using JS Map() or Set() with a medium to larget amount of data.
|
|
506
|
+
* This function checks if the object is an array-like Object, and returns a plain Object.
|
|
507
|
+
* @param obj - The object to check
|
|
508
|
+
* @returns - Returns a plain Object if the input is an array-like Object, otherwise returns the original object.
|
|
509
|
+
*/
|
|
510
|
+
|
|
511
|
+
export function mapArrayLikeObjects(obj: any) {
|
|
512
|
+
if (
|
|
513
|
+
!Array.isArray(obj) && obj !== null && typeof obj === 'object' &&
|
|
514
|
+
Object.keys(obj).every((key, i) => key === i.toString())
|
|
515
|
+
) {
|
|
516
|
+
return Object.values(obj);
|
|
517
|
+
}
|
|
518
|
+
return obj;
|
|
519
|
+
}
|
|
520
|
+
|
|
397
521
|
/**
|
|
398
522
|
* Check if Object is iterable
|
|
523
|
+
* @param obj - The object to check
|
|
524
|
+
* @returns - Returns true if the object is iterable, otherwise false
|
|
399
525
|
*/
|
|
400
526
|
|
|
401
527
|
export function isIterable(obj: any) {
|
|
@@ -404,6 +530,8 @@ export function isIterable(obj: any) {
|
|
|
404
530
|
|
|
405
531
|
/**
|
|
406
532
|
* Get the last key from a JS Map
|
|
533
|
+
* @param map - The Map to get the last key from
|
|
534
|
+
* @returns - Returns the last key from the Map, or undefined if the Map is empty
|
|
407
535
|
*/
|
|
408
536
|
|
|
409
537
|
export function getLastMapKey(map: Map<any, any>) {
|
|
@@ -415,6 +543,8 @@ export function getLastMapKey(map: Map<any, any>) {
|
|
|
415
543
|
|
|
416
544
|
/**
|
|
417
545
|
* Get the first key from a JS Map
|
|
546
|
+
* @param map - The Map to get the first key from
|
|
547
|
+
* @returns - Returns the first key from the Map, or undefined if the Map is empty
|
|
418
548
|
*/
|
|
419
549
|
|
|
420
550
|
export function getFirstMapKey(map: Map<any, any>) {
|
|
@@ -423,3 +553,93 @@ export function getFirstMapKey(map: Map<any, any>) {
|
|
|
423
553
|
}
|
|
424
554
|
return Array.from(map.keys()).shift();
|
|
425
555
|
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Get value at path in an object
|
|
559
|
+
* @param obj - The object to get the value from
|
|
560
|
+
* @param path - The path to the value, e.g. 'a.b.c'
|
|
561
|
+
* @returns - Returns the value at the path, or undefined if the path does not exist
|
|
562
|
+
*/
|
|
563
|
+
|
|
564
|
+
export function getObject(obj: any, path: string): any {
|
|
565
|
+
if (!obj || !path) {
|
|
566
|
+
return undefined;
|
|
567
|
+
}
|
|
568
|
+
return path.split('.').reduce((o, key) => {
|
|
569
|
+
if (o && typeof o === 'object' && key in o) {
|
|
570
|
+
return o[key];
|
|
571
|
+
}
|
|
572
|
+
return undefined;
|
|
573
|
+
}, obj);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* Set value at path in an object
|
|
578
|
+
* @param obj - The object to set the value in
|
|
579
|
+
* @param path - The path to the value, e.g. 'a.b.c'
|
|
580
|
+
* @param value - The value to set
|
|
581
|
+
* @returns - Returns the modified object
|
|
582
|
+
*/
|
|
583
|
+
|
|
584
|
+
export function setObject(obj: any, path: string, value: any): any {
|
|
585
|
+
if (!obj || !path) {
|
|
586
|
+
return obj;
|
|
587
|
+
}
|
|
588
|
+
const keys = path.split('.');
|
|
589
|
+
const lastKey = keys.pop();
|
|
590
|
+
const nestedObj = keys.reduce((o, key) => {
|
|
591
|
+
if (!o[key]) {
|
|
592
|
+
o[key] = {};
|
|
593
|
+
}
|
|
594
|
+
return o[key];
|
|
595
|
+
}, obj);
|
|
596
|
+
|
|
597
|
+
if (lastKey) {
|
|
598
|
+
if (typeof nestedObj === 'object' && nestedObj !== null) {
|
|
599
|
+
nestedObj[lastKey] = value;
|
|
600
|
+
} else {
|
|
601
|
+
console.warn(`(!1) Cannot assign value to ${lastKey} because its reference is not an object.`);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
return obj;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* Check if namespace exists in an object
|
|
610
|
+
* @param obj - The object to check
|
|
611
|
+
* @param namespace - The namespace to check, e.g. 'a.b.c'
|
|
612
|
+
* @returns - Returns true if the namespace exists, otherwise false
|
|
613
|
+
*/
|
|
614
|
+
|
|
615
|
+
export function hasObject(obj: any, namespace: string): boolean {
|
|
616
|
+
if (!obj || !namespace) {
|
|
617
|
+
return false;
|
|
618
|
+
}
|
|
619
|
+
return namespace.split('.').every((key) => {
|
|
620
|
+
if (obj && typeof obj === 'object' && key in obj) {
|
|
621
|
+
obj = obj[key];
|
|
622
|
+
return true;
|
|
623
|
+
}
|
|
624
|
+
return false;
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* Sort an Object by keys alphabetically
|
|
630
|
+
* @param obj - The object to sort
|
|
631
|
+
* @returns - Returns a new object with keys sorted alphabetically
|
|
632
|
+
*/
|
|
633
|
+
|
|
634
|
+
export function sortObjectByKeys(obj: Record<string, any>): Record<string, any> {
|
|
635
|
+
if (!obj || typeof obj !== 'object') {
|
|
636
|
+
return obj;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
const sortedKeys = Object.keys(obj).sort();
|
|
640
|
+
const sortedObj: Record<string, any> = {};
|
|
641
|
+
for (const key of sortedKeys) {
|
|
642
|
+
sortedObj[key] = obj[key];
|
|
643
|
+
}
|
|
644
|
+
return sortedObj;
|
|
645
|
+
}
|
package/utils/string.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
'kajiwoto',
|
|
4
|
-
];
|
|
1
|
+
import i18n from '@jsb188/app/i18n';
|
|
2
|
+
import type { AddressObj } from '../types/other.d';
|
|
5
3
|
|
|
6
4
|
/**
|
|
7
5
|
* Combine list of class names
|
|
@@ -72,8 +70,8 @@ export function guessFirstName(str: string, len: number = 40): string {
|
|
|
72
70
|
export function isEmail(str: string) {
|
|
73
71
|
// return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(str);
|
|
74
72
|
const emailRegex =
|
|
75
|
-
/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])
|
|
76
|
-
return emailRegex.test(str.trim());
|
|
73
|
+
/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i;
|
|
74
|
+
return !!str && emailRegex.test(str.trim());
|
|
77
75
|
}
|
|
78
76
|
|
|
79
77
|
/**
|
|
@@ -89,7 +87,7 @@ export function convertToInternationalPhone(str: string) {
|
|
|
89
87
|
}
|
|
90
88
|
|
|
91
89
|
const digits = str.replace(/[^0-9]/g, '');
|
|
92
|
-
if (digits.startsWith('0')) {
|
|
90
|
+
if (!digits || digits.startsWith('0')) {
|
|
93
91
|
// Invalid
|
|
94
92
|
return null;
|
|
95
93
|
} else if (
|
|
@@ -106,6 +104,7 @@ export function convertToInternationalPhone(str: string) {
|
|
|
106
104
|
/**
|
|
107
105
|
* Check if phone number
|
|
108
106
|
* @returns TRUE if starts with + and has 7-15 digits
|
|
107
|
+
* NOTE: This will convert phone-like strings to international format
|
|
109
108
|
*/
|
|
110
109
|
|
|
111
110
|
export function isPhone(str: string, doNotConvertInternational: boolean = false) {
|
|
@@ -119,18 +118,6 @@ export function isPhone(str: string, doNotConvertInternational: boolean = false)
|
|
|
119
118
|
return /^\+[0-9]{7,15}$/.test(phoneNumber);
|
|
120
119
|
}
|
|
121
120
|
|
|
122
|
-
/**
|
|
123
|
-
* Check if valid username
|
|
124
|
-
*/
|
|
125
|
-
|
|
126
|
-
export function isValidUsername(str: string) {
|
|
127
|
-
const username = str.replace(/^@/, '').toLowerCase();
|
|
128
|
-
return (
|
|
129
|
-
/^[a-z0-9_]+$/i.test(username) &&
|
|
130
|
-
!NOT_ALLOWED_USERNAMES.includes(username)
|
|
131
|
-
);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
121
|
/**
|
|
135
122
|
* Check if password is secure enough
|
|
136
123
|
* Rules:
|
|
@@ -266,10 +253,25 @@ export function isInvisibleString(str: string) {
|
|
|
266
253
|
* NOTE: Userful for building AI prompts
|
|
267
254
|
*/
|
|
268
255
|
|
|
269
|
-
|
|
256
|
+
type AnyLineItem = string | undefined | false | null;
|
|
257
|
+
|
|
258
|
+
export function buildSingleText(
|
|
259
|
+
lines: (AnyLineItem | Record<string, AnyLineItem[]>)[],
|
|
260
|
+
delemiter: string = '\n',
|
|
261
|
+
features: string[] = [],
|
|
262
|
+
) {
|
|
270
263
|
return lines
|
|
271
264
|
.reduce((acc, line_) => {
|
|
272
265
|
const line = line_ || '';
|
|
266
|
+
if (line && typeof line === 'object') {
|
|
267
|
+
for (const key in line) {
|
|
268
|
+
const lineItem = line[key];
|
|
269
|
+
if (features.includes(key)) {
|
|
270
|
+
acc.push(buildSingleText(lineItem, delemiter, features));
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return acc;
|
|
274
|
+
}
|
|
273
275
|
if (line || acc[acc.length - 1] !== '') {
|
|
274
276
|
acc.push(line);
|
|
275
277
|
}
|
|
@@ -294,7 +296,7 @@ export function joinReadable(
|
|
|
294
296
|
lastSeparator: string = ' and ',
|
|
295
297
|
) {
|
|
296
298
|
const lastIx = texts.length - 1;
|
|
297
|
-
return texts.reduce((acc, text, i) => {
|
|
299
|
+
return texts.filter(Boolean).reduce((acc, text, i) => {
|
|
298
300
|
return acc + (!acc ? '' : i === lastIx ? lastSeparator : separator) + text;
|
|
299
301
|
}, '');
|
|
300
302
|
}
|
|
@@ -342,3 +344,163 @@ export function removeGeneratedTextGarbage(str: string) {
|
|
|
342
344
|
// Remove all line breaks and spaces
|
|
343
345
|
return str.replace(/[\n\r\s]+/g, ' ').trim();
|
|
344
346
|
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Escape values not allowed in CSV column
|
|
350
|
+
* @param value String value to escape
|
|
351
|
+
* @returns Escaped string value
|
|
352
|
+
*/
|
|
353
|
+
|
|
354
|
+
export function escapeCSVValue(value: string): string {
|
|
355
|
+
if (/[",\n]/.test(value)) {
|
|
356
|
+
return `"${value.replace(/"/g, '""')}"`;
|
|
357
|
+
}
|
|
358
|
+
return value;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Convert Intl phone format to a readable format.
|
|
363
|
+
* Example: "+1 (555) 123-4567"
|
|
364
|
+
*/
|
|
365
|
+
|
|
366
|
+
export function formatPhoneNumber(phone?: string | null) {
|
|
367
|
+
const match = phone?.match(/(\d{3})(\d{3})(\d{4})$/);
|
|
368
|
+
if (match) {
|
|
369
|
+
const [, area, first, second] = match;
|
|
370
|
+
const countryCode = phone!.substring(0, phone.length - area.length - first.length - second.length);
|
|
371
|
+
// NOTE: This does not account for non North American (+1) phone numbers
|
|
372
|
+
return `${countryCode} (${area}) ${first}-${second}`;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
return phone;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Split a long text into chunks, making sure each chunk is under the chunk size character limit.
|
|
380
|
+
* @param text - The long text to split.
|
|
381
|
+
* @param chunkSize - The maximum number of characters allowed in each chunk.
|
|
382
|
+
* @param linebreakDistance - The maximum distance from the chunk boundary to look for a line break.
|
|
383
|
+
* @returns An array of text chunks.
|
|
384
|
+
*/
|
|
385
|
+
|
|
386
|
+
export function splitTextIntoChunks(text: string, chunkSize: number, linebreakDistance: number = 50): string[] {
|
|
387
|
+
if (!text || chunkSize <= 0 || text.length < chunkSize) {
|
|
388
|
+
return [text || ''];
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const chunks: string[] = [];
|
|
392
|
+
let start = 0;
|
|
393
|
+
|
|
394
|
+
while (start < text.length) {
|
|
395
|
+
let end = Math.min(start + chunkSize, text.length);
|
|
396
|
+
|
|
397
|
+
if (end < text.length) {
|
|
398
|
+
// Find the nearest line break around the target chunk boundary
|
|
399
|
+
const beforeBreak = text.lastIndexOf('\n', end);
|
|
400
|
+
|
|
401
|
+
let chosenBreak = -1;
|
|
402
|
+
if (beforeBreak !== -1 && end - beforeBreak <= linebreakDistance && beforeBreak > start) {
|
|
403
|
+
chosenBreak = beforeBreak + 1;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (chosenBreak !== -1) {
|
|
407
|
+
end = chosenBreak;
|
|
408
|
+
} else {
|
|
409
|
+
// Fall back to last whitespace near the end
|
|
410
|
+
const lastSpace = text.lastIndexOf(' ', end);
|
|
411
|
+
if (lastSpace > start && end - lastSpace < 100) {
|
|
412
|
+
end = lastSpace;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const chunk = text.slice(start, end).trim();
|
|
418
|
+
if (chunk) chunks.push(chunk);
|
|
419
|
+
start = end;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
return chunks;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Format reference number
|
|
427
|
+
*/
|
|
428
|
+
|
|
429
|
+
export function formatReferenceNumber(idStr: string) {
|
|
430
|
+
if (idStr && /^\d+$/.test(idStr)) {
|
|
431
|
+
return '#' + idStr;
|
|
432
|
+
}
|
|
433
|
+
return idStr;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Get "nth" string for a number
|
|
438
|
+
* @param num - The number to convert
|
|
439
|
+
* @returns The number with its ordinal suffix (e.g., "1st", "2nd", "3rd", "4th")
|
|
440
|
+
*/
|
|
441
|
+
|
|
442
|
+
export function getNthString(num: number): string {
|
|
443
|
+
const mod10 = num % 10;
|
|
444
|
+
const mod100 = num % 100;
|
|
445
|
+
|
|
446
|
+
if (mod10 === 1 && mod100 !== 11) {
|
|
447
|
+
return `${num}st`;
|
|
448
|
+
} else if (mod10 === 2 && mod100 !== 12) {
|
|
449
|
+
return `${num}nd`;
|
|
450
|
+
} else if (mod10 === 3 && mod100 !== 13) {
|
|
451
|
+
return `${num}rd`;
|
|
452
|
+
} else {
|
|
453
|
+
return `${num}th`;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Make address text
|
|
459
|
+
* @param address - Address object
|
|
460
|
+
* @param useFullCountryName - Whether to use full country name instead of code
|
|
461
|
+
* @param regionOnly - Whether to return only region (city, state, country)
|
|
462
|
+
* @returns Formatted address string
|
|
463
|
+
*/
|
|
464
|
+
|
|
465
|
+
export function makeAddressText(
|
|
466
|
+
address: Partial<AddressObj>,
|
|
467
|
+
format: 'REGION_ONLY' | 'SINGLE_LINE' | 'NORMAL' = 'NORMAL',
|
|
468
|
+
useFullCountryName: boolean = false,
|
|
469
|
+
delimiter: string = ', ',
|
|
470
|
+
) {
|
|
471
|
+
if (!address) {
|
|
472
|
+
return null;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const countryName = useFullCountryName && i18n.has(`country.from_code.${address.country}`)
|
|
476
|
+
? i18n.t(`country.from_code.${address.country}`)
|
|
477
|
+
: address.country;
|
|
478
|
+
|
|
479
|
+
if (format === 'REGION_ONLY') {
|
|
480
|
+
return [
|
|
481
|
+
address.city,
|
|
482
|
+
address.state,
|
|
483
|
+
countryName,
|
|
484
|
+
].filter(Boolean).join(delimiter);
|
|
485
|
+
} else if (format === 'SINGLE_LINE') {
|
|
486
|
+
return [
|
|
487
|
+
address.line1,
|
|
488
|
+
address.line2,
|
|
489
|
+
address.city,
|
|
490
|
+
address.state,
|
|
491
|
+
address.postalCode,
|
|
492
|
+
countryName,
|
|
493
|
+
].filter(Boolean).join(delimiter);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// NORMAL format
|
|
497
|
+
return [
|
|
498
|
+
address.line1,
|
|
499
|
+
address.line2,
|
|
500
|
+
[
|
|
501
|
+
[address.city, address.state].filter(Boolean).join(', '),
|
|
502
|
+
address.postalCode,
|
|
503
|
+
].filter(Boolean).join(' '),
|
|
504
|
+
countryName,
|
|
505
|
+
].filter(Boolean).join('\n');
|
|
506
|
+
}
|