@jsb188/app 1.1.5 → 1.1.7
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 +31 -30
- package/src/constants/app.ts +367 -0
- package/src/constants/other.ts +17 -0
- package/src/constants/storage.ts +4 -0
- package/src/constants/wsActions.ts +21 -0
- package/{i18n → src/i18n}/account.ts +19 -6
- package/src/i18n/ai_agent.ts +88 -0
- package/{i18n → src/i18n}/app.ts +4 -18
- package/{i18n → src/i18n}/auth.ts +18 -8
- package/{i18n → src/i18n}/billing.ts +2 -2
- package/src/i18n/country.ts +249 -0
- package/src/i18n/datetime.ts +60 -0
- package/{i18n → src/i18n}/error.ts +18 -7
- package/src/i18n/form.ts +295 -0
- package/src/i18n/index.ts +35 -0
- package/src/i18n/log.ts +484 -0
- package/src/i18n/message.ts +11 -0
- package/src/i18n/org.ts +165 -0
- package/src/i18n/product.ts +75 -0
- package/{main.ts → src/main.ts} +20 -29
- package/{types → src/types}/account.d.ts +9 -8
- package/src/types/app.d.ts +74 -0
- package/{types → src/types}/auth.d.ts +1 -2
- package/{types → src/types}/form.d.ts +1 -1
- package/src/types/other.d.ts +44 -0
- package/src/types/storage.d.ts +70 -0
- package/{utils/user.ts → src/utils/account.ts} +7 -84
- package/{utils → src/utils}/api.ts +4 -4
- package/{utils → src/utils}/billing.ts +1 -1
- package/{utils → src/utils}/datetime.ts +295 -33
- package/{utils → src/utils}/logic.ts +42 -5
- package/{utils → src/utils}/number.ts +89 -16
- package/{utils → src/utils}/object.ts +164 -11
- package/{utils → src/utils}/string.ts +165 -34
- package/{utils → src/utils}/timeZone.ts +168 -13
- package/{utils → src/utils}/url_client.ts +1 -1
- package/tsconfig.json +13 -0
- package/constants/app.ts +0 -80
- package/constants/organization.ts +0 -12
- package/constants/ws.ts +0 -47
- package/i18n/datetime.ts +0 -21
- package/i18n/form.ts +0 -115
- package/i18n/index.ts +0 -5
- package/types/app.d.ts +0 -49
- package/types/organization.d.ts +0 -94
- package/utils/organization.ts +0 -102
- /package/{constants → src/constants}/billing.ts +0 -0
- /package/{constants → src/constants}/emoji.ts +0 -0
- /package/{constants → src/constants}/user.ts +0 -0
- /package/{platform.ts → src/platform.ts} +0 -0
- /package/{types → src/types}/ai.d.ts +0 -0
- /package/{types.d.ts → src/types.d.ts} +0 -0
- /package/{utils → src/utils}/emoji.ts +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { DateTime } from 'luxon';
|
|
2
|
-
import i18n from '../i18n';
|
|
3
|
-
import { getObject } from './object';
|
|
4
|
-
import { isValidTimeZone, parseDateInTimezone } from './timeZone';
|
|
2
|
+
import i18n from '../i18n/index.ts';
|
|
3
|
+
import { getObject } from './object.ts';
|
|
4
|
+
import { isValidTimeZone, parseDateInTimezone } from './timeZone.ts';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Types
|
|
@@ -49,10 +49,44 @@ function getDaysDiff(date1: Date, date2: Date) {
|
|
|
49
49
|
return Math.floor((getStartOfDay(date1).getTime() - getStartOfDay(date2).getTime()) / (1000 * 60 * 60 * 24));
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Get difference in weeks
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
function getWeeksDiff(date1: Date, date2: Date) {
|
|
57
|
+
return Math.floor(getDaysDiff(date1, date2) / 7);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Get difference in months
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
function getMonthsDiff(date1: Date, date2: Date) {
|
|
65
|
+
return (date1.getFullYear() - date2.getFullYear()) * 12 + (date1.getMonth() - date2.getMonth());
|
|
66
|
+
}
|
|
67
|
+
|
|
52
68
|
/**
|
|
53
69
|
* Get date period from date
|
|
54
70
|
*/
|
|
55
71
|
|
|
72
|
+
export function getWeeksMonthAgo(d1: Date, d2: Date, weeksThresh = 12): string {
|
|
73
|
+
const diffWeeks = getWeeksDiff(d2, d1);
|
|
74
|
+
if (diffWeeks === 0) {
|
|
75
|
+
return i18n.t('datetime.period_THIS_WEEK');
|
|
76
|
+
} else if (diffWeeks <= weeksThresh) {
|
|
77
|
+
return i18n.t('datetime.weeks_ago_ct', { smart_count: diffWeeks });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const diffMonths = getMonthsDiff(d2, d1);
|
|
81
|
+
return i18n.t('datetime.months_ago_ct', { smart_count: diffMonths });
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Get date period from date
|
|
86
|
+
* @param d1 - Date to check
|
|
87
|
+
* @param d2 - Reference date (usually current date)
|
|
88
|
+
*/
|
|
89
|
+
|
|
56
90
|
export function getDatePeriod(d1: Date, d2: Date): DatePeriodEnum {
|
|
57
91
|
const diffDays = getDaysDiff(d1, d2);
|
|
58
92
|
|
|
@@ -95,7 +129,7 @@ export function getDatePeriod(d1: Date, d2: Date): DatePeriodEnum {
|
|
|
95
129
|
*/
|
|
96
130
|
|
|
97
131
|
export function getFullDateTime(
|
|
98
|
-
|
|
132
|
+
d_: Date | string | number | null,
|
|
99
133
|
params?: Partial<{
|
|
100
134
|
timeZone: string | null;
|
|
101
135
|
locales: string;
|
|
@@ -103,6 +137,7 @@ export function getFullDateTime(
|
|
|
103
137
|
hideDate: boolean;
|
|
104
138
|
hideTime: boolean;
|
|
105
139
|
textDateStyle: 'long' | 'short' | 'numeric' | null;
|
|
140
|
+
militaryTime: boolean;
|
|
106
141
|
alwaysShowYear: boolean;
|
|
107
142
|
includeWeekday: boolean | 'long' | 'short' | 'narrow';
|
|
108
143
|
}>,
|
|
@@ -114,16 +149,17 @@ export function getFullDateTime(
|
|
|
114
149
|
hideDate,
|
|
115
150
|
hideTime,
|
|
116
151
|
textDateStyle,
|
|
152
|
+
militaryTime,
|
|
117
153
|
alwaysShowYear,
|
|
118
154
|
includeWeekday,
|
|
119
155
|
} = params || {};
|
|
120
156
|
|
|
121
157
|
let d;
|
|
122
|
-
if (
|
|
123
|
-
d =
|
|
158
|
+
if (d_ instanceof Date) {
|
|
159
|
+
d = d_;
|
|
124
160
|
} else {
|
|
125
|
-
// Make sure
|
|
126
|
-
d =
|
|
161
|
+
// Make sure d_ is not null, before using this function
|
|
162
|
+
d = d_ ? new Date(d_) : new Date(-1);
|
|
127
163
|
}
|
|
128
164
|
|
|
129
165
|
let timeZone = timeZone_ || undefined; // cannot be null, else it will throw error
|
|
@@ -134,8 +170,9 @@ export function getFullDateTime(
|
|
|
134
170
|
if (hideDate) {
|
|
135
171
|
return d.toLocaleTimeString(locales, {
|
|
136
172
|
timeZone,
|
|
137
|
-
hour: 'numeric',
|
|
138
|
-
minute: '2-digit',
|
|
173
|
+
hour: hideTime ? undefined : militaryTime ? '2-digit' : 'numeric',
|
|
174
|
+
minute: hideTime ? undefined : '2-digit',
|
|
175
|
+
hour12: !militaryTime,
|
|
139
176
|
});
|
|
140
177
|
}
|
|
141
178
|
|
|
@@ -162,8 +199,9 @@ export function getFullDateTime(
|
|
|
162
199
|
weekday: weekday as 'long' | 'short' | 'narrow',
|
|
163
200
|
month: textDateStyle || 'numeric',
|
|
164
201
|
year: hideYear_ ? undefined : 'numeric',
|
|
165
|
-
hour: hideTime ? undefined : 'numeric',
|
|
202
|
+
hour: hideTime ? undefined : militaryTime ? '2-digit' : 'numeric',
|
|
166
203
|
minute: hideTime ? undefined : '2-digit',
|
|
204
|
+
hour12: !militaryTime,
|
|
167
205
|
});
|
|
168
206
|
}
|
|
169
207
|
|
|
@@ -200,10 +238,10 @@ export function getTodayStartOfDay(timeZone?: string | null) {
|
|
|
200
238
|
}
|
|
201
239
|
|
|
202
240
|
/**
|
|
203
|
-
* Get timezone enforced date
|
|
241
|
+
* Get timezone enforced calendar date
|
|
204
242
|
* @param d - Date to convert
|
|
205
243
|
* @param timeZone - Timezone to enforce
|
|
206
|
-
* @returns Date - Enforced date in the specified timezone
|
|
244
|
+
* @returns Date - Enforced calendar date in the specified timezone
|
|
207
245
|
*/
|
|
208
246
|
|
|
209
247
|
export function getCalDate(d: Date, timeZone_?: string | null) {
|
|
@@ -227,6 +265,42 @@ export function getCalDate(d: Date, timeZone_?: string | null) {
|
|
|
227
265
|
};
|
|
228
266
|
}
|
|
229
267
|
|
|
268
|
+
/**
|
|
269
|
+
* Get timezone enforced calendar date; but month-day first
|
|
270
|
+
* @param d - Date to convert
|
|
271
|
+
* @param timeZone - Timezone to enforce
|
|
272
|
+
* @returns Date - Enforced calendar date in the specified timezone
|
|
273
|
+
*/
|
|
274
|
+
|
|
275
|
+
export function getReadableCalDate(d_: string | Date, timeZone?: string | null) {
|
|
276
|
+
let d;
|
|
277
|
+
if (typeof d_ === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(d_)) {
|
|
278
|
+
// Assume YYYY-MM-DD format
|
|
279
|
+
const [year, month, day] = d_.split('-').map(Number);
|
|
280
|
+
d = new Date(year, month - 1, day);
|
|
281
|
+
} else if (d_ instanceof Date) {
|
|
282
|
+
d = d_;
|
|
283
|
+
} else {
|
|
284
|
+
d = d_ && new Date(d_);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (!d || isNaN(d.getTime())) {
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
let dt = DateTime.fromJSDate(d, { zone: timeZone || undefined });
|
|
292
|
+
// If timezone is invalid, fallback to system/local
|
|
293
|
+
if (!dt.isValid) {
|
|
294
|
+
dt = DateTime.fromJSDate(d); // system default zone
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (!dt.isValid) {
|
|
298
|
+
return null;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return dt.toFormat('MM/dd/yyyy');
|
|
302
|
+
}
|
|
303
|
+
|
|
230
304
|
/**
|
|
231
305
|
* Update JS date with year/month/day and hour/minutes
|
|
232
306
|
*/
|
|
@@ -294,10 +368,17 @@ export function updateDate(
|
|
|
294
368
|
* Convert Int cal date to String YYYY-MM-DD format
|
|
295
369
|
*/
|
|
296
370
|
|
|
297
|
-
export function convertIntToCalDate(
|
|
371
|
+
export function convertIntToCalDate(
|
|
372
|
+
calDateInt: number | string,
|
|
373
|
+
delimiter: string = '-',
|
|
374
|
+
yearsFirst: boolean = true,
|
|
375
|
+
): string {
|
|
298
376
|
const value = String(calDateInt);
|
|
299
377
|
if (value.length === 8) {
|
|
300
|
-
|
|
378
|
+
if (yearsFirst) {
|
|
379
|
+
return `${value.slice(0, 4)}${delimiter}${value.slice(4, 6)}${delimiter}${value.slice(6, 8)}`;
|
|
380
|
+
}
|
|
381
|
+
return `${value.slice(4, 6)}${delimiter}${value.slice(6, 8)}${delimiter}${value.slice(0, 4)}`;
|
|
301
382
|
}
|
|
302
383
|
// If not 8 digits, return as is
|
|
303
384
|
return value;
|
|
@@ -311,9 +392,21 @@ export function convertIntToCalDate(calDateInt: number | string, delimiter: stri
|
|
|
311
392
|
*/
|
|
312
393
|
|
|
313
394
|
export function isValidCalDate(
|
|
314
|
-
|
|
395
|
+
calDate_: string | number,
|
|
396
|
+
allowDashes: boolean = false,
|
|
315
397
|
minCalDate: number = 19500101,
|
|
316
398
|
): boolean {
|
|
399
|
+
let calDate;
|
|
400
|
+
if (
|
|
401
|
+
allowDashes &&
|
|
402
|
+
typeof calDate_ === 'string' &&
|
|
403
|
+
/^\d{4}-\d{2}-\d{2}$/.test(calDate_)
|
|
404
|
+
) {
|
|
405
|
+
calDate = calDate_.replace(/-/g, '');
|
|
406
|
+
} else {
|
|
407
|
+
calDate = calDate_;
|
|
408
|
+
}
|
|
409
|
+
|
|
317
410
|
const value = Number(calDate);
|
|
318
411
|
if (!calDate || !Number.isInteger(value) || value < 10000000 || value > 90000000) {
|
|
319
412
|
return false;
|
|
@@ -343,10 +436,12 @@ export function isFutureCalDate(
|
|
|
343
436
|
|
|
344
437
|
/**
|
|
345
438
|
* Convert date time "time" ago
|
|
439
|
+
* @param d_ - Date to convert
|
|
440
|
+
* @param params - Optional parameters for time ago conversion
|
|
346
441
|
*/
|
|
347
442
|
|
|
348
443
|
export function getTimeAgo(
|
|
349
|
-
|
|
444
|
+
d_: Date | string | number | null,
|
|
350
445
|
params?: Partial<TimeAgoParams>,
|
|
351
446
|
) {
|
|
352
447
|
const {
|
|
@@ -359,13 +454,13 @@ export function getTimeAgo(
|
|
|
359
454
|
} = params || {};
|
|
360
455
|
|
|
361
456
|
let d;
|
|
362
|
-
if (
|
|
363
|
-
d =
|
|
364
|
-
} else if (!isNaN(Number(
|
|
365
|
-
d = new Date(Number(
|
|
457
|
+
if (d_ instanceof Date) {
|
|
458
|
+
d = d_;
|
|
459
|
+
} else if (!isNaN(Number(d_))) {
|
|
460
|
+
d = new Date(Number(d_));
|
|
366
461
|
} else {
|
|
367
462
|
// Make sure date is not null, before using this function
|
|
368
|
-
d =
|
|
463
|
+
d = d_ ? new Date(d_) : new Date(-1);
|
|
369
464
|
}
|
|
370
465
|
|
|
371
466
|
const ts = d.getTime();
|
|
@@ -430,25 +525,68 @@ export function getTimeAgo(
|
|
|
430
525
|
*/
|
|
431
526
|
|
|
432
527
|
export function getFullDate(
|
|
433
|
-
|
|
434
|
-
|
|
528
|
+
d_: Date | string | number | null,
|
|
529
|
+
outputStyle_:
|
|
530
|
+
| 'NUMERIC'
|
|
531
|
+
| 'NUMERIC_TIME'
|
|
532
|
+
| 'DATE_ONLY_SHORT'
|
|
533
|
+
| 'DAY_IF_WEEK'
|
|
534
|
+
| 'TOMORROW_OR_NUMERIC'
|
|
535
|
+
| 'DATE_TEXT'
|
|
536
|
+
| 'MINIMAL'
|
|
537
|
+
| 'DETAILED' = 'NUMERIC',
|
|
435
538
|
timeZone: string | null,
|
|
436
539
|
locales: string = 'en-US',
|
|
437
540
|
) {
|
|
438
541
|
let d;
|
|
439
|
-
if (
|
|
440
|
-
d =
|
|
542
|
+
if (d_ instanceof Date) {
|
|
543
|
+
d = d_;
|
|
441
544
|
} else {
|
|
442
|
-
// Make sure
|
|
443
|
-
d =
|
|
545
|
+
// Make sure d_ is not null, before using this function
|
|
546
|
+
d = d_ ? new Date(d_) : new Date(-1);
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
let outputStyle;
|
|
550
|
+
if (['TOMORROW_OR_NUMERIC', 'DAY_IF_WEEK'].includes(outputStyle_)) {
|
|
551
|
+
const datePeriod = getDatePeriod(d, new Date());
|
|
552
|
+
if (['TODAY', 'TOMORROW'].includes(datePeriod)) {
|
|
553
|
+
return i18n.t(`datetime.period_${datePeriod}`);
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
const daysRemaining = getDaysDiff(d, new Date());
|
|
557
|
+
if (daysRemaining >= 0 && daysRemaining <= 6) {
|
|
558
|
+
// return "Monday", "Tuesday", etc.
|
|
559
|
+
const dayOfWeekText = d.toLocaleDateString(locales, {
|
|
560
|
+
timeZone: timeZone || undefined, // null is not allowed, it will throw error
|
|
561
|
+
...(outputStyle_ === 'TOMORROW_OR_NUMERIC' ? {} : {
|
|
562
|
+
weekday: 'long',
|
|
563
|
+
}),
|
|
564
|
+
});
|
|
565
|
+
return dayOfWeekText;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
outputStyle = 'NUMERIC';
|
|
569
|
+
} else {
|
|
570
|
+
outputStyle = outputStyle_;
|
|
444
571
|
}
|
|
445
572
|
|
|
446
573
|
switch (outputStyle) {
|
|
447
|
-
case '
|
|
574
|
+
case 'NUMERIC':
|
|
448
575
|
// Expected output: "9/1/2024"
|
|
449
576
|
return new Intl.DateTimeFormat(locales, {
|
|
450
577
|
timeZone: timeZone || undefined, // null is not allowed, it will throw error
|
|
451
578
|
}).format(d);
|
|
579
|
+
case 'NUMERIC_TIME':
|
|
580
|
+
// Expected output: "9/1/2024, 8:30 PM"
|
|
581
|
+
return new Intl.DateTimeFormat(locales, {
|
|
582
|
+
month: '2-digit',
|
|
583
|
+
day: '2-digit',
|
|
584
|
+
year: 'numeric',
|
|
585
|
+
hour: 'numeric',
|
|
586
|
+
minute: '2-digit',
|
|
587
|
+
hour12: true,
|
|
588
|
+
timeZone: timeZone || undefined, // null is not allowed, it will throw error
|
|
589
|
+
}).format(d);
|
|
452
590
|
case 'MINIMAL':
|
|
453
591
|
// Expected output: "Sep 1, 2024, 6:54 PM"
|
|
454
592
|
return new Intl.DateTimeFormat(locales, {
|
|
@@ -456,6 +594,38 @@ export function getFullDate(
|
|
|
456
594
|
timeStyle: 'short', // 'full', 'long', 'medium', 'short'
|
|
457
595
|
timeZone: timeZone || undefined, // null is not allowed, it will throw error
|
|
458
596
|
}).format(d);
|
|
597
|
+
// return DateTime.fromJSDate(d, timeZone ? { zone: timeZone } : undefined).toFormat('MMM d yyyy, h:mm a');
|
|
598
|
+
case 'DATE_ONLY_SHORT': {
|
|
599
|
+
// Expected output: "Sep 1" (or "Sep 1, 25" if not current year)
|
|
600
|
+
const dateYear = d.getFullYear();
|
|
601
|
+
const currentYear = new Date().getFullYear();
|
|
602
|
+
|
|
603
|
+
// This will crash because "medium" style with "numeric" year is not allowed
|
|
604
|
+
// return new Intl.DateTimeFormat(locales, {
|
|
605
|
+
// dateStyle: 'medium',
|
|
606
|
+
// year: dateYear === currentYear ? undefined : 'numeric',
|
|
607
|
+
// timeZone: timeZone || undefined, // null is not allowed, it will throw error
|
|
608
|
+
// }).format(d);
|
|
609
|
+
|
|
610
|
+
if (dateYear === currentYear) {
|
|
611
|
+
return new Intl.DateTimeFormat(locales, {
|
|
612
|
+
year: undefined,
|
|
613
|
+
month: 'short',
|
|
614
|
+
day: 'numeric',
|
|
615
|
+
timeZone: timeZone || undefined, // null is not allowed, it will throw error
|
|
616
|
+
}).format(d);
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
return new Intl.DateTimeFormat(locales, {
|
|
620
|
+
// Use this if you want 4 digit years
|
|
621
|
+
// dateStyle: 'medium',
|
|
622
|
+
// Use this if you want 2 digit years
|
|
623
|
+
year: '2-digit',
|
|
624
|
+
month: 'short',
|
|
625
|
+
day: 'numeric',
|
|
626
|
+
timeZone: timeZone || undefined, // null is not allowed, it will throw error
|
|
627
|
+
}).format(d).replace(', ', ", '");
|
|
628
|
+
}
|
|
459
629
|
case 'DATE_TEXT':
|
|
460
630
|
// Expected output: "September 1, 2024"
|
|
461
631
|
return new Intl.DateTimeFormat(locales, {
|
|
@@ -508,9 +678,13 @@ export function getDayPeriod(): 'MORNING' | 'AFTERNOON' | 'EVENING' | 'NIGHT' {
|
|
|
508
678
|
* Convert YYYY-MM-DD to Date
|
|
509
679
|
*/
|
|
510
680
|
|
|
511
|
-
export function getDateFromCalDate(
|
|
512
|
-
const
|
|
513
|
-
|
|
681
|
+
export function getDateFromCalDate(value: string | number, timeZone?: string | null): Date {
|
|
682
|
+
const calDate = typeof value === 'number' ? convertIntToCalDate(value) : value;
|
|
683
|
+
const dt = DateTime.fromISO(calDate, timeZone ? { zone: timeZone } : undefined);
|
|
684
|
+
const jsDate = dt.toJSDate();
|
|
685
|
+
// const [year, month, day] = calDate.split('-');
|
|
686
|
+
// console.log(new Date(Number(year), Number(month) - 1, Number(day)));
|
|
687
|
+
return jsDate;
|
|
514
688
|
}
|
|
515
689
|
|
|
516
690
|
/**
|
|
@@ -596,7 +770,7 @@ export function labelSortByDatePeriod<T>(
|
|
|
596
770
|
timeZone?: string | null,
|
|
597
771
|
mapFn?: (item: any) => any,
|
|
598
772
|
): DatePeriodObj[] {
|
|
599
|
-
const ordered = items.sort((a: any, b: any) => {
|
|
773
|
+
const ordered: any[] = items.sort((a: any, b: any) => {
|
|
600
774
|
const valueA = a[dateKey];
|
|
601
775
|
const valueB = b[dateKey];
|
|
602
776
|
|
|
@@ -679,3 +853,91 @@ export function getHHMMIncrements(
|
|
|
679
853
|
|
|
680
854
|
return hhmm;
|
|
681
855
|
}
|
|
856
|
+
|
|
857
|
+
/**
|
|
858
|
+
* Get color indicator based on expiration date
|
|
859
|
+
* @param expirationDate - Expiration date in YYYY-MM-DD format or Date object
|
|
860
|
+
* @returns 'red' | 'yellow' | 'green' | null
|
|
861
|
+
*/
|
|
862
|
+
|
|
863
|
+
export function getExpirationColor(
|
|
864
|
+
expirationDate: string | Date, // YYYY-MM-DD format or Date object
|
|
865
|
+
validColor: 'green' | null = null,
|
|
866
|
+
): 'red' | 'yellow' | 'green' | null {
|
|
867
|
+
let expDate: Date;
|
|
868
|
+
if (expirationDate instanceof Date) {
|
|
869
|
+
expDate = expirationDate;
|
|
870
|
+
} else {
|
|
871
|
+
expDate = new Date(expirationDate);
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
if (!isValidDate(expDate)) {
|
|
875
|
+
return 'red'; // Invalid date
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
const now = new Date();
|
|
879
|
+
const diff = expDate.getTime() - now.getTime();
|
|
880
|
+
const diffDays = Math.ceil(diff / (1000 * 3600 * 24));
|
|
881
|
+
|
|
882
|
+
if (diffDays < 0) {
|
|
883
|
+
return 'red'; // Expired
|
|
884
|
+
} else if (diffDays <= 30) {
|
|
885
|
+
return 'yellow'; // Expiring soon
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
return validColor; // Valid
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
/**
|
|
892
|
+
* Get HHMM time from "hh:mm" string
|
|
893
|
+
*/
|
|
894
|
+
|
|
895
|
+
export function timeToHHMM(timeStr: string): string {
|
|
896
|
+
const [hh, mm] = timeStr.split(':');
|
|
897
|
+
if (hh && mm) {
|
|
898
|
+
return `${hh.padStart(2, '0')}${mm.padStart(2, '0')}`;
|
|
899
|
+
}
|
|
900
|
+
return timeStr.padStart(4, '0');
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
/**
|
|
904
|
+
* Sort & group by date/month
|
|
905
|
+
*/
|
|
906
|
+
|
|
907
|
+
export function groupByDateMonth<T extends { date: Date }>(
|
|
908
|
+
items: T[],
|
|
909
|
+
timeZone: string
|
|
910
|
+
): {
|
|
911
|
+
byDate: Record<string, T[]>;
|
|
912
|
+
byMonth: Record<string, T[]>;
|
|
913
|
+
} {
|
|
914
|
+
// 1. Sort by absolute time
|
|
915
|
+
const sorted = [...items].sort(
|
|
916
|
+
(a, b) => a.date.getTime() - b.date.getTime()
|
|
917
|
+
);
|
|
918
|
+
|
|
919
|
+
const byDate: Record<string, T[]> = {};
|
|
920
|
+
const byMonth: Record<string, T[]> = {};
|
|
921
|
+
|
|
922
|
+
for (const item of sorted) {
|
|
923
|
+
const dt = DateTime
|
|
924
|
+
.fromJSDate(item.date, { zone: timeZone });
|
|
925
|
+
|
|
926
|
+
const dateKey = dt.toFormat('yyyy-MM-dd');
|
|
927
|
+
const monthKey = dt.toFormat('yyyy-MM');
|
|
928
|
+
|
|
929
|
+
// Group by day
|
|
930
|
+
if (!byDate[dateKey]) {
|
|
931
|
+
byDate[dateKey] = [];
|
|
932
|
+
}
|
|
933
|
+
byDate[dateKey].push(item);
|
|
934
|
+
|
|
935
|
+
// Group by month
|
|
936
|
+
if (!byMonth[monthKey]) {
|
|
937
|
+
byMonth[monthKey] = [];
|
|
938
|
+
}
|
|
939
|
+
byMonth[monthKey].push(item);
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
return { byDate, byMonth };
|
|
943
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { sortObjectByKeys } from './object.ts';
|
|
1
|
+
import { sortObjectByKeys, stringifyJSON } from './object.ts';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Types
|
|
@@ -40,8 +40,8 @@ export function getDatabaseAction(
|
|
|
40
40
|
if (value instanceof Date) {
|
|
41
41
|
value = value.toISOString();
|
|
42
42
|
} else if (value && typeof value === 'object') {
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
value = stringifyJSON(sortObjectByKeys(value));
|
|
44
|
+
}
|
|
45
45
|
|
|
46
46
|
const isNullValue = value !== null || allowNull;
|
|
47
47
|
if (isNullValue) {
|
|
@@ -52,8 +52,8 @@ export function getDatabaseAction(
|
|
|
52
52
|
if (currentValue instanceof Date) {
|
|
53
53
|
currentValue = currentValue.toISOString();
|
|
54
54
|
} else if (currentValue && typeof currentValue === 'object') {
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
currentValue = stringifyJSON(sortObjectByKeys(currentValue));
|
|
56
|
+
}
|
|
57
57
|
|
|
58
58
|
if (
|
|
59
59
|
// Doing loose != check instead of !== type check
|
|
@@ -130,3 +130,40 @@ export function timeoutPromise<T>(
|
|
|
130
130
|
}
|
|
131
131
|
});
|
|
132
132
|
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Make String based key from variables Object
|
|
136
|
+
* @param variables - any variables or arguments object
|
|
137
|
+
* @param doNotLowerCaseCacheKey - If true, the returned key will maintain original casing
|
|
138
|
+
* @return string key
|
|
139
|
+
*/
|
|
140
|
+
|
|
141
|
+
export function makeVariablesKey(
|
|
142
|
+
variables: Record<string, any> | null | undefined,
|
|
143
|
+
doNotLowerCaseCacheKey?: boolean,
|
|
144
|
+
// gqlDefinitions?: any[],
|
|
145
|
+
): string {
|
|
146
|
+
if (variables) {
|
|
147
|
+
|
|
148
|
+
// const gqlOperation = gqlDefinitions?.find((def: any) => def.kind === 'OperationDefinition');
|
|
149
|
+
const key = Object.keys(variables).sort().reduce((acc, key) => {
|
|
150
|
+
// I *could* loop and make sure the variables match those defined in the GQL operation,
|
|
151
|
+
// But instead of wasting compute, I rather make sure the variables are passed in identical manner from the app.
|
|
152
|
+
// if (gqlOperation) {
|
|
153
|
+
// console.log(key);
|
|
154
|
+
// ..
|
|
155
|
+
// }
|
|
156
|
+
const value = variables[key];
|
|
157
|
+
if (value || value === 0 || value === false) {
|
|
158
|
+
return `${acc}$${key}:${typeof value === 'object' ? stringifyJSON(value) : value}`;
|
|
159
|
+
}
|
|
160
|
+
return acc;
|
|
161
|
+
}, '');
|
|
162
|
+
|
|
163
|
+
if (!doNotLowerCaseCacheKey) {
|
|
164
|
+
return key.toLowerCase();
|
|
165
|
+
}
|
|
166
|
+
return key;
|
|
167
|
+
}
|
|
168
|
+
return 'none';
|
|
169
|
+
}
|
|
@@ -1,3 +1,27 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* Convert string value to BigInt safely
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export function toBigInt(value: string | number | bigint): bigint | null {
|
|
7
|
+
try {
|
|
8
|
+
if (typeof value === 'bigint') {
|
|
9
|
+
return value;
|
|
10
|
+
} else if (typeof value === 'number') {
|
|
11
|
+
return BigInt(value);
|
|
12
|
+
} else if (typeof value === 'string') {
|
|
13
|
+
const trimmed = value.trim();
|
|
14
|
+
if (trimmed === '') {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
return BigInt(trimmed);
|
|
18
|
+
}
|
|
19
|
+
return null;
|
|
20
|
+
} catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
1
25
|
/**
|
|
2
26
|
* Check if number
|
|
3
27
|
*/
|
|
@@ -6,6 +30,8 @@ export function isNumber(input: unknown): input is number | string {
|
|
|
6
30
|
switch (typeof input) {
|
|
7
31
|
case 'number':
|
|
8
32
|
return input - input === 0;
|
|
33
|
+
case 'bigint':
|
|
34
|
+
return true;
|
|
9
35
|
case 'string':
|
|
10
36
|
return input.trim() !== '' && !Number.isNaN(Number(input));
|
|
11
37
|
}
|
|
@@ -147,6 +173,25 @@ export function formatDecimal(
|
|
|
147
173
|
return doNotAllowNaN && isNaN(Number(formattedStr)) ? '' : formattedStr;
|
|
148
174
|
}
|
|
149
175
|
|
|
176
|
+
/**
|
|
177
|
+
* Get quantity text by combining quantity and unit together
|
|
178
|
+
*
|
|
179
|
+
* @param quantity - The quantity value
|
|
180
|
+
* @param unit - The unit string
|
|
181
|
+
* @returns Combined quantity and unit string
|
|
182
|
+
*/
|
|
183
|
+
|
|
184
|
+
export function getQuantityText(
|
|
185
|
+
quantity: string | number,
|
|
186
|
+
unit?: string,
|
|
187
|
+
): string {
|
|
188
|
+
const value = formatDecimal(quantity, true, true);
|
|
189
|
+
return [
|
|
190
|
+
value + (value && unit && /^[0-9]/.test(unit) ? ', ' : ''),
|
|
191
|
+
unit,
|
|
192
|
+
].filter(Boolean).join(' ');
|
|
193
|
+
}
|
|
194
|
+
|
|
150
195
|
/**
|
|
151
196
|
* Get currency symbol from settings
|
|
152
197
|
* Example 1: getCurrencySymbol('en-US', 'USD'); // "$"
|
|
@@ -177,24 +222,25 @@ export function getCurrencySymbol(locale: string, currency: string): string {
|
|
|
177
222
|
|
|
178
223
|
export function formatCurrency(
|
|
179
224
|
amount: string | number,
|
|
225
|
+
trailZeros: boolean = true,
|
|
180
226
|
locale: string = 'en-US',
|
|
181
227
|
currency: string = 'USD',
|
|
182
228
|
): string {
|
|
183
229
|
const num = typeof amount === 'string' ? parseFloat(amount) : amount;
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
230
|
+
if (!num && num !== 0) {
|
|
231
|
+
return '';
|
|
232
|
+
}
|
|
187
233
|
|
|
188
234
|
if (isNaN(num)) {
|
|
189
|
-
return '$0';
|
|
235
|
+
return trailZeros ? '$0.00' : '$0';
|
|
190
236
|
}
|
|
191
237
|
|
|
192
238
|
const hasDecimals = num % 1 !== 0;
|
|
193
239
|
const symbol = getCurrencySymbol(locale, currency);
|
|
194
240
|
|
|
195
241
|
return symbol + num.toLocaleString(locale, {
|
|
196
|
-
minimumFractionDigits: hasDecimals ? 2 : 0,
|
|
197
|
-
maximumFractionDigits: hasDecimals ? 2 : 0,
|
|
242
|
+
minimumFractionDigits: hasDecimals || trailZeros ? 2 : 0,
|
|
243
|
+
maximumFractionDigits: hasDecimals || trailZeros ? 2 : 0,
|
|
198
244
|
});
|
|
199
245
|
}
|
|
200
246
|
|
|
@@ -203,16 +249,43 @@ export function formatCurrency(
|
|
|
203
249
|
*/
|
|
204
250
|
|
|
205
251
|
export function kFormat(
|
|
206
|
-
|
|
207
|
-
|
|
252
|
+
num: string | number,
|
|
253
|
+
locale: string = 'en-US',
|
|
208
254
|
): string {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
255
|
+
const number = typeof num === 'string' ? parseFloat(num) : num;
|
|
256
|
+
if (isNaN(number)) {
|
|
257
|
+
return String(num);
|
|
258
|
+
}
|
|
213
259
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
260
|
+
return number.toLocaleString(locale, {
|
|
261
|
+
minimumFractionDigits: 0,
|
|
262
|
+
maximumFractionDigits: 0,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Convert number based time to military time format (e.g., 1300 to 13:00)
|
|
268
|
+
*/
|
|
269
|
+
|
|
270
|
+
export function convertToMilitaryTime(time?: number | string | null): string | null {
|
|
271
|
+
if (time) {
|
|
272
|
+
const timeString = String(time).padStart(4, '0');
|
|
273
|
+
return `${timeString.slice(0, 2)}:${timeString.slice(2)}`;
|
|
274
|
+
}
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Calculate totals from LabelAndValueObj[]
|
|
280
|
+
*/
|
|
281
|
+
|
|
282
|
+
export function calculateTotalAmount(values: any[], tax: number = 0): number {
|
|
283
|
+
return (values || []).reduce((acc, obj) => {
|
|
284
|
+
// NOTE: obj.value is the total, so obj.quantity can be ignored
|
|
285
|
+
const val = parseFloat(obj?.value);
|
|
286
|
+
if (!isNaN(val)) {
|
|
287
|
+
return acc + val;
|
|
288
|
+
}
|
|
289
|
+
return acc;
|
|
290
|
+
}, tax || 0);
|
|
218
291
|
}
|