@jsb188/app 1.1.1 → 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 -42
- 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/{utils → src/utils}/timeZone.ts +531 -852
- 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/journal.ts +0 -74
- package/constants/user.ts +0 -36
- package/constants/ws.ts +0 -47
- package/i18n/app.ts +0 -101
- package/i18n/auth.ts +0 -63
- package/i18n/billing.ts +0 -94
- package/i18n/datetime.ts +0 -21
- package/i18n/error.ts +0 -143
- package/i18n/form.ts +0 -74
- package/i18n/index.ts +0 -5
- package/i18n/user.ts +0 -101
- package/main.ts +0 -94
- package/platform.ts +0 -125
- package/types/ai.d.ts +0 -30
- package/types/app.d.ts +0 -48
- package/types/auth.d.ts +0 -63
- package/types/form.d.ts +0 -56
- package/types/journal.d.ts +0 -24
- package/types/organization.d.ts +0 -38
- package/types/user.d.ts +0 -52
- package/types.d.ts +0 -31
- package/utils/api.ts +0 -99
- package/utils/billing.ts +0 -134
- package/utils/datetime.ts +0 -542
- package/utils/emoji.ts +0 -151
- package/utils/journal.ts +0 -17
- package/utils/logic.ts +0 -118
- package/utils/number.ts +0 -139
- package/utils/object.ts +0 -425
- package/utils/string.ts +0 -323
- package/utils/url_client.ts +0 -138
- package/utils/user.ts +0 -273
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { COLORS } from '../constants/app.ts';
|
|
2
|
+
import type { ColorEnum } from '../types/app.d.ts';
|
|
3
|
+
import { hashStringFNV1a } from './string.ts';
|
|
4
|
+
|
|
5
|
+
type RGBColorObj = {
|
|
6
|
+
blue: number;
|
|
7
|
+
green: number;
|
|
8
|
+
red: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const APP_COLOR_HEX_VALUES: Record<ColorEnum, string> = {
|
|
12
|
+
amber: '#f59e0b',
|
|
13
|
+
blue: '#3b82f6',
|
|
14
|
+
brown: '#a16207',
|
|
15
|
+
cyan: '#06b6d4',
|
|
16
|
+
emerald: '#10b981',
|
|
17
|
+
fuchsia: '#d946ef',
|
|
18
|
+
green: '#22c55e',
|
|
19
|
+
indigo: '#6366f1',
|
|
20
|
+
lime: '#84cc16',
|
|
21
|
+
orange: '#f97316',
|
|
22
|
+
pink: '#ec4899',
|
|
23
|
+
purple: '#a855f7',
|
|
24
|
+
red: '#ef4444',
|
|
25
|
+
rose: '#f43f5e',
|
|
26
|
+
sky: '#0ea5e9',
|
|
27
|
+
slate: '#64748b',
|
|
28
|
+
stone: '#78716c',
|
|
29
|
+
teal: '#14b8a6',
|
|
30
|
+
violet: '#8b5cf6',
|
|
31
|
+
yellow: '#eab308',
|
|
32
|
+
zinc: '#71717a',
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Return an app palette name only when the supplied value belongs to the palette.
|
|
37
|
+
*/
|
|
38
|
+
export function getAppColorName(
|
|
39
|
+
color: string | null | undefined,
|
|
40
|
+
): ColorEnum | undefined {
|
|
41
|
+
if (!color || !Object.hasOwn(APP_COLOR_HEX_VALUES, color)) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return color as ColorEnum;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Resolve an app palette name to its CSS hex value while preserving custom colors.
|
|
50
|
+
*/
|
|
51
|
+
export function getAppColorValue(
|
|
52
|
+
color: string | null | undefined,
|
|
53
|
+
): string | undefined {
|
|
54
|
+
return color && Object.hasOwn(APP_COLOR_HEX_VALUES, color)
|
|
55
|
+
? APP_COLOR_HEX_VALUES[color as ColorEnum]
|
|
56
|
+
: color || undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Return one app palette hex color picked deterministically by index.
|
|
61
|
+
*/
|
|
62
|
+
export function getAppColorHexByIndex(index: number): string {
|
|
63
|
+
const hexValues = Object.values(APP_COLOR_HEX_VALUES);
|
|
64
|
+
return hexValues[Math.abs(Math.floor(index)) % hexValues.length];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Return a valid app color name, falling back to a stable color selected from the fully mixed id string.
|
|
69
|
+
*/
|
|
70
|
+
export function getValidColor(
|
|
71
|
+
color: string | null | undefined,
|
|
72
|
+
id: string | number | null | undefined,
|
|
73
|
+
): ColorEnum {
|
|
74
|
+
const normalizedColor = String(color || '')
|
|
75
|
+
.trim()
|
|
76
|
+
.toLowerCase();
|
|
77
|
+
|
|
78
|
+
if (COLORS.includes(normalizedColor as ColorEnum)) {
|
|
79
|
+
return normalizedColor as ColorEnum;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let colorIndexHash = parseInt(hashStringFNV1a(String(id)), 16);
|
|
83
|
+
colorIndexHash ^= colorIndexHash >>> 16;
|
|
84
|
+
colorIndexHash = Math.imul(colorIndexHash, 0x85ebca6b);
|
|
85
|
+
colorIndexHash ^= colorIndexHash >>> 13;
|
|
86
|
+
colorIndexHash = Math.imul(colorIndexHash, 0xc2b2ae35);
|
|
87
|
+
colorIndexHash ^= colorIndexHash >>> 16;
|
|
88
|
+
|
|
89
|
+
return COLORS[(colorIndexHash >>> 0) % COLORS.length] as ColorEnum;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const RGB_COLOR_REGEX = /^rgba?\(([^)]+)\)$/i;
|
|
93
|
+
const HEX_COLOR_VALUE_REGEX = /^#[0-9a-f]{6}$/i;
|
|
94
|
+
const HEX_COLOR_DRAFT_VALUE_REGEX = /^#[0-9a-f]{0,6}$/i;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Return whether one value is a valid hex color value or editable hex draft.
|
|
98
|
+
*/
|
|
99
|
+
export function isHexColorValue(
|
|
100
|
+
value: string | null | undefined,
|
|
101
|
+
allowDraft: boolean = false,
|
|
102
|
+
): boolean {
|
|
103
|
+
const normalized = String(value || '').trim();
|
|
104
|
+
|
|
105
|
+
if (allowDraft && !normalized) {
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return allowDraft
|
|
110
|
+
? HEX_COLOR_DRAFT_VALUE_REGEX.test(normalized)
|
|
111
|
+
: HEX_COLOR_VALUE_REGEX.test(normalized);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Convert a CSS hex color into RGB channel values.
|
|
116
|
+
*/
|
|
117
|
+
function getRGBColorFromHex(color: string): RGBColorObj | null {
|
|
118
|
+
const normalized = color.trim().replace('#', '');
|
|
119
|
+
const isShortHex = normalized.length === 3;
|
|
120
|
+
const isLongHex = normalized.length === 6;
|
|
121
|
+
|
|
122
|
+
if ((!isShortHex && !isLongHex) || /[^0-9a-f]/i.test(normalized)) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const fullHex = isShortHex
|
|
127
|
+
? normalized
|
|
128
|
+
.split('')
|
|
129
|
+
.map((char) => char + char)
|
|
130
|
+
.join('')
|
|
131
|
+
: normalized;
|
|
132
|
+
|
|
133
|
+
return {
|
|
134
|
+
red: parseInt(fullHex.slice(0, 2), 16),
|
|
135
|
+
green: parseInt(fullHex.slice(2, 4), 16),
|
|
136
|
+
blue: parseInt(fullHex.slice(4, 6), 16),
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Convert a CSS rgb() or rgba() color into RGB channel values.
|
|
142
|
+
*/
|
|
143
|
+
function getRGBColorFromRGBFunction(color: string): RGBColorObj | null {
|
|
144
|
+
const match = color.trim().match(RGB_COLOR_REGEX);
|
|
145
|
+
if (!match) {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const channels = match[1]
|
|
150
|
+
.split(',')
|
|
151
|
+
.slice(0, 3)
|
|
152
|
+
.map((channel) => Number(channel.trim()));
|
|
153
|
+
if (
|
|
154
|
+
channels.length < 3 ||
|
|
155
|
+
channels.some((channel) => Number.isNaN(channel))
|
|
156
|
+
) {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
red: channels[0],
|
|
162
|
+
green: channels[1],
|
|
163
|
+
blue: channels[2],
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Convert a supported color value into RGB channel values.
|
|
169
|
+
*/
|
|
170
|
+
export function getRGBColor(
|
|
171
|
+
color: string | null | undefined,
|
|
172
|
+
): RGBColorObj | null {
|
|
173
|
+
if (!color) {
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const normalized = color.trim().toLowerCase();
|
|
178
|
+
const appColorHex = APP_COLOR_HEX_VALUES[normalized as ColorEnum];
|
|
179
|
+
|
|
180
|
+
if (appColorHex) {
|
|
181
|
+
return getRGBColorFromHex(appColorHex);
|
|
182
|
+
} else if (normalized.startsWith('#')) {
|
|
183
|
+
return getRGBColorFromHex(normalized);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return getRGBColorFromRGBFunction(normalized);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Return a darker hex color derived from any supported color value, scaling
|
|
191
|
+
* every RGB channel toward black by the given factor (0-1). Useful for
|
|
192
|
+
* deriving an accent tone from a soft pastel background color.
|
|
193
|
+
*/
|
|
194
|
+
export function getDarkerHexColor(
|
|
195
|
+
color: string | null | undefined,
|
|
196
|
+
factor: number = 0.5,
|
|
197
|
+
): string | null {
|
|
198
|
+
const rgb = getRGBColor(color);
|
|
199
|
+
if (!rgb) {
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const channel = (value: number) =>
|
|
204
|
+
Math.max(0, Math.min(255, Math.round(value * factor)))
|
|
205
|
+
.toString(16)
|
|
206
|
+
.padStart(2, '0');
|
|
207
|
+
|
|
208
|
+
return `#${channel(rgb.red)}${channel(rgb.green)}${channel(rgb.blue)}`;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Return whether a color should be treated as dark based on perceived brightness.
|
|
213
|
+
*/
|
|
214
|
+
export function isDarkColor(
|
|
215
|
+
color: string | null | undefined,
|
|
216
|
+
fallback: boolean = false,
|
|
217
|
+
): boolean {
|
|
218
|
+
const rgb = getRGBColor(color);
|
|
219
|
+
if (!rgb) {
|
|
220
|
+
return fallback;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const brightness = (rgb.red * 299 + rgb.green * 587 + rgb.blue * 114) / 1000;
|
|
224
|
+
|
|
225
|
+
return brightness < 128;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Normalize one numeric RGB channel to an integer within the supported range.
|
|
230
|
+
*/
|
|
231
|
+
function getNormalizedRGBChannel(channel: number): number {
|
|
232
|
+
return Math.max(0, Math.min(255, Math.round(channel)));
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Convert one numeric RGB channel into a two-digit hex value.
|
|
237
|
+
*/
|
|
238
|
+
function getHexColorChannel(channel: number): string {
|
|
239
|
+
return getNormalizedRGBChannel(channel).toString(16).padStart(2, '0');
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Convert numeric RGB channels into a six-digit CSS hex color.
|
|
244
|
+
*/
|
|
245
|
+
export function getHexColorFromRGB(
|
|
246
|
+
red: number,
|
|
247
|
+
green: number,
|
|
248
|
+
blue: number,
|
|
249
|
+
): string {
|
|
250
|
+
return `#${getHexColorChannel(red)}${getHexColorChannel(green)}${getHexColorChannel(blue)}`;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Convert one sRGB channel into its linear-light value.
|
|
255
|
+
*/
|
|
256
|
+
function getLinearRGBChannel(channel: number): number {
|
|
257
|
+
const normalizedChannel = channel / 255;
|
|
258
|
+
return normalizedChannel <= 0.04045
|
|
259
|
+
? normalizedChannel / 12.92
|
|
260
|
+
: Math.pow((normalizedChannel + 0.055) / 1.055, 2.4);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Calculate WCAG relative luminance for a supported sRGB color.
|
|
265
|
+
*/
|
|
266
|
+
export function getRelativeLuminance(
|
|
267
|
+
color: string | null | undefined,
|
|
268
|
+
): number | null {
|
|
269
|
+
const rgb = getRGBColor(color);
|
|
270
|
+
if (!rgb) {
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return (
|
|
275
|
+
0.2126 * getLinearRGBChannel(rgb.red) +
|
|
276
|
+
0.7152 * getLinearRGBChannel(rgb.green) +
|
|
277
|
+
0.0722 * getLinearRGBChannel(rgb.blue)
|
|
278
|
+
);
|
|
279
|
+
}
|
|
@@ -0,0 +1,310 @@
|
|
|
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 } =
|
|
11
|
+
parsed.fields;
|
|
12
|
+
const secondRawValues = second.values as Array<number | string>;
|
|
13
|
+
const minuteRawValues = minute.values as Array<number | string>;
|
|
14
|
+
const hourRawValues = hour.values as Array<number | string>;
|
|
15
|
+
const dayOfMonthRawValues = dayOfMonth.values as Array<number | string>;
|
|
16
|
+
const monthRawValues = month.values as Array<number | string>;
|
|
17
|
+
const dayOfWeekRawValues = dayOfWeek.values as Array<number | string>;
|
|
18
|
+
|
|
19
|
+
if (
|
|
20
|
+
!allNumbers(secondRawValues) ||
|
|
21
|
+
!allNumbers(minuteRawValues) ||
|
|
22
|
+
!allNumbers(hourRawValues) ||
|
|
23
|
+
!allNumbers(dayOfMonthRawValues) ||
|
|
24
|
+
!allNumbers(monthRawValues) ||
|
|
25
|
+
!allNumbers(dayOfWeekRawValues)
|
|
26
|
+
) {
|
|
27
|
+
return `Cron ${parsed.toString()}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const secondValues = normalizeValues(secondRawValues);
|
|
31
|
+
const minuteValues = normalizeValues(minuteRawValues);
|
|
32
|
+
const hourValues = normalizeValues(hourRawValues);
|
|
33
|
+
const dayOfMonthValues = normalizeValues(dayOfMonthRawValues);
|
|
34
|
+
const monthValues = normalizeValues(monthRawValues);
|
|
35
|
+
const dayOfWeekValues = normalizeDayOfWeekValues(dayOfWeekRawValues);
|
|
36
|
+
|
|
37
|
+
const hasSimpleTimeNoSeconds =
|
|
38
|
+
secondValues.length === 1 &&
|
|
39
|
+
secondValues[0] === 0 &&
|
|
40
|
+
minuteValues.length === 1 &&
|
|
41
|
+
hourValues.length === 1;
|
|
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 (
|
|
55
|
+
isEveryMonth &&
|
|
56
|
+
isEveryDayOfMonth &&
|
|
57
|
+
isExactValues(dayOfWeekValues, [1, 2, 3, 4, 5])
|
|
58
|
+
) {
|
|
59
|
+
return `Mon-Fri${timeJoinText}${timeText}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (
|
|
63
|
+
isEveryMonth &&
|
|
64
|
+
isEveryDayOfMonth &&
|
|
65
|
+
isExactValues(dayOfWeekValues, [1, 3, 5])
|
|
66
|
+
) {
|
|
67
|
+
return `Every other Mon-Fri${timeJoinText}${timeText}`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (isEveryMonth && isEveryDayOfWeek && dayOfMonthValues.length === 1) {
|
|
71
|
+
return dayOfMonthValues[0] === 1
|
|
72
|
+
? `Every month${timeJoinText}${timeText}`
|
|
73
|
+
: `Every month on day ${dayOfMonthValues[0]}${timeJoinText}${timeText}`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return `${describeCalendar(dayOfMonthValues, monthValues, dayOfWeekValues)}${timeJoinText}${timeText}`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return [
|
|
80
|
+
describeTime(secondValues, minuteValues, hourValues),
|
|
81
|
+
describeDayOfMonth(dayOfMonthValues),
|
|
82
|
+
describeMonth(monthValues),
|
|
83
|
+
describeDayOfWeek(dayOfWeekValues),
|
|
84
|
+
].join('; ');
|
|
85
|
+
} catch {
|
|
86
|
+
return schedule;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function formatCronTime(hour24: number, minute: number): string {
|
|
91
|
+
const amPm = hour24 >= 12 ? 'PM' : 'AM';
|
|
92
|
+
const hour12 = hour24 % 12 || 12;
|
|
93
|
+
const minuteText = String(minute).padStart(2, '0');
|
|
94
|
+
return `${hour12}:${minuteText} ${amPm}`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function formatCronTimeWithSeconds(
|
|
98
|
+
hour24: number,
|
|
99
|
+
minute: number,
|
|
100
|
+
second: number,
|
|
101
|
+
): string {
|
|
102
|
+
const amPm = hour24 >= 12 ? 'PM' : 'AM';
|
|
103
|
+
const hour12 = hour24 % 12 || 12;
|
|
104
|
+
const minuteText = String(minute).padStart(2, '0');
|
|
105
|
+
const secondText = String(second).padStart(2, '0');
|
|
106
|
+
return `${hour12}:${minuteText}:${secondText} ${amPm}`;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function isExactValues(values: number[], expected: number[]): boolean {
|
|
110
|
+
if (values.length !== expected.length) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return expected.every((v, i) => values[i] === v);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function isRangeFull(values: number[], min: number, max: number): boolean {
|
|
118
|
+
const total = max - min + 1;
|
|
119
|
+
if (values.length !== total) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const seen = new Set(values);
|
|
124
|
+
for (let i = min; i <= max; i += 1) {
|
|
125
|
+
if (!seen.has(i)) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function normalizeValues(values: number[]): number[] {
|
|
134
|
+
return [...new Set(values)].sort((a, b) => a - b);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function allNumbers(values: Array<number | string>): values is number[] {
|
|
138
|
+
return values.every((v) => typeof v === 'number');
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function normalizeDayOfWeekValues(values: number[]): number[] {
|
|
142
|
+
return normalizeValues(values.map((v) => (v === 7 ? 0 : v)));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function describeCalendar(
|
|
146
|
+
dayOfMonthValues: number[],
|
|
147
|
+
monthValues: number[],
|
|
148
|
+
dayOfWeekValues: number[],
|
|
149
|
+
): string {
|
|
150
|
+
const dom = describeDayOfMonth(dayOfMonthValues);
|
|
151
|
+
const mon = describeMonth(monthValues);
|
|
152
|
+
const dow = describeDayOfWeek(dayOfWeekValues);
|
|
153
|
+
return `${dom}, ${mon}, ${dow}`;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function describeTime(
|
|
157
|
+
secondValues: number[],
|
|
158
|
+
minuteValues: number[],
|
|
159
|
+
hourValues: number[],
|
|
160
|
+
): string {
|
|
161
|
+
const singleHour = hourValues.length === 1;
|
|
162
|
+
const singleMinute = minuteValues.length === 1;
|
|
163
|
+
const singleSecond = secondValues.length === 1;
|
|
164
|
+
|
|
165
|
+
if (singleHour && singleMinute && singleSecond) {
|
|
166
|
+
if (secondValues[0] === 0) {
|
|
167
|
+
return `At ${formatCronTime(hourValues[0], minuteValues[0])}`;
|
|
168
|
+
}
|
|
169
|
+
return `At ${formatCronTimeWithSeconds(hourValues[0], minuteValues[0], secondValues[0])}`;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return [
|
|
173
|
+
`Hours ${describeNumericValues(hourValues, 0, 23)}`,
|
|
174
|
+
`Minutes ${describeNumericValues(minuteValues, 0, 59)}`,
|
|
175
|
+
`Seconds ${describeNumericValues(secondValues, 0, 59)}`,
|
|
176
|
+
].join(', ');
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function describeDayOfMonth(values: number[]): string {
|
|
180
|
+
const normalized = normalizeValues(values);
|
|
181
|
+
if (isRangeFull(normalized, 1, 31)) {
|
|
182
|
+
return 'Every day';
|
|
183
|
+
}
|
|
184
|
+
if (normalized.length === 1) {
|
|
185
|
+
return `Day ${normalized[0]}`;
|
|
186
|
+
}
|
|
187
|
+
return `Days ${formatRanges(normalized)}`;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function describeMonth(values: number[]): string {
|
|
191
|
+
const normalized = normalizeValues(values);
|
|
192
|
+
if (isRangeFull(normalized, 1, 12)) {
|
|
193
|
+
return 'Every month';
|
|
194
|
+
}
|
|
195
|
+
if (normalized.length === 1) {
|
|
196
|
+
return `In ${MONTH_LABELS[normalized[0]]}`;
|
|
197
|
+
}
|
|
198
|
+
return `In ${formatRanges(normalized, (v) => MONTH_LABELS[v])}`;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function describeDayOfWeek(values: number[]): string {
|
|
202
|
+
const normalized = normalizeDayOfWeekValues(values);
|
|
203
|
+
if (isRangeFull(normalized, 0, 6)) {
|
|
204
|
+
return 'Every day of week';
|
|
205
|
+
}
|
|
206
|
+
if (isExactValues(normalized, [1, 2, 3, 4, 5])) {
|
|
207
|
+
return 'Mon-Fri';
|
|
208
|
+
}
|
|
209
|
+
if (isExactValues(normalized, [1, 3, 5])) {
|
|
210
|
+
return 'Every other Mon-Fri';
|
|
211
|
+
}
|
|
212
|
+
if (normalized.length === 1) {
|
|
213
|
+
return DAY_LABELS[normalized[0]];
|
|
214
|
+
}
|
|
215
|
+
return formatRanges(normalized, (v) => DAY_LABELS[v]);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function describeNumericValues(
|
|
219
|
+
values: number[],
|
|
220
|
+
min: number,
|
|
221
|
+
max: number,
|
|
222
|
+
): string {
|
|
223
|
+
const normalized = normalizeValues(values);
|
|
224
|
+
if (isRangeFull(normalized, min, max)) {
|
|
225
|
+
return 'every value';
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const step = detectStep(normalized);
|
|
229
|
+
if (step && step.start === min && step.end >= max - (step.value - 1)) {
|
|
230
|
+
return `every ${step.value}`;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return formatRanges(normalized);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function detectStep(
|
|
237
|
+
values: number[],
|
|
238
|
+
): { value: number; start: number; end: number } | null {
|
|
239
|
+
if (values.length < 3) {
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
const diff = values[1] - values[0];
|
|
243
|
+
if (diff <= 1) {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
for (let i = 2; i < values.length; i += 1) {
|
|
247
|
+
if (values[i] - values[i - 1] !== diff) {
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return { value: diff, start: values[0], end: values[values.length - 1] };
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function formatRanges(
|
|
255
|
+
values: number[],
|
|
256
|
+
labeler?: (v: number) => string,
|
|
257
|
+
): string {
|
|
258
|
+
if (values.length === 0) {
|
|
259
|
+
return '';
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const segments: Array<{ start: number; end: number }> = [];
|
|
263
|
+
let start = values[0];
|
|
264
|
+
let prev = values[0];
|
|
265
|
+
|
|
266
|
+
for (let i = 1; i < values.length; i += 1) {
|
|
267
|
+
const curr = values[i];
|
|
268
|
+
if (curr === prev + 1) {
|
|
269
|
+
prev = curr;
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
segments.push({ start, end: prev });
|
|
273
|
+
start = curr;
|
|
274
|
+
prev = curr;
|
|
275
|
+
}
|
|
276
|
+
segments.push({ start, end: prev });
|
|
277
|
+
|
|
278
|
+
return segments
|
|
279
|
+
.map(({ start: s, end: e }) => {
|
|
280
|
+
const a = labeler ? labeler(s) : String(s);
|
|
281
|
+
const b = labeler ? labeler(e) : String(e);
|
|
282
|
+
return s === e ? a : `${a}-${b}`;
|
|
283
|
+
})
|
|
284
|
+
.join(', ');
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const DAY_LABELS: Record<number, string> = {
|
|
288
|
+
0: 'Sun',
|
|
289
|
+
1: 'Mon',
|
|
290
|
+
2: 'Tue',
|
|
291
|
+
3: 'Wed',
|
|
292
|
+
4: 'Thu',
|
|
293
|
+
5: 'Fri',
|
|
294
|
+
6: 'Sat',
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
const MONTH_LABELS: Record<number, string> = {
|
|
298
|
+
1: 'Jan',
|
|
299
|
+
2: 'Feb',
|
|
300
|
+
3: 'Mar',
|
|
301
|
+
4: 'Apr',
|
|
302
|
+
5: 'May',
|
|
303
|
+
6: 'Jun',
|
|
304
|
+
7: 'Jul',
|
|
305
|
+
8: 'Aug',
|
|
306
|
+
9: 'Sep',
|
|
307
|
+
10: 'Oct',
|
|
308
|
+
11: 'Nov',
|
|
309
|
+
12: 'Dec',
|
|
310
|
+
};
|
package/src/utils/csv.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Identify spreadsheet formulas while preserving plain signed numbers and
|
|
3
|
+
* phone-like text. CSV quoting alone does not prevent formula execution.
|
|
4
|
+
*/
|
|
5
|
+
export function isCSVFormulaLikeValue(text: string) {
|
|
6
|
+
if (/^[=@\t\r\n]/.test(text)) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
return /^[+-]/.test(text) && !/^[+-]?[\d\s().-]+$/.test(text) && !/^[+-]?(?:\d+\.?\d*|\.\d+)[eE][+-]?\d+$/.test(text);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/* Escape one CSV cell and neutralize spreadsheet formulas with an apostrophe. */
|
|
13
|
+
export function escapeCSVValue(value: unknown) {
|
|
14
|
+
let text = String(value ?? '');
|
|
15
|
+
if (isCSVFormulaLikeValue(text)) {
|
|
16
|
+
text = `'${text}`;
|
|
17
|
+
}
|
|
18
|
+
return /[",\n\r]/.test(text) ? `"${text.replace(/"/g, '""')}"` : text;
|
|
19
|
+
}
|