@jsb188/app 1.1.8 → 1.1.9
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 +29 -29
- package/src/constants/agent.ts +49 -0
- package/src/constants/app.ts +20 -19
- package/src/constants/wsActions.ts +2 -1
- package/src/i18n/account.ts +40 -17
- package/src/i18n/{ai_agent.ts → agent.ts} +60 -10
- package/src/i18n/app.ts +31 -4
- package/src/i18n/auth.ts +32 -32
- package/src/i18n/billing.ts +1 -1
- package/src/i18n/error.ts +48 -20
- package/src/i18n/form.ts +86 -6
- package/src/i18n/index.ts +9 -3
- package/src/i18n/log.ts +121 -73
- package/src/i18n/message.ts +4 -1
- package/src/i18n/org.ts +96 -10
- package/src/i18n/product.ts +22 -21
- package/src/i18n/sheet.ts +10 -0
- package/src/i18n/workflow.ts +25 -0
- package/src/types/ai.d.ts +2 -1
- package/src/types/app.d.ts +3 -1
- package/src/types/form.d.ts +56 -21
- package/src/types/node-polyglot.d.ts +23 -0
- package/src/types/other.d.ts +15 -0
- package/src/utils/api.ts +30 -19
- package/src/utils/cron.ts +279 -0
- package/src/utils/datetime.ts +206 -83
- package/src/utils/logic.ts +110 -0
- package/src/utils/number.ts +1 -1
- package/src/utils/object.ts +87 -18
- package/src/utils/string.test.ts +281 -0
- package/src/utils/string.ts +532 -292
- package/src/utils/timeZone.ts +6 -3
- package/src/utils/url_client.ts +11 -11
- package/tsconfig.json +0 -1
- package/src/constants/storage.ts +0 -4
- package/src/types/account.d.ts +0 -63
- package/src/types/storage.d.ts +0 -70
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
en: {
|
|
3
|
+
|
|
4
|
+
workflow_missing: 'Workflow is missing',
|
|
5
|
+
workflow_missing_msg: 'The workflow you\'re looking for could not be found.',
|
|
6
|
+
workflows_not_found_msg: 'Automated workflows were not found for this organization.',
|
|
7
|
+
not_active_msg: 'Not active – no upcoming jobs',
|
|
8
|
+
active_msg: 'Active – waiting for %{logType} logs',
|
|
9
|
+
|
|
10
|
+
// Below is old but some may be useful
|
|
11
|
+
|
|
12
|
+
// Actions related
|
|
13
|
+
not_started_msg: 'Tasks haven\'t started',
|
|
14
|
+
waiting_for_task_msg: 'Waiting for the task to start...',
|
|
15
|
+
waiting_for_tasks_to_be_created: 'Waiting for tasks to be created...',
|
|
16
|
+
view_workflow: 'View workflow',
|
|
17
|
+
delete_workflow: 'Delete workflow',
|
|
18
|
+
workflows_empty_msg: 'Please email us at hello@marketday.ai to create workflows for your %{orgType}.',
|
|
19
|
+
|
|
20
|
+
for_operation: {
|
|
21
|
+
FARMERS_MARKET: 'Vendor',
|
|
22
|
+
GROWER_NETWORK: 'Grower',
|
|
23
|
+
},
|
|
24
|
+
}
|
|
25
|
+
};
|
package/src/types/ai.d.ts
CHANGED
|
@@ -24,10 +24,11 @@ export type AIEventErrorName =
|
|
|
24
24
|
|
|
25
25
|
// AI chat
|
|
26
26
|
|
|
27
|
-
export interface
|
|
27
|
+
export interface AIChatData {
|
|
28
28
|
id: string;
|
|
29
29
|
calDate: string;
|
|
30
30
|
summary: string;
|
|
31
|
+
archivedAt: string | null;
|
|
31
32
|
createdAt: string;
|
|
32
33
|
updatedAt: string;
|
|
33
34
|
}
|
package/src/types/app.d.ts
CHANGED
|
@@ -50,7 +50,8 @@ export interface SimpleErrorType {
|
|
|
50
50
|
|
|
51
51
|
// Web Socket events
|
|
52
52
|
|
|
53
|
-
type ResetQueryRuleEnum = 'ALWAYS' | 'IF_FRAGMENT_NOT_FOUND' | 'IF_FRAGMENT_FOUND' | 'NEVER';
|
|
53
|
+
export type ResetQueryRuleEnum = 'ALWAYS' | 'IF_FRAGMENT_NOT_FOUND' | 'IF_FRAGMENT_FOUND' | 'NEVER' | 'RESET_ONLY';
|
|
54
|
+
export type AppendRuleEnum = 'APPEND_NEW' | 'APPEND_AT_END' | 'APPEND_AT_FRONT';
|
|
54
55
|
|
|
55
56
|
export interface WSDataUpdateObj {
|
|
56
57
|
id: string;
|
|
@@ -64,6 +65,7 @@ export interface WSDataUpdateObj {
|
|
|
64
65
|
name: string;
|
|
65
66
|
dataId: string;
|
|
66
67
|
replaceId?: string; // Use this where client has a temporary ID that will be replaced by the server-side ID later
|
|
68
|
+
appendRule?: AppendRuleEnum | null;
|
|
67
69
|
data: any;
|
|
68
70
|
} | null)[];
|
|
69
71
|
}
|
package/src/types/form.d.ts
CHANGED
|
@@ -3,60 +3,95 @@
|
|
|
3
3
|
* Types; form schema
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
export type FormValuesObj = {
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
__errorFields?: string[];
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type FormValueSetter<TFormValues extends FormValuesObj = any> = (
|
|
12
|
+
values: TFormValues | ((prev: TFormValues) => TFormValues)
|
|
13
|
+
) => void;
|
|
14
|
+
|
|
15
|
+
export type FormItemType =
|
|
16
|
+
| 'subtitle'
|
|
17
|
+
| 'break'
|
|
18
|
+
| 'group'
|
|
19
|
+
| 'input'
|
|
20
|
+
| 'section_title'
|
|
21
|
+
| 'input_click'
|
|
22
|
+
| 'input_w_button'
|
|
23
|
+
| 'input_time_from_date'
|
|
24
|
+
| 'password'
|
|
25
|
+
| 'textarea'
|
|
26
|
+
| 'none'
|
|
27
|
+
| (string & {});
|
|
28
|
+
|
|
29
|
+
interface SchemaItemAny<TCurrentData = any, TDataForSchema = any> {
|
|
7
30
|
[key: string]: any;
|
|
8
31
|
__typename?: string;
|
|
9
|
-
setter?: (currentData?:
|
|
32
|
+
setter?: (currentData?: TCurrentData, dataForSchema?: TDataForSchema) => any;
|
|
10
33
|
}
|
|
11
34
|
|
|
12
|
-
export interface FormItemSchemaObj {
|
|
13
|
-
__type:
|
|
35
|
+
export interface FormItemSchemaObj<TCurrentData = any, TDataForSchema = any> {
|
|
36
|
+
__type: FormItemType;
|
|
14
37
|
autoFocus?: boolean;
|
|
15
38
|
disabled?: boolean;
|
|
16
|
-
hidden?: boolean | ((dataForSchema:
|
|
17
|
-
locked?: boolean | ((dataForSchema:
|
|
39
|
+
hidden?: boolean | ((dataForSchema: TDataForSchema) => boolean);
|
|
40
|
+
locked?: boolean | ((dataForSchema: TDataForSchema) => boolean);
|
|
18
41
|
labelClassName?: string;
|
|
19
42
|
listenToInput?: boolean;
|
|
20
43
|
value?: any;
|
|
21
|
-
item: SchemaItemAny
|
|
44
|
+
item: SchemaItemAny<TCurrentData, TDataForSchema>;
|
|
22
45
|
}
|
|
23
46
|
|
|
24
|
-
interface SchemaRule {
|
|
47
|
+
interface SchemaRule<TFormValues extends FormValuesObj = any> {
|
|
25
48
|
for: string;
|
|
26
|
-
rule: (value: any, formValues:
|
|
49
|
+
rule: (value: any, formValues: TFormValues) => boolean;
|
|
27
50
|
error: any;
|
|
28
51
|
};
|
|
29
52
|
|
|
30
|
-
export interface FormSchemaObj
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
53
|
+
export interface FormSchemaObj<
|
|
54
|
+
TFormValues extends FormValuesObj = any,
|
|
55
|
+
TCurrentData = any,
|
|
56
|
+
TDataForSchema = any,
|
|
57
|
+
> {
|
|
58
|
+
listData: FormItemSchemaObj<TCurrentData, TDataForSchema>[];
|
|
59
|
+
rules?: SchemaRule<TFormValues>[];
|
|
60
|
+
isButtonDisabled?: (formValues: TFormValues) => boolean;
|
|
34
61
|
}
|
|
35
62
|
|
|
36
63
|
/**
|
|
37
64
|
* Types; form items
|
|
38
65
|
*/
|
|
39
66
|
|
|
40
|
-
interface FormGroupType
|
|
67
|
+
interface FormGroupType<
|
|
68
|
+
TFormValues extends FormValuesObj = any,
|
|
69
|
+
TCurrentData = any,
|
|
70
|
+
TDataForSchema = any,
|
|
71
|
+
> {
|
|
41
72
|
id?: string;
|
|
42
73
|
name: string;
|
|
43
74
|
label?: string;
|
|
44
75
|
value: string | number | null;
|
|
45
|
-
items: Array<FormItemIfaceObj
|
|
76
|
+
items: Array<FormItemIfaceObj<TFormValues, TCurrentData, TDataForSchema>>;
|
|
46
77
|
}
|
|
47
78
|
|
|
48
|
-
export interface FormItemIfaceObj
|
|
79
|
+
export interface FormItemIfaceObj<
|
|
80
|
+
TFormValues extends FormValuesObj = any,
|
|
81
|
+
TCurrentData = any,
|
|
82
|
+
TDataForSchema = any,
|
|
83
|
+
> extends FormItemSchemaObj<TCurrentData, TDataForSchema> {
|
|
49
84
|
name: string;
|
|
50
85
|
preset?: string;
|
|
51
86
|
className?: string;
|
|
52
87
|
inputClassName?: string;
|
|
53
88
|
focusStyle?: string;
|
|
54
|
-
hidden?: boolean | ((dataForSchema:
|
|
55
|
-
locked?: boolean | ((dataForSchema:
|
|
56
|
-
formValues:
|
|
57
|
-
setFormValues:
|
|
89
|
+
hidden?: boolean | ((dataForSchema: TDataForSchema) => boolean);
|
|
90
|
+
locked?: boolean | ((dataForSchema: TDataForSchema) => boolean);
|
|
91
|
+
formValues: TFormValues;
|
|
92
|
+
setFormValues: FormValueSetter<TFormValues>;
|
|
58
93
|
onInput?: (e: React.FormEvent, name: string) => void;
|
|
59
94
|
onKeyDown?: (e: React.KeyboardEvent, name: string) => void;
|
|
60
95
|
onSubmit?: (e: React.MouseEvent, name: string) => void;
|
|
61
|
-
onChange?: (e: React.ChangeEvent
|
|
96
|
+
onChange?: (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>, name: string) => void;
|
|
62
97
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
declare module 'node-polyglot' {
|
|
2
|
+
class Polyglot {
|
|
3
|
+
constructor(options?: any);
|
|
4
|
+
|
|
5
|
+
clear(): void;
|
|
6
|
+
|
|
7
|
+
extend(morePhrases: any, prefix?: any): void;
|
|
8
|
+
|
|
9
|
+
has(key: any): any;
|
|
10
|
+
|
|
11
|
+
locale(newLocale: any): any;
|
|
12
|
+
|
|
13
|
+
replace(newPhrases: any): void;
|
|
14
|
+
|
|
15
|
+
t(key: any, options?: any): any;
|
|
16
|
+
|
|
17
|
+
unset(morePhrases: any, prefix: any): void;
|
|
18
|
+
|
|
19
|
+
static transformPhrase(phrase: any, substitutions: any, locale: any): any;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default Polyglot;
|
|
23
|
+
}
|
package/src/types/other.d.ts
CHANGED
|
@@ -34,6 +34,20 @@ export interface ScheduleObj {
|
|
|
34
34
|
];
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Generic label/value object
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
export interface LabelAndValue {
|
|
42
|
+
iconName?: String
|
|
43
|
+
label: string;
|
|
44
|
+
value: string;
|
|
45
|
+
quantity?: number;
|
|
46
|
+
unit?: string;
|
|
47
|
+
tax?: number
|
|
48
|
+
note?: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
37
51
|
/**
|
|
38
52
|
* File object
|
|
39
53
|
*/
|
|
@@ -41,4 +55,5 @@ export interface ScheduleObj {
|
|
|
41
55
|
export interface FileObj {
|
|
42
56
|
buffer: Uint8Array;
|
|
43
57
|
contentType: string;
|
|
58
|
+
fileName?: string;
|
|
44
59
|
}
|
package/src/utils/api.ts
CHANGED
|
@@ -28,8 +28,7 @@ export function getAuthToken(): string | null {
|
|
|
28
28
|
*/
|
|
29
29
|
|
|
30
30
|
function i18nErrCodeExists(errorCode: string): boolean {
|
|
31
|
-
return
|
|
32
|
-
i18n.has(`error.${errorCode}_other`));
|
|
31
|
+
return i18n.has(`error.${errorCode}`) || i18n.has(`error.${errorCode}_other`);
|
|
33
32
|
}
|
|
34
33
|
|
|
35
34
|
/**
|
|
@@ -51,22 +50,20 @@ export function normalizeServerError(error?: any): ServerErrorObj {
|
|
|
51
50
|
}
|
|
52
51
|
}
|
|
53
52
|
|
|
54
|
-
if (!errorCode) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
errorValue = error.errors[0].variableWord;
|
|
69
|
-
}
|
|
53
|
+
if (!errorCode && error?.errors?.length) {
|
|
54
|
+
const loggedError = error.errors.find((gErr: ServerErrorObj) =>
|
|
55
|
+
gErr.errorCode &&
|
|
56
|
+
i18nErrCodeExists(gErr.errorCode)
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
if (loggedError) {
|
|
60
|
+
statusCode = loggedError.statusCode;
|
|
61
|
+
errorCode = loggedError.errorCode;
|
|
62
|
+
errorValue = loggedError.variableWord;
|
|
63
|
+
} else {
|
|
64
|
+
statusCode = error.errors[0].statusCode;
|
|
65
|
+
errorCode = error.errors[0].errorCode;
|
|
66
|
+
errorValue = error.errors[0].variableWord;
|
|
70
67
|
}
|
|
71
68
|
}
|
|
72
69
|
|
|
@@ -86,7 +83,21 @@ export function normalizeServerError(error?: any): ServerErrorObj {
|
|
|
86
83
|
console.warn(error);
|
|
87
84
|
}
|
|
88
85
|
|
|
89
|
-
|
|
86
|
+
let title;
|
|
87
|
+
if (i18n.has(`error.${errorCode}_title`)) {
|
|
88
|
+
title = i18n.t(`error.${errorCode}_title`);
|
|
89
|
+
} else if (i18n.has(`error.${statusCode}_title`)) {
|
|
90
|
+
title = i18n.t(`error.${statusCode}_title`);
|
|
91
|
+
} else {
|
|
92
|
+
title = i18n.t('error.error');
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (i18n.has(`error.${errorCode}_icon`)) {
|
|
96
|
+
iconName = i18n.t(`error.${errorCode}_icon`);
|
|
97
|
+
} else if (statusCode == 404) {
|
|
98
|
+
iconName = 'folder-question';
|
|
99
|
+
}
|
|
100
|
+
|
|
90
101
|
return {
|
|
91
102
|
statusCode,
|
|
92
103
|
errorCode,
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { CronExpressionParser } from 'cron-parser';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Convert cron expression into a readable sentence
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export function getCronDescription(schedule: string, verbose = false): string {
|
|
8
|
+
try {
|
|
9
|
+
const parsed = CronExpressionParser.parse(schedule);
|
|
10
|
+
const { second, minute, hour, dayOfMonth, month, dayOfWeek } = parsed.fields;
|
|
11
|
+
const secondRawValues = second.values as Array<number | string>;
|
|
12
|
+
const minuteRawValues = minute.values as Array<number | string>;
|
|
13
|
+
const hourRawValues = hour.values as Array<number | string>;
|
|
14
|
+
const dayOfMonthRawValues = dayOfMonth.values as Array<number | string>;
|
|
15
|
+
const monthRawValues = month.values as Array<number | string>;
|
|
16
|
+
const dayOfWeekRawValues = dayOfWeek.values as Array<number | string>;
|
|
17
|
+
|
|
18
|
+
if (
|
|
19
|
+
!allNumbers(secondRawValues) ||
|
|
20
|
+
!allNumbers(minuteRawValues) ||
|
|
21
|
+
!allNumbers(hourRawValues) ||
|
|
22
|
+
!allNumbers(dayOfMonthRawValues) ||
|
|
23
|
+
!allNumbers(monthRawValues) ||
|
|
24
|
+
!allNumbers(dayOfWeekRawValues)
|
|
25
|
+
) {
|
|
26
|
+
return `Cron ${parsed.toString()}`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const secondValues = normalizeValues(secondRawValues);
|
|
30
|
+
const minuteValues = normalizeValues(minuteRawValues);
|
|
31
|
+
const hourValues = normalizeValues(hourRawValues);
|
|
32
|
+
const dayOfMonthValues = normalizeValues(dayOfMonthRawValues);
|
|
33
|
+
const monthValues = normalizeValues(monthRawValues);
|
|
34
|
+
const dayOfWeekValues = normalizeDayOfWeekValues(dayOfWeekRawValues);
|
|
35
|
+
|
|
36
|
+
const hasSimpleTimeNoSeconds = (
|
|
37
|
+
secondValues.length === 1 &&
|
|
38
|
+
secondValues[0] === 0 &&
|
|
39
|
+
minuteValues.length === 1 &&
|
|
40
|
+
hourValues.length === 1
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
if (hasSimpleTimeNoSeconds) {
|
|
44
|
+
const timeText = formatCronTime(hourValues[0], minuteValues[0]);
|
|
45
|
+
const timeJoinText = verbose ? ' at ' : ', ';
|
|
46
|
+
const isEveryMonth = isRangeFull(monthValues, 1, 12);
|
|
47
|
+
const isEveryDayOfMonth = isRangeFull(dayOfMonthValues, 1, 31);
|
|
48
|
+
const isEveryDayOfWeek = isRangeFull(dayOfWeekValues, 0, 6);
|
|
49
|
+
|
|
50
|
+
if (isEveryMonth && isEveryDayOfMonth && isEveryDayOfWeek) {
|
|
51
|
+
return `Every day${timeJoinText}${timeText}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (isEveryMonth && isEveryDayOfMonth && isExactValues(dayOfWeekValues, [1, 2, 3, 4, 5])) {
|
|
55
|
+
return `Mon-Fri${timeJoinText}${timeText}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (isEveryMonth && isEveryDayOfMonth && isExactValues(dayOfWeekValues, [1, 3, 5])) {
|
|
59
|
+
return `Every other Mon-Fri${timeJoinText}${timeText}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (isEveryMonth && isEveryDayOfWeek && dayOfMonthValues.length === 1) {
|
|
63
|
+
return dayOfMonthValues[0] === 1
|
|
64
|
+
? `Every month${timeJoinText}${timeText}`
|
|
65
|
+
: `Every month on day ${dayOfMonthValues[0]}${timeJoinText}${timeText}`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return `${describeCalendar(dayOfMonthValues, monthValues, dayOfWeekValues)}${timeJoinText}${timeText}`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return [
|
|
72
|
+
describeTime(secondValues, minuteValues, hourValues),
|
|
73
|
+
describeDayOfMonth(dayOfMonthValues),
|
|
74
|
+
describeMonth(monthValues),
|
|
75
|
+
describeDayOfWeek(dayOfWeekValues),
|
|
76
|
+
].join('; ');
|
|
77
|
+
} catch {
|
|
78
|
+
return schedule;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function formatCronTime(hour24: number, minute: number): string {
|
|
83
|
+
const amPm = hour24 >= 12 ? 'PM' : 'AM';
|
|
84
|
+
const hour12 = hour24 % 12 || 12;
|
|
85
|
+
const minuteText = String(minute).padStart(2, '0');
|
|
86
|
+
return `${hour12}:${minuteText} ${amPm}`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function formatCronTimeWithSeconds(hour24: number, minute: number, second: number): string {
|
|
90
|
+
const amPm = hour24 >= 12 ? 'PM' : 'AM';
|
|
91
|
+
const hour12 = hour24 % 12 || 12;
|
|
92
|
+
const minuteText = String(minute).padStart(2, '0');
|
|
93
|
+
const secondText = String(second).padStart(2, '0');
|
|
94
|
+
return `${hour12}:${minuteText}:${secondText} ${amPm}`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function isExactValues(values: number[], expected: number[]): boolean {
|
|
98
|
+
if (values.length !== expected.length) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return expected.every((v, i) => values[i] === v);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function isRangeFull(values: number[], min: number, max: number): boolean {
|
|
106
|
+
const total = max - min + 1;
|
|
107
|
+
if (values.length !== total) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const seen = new Set(values);
|
|
112
|
+
for (let i = min; i <= max; i += 1) {
|
|
113
|
+
if (!seen.has(i)) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function normalizeValues(values: number[]): number[] {
|
|
122
|
+
return [...new Set(values)].sort((a, b) => a - b);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function allNumbers(values: Array<number | string>): values is number[] {
|
|
126
|
+
return values.every((v) => typeof v === 'number');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function normalizeDayOfWeekValues(values: number[]): number[] {
|
|
130
|
+
return normalizeValues(values.map((v) => (v === 7 ? 0 : v)));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function describeCalendar(dayOfMonthValues: number[], monthValues: number[], dayOfWeekValues: number[]): string {
|
|
134
|
+
const dom = describeDayOfMonth(dayOfMonthValues);
|
|
135
|
+
const mon = describeMonth(monthValues);
|
|
136
|
+
const dow = describeDayOfWeek(dayOfWeekValues);
|
|
137
|
+
return `${dom}, ${mon}, ${dow}`;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function describeTime(secondValues: number[], minuteValues: number[], hourValues: number[]): string {
|
|
141
|
+
const singleHour = hourValues.length === 1;
|
|
142
|
+
const singleMinute = minuteValues.length === 1;
|
|
143
|
+
const singleSecond = secondValues.length === 1;
|
|
144
|
+
|
|
145
|
+
if (singleHour && singleMinute && singleSecond) {
|
|
146
|
+
if (secondValues[0] === 0) {
|
|
147
|
+
return `At ${formatCronTime(hourValues[0], minuteValues[0])}`;
|
|
148
|
+
}
|
|
149
|
+
return `At ${formatCronTimeWithSeconds(hourValues[0], minuteValues[0], secondValues[0])}`;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return [
|
|
153
|
+
`Hours ${describeNumericValues(hourValues, 0, 23)}`,
|
|
154
|
+
`Minutes ${describeNumericValues(minuteValues, 0, 59)}`,
|
|
155
|
+
`Seconds ${describeNumericValues(secondValues, 0, 59)}`,
|
|
156
|
+
].join(', ');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function describeDayOfMonth(values: number[]): string {
|
|
160
|
+
const normalized = normalizeValues(values);
|
|
161
|
+
if (isRangeFull(normalized, 1, 31)) {
|
|
162
|
+
return 'Every day';
|
|
163
|
+
}
|
|
164
|
+
if (normalized.length === 1) {
|
|
165
|
+
return `Day ${normalized[0]}`;
|
|
166
|
+
}
|
|
167
|
+
return `Days ${formatRanges(normalized)}`;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function describeMonth(values: number[]): string {
|
|
171
|
+
const normalized = normalizeValues(values);
|
|
172
|
+
if (isRangeFull(normalized, 1, 12)) {
|
|
173
|
+
return 'Every month';
|
|
174
|
+
}
|
|
175
|
+
if (normalized.length === 1) {
|
|
176
|
+
return `In ${MONTH_LABELS[normalized[0]]}`;
|
|
177
|
+
}
|
|
178
|
+
return `In ${formatRanges(normalized, (v) => MONTH_LABELS[v])}`;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function describeDayOfWeek(values: number[]): string {
|
|
182
|
+
const normalized = normalizeDayOfWeekValues(values);
|
|
183
|
+
if (isRangeFull(normalized, 0, 6)) {
|
|
184
|
+
return 'Every day of week';
|
|
185
|
+
}
|
|
186
|
+
if (isExactValues(normalized, [1, 2, 3, 4, 5])) {
|
|
187
|
+
return 'Mon-Fri';
|
|
188
|
+
}
|
|
189
|
+
if (isExactValues(normalized, [1, 3, 5])) {
|
|
190
|
+
return 'Every other Mon-Fri';
|
|
191
|
+
}
|
|
192
|
+
if (normalized.length === 1) {
|
|
193
|
+
return DAY_LABELS[normalized[0]];
|
|
194
|
+
}
|
|
195
|
+
return formatRanges(normalized, (v) => DAY_LABELS[v]);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function describeNumericValues(values: number[], min: number, max: number): string {
|
|
199
|
+
const normalized = normalizeValues(values);
|
|
200
|
+
if (isRangeFull(normalized, min, max)) {
|
|
201
|
+
return 'every value';
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const step = detectStep(normalized);
|
|
205
|
+
if (step && step.start === min && step.end >= max - (step.value - 1)) {
|
|
206
|
+
return `every ${step.value}`;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return formatRanges(normalized);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function detectStep(values: number[]): { value: number; start: number; end: number } | null {
|
|
213
|
+
if (values.length < 3) {
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
const diff = values[1] - values[0];
|
|
217
|
+
if (diff <= 1) {
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
for (let i = 2; i < values.length; i += 1) {
|
|
221
|
+
if ((values[i] - values[i - 1]) !== diff) {
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return { value: diff, start: values[0], end: values[values.length - 1] };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function formatRanges(values: number[], labeler?: (v: number) => string): string {
|
|
229
|
+
if (values.length === 0) {
|
|
230
|
+
return '';
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const segments: Array<{ start: number; end: number }> = [];
|
|
234
|
+
let start = values[0];
|
|
235
|
+
let prev = values[0];
|
|
236
|
+
|
|
237
|
+
for (let i = 1; i < values.length; i += 1) {
|
|
238
|
+
const curr = values[i];
|
|
239
|
+
if (curr === prev + 1) {
|
|
240
|
+
prev = curr;
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
segments.push({ start, end: prev });
|
|
244
|
+
start = curr;
|
|
245
|
+
prev = curr;
|
|
246
|
+
}
|
|
247
|
+
segments.push({ start, end: prev });
|
|
248
|
+
|
|
249
|
+
return segments.map(({ start: s, end: e }) => {
|
|
250
|
+
const a = labeler ? labeler(s) : String(s);
|
|
251
|
+
const b = labeler ? labeler(e) : String(e);
|
|
252
|
+
return s === e ? a : `${a}-${b}`;
|
|
253
|
+
}).join(', ');
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const DAY_LABELS: Record<number, string> = {
|
|
257
|
+
0: 'Sun',
|
|
258
|
+
1: 'Mon',
|
|
259
|
+
2: 'Tue',
|
|
260
|
+
3: 'Wed',
|
|
261
|
+
4: 'Thu',
|
|
262
|
+
5: 'Fri',
|
|
263
|
+
6: 'Sat',
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
const MONTH_LABELS: Record<number, string> = {
|
|
267
|
+
1: 'Jan',
|
|
268
|
+
2: 'Feb',
|
|
269
|
+
3: 'Mar',
|
|
270
|
+
4: 'Apr',
|
|
271
|
+
5: 'May',
|
|
272
|
+
6: 'Jun',
|
|
273
|
+
7: 'Jul',
|
|
274
|
+
8: 'Aug',
|
|
275
|
+
9: 'Sep',
|
|
276
|
+
10: 'Oct',
|
|
277
|
+
11: 'Nov',
|
|
278
|
+
12: 'Dec',
|
|
279
|
+
};
|