@jsb188/schedule 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.
Files changed (60) hide show
  1. package/package.json +62 -0
  2. package/src/constants/schedule.ts +549 -0
  3. package/src/css.d.ts +7 -0
  4. package/src/libs/gantt-geometry.ts +771 -0
  5. package/src/libs/gantt-histogram.ts +124 -0
  6. package/src/libs/gantt-keyboard.ts +113 -0
  7. package/src/libs/gantt-state.ts +57 -0
  8. package/src/libs/gantt-url.ts +63 -0
  9. package/src/modules/Gantt.helpers.ts +119 -0
  10. package/src/modules/Gantt.tsx +584 -0
  11. package/src/types/schedule.d.ts +1149 -0
  12. package/src/ui/ActivityFormUI.tsx +133 -0
  13. package/src/ui/GanttEditorsUI.tsx +102 -0
  14. package/src/ui/GanttHistogramUI.tsx +236 -0
  15. package/src/ui/GanttUI.tsx +792 -0
  16. package/src/ui/ProjectFormUI.tsx +250 -0
  17. package/src/ui/ProjectMobileListUI.tsx +80 -0
  18. package/src/ui/ProjectToolbarUI.tsx +54 -0
  19. package/src/ui/RelationshipListUI.tsx +176 -0
  20. package/src/ui/ScheduleRunUI.tsx +173 -0
  21. package/src/ui/ScheduleSettingsUI.tsx +420 -0
  22. package/src/utils/coverage.helpers.ts +679 -0
  23. package/src/utils/coverage.ts +630 -0
  24. package/src/utils/cpm.helpers.ts +674 -0
  25. package/src/utils/cpm.ts +907 -0
  26. package/src/utils/cpmLeveling.helpers.ts +564 -0
  27. package/src/utils/cpmLeveling.ts +367 -0
  28. package/src/utils/defaults.ts +8 -0
  29. package/src/utils/eligibility.ts +457 -0
  30. package/src/utils/planCsv.ts +344 -0
  31. package/src/utils/predecessorText.ts +96 -0
  32. package/src/utils/recurrence.ts +616 -0
  33. package/src/utils/request.ts +153 -0
  34. package/src/utils/resource.ts +182 -0
  35. package/src/utils/rule.ts +216 -0
  36. package/src/utils/run.ts +43 -0
  37. package/src/utils/series.ts +161 -0
  38. package/src/utils/slot.ts +208 -0
  39. package/src/utils/workTime.ts +1382 -0
  40. package/tests/coverage.fixtures.ts +111 -0
  41. package/tests/coverage.test.ts +503 -0
  42. package/tests/cpm.bench.test.ts +154 -0
  43. package/tests/cpm.test.ts +354 -0
  44. package/tests/cpmCorrectness.test.ts +327 -0
  45. package/tests/cpmLeveling.test.ts +471 -0
  46. package/tests/eligibility.test.ts +320 -0
  47. package/tests/gantt-geometry.test.ts +367 -0
  48. package/tests/gantt-histogram.test.ts +78 -0
  49. package/tests/gantt-keyboard.test.ts +69 -0
  50. package/tests/helpers/cpmFixtures.ts +101 -0
  51. package/tests/projectInputs.test.ts +165 -0
  52. package/tests/recurrence.test.ts +68 -0
  53. package/tests/request.test.ts +99 -0
  54. package/tests/resource.test.ts +95 -0
  55. package/tests/rule.test.ts +98 -0
  56. package/tests/series.test.ts +120 -0
  57. package/tests/workTime.test.ts +1007 -0
  58. package/tests/workTimeContracts.test.ts +111 -0
  59. package/tsconfig.json +13 -0
  60. package/vitest.config.ts +8 -0
@@ -0,0 +1,344 @@
1
+ import { DateTime } from 'luxon';
2
+ import type {
3
+ ScheduleActivityTypeEnum,
4
+ ScheduleConstraintTypeEnum,
5
+ ScheduleWorkCalendarDef,
6
+ } from '../types/schedule.d.ts';
7
+ import {
8
+ SCHEDULE_ACTIVITY_TYPE_ENUMS,
9
+ SCHEDULE_CONSTRAINT_TYPE_ENUMS,
10
+ } from '../constants/schedule.ts';
11
+ import { daysTextToMinutes, parsePredecessorText } from './predecessorText.ts';
12
+ import {
13
+ getStandardWorkCalendarDef,
14
+ snapToWorkingFinish,
15
+ snapToWorkingStart,
16
+ } from './workTime.ts';
17
+
18
+ export interface ScheduleCsvError {
19
+ code: 'INVALID_FIELD' | 'UNKNOWN_ACTIVITY' | 'DUPLICATE_CODE';
20
+ section: string;
21
+ index: number;
22
+ rowCode: string | null;
23
+ message: string;
24
+ }
25
+ export interface ScheduleCsvPlan {
26
+ wbs: Array<{ code: string; name: string; parentCode: string | null }>;
27
+ activities: Array<{
28
+ code: string;
29
+ name: string;
30
+ wbsCode?: string;
31
+ type?: ScheduleActivityTypeEnum;
32
+ plannedDurationMinutes?: number;
33
+ workCalendarKey?: string;
34
+ levelingPriority?: number;
35
+ primaryConstraintType?: ScheduleConstraintTypeEnum;
36
+ primaryConstraintAt?: string;
37
+ secondaryConstraintType?: ScheduleConstraintTypeEnum;
38
+ secondaryConstraintAt?: string;
39
+ codes: Array<{ typeKey: string; valueKey: string }>;
40
+ }>;
41
+ relationships: Array<{
42
+ predecessorCode: string;
43
+ successorCode: string;
44
+ type: 'FS' | 'SS' | 'FF' | 'SF';
45
+ lagMinutes: number;
46
+ }>;
47
+ assignments: Array<{
48
+ activityCode: string;
49
+ resourceKey: string;
50
+ unitsPerDay: number | null;
51
+ }>;
52
+ }
53
+
54
+ /* Read RFC 4180 fields including quoted commas, escaped quotes and embedded newlines. */
55
+ export function readScheduleCsvRows(text: string): string[][] {
56
+ const rows: string[][] = [];
57
+ let row: string[] = [];
58
+ let field = '';
59
+ let quoted = false;
60
+ let closed = false;
61
+ for (let i = 0; i < text.length; i++) {
62
+ const char = text[i];
63
+ if (quoted) {
64
+ if (char === '"' && text[i + 1] === '"') {
65
+ field += '"';
66
+ i++;
67
+ } else if (char === '"') {
68
+ quoted = false;
69
+ closed = true;
70
+ } else field += char;
71
+ continue;
72
+ }
73
+ if (char === '"') {
74
+ if (field || closed) throw new Error('CSV_QUOTE');
75
+ quoted = true;
76
+ } else if (char === ',' || char === '\n' || char === '\r') {
77
+ row.push(field);
78
+ field = '';
79
+ closed = false;
80
+ if (char !== ',') {
81
+ if (char === '\r' && text[i + 1] === '\n') i++;
82
+ if (row.some((value) => value.trim())) rows.push(row);
83
+ row = [];
84
+ }
85
+ } else {
86
+ if (closed && char.trim()) throw new Error('CSV_QUOTE');
87
+ field += char;
88
+ }
89
+ }
90
+ if (quoted) throw new Error('CSV_QUOTE');
91
+ row.push(field);
92
+ if (row.some((value) => value.trim())) rows.push(row);
93
+ return rows;
94
+ }
95
+ /* Convert date text into a calendar-aware working boundary for an import. */
96
+ function csvDate(
97
+ text: string,
98
+ calendar: ScheduleWorkCalendarDef,
99
+ timeZone: string,
100
+ finish: boolean,
101
+ ): string {
102
+ let date = DateTime.fromISO(text, { zone: timeZone });
103
+ if (!date.isValid)
104
+ date = DateTime.fromFormat(text, 'M/d/yyyy', { zone: timeZone });
105
+ if (!date.isValid) throw new Error('DATE');
106
+ return (
107
+ finish
108
+ ? snapToWorkingFinish(
109
+ calendar,
110
+ date.plus({ days: 1 }).startOf('day').toJSDate(),
111
+ )
112
+ : snapToWorkingStart(calendar, date.startOf('day').toJSDate())
113
+ ).toISOString();
114
+ }
115
+ /* Turn one bounded CSV into typed activity, hierarchy, relationship and resource import sections. */
116
+ export function parseSchedulePlanCsv(
117
+ text: string,
118
+ options: {
119
+ minutesPerDay: number;
120
+ timeZone: string;
121
+ calendar?: ScheduleWorkCalendarDef;
122
+ },
123
+ ): { plan: ScheduleCsvPlan; errors: ScheduleCsvError[] } {
124
+ const plan: ScheduleCsvPlan = {
125
+ wbs: [],
126
+ activities: [],
127
+ relationships: [],
128
+ assignments: [],
129
+ };
130
+ const errors: ScheduleCsvError[] = [];
131
+ let rows: string[][];
132
+ try {
133
+ if (new TextEncoder().encode(text).length > 2 * 1024 * 1024)
134
+ throw new Error('CSV_SIZE');
135
+ rows = readScheduleCsvRows(text.replace(/^\uFEFF/, ''));
136
+ } catch {
137
+ return {
138
+ plan,
139
+ errors: [
140
+ {
141
+ code: 'INVALID_FIELD',
142
+ section: 'csv',
143
+ index: 0,
144
+ rowCode: null,
145
+ message: 'INVALID_CSV',
146
+ },
147
+ ],
148
+ };
149
+ }
150
+ if (!rows.length)
151
+ return {
152
+ plan,
153
+ errors: [
154
+ {
155
+ code: 'INVALID_FIELD',
156
+ section: 'csv',
157
+ index: 0,
158
+ rowCode: null,
159
+ message: 'EMPTY_CSV',
160
+ },
161
+ ],
162
+ };
163
+ const aliases: Record<string, string> = {
164
+ id: 'code',
165
+ 'activity id': 'code',
166
+ activity: 'name',
167
+ 'activity name': 'name',
168
+ description: 'name',
169
+ 'wbs code': 'wbs',
170
+ dur: 'duration',
171
+ duration_days: 'duration',
172
+ days: 'duration',
173
+ preds: 'predecessors',
174
+ predecessor: 'predecessors',
175
+ 'start date': 'start',
176
+ 'finish date': 'finish',
177
+ resource: 'resources',
178
+ };
179
+ const headers = rows.shift()!.map((value) => {
180
+ const key = value.trim().toLowerCase();
181
+ return aliases[key] || key;
182
+ });
183
+ const rawRows = rows.map((row) =>
184
+ Object.fromEntries(
185
+ headers.map((key, index) => [key, (row[index] || '').trim()]),
186
+ ),
187
+ );
188
+ const calendar =
189
+ options.calendar || getStandardWorkCalendarDef(options.timeZone);
190
+ const codeToId = new Map<string, string>();
191
+ const wbsCodes = new Set<string>();
192
+ for (let index = 0; index < rawRows.length; index++) {
193
+ const raw = rawRows[index];
194
+ const code = raw.code || `A${index + 1}`;
195
+ const activity: ScheduleCsvPlan['activities'][number] = {
196
+ code,
197
+ name: raw.name || code,
198
+ codes: [],
199
+ };
200
+ /* Preserve actionable row locations without throwing away the rest of the preview. */
201
+ const invalid = (message: string) =>
202
+ errors.push({
203
+ code: 'INVALID_FIELD',
204
+ section: 'activities',
205
+ index,
206
+ rowCode: code,
207
+ message,
208
+ });
209
+ if (codeToId.has(code))
210
+ errors.push({
211
+ code: 'DUPLICATE_CODE',
212
+ section: 'activities',
213
+ index,
214
+ rowCode: code,
215
+ message: code,
216
+ });
217
+ codeToId.set(code, code);
218
+ if (raw.duration) {
219
+ const minutes = daysTextToMinutes(raw.duration, options.minutesPerDay);
220
+ if (minutes == null) invalid('duration');
221
+ else activity.plannedDurationMinutes = minutes;
222
+ }
223
+ if (raw.type) {
224
+ const type = raw.type.toUpperCase();
225
+ if (!(SCHEDULE_ACTIVITY_TYPE_ENUMS as readonly string[]).includes(type))
226
+ invalid('type');
227
+ else activity.type = type as ScheduleActivityTypeEnum;
228
+ }
229
+ if (activity.type?.includes('MILESTONE'))
230
+ activity.plannedDurationMinutes = 0;
231
+ if (raw.wbs) {
232
+ activity.wbsCode = raw.wbs;
233
+ const path = raw.wbs.split('.');
234
+ for (let n = 1; n <= path.length; n++) {
235
+ const key = path.slice(0, n).join('.');
236
+ if (!wbsCodes.has(key)) {
237
+ wbsCodes.add(key);
238
+ plan.wbs.push({
239
+ code: key,
240
+ name: key,
241
+ parentCode: n > 1 ? path.slice(0, n - 1).join('.') : null,
242
+ });
243
+ }
244
+ }
245
+ }
246
+ if (raw.calendar) activity.workCalendarKey = raw.calendar;
247
+ if (raw.priority) {
248
+ const priority = Number(raw.priority);
249
+ if (!Number.isInteger(priority) || priority < 1 || priority > 10)
250
+ invalid('priority');
251
+ else activity.levelingPriority = priority;
252
+ }
253
+ try {
254
+ if (raw.start) {
255
+ activity.primaryConstraintType = 'START_ON_OR_AFTER';
256
+ activity.primaryConstraintAt = csvDate(
257
+ raw.start,
258
+ calendar,
259
+ options.timeZone,
260
+ false,
261
+ );
262
+ }
263
+ if (raw.finish) {
264
+ const slot = raw.start ? 'secondary' : 'primary';
265
+ activity[`${slot}ConstraintType`] = 'FINISH_ON_OR_BEFORE';
266
+ activity[`${slot}ConstraintAt`] = csvDate(
267
+ raw.finish,
268
+ calendar,
269
+ options.timeZone,
270
+ true,
271
+ );
272
+ }
273
+ if (raw.constraint) {
274
+ const type = raw.constraint.toUpperCase();
275
+ if (
276
+ !(SCHEDULE_CONSTRAINT_TYPE_ENUMS as readonly string[]).includes(type)
277
+ )
278
+ invalid('constraint');
279
+ else {
280
+ activity.primaryConstraintType = type as ScheduleConstraintTypeEnum;
281
+ if (raw['constraint date'])
282
+ activity.primaryConstraintAt = csvDate(
283
+ raw['constraint date'],
284
+ calendar,
285
+ options.timeZone,
286
+ type.includes('FINISH'),
287
+ );
288
+ }
289
+ }
290
+ } catch {
291
+ invalid('date');
292
+ }
293
+ for (const [key, value] of Object.entries(raw))
294
+ if (key.startsWith('code:') && value)
295
+ activity.codes.push({ typeKey: key.slice(5), valueKey: value });
296
+ for (const token of (raw.resources || '').split(';').filter(Boolean)) {
297
+ const match = token.trim().match(/^([^:]+)(?::(\d+(?:\.\d+)?))?$/);
298
+ if (!match || (match[2] && Number(match[2]) <= 0)) invalid('resources');
299
+ else
300
+ plan.assignments.push({
301
+ activityCode: code,
302
+ resourceKey: match[1].trim(),
303
+ unitsPerDay: match[2] ? Number(match[2]) : null,
304
+ });
305
+ }
306
+ plan.activities.push(activity);
307
+ }
308
+ const codeIndex = new Map(
309
+ [...codeToId.keys()].map((code) => [code.toLowerCase(), code]),
310
+ );
311
+ rawRows.forEach((raw, index) => {
312
+ const code = plan.activities[index].code;
313
+ const parsed = parsePredecessorText(
314
+ raw.predecessors || '',
315
+ codeToId,
316
+ options.minutesPerDay,
317
+ codeIndex,
318
+ );
319
+ for (const unknown of parsed.unknownCodes)
320
+ errors.push({
321
+ code: 'UNKNOWN_ACTIVITY',
322
+ section: 'relationships',
323
+ index,
324
+ rowCode: code,
325
+ message: unknown,
326
+ });
327
+ for (const duplicate of parsed.duplicateCodes)
328
+ errors.push({
329
+ code: 'DUPLICATE_CODE',
330
+ section: 'relationships',
331
+ index,
332
+ rowCode: code,
333
+ message: duplicate,
334
+ });
335
+ for (const row of parsed.items)
336
+ plan.relationships.push({
337
+ predecessorCode: row.predecessorId,
338
+ successorCode: code,
339
+ type: row.type,
340
+ lagMinutes: row.lagMinutes,
341
+ });
342
+ });
343
+ return { plan, errors };
344
+ }
@@ -0,0 +1,96 @@
1
+ import type { ScheduleRelationshipTypeEnum } from '../types/schedule.d.ts';
2
+ export interface PredecessorTextItemObj {
3
+ predecessorId: string;
4
+ type: ScheduleRelationshipTypeEnum;
5
+ lagMinutes: number;
6
+ }
7
+ /* Parse a full days value; trailing junk and negative duration values are refused. */
8
+ export function daysTextToMinutes(
9
+ text: string,
10
+ minutesPerDay: number,
11
+ ): number | null {
12
+ if (
13
+ !/^\d+(?:\.\d+)?$/.test(text.trim()) ||
14
+ !Number.isFinite(minutesPerDay) ||
15
+ minutesPerDay <= 0
16
+ )
17
+ return null;
18
+ const result = Math.round(Number(text) * minutesPerDay);
19
+ return Number.isSafeInteger(result) ? result : null;
20
+ }
21
+ /* Resolve longest matching activity codes before interpreting relationship suffixes and signed lag. */
22
+ export function parsePredecessorText(
23
+ text: string,
24
+ activityIdByCode: Map<string, string>,
25
+ minutesPerDay: number,
26
+ codeIndex?: Map<string, string>,
27
+ ): {
28
+ items: PredecessorTextItemObj[];
29
+ unknownCodes: string[];
30
+ duplicateCodes: string[];
31
+ } {
32
+ const items: PredecessorTextItemObj[] = [];
33
+ const unknownCodes: string[] = [];
34
+ const duplicateCodes: string[] = [];
35
+ const seen = new Set<string>();
36
+ /* Resolve canonical codes directly; case-insensitive fallback is needed only for noncanonical user text. */
37
+ const normalized = codeIndex || new Map<string, string>();
38
+ const resolveCode = (value: string): string | null => {
39
+ if (activityIdByCode.has(value)) return value;
40
+ if (!normalized.size)
41
+ for (const code of activityIdByCode.keys())
42
+ normalized.set(code.toLowerCase(), code);
43
+ return normalized.get(value.toLowerCase()) || null;
44
+ };
45
+ for (const token of text
46
+ .split(/[,;]/)
47
+ .map((value) => value.trim())
48
+ .filter(Boolean)) {
49
+ let code = resolveCode(token);
50
+ let type: ScheduleRelationshipTypeEnum = 'FS';
51
+ let lagMinutes = 0;
52
+ if (!code) {
53
+ const lag = token.match(/([+-])\s*(\d+(?:\.\d+)?)\s*([dh])?$/i);
54
+ const core = (lag ? token.slice(0, lag.index) : token).trim();
55
+ code = resolveCode(core);
56
+ if (!code) {
57
+ const relationship = core.match(/(FS|SS|FF|SF)$/i);
58
+ if (relationship) {
59
+ code = resolveCode(core.slice(0, relationship.index).trim());
60
+ type = relationship[1].toUpperCase() as ScheduleRelationshipTypeEnum;
61
+ }
62
+ }
63
+ if (lag)
64
+ lagMinutes =
65
+ Math.round(
66
+ Number(lag[2]) *
67
+ (lag[3]?.toLowerCase() === 'h' ? 60 : minutesPerDay),
68
+ ) * (lag[1] === '-' ? -1 : 1);
69
+ }
70
+ if (!code || !Number.isSafeInteger(lagMinutes)) {
71
+ unknownCodes.push(token);
72
+ continue;
73
+ }
74
+ const id = activityIdByCode.get(code)!;
75
+ if (seen.has(id)) {
76
+ duplicateCodes.push(code);
77
+ continue;
78
+ }
79
+ seen.add(id);
80
+ items.push({ predecessorId: id, type, lagMinutes });
81
+ }
82
+ return { items, unknownCodes, duplicateCodes };
83
+ }
84
+ /* Serialize relationship types and lag with a stable, editable code grammar. */
85
+ export function formatPredecessorText(
86
+ rows: PredecessorTextItemObj[],
87
+ codeById: Map<string, string>,
88
+ minutesPerDay: number,
89
+ ): string {
90
+ return rows
91
+ .map(
92
+ (row) =>
93
+ `${codeById.get(row.predecessorId) || row.predecessorId}${row.type === 'FS' && !row.lagMinutes ? '' : row.type}${row.lagMinutes ? `${row.lagMinutes > 0 ? '+' : '-'}${Number((Math.abs(row.lagMinutes) / minutesPerDay).toFixed(6))}d` : ''}`,
94
+ )
95
+ .join(', ');
96
+ }