@pigknowsdev/date-helpers 2.0.0 → 2.0.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.
@@ -1,6 +1,8 @@
1
1
  import { DateTime, DateObjectUnits } from 'luxon';
2
+ import type { FormatNames, ReportFormats } from './types.js';
3
+ // Apps import these types from 'dist/dateHelpers', where they lived before types.ts.
4
+ export type { FormatNames, ReportFormats } from './types.js';
2
5
 
3
- const currentYear = new Date().getFullYear();
4
6
  const NEWSHAM_DAY_ZERO = '1985-06-05';
5
7
  const THOUSAND_DAY_ZERO = '1971-09-27';
6
8
 
@@ -22,21 +24,6 @@ function padNum(input: number | string, places = 2) {
22
24
  return temp;
23
25
  }
24
26
 
25
- export type FormatNames = 'AMERICAN' | 'american' | 'American'
26
- | 'EUROPEAN' | 'european' | 'European'
27
- | 'ISO' | 'iso' | 'Iso'
28
- | 'JULIAN' | 'Julian' | 'julian'
29
- | 'NEWSHAM' | 'Newsham' | 'newsham'
30
- | 'REGULAR' | 'Regular' | 'regular'
31
- | 'THOUSAND' | 'Thousand' | 'thousand';
32
-
33
- export type ReportFormats = '%b%d' | '%b%d%y' |
34
- '%b%d%Y' | '%d%b' | '%d%b%y' | '%d%b%Y' | '%d-%m-%y' |
35
- '%d/%m/%y' | '%d/%m/%Y' | '%m/%d' | '%m/%d/%y' | '%m/%d/%Y' |
36
- '%d-%m-%Y' | '%m-%d' | '%m-%d-%y' | '%m-%d-%Y' | '%j' |
37
- '%J' | '%N' | '%n' | '%t' | '%T' | '%y-%m-%d' | '%Y-%m-%d' |
38
- '%y/%m/%d' | '%Y/%m/%d';
39
-
40
27
  // dateFormatsMap requires Regular-like input
41
28
  // convert to regular first using helper function below
42
29
  const dateFormatsMap = {
@@ -218,7 +205,11 @@ export function getConflictingFormatsForType(formatType: FormatNames | ReportFor
218
205
  return [];
219
206
  }
220
207
 
221
- export function convertDateToRegularString(inputFormat: FormatNames | ReportFormats, date: string): string {
208
+ export function convertDateToRegularString(
209
+ inputFormat: FormatNames | ReportFormats,
210
+ date: string,
211
+ daysAheadAllowed?: number,
212
+ ): string {
222
213
  // if (typeof date !== 'string') {
223
214
  // console.error('Date must be a string.');
224
215
  // }
@@ -233,7 +224,7 @@ export function convertDateToRegularString(inputFormat: FormatNames | ReportForm
233
224
  case 'American':
234
225
  case 'american':
235
226
  if (dateStr.length !== 8) {
236
- dateStr = convertShortcutDate('AMERICAN', dateStr).replace(/\D/g, '');
227
+ dateStr = convertShortcutDate('AMERICAN', dateStr, daysAheadAllowed).replace(/\D/g, '');
237
228
  } else if (date.length === 10 && (date[4] === '-')) {
238
229
  // regular format was entered. convert.
239
230
  dateConfigObj.month = parseInt(date.slice(5, 7));
@@ -249,7 +240,7 @@ export function convertDateToRegularString(inputFormat: FormatNames | ReportForm
249
240
  case 'European':
250
241
  case 'european':
251
242
  if (dateStr.length !== 8) {
252
- dateStr = convertShortcutDate('European', dateStr).replace(/\D/g, '');
243
+ dateStr = convertShortcutDate('European', dateStr, daysAheadAllowed).replace(/\D/g, '');
253
244
  } else if (date.length === 10 && (date[4] === '-')) {
254
245
  // regular format was entered. convert.
255
246
  dateConfigObj.month = parseInt(date.slice(5, 7));
@@ -267,7 +258,7 @@ export function convertDateToRegularString(inputFormat: FormatNames | ReportForm
267
258
  case 'Regular':
268
259
  case 'regular':
269
260
  if (dateStr.length !== 8) {
270
- dateStr = convertShortcutDate('REGULAR', dateStr).replace(/\D/g, '');
261
+ dateStr = convertShortcutDate('REGULAR', dateStr, daysAheadAllowed).replace(/\D/g, '');
271
262
  }
272
263
  dateConfigObj.year = parseInt(dateStr.slice(0, 4));
273
264
  dateConfigObj.month = parseInt(dateStr.slice(4, 6));
@@ -278,7 +269,7 @@ export function convertDateToRegularString(inputFormat: FormatNames | ReportForm
278
269
  case 'Julian':
279
270
  case 'julian':
280
271
  if (dateStr.length !== 5) {
281
- dateStr = convertShortcutDate('JULIAN', dateStr).replace(/\D/g, '');
272
+ dateStr = convertShortcutDate('JULIAN', dateStr, daysAheadAllowed).replace(/\D/g, '');
282
273
  }
283
274
  dateConfigObj.year = parseInt(`20${dateStr.slice(0, 2)}`);
284
275
  dateConfigObj.ordinal = parseInt(dateStr.slice(2, 6));
@@ -290,7 +281,7 @@ export function convertDateToRegularString(inputFormat: FormatNames | ReportForm
290
281
  case 'Thousand':
291
282
  case 'thousand':
292
283
  if (dateStr.length !== 5) {
293
- dateStr = convertShortcutDate(inputFormat, dateStr).replace(/\D/g, '');
284
+ dateStr = convertShortcutDate(inputFormat, dateStr, daysAheadAllowed).replace(/\D/g, '');
294
285
  }
295
286
  const cycles = parseInt(dateStr.slice(0, 2));
296
287
  const leftover = parseInt(dateStr.slice(2));
@@ -326,16 +317,24 @@ export function convertDateToRegularString(inputFormat: FormatNames | ReportForm
326
317
  case '%m/%d/%Y':
327
318
  return DateTime.fromFormat(dateStr, 'MMddyyyy').toFormat('yyyy-MM-dd');
328
319
  case '%j':
329
- return convertDateToRegularString('JULIAN', convertShortcutDate('JULIAN', dateStr));
320
+ return convertDateToRegularString(
321
+ 'JULIAN',
322
+ convertShortcutDate('JULIAN', dateStr, daysAheadAllowed),
323
+ daysAheadAllowed,
324
+ );
330
325
  case '%N':
331
- return convertDateToRegularString('NEWSHAM', dateStr);
326
+ return convertDateToRegularString('NEWSHAM', dateStr, daysAheadAllowed);
332
327
  case '%n':
333
- return convertDateToRegularString('NEWSHAM', convertShortcutDate('NEWSHAM', dateStr));
328
+ return convertDateToRegularString(
329
+ 'NEWSHAM',
330
+ convertShortcutDate('NEWSHAM', dateStr, daysAheadAllowed),
331
+ daysAheadAllowed,
332
+ );
334
333
  case '%t':
335
- const shortcutToFull = convertShortcutDate('THOUSAND', dateStr);
336
- return convertDateToRegularString('THOUSAND', shortcutToFull);
334
+ const shortcutToFull = convertShortcutDate('THOUSAND', dateStr, daysAheadAllowed);
335
+ return convertDateToRegularString('THOUSAND', shortcutToFull, daysAheadAllowed);
337
336
  case '%T':
338
- return convertDateToRegularString('THOUSAND', dateStr);
337
+ return convertDateToRegularString('THOUSAND', dateStr, daysAheadAllowed);
339
338
  case '%y-%m-%d':
340
339
  case '%y/%m/%d':
341
340
  return DateTime.fromFormat(dateStr, 'yyMMdd').toFormat('yyyy-MM-dd');
@@ -352,7 +351,9 @@ export function convertDateToRegularString(inputFormat: FormatNames | ReportForm
352
351
  export function convertDateToFormatType(
353
352
  inputFormatType: FormatNames | ReportFormats,
354
353
  destinationFormatType: FormatNames,
355
- date: string): string {
354
+ date: string,
355
+ daysAheadAllowed?: number,
356
+ ): string {
356
357
 
357
358
  if (!date || !inputFormatType || !destinationFormatType) {
358
359
  // console.error('Trying to convert with missing fields.');
@@ -366,7 +367,7 @@ export function convertDateToFormatType(
366
367
  const isInRegular = /^\d{4}-\d{2}-\d{2}$/.test(date.toString());
367
368
  let dateConvertedToRegular = isInRegular
368
369
  ? date.toString()
369
- : convertDateToRegularString(inputFormatType, date);
370
+ : convertDateToRegularString(inputFormatType, date, daysAheadAllowed);
370
371
 
371
372
  if (!dateConvertedToRegular) {
372
373
  return '';
@@ -404,13 +405,11 @@ export function detectIfFutureDate(regularDate) {
404
405
  }
405
406
 
406
407
  export function detectIfValidPKDate(regularDate) {
407
- const year = regularDate.slice(0, 4);
408
408
  const month = regularDate.slice(5, 7);
409
409
 
410
- // check valid year
411
- if (parseInt(year) < 1900 || (parseInt(year) > currentYear)) {
412
- return false;
413
- }
410
+ // The year is not limited, as in 1.0.5. Reports format historic and future dates through
411
+ // convertPercentDateFormat, which throws when this returns false, and batch uploads call this
412
+ // directly. Whether an event date may be in the future is for the caller to decide.
414
413
 
415
414
  // check valid month
416
415
  if (parseInt(month) < 1 || (parseInt(month) > 12)) {
@@ -420,9 +419,72 @@ export function detectIfValidPKDate(regularDate) {
420
419
  return DateTime.fromISO(regularDate).isValid;
421
420
  }
422
421
 
422
+ /**
423
+ * Which occurrence a shortcut with no year (or no cycle) means, given its date in the current
424
+ * year or cycle (yyyy-MM-dd): 0 is that date, -1 the occurrence before it, 1 the one after it.
425
+ * No daysAheadAllowed → that date, or the one before it when it is more than a day ahead.
426
+ * With N → dates up to N days ahead stay; later ones fall back; a date already past moves
427
+ * forward when its next occurrence is inside the window (New Year, or the start of a cycle) (F198).
428
+ */
429
+ function resolveShortcutOffset(
430
+ thisYmd: string,
431
+ daysAheadAllowed: number | undefined,
432
+ toNextOccurrence: (date: DateTime) => DateTime,
433
+ ): -1 | 0 | 1 {
434
+ if (daysAheadAllowed === undefined || daysAheadAllowed === null) {
435
+ return detectIfFutureDate(thisYmd) ? -1 : 0;
436
+ }
437
+
438
+ const candidateThis = DateTime.fromISO(thisYmd).startOf('day');
439
+ if (!candidateThis.isValid) {
440
+ return 0;
441
+ }
442
+
443
+ const now = DateTime.local().startOf('day');
444
+ const n = Number(daysAheadAllowed);
445
+ const maxAllowed = now.plus({ days: Number.isFinite(n) ? Math.max(0, n) : 1 });
446
+
447
+ if (candidateThis > maxAllowed) {
448
+ return -1;
449
+ }
450
+
451
+ if (candidateThis < now && toNextOccurrence(candidateThis) <= maxAllowed) {
452
+ return 1;
453
+ }
454
+
455
+ return 0;
456
+ }
457
+
458
+ // Year for a year-less shortcut assumed as thisYearYmd (yyyy-MM-dd): this, last or next year.
459
+ function resolveYearForYearLess(thisYearYmd: string, daysAheadAllowed?: number): number {
460
+ const year = parseInt(thisYearYmd.slice(0, 4), 10);
461
+ return year + resolveShortcutOffset(thisYearYmd, daysAheadAllowed, (date) => date.plus({ years: 1 }));
462
+ }
463
+
464
+ // Thousand and Newsham count days in 1000-day cycles, so a 3-digit shortcut picks a cycle, not a
465
+ // year. A cycle runs across calendar years, so the year of the date says nothing about the cycle.
466
+ function resolveCycleForDayOnly(
467
+ formatType: 'NEWSHAM' | 'THOUSAND',
468
+ currentCycle: string,
469
+ day: string,
470
+ daysAheadAllowed?: number,
471
+ ): string {
472
+ const thisCycle = `${currentCycle}${day}`;
473
+ const offset = resolveShortcutOffset(
474
+ convertDateToFormatType(formatType, 'REGULAR', thisCycle),
475
+ daysAheadAllowed,
476
+ (date) => date.plus({ days: 1000 }),
477
+ );
478
+ return offset === 0 ? thisCycle : `${parseInt(currentCycle) + offset}${day}`;
479
+ }
480
+
423
481
  // shortcut used must belong to the destination formatType.
424
482
  // example: 4 digit American format means something different than 4 digit European
425
- export function convertShortcutDate(formatType: FormatNames, date: string) {
483
+ export function convertShortcutDate(
484
+ formatType: FormatNames,
485
+ date: string,
486
+ daysAheadAllowed?: number,
487
+ ) {
426
488
  const inputDate = date.toString().replace(/\D/g, '');
427
489
  const currentYear = (new Date()).getFullYear();
428
490
  const currentYearEnd = currentYear.toString().slice(2);
@@ -445,9 +507,11 @@ export function convertShortcutDate(formatType: FormatNames, date: string) {
445
507
  const day = formatType.toUpperCase() === 'EUROPEAN'
446
508
  ? inputDate.slice(0, 2)
447
509
  : inputDate.slice(2, 4);
448
- return detectIfFutureDate(`${currentYear}-${month}-${day}`)
449
- ? `${inputDate.slice(0, 2)}-${inputDate.slice(2, 4)}-${currentYear - 1}`
450
- : `${inputDate.slice(0, 2)}-${inputDate.slice(2, 4)}-${currentYear}`;
510
+ const year = resolveYearForYearLess(
511
+ `${currentYear}-${month}-${day}`,
512
+ daysAheadAllowed,
513
+ );
514
+ return `${inputDate.slice(0, 2)}-${inputDate.slice(2, 4)}-${year}`;
451
515
  } else if (inputDate.length === 6) {
452
516
  return `${inputDate.slice(0, 2)}-${inputDate.slice(2, 4)}-20${inputDate.slice(4)}`;
453
517
  } else if (inputDate.length !== 8) {
@@ -460,9 +524,8 @@ export function convertShortcutDate(formatType: FormatNames, date: string) {
460
524
  const paddedDate = `${currentYearEnd}-${padNum(inputDate, 3)}`;
461
525
  const regularizedDate = convertDateToFormatType('JULIAN', 'Regular', paddedDate);
462
526
  if (detectIfValidPKDate(regularizedDate)) {
463
- return detectIfFutureDate(regularizedDate)
464
- ? `${(currentYear - 1).toString().slice(2)}-${padNum(inputDate, 3)}`
465
- : `${currentYearEnd}-${padNum(inputDate, 3)}`;
527
+ const year = resolveYearForYearLess(regularizedDate, daysAheadAllowed);
528
+ return `${year.toString().slice(2)}-${padNum(inputDate, 3)}`;
466
529
  }
467
530
  throw new Error(`${inputDate} does not match 'JULIAN' format or any of its shortcuts.`);
468
531
  } else if (inputDate.length === 4) {
@@ -472,9 +535,16 @@ export function convertShortcutDate(formatType: FormatNames, date: string) {
472
535
  day: parseInt(inputDate.slice(2)),
473
536
  });
474
537
  if (luxonDate.isValid) {
475
- return detectIfFutureDate(luxonDate.toFormat('yyyy-MM-dd'))
476
- ? `${(currentYear - 1).toString().slice(2)}-${luxonDate.toFormat('ooo')}`
477
- : `${currentYearEnd}-${luxonDate.toFormat('ooo')}`;
538
+ const year = resolveYearForYearLess(
539
+ luxonDate.toFormat('yyyy-MM-dd'),
540
+ daysAheadAllowed,
541
+ );
542
+ const ordinalDate = DateTime.fromObject({
543
+ year,
544
+ month: parseInt(inputDate.slice(0, 2)),
545
+ day: parseInt(inputDate.slice(2)),
546
+ });
547
+ return `${year.toString().slice(2)}-${ordinalDate.toFormat('ooo')}`;
478
548
  }
479
549
  throw new Error(`${inputDate} does not match 'JULIAN' format or any of its shortcuts.`);
480
550
  } else if (inputDate.length === 5) {
@@ -501,9 +571,8 @@ export function convertShortcutDate(formatType: FormatNames, date: string) {
501
571
  // allowed shortcuts: MMdd and yyMMdd
502
572
  if (inputDate.length === 4) {
503
573
  const date = `${currentYear}-${inputDate.slice(0, 2)}-${inputDate.slice(2)}`;
504
- return detectIfFutureDate(date)
505
- ? `${currentYear - 1}-${inputDate.slice(0, 2)}-${inputDate.slice(2)}`
506
- : date;
574
+ const year = resolveYearForYearLess(date, daysAheadAllowed);
575
+ return `${year}-${inputDate.slice(0, 2)}-${inputDate.slice(2)}`;
507
576
  } else if (inputDate.length === 6) {
508
577
  return `20${inputDate.slice(0, 2)}-${inputDate.slice(2, 4)}-${inputDate.slice(4)}`;
509
578
  } else if (inputDate.length !== 8) {
@@ -515,11 +584,7 @@ export function convertShortcutDate(formatType: FormatNames, date: string) {
515
584
  const todayNewsham = convertDateToFormatType('REGULAR', 'NEWSHAM', DateTime.local().toFormat('yyyy-MM-dd'));
516
585
  const currentNewshamCycle = todayNewsham.slice(0, 2);
517
586
  if (inputDate.length < 4) {
518
- const paddedDate = `${currentNewshamCycle}${padNum(inputDate, 3)}`;
519
- const regularizedDate = convertDateToFormatType('NEWSHAM', 'REGULAR', paddedDate);
520
- return detectIfFutureDate(regularizedDate)
521
- ? `${parseInt(currentNewshamCycle) - 1}${padNum(inputDate, 3)}`
522
- : paddedDate;
587
+ return resolveCycleForDayOnly('NEWSHAM', currentNewshamCycle, padNum(inputDate, 3), daysAheadAllowed);
523
588
  } else if (inputDate.length === 4) {
524
589
  return `1${inputDate}`;
525
590
  } else if (inputDate.length === 6) {
@@ -537,17 +602,16 @@ export function convertShortcutDate(formatType: FormatNames, date: string) {
537
602
  const todayThousand = convertDateToFormatType('REGULAR', 'THOUSAND', DateTime.local().toFormat('yyyy-MM-dd'));
538
603
  const currentCycle = todayThousand.slice(0, 2);
539
604
  if (inputDate.length < 4) {
540
- const paddedDate = `${currentCycle}${padNum(inputDate, 3)}`;
541
- const regularizedDate = convertDateToFormatType('THOUSAND', 'REGULAR', paddedDate);
542
- return detectIfFutureDate(regularizedDate)
543
- ? `${parseInt(currentCycle) - 1}${padNum(inputDate, 3)}`
544
- : paddedDate;
605
+ return resolveCycleForDayOnly('THOUSAND', currentCycle, padNum(inputDate, 3), daysAheadAllowed);
545
606
  } else if (inputDate.length === 4) {
546
607
  const regularDate = `${currentYear}-${inputDate.slice(0, 2)}-${inputDate.slice(2)}`;
547
608
  if (detectIfValidPKDate(regularDate)) {
548
- return detectIfFutureDate(regularDate)
549
- ? convertDateToFormatType('REGULAR', 'THOUSAND', regularDate.replace(currentYear.toString(), (currentYear - 1).toString()))
550
- : convertDateToFormatType('REGULAR', 'THOUSAND', regularDate);
609
+ const year = resolveYearForYearLess(regularDate, daysAheadAllowed);
610
+ return convertDateToFormatType(
611
+ 'REGULAR',
612
+ 'THOUSAND',
613
+ regularDate.replace(currentYear.toString(), year.toString()),
614
+ );
551
615
  }
552
616
  throw new Error(`${inputDate} does not match 'THOUSAND' format or any of its shortcuts.`);
553
617
  } else if (inputDate.length === 6) {
package/src/index.ts CHANGED
@@ -7,7 +7,7 @@ import {
7
7
  detectIfFutureDate,
8
8
  detectIfValidPKDate,
9
9
  getConflictingFormatsForType,
10
- } from './dateHelpers';
10
+ } from './dateHelpers.js';
11
11
 
12
12
  export {
13
13
  convertDateToFormatType,
@@ -19,3 +19,5 @@ export {
19
19
  detectIfValidPKDate,
20
20
  getConflictingFormatsForType,
21
21
  }
22
+
23
+ export type { FormatNames, ReportFormats } from './types.js';
package/src/types.ts ADDED
@@ -0,0 +1,50 @@
1
+ export type FormatNames =
2
+ | "AMERICAN"
3
+ | "american"
4
+ | "American"
5
+ | "EUROPEAN"
6
+ | "european"
7
+ | "European"
8
+ | "ISO"
9
+ | "iso"
10
+ | "Iso"
11
+ | "JULIAN"
12
+ | "Julian"
13
+ | "julian"
14
+ | "NEWSHAM"
15
+ | "Newsham"
16
+ | "newsham"
17
+ | "REGULAR"
18
+ | "Regular"
19
+ | "regular"
20
+ | "THOUSAND"
21
+ | "Thousand"
22
+ | "thousand";
23
+
24
+ export type ReportFormats =
25
+ | "%b%d"
26
+ | "%b%d%y"
27
+ | "%b%d%Y"
28
+ | "%d%b"
29
+ | "%d%b%y"
30
+ | "%d%b%Y"
31
+ | "%d-%m-%y"
32
+ | "%d/%m/%y"
33
+ | "%d/%m/%Y"
34
+ | "%m/%d"
35
+ | "%m/%d/%y"
36
+ | "%m/%d/%Y"
37
+ | "%d-%m-%Y"
38
+ | "%m-%d"
39
+ | "%m-%d-%y"
40
+ | "%m-%d-%Y"
41
+ | "%j"
42
+ | "%J"
43
+ | "%N"
44
+ | "%n"
45
+ | "%t"
46
+ | "%T"
47
+ | "%y-%m-%d"
48
+ | "%Y-%m-%d"
49
+ | "%y/%m/%d"
50
+ | "%Y/%m/%d";