@pigknowsdev/date-helpers 2.0.1 → 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.
- package/README.md +24 -6
- package/dist/cjs/dateHelpers.d.ts +10 -0
- package/dist/cjs/dateHelpers.js +670 -0
- package/dist/cjs/dateHelpers.js.map +1 -0
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.js +13 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/types.d.ts +2 -0
- package/dist/cjs/types.js +3 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/dateHelpers.d.ts +5 -5
- package/dist/dateHelpers.js +73 -46
- package/dist/dateHelpers.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +2 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +11 -5
- package/src/dateHelpers.ts +125 -47
- package/src/index.ts +3 -1
package/src/dateHelpers.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { DateTime, DateObjectUnits } from 'luxon';
|
|
2
|
-
import type { FormatNames, ReportFormats } from './types'
|
|
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';
|
|
3
5
|
|
|
4
|
-
const currentYear = new Date().getFullYear();
|
|
5
6
|
const NEWSHAM_DAY_ZERO = '1985-06-05';
|
|
6
7
|
const THOUSAND_DAY_ZERO = '1971-09-27';
|
|
7
8
|
|
|
@@ -204,7 +205,11 @@ export function getConflictingFormatsForType(formatType: FormatNames | ReportFor
|
|
|
204
205
|
return [];
|
|
205
206
|
}
|
|
206
207
|
|
|
207
|
-
export function convertDateToRegularString(
|
|
208
|
+
export function convertDateToRegularString(
|
|
209
|
+
inputFormat: FormatNames | ReportFormats,
|
|
210
|
+
date: string,
|
|
211
|
+
daysAheadAllowed?: number,
|
|
212
|
+
): string {
|
|
208
213
|
// if (typeof date !== 'string') {
|
|
209
214
|
// console.error('Date must be a string.');
|
|
210
215
|
// }
|
|
@@ -219,7 +224,7 @@ export function convertDateToRegularString(inputFormat: FormatNames | ReportForm
|
|
|
219
224
|
case 'American':
|
|
220
225
|
case 'american':
|
|
221
226
|
if (dateStr.length !== 8) {
|
|
222
|
-
dateStr = convertShortcutDate('AMERICAN', dateStr).replace(/\D/g, '');
|
|
227
|
+
dateStr = convertShortcutDate('AMERICAN', dateStr, daysAheadAllowed).replace(/\D/g, '');
|
|
223
228
|
} else if (date.length === 10 && (date[4] === '-')) {
|
|
224
229
|
// regular format was entered. convert.
|
|
225
230
|
dateConfigObj.month = parseInt(date.slice(5, 7));
|
|
@@ -235,7 +240,7 @@ export function convertDateToRegularString(inputFormat: FormatNames | ReportForm
|
|
|
235
240
|
case 'European':
|
|
236
241
|
case 'european':
|
|
237
242
|
if (dateStr.length !== 8) {
|
|
238
|
-
dateStr = convertShortcutDate('European', dateStr).replace(/\D/g, '');
|
|
243
|
+
dateStr = convertShortcutDate('European', dateStr, daysAheadAllowed).replace(/\D/g, '');
|
|
239
244
|
} else if (date.length === 10 && (date[4] === '-')) {
|
|
240
245
|
// regular format was entered. convert.
|
|
241
246
|
dateConfigObj.month = parseInt(date.slice(5, 7));
|
|
@@ -253,7 +258,7 @@ export function convertDateToRegularString(inputFormat: FormatNames | ReportForm
|
|
|
253
258
|
case 'Regular':
|
|
254
259
|
case 'regular':
|
|
255
260
|
if (dateStr.length !== 8) {
|
|
256
|
-
dateStr = convertShortcutDate('REGULAR', dateStr).replace(/\D/g, '');
|
|
261
|
+
dateStr = convertShortcutDate('REGULAR', dateStr, daysAheadAllowed).replace(/\D/g, '');
|
|
257
262
|
}
|
|
258
263
|
dateConfigObj.year = parseInt(dateStr.slice(0, 4));
|
|
259
264
|
dateConfigObj.month = parseInt(dateStr.slice(4, 6));
|
|
@@ -264,7 +269,7 @@ export function convertDateToRegularString(inputFormat: FormatNames | ReportForm
|
|
|
264
269
|
case 'Julian':
|
|
265
270
|
case 'julian':
|
|
266
271
|
if (dateStr.length !== 5) {
|
|
267
|
-
dateStr = convertShortcutDate('JULIAN', dateStr).replace(/\D/g, '');
|
|
272
|
+
dateStr = convertShortcutDate('JULIAN', dateStr, daysAheadAllowed).replace(/\D/g, '');
|
|
268
273
|
}
|
|
269
274
|
dateConfigObj.year = parseInt(`20${dateStr.slice(0, 2)}`);
|
|
270
275
|
dateConfigObj.ordinal = parseInt(dateStr.slice(2, 6));
|
|
@@ -276,7 +281,7 @@ export function convertDateToRegularString(inputFormat: FormatNames | ReportForm
|
|
|
276
281
|
case 'Thousand':
|
|
277
282
|
case 'thousand':
|
|
278
283
|
if (dateStr.length !== 5) {
|
|
279
|
-
dateStr = convertShortcutDate(inputFormat, dateStr).replace(/\D/g, '');
|
|
284
|
+
dateStr = convertShortcutDate(inputFormat, dateStr, daysAheadAllowed).replace(/\D/g, '');
|
|
280
285
|
}
|
|
281
286
|
const cycles = parseInt(dateStr.slice(0, 2));
|
|
282
287
|
const leftover = parseInt(dateStr.slice(2));
|
|
@@ -312,16 +317,24 @@ export function convertDateToRegularString(inputFormat: FormatNames | ReportForm
|
|
|
312
317
|
case '%m/%d/%Y':
|
|
313
318
|
return DateTime.fromFormat(dateStr, 'MMddyyyy').toFormat('yyyy-MM-dd');
|
|
314
319
|
case '%j':
|
|
315
|
-
return convertDateToRegularString(
|
|
320
|
+
return convertDateToRegularString(
|
|
321
|
+
'JULIAN',
|
|
322
|
+
convertShortcutDate('JULIAN', dateStr, daysAheadAllowed),
|
|
323
|
+
daysAheadAllowed,
|
|
324
|
+
);
|
|
316
325
|
case '%N':
|
|
317
|
-
return convertDateToRegularString('NEWSHAM', dateStr);
|
|
326
|
+
return convertDateToRegularString('NEWSHAM', dateStr, daysAheadAllowed);
|
|
318
327
|
case '%n':
|
|
319
|
-
return convertDateToRegularString(
|
|
328
|
+
return convertDateToRegularString(
|
|
329
|
+
'NEWSHAM',
|
|
330
|
+
convertShortcutDate('NEWSHAM', dateStr, daysAheadAllowed),
|
|
331
|
+
daysAheadAllowed,
|
|
332
|
+
);
|
|
320
333
|
case '%t':
|
|
321
|
-
const shortcutToFull = convertShortcutDate('THOUSAND', dateStr);
|
|
322
|
-
return convertDateToRegularString('THOUSAND', shortcutToFull);
|
|
334
|
+
const shortcutToFull = convertShortcutDate('THOUSAND', dateStr, daysAheadAllowed);
|
|
335
|
+
return convertDateToRegularString('THOUSAND', shortcutToFull, daysAheadAllowed);
|
|
323
336
|
case '%T':
|
|
324
|
-
return convertDateToRegularString('THOUSAND', dateStr);
|
|
337
|
+
return convertDateToRegularString('THOUSAND', dateStr, daysAheadAllowed);
|
|
325
338
|
case '%y-%m-%d':
|
|
326
339
|
case '%y/%m/%d':
|
|
327
340
|
return DateTime.fromFormat(dateStr, 'yyMMdd').toFormat('yyyy-MM-dd');
|
|
@@ -338,7 +351,9 @@ export function convertDateToRegularString(inputFormat: FormatNames | ReportForm
|
|
|
338
351
|
export function convertDateToFormatType(
|
|
339
352
|
inputFormatType: FormatNames | ReportFormats,
|
|
340
353
|
destinationFormatType: FormatNames,
|
|
341
|
-
date: string
|
|
354
|
+
date: string,
|
|
355
|
+
daysAheadAllowed?: number,
|
|
356
|
+
): string {
|
|
342
357
|
|
|
343
358
|
if (!date || !inputFormatType || !destinationFormatType) {
|
|
344
359
|
// console.error('Trying to convert with missing fields.');
|
|
@@ -352,7 +367,7 @@ export function convertDateToFormatType(
|
|
|
352
367
|
const isInRegular = /^\d{4}-\d{2}-\d{2}$/.test(date.toString());
|
|
353
368
|
let dateConvertedToRegular = isInRegular
|
|
354
369
|
? date.toString()
|
|
355
|
-
: convertDateToRegularString(inputFormatType, date);
|
|
370
|
+
: convertDateToRegularString(inputFormatType, date, daysAheadAllowed);
|
|
356
371
|
|
|
357
372
|
if (!dateConvertedToRegular) {
|
|
358
373
|
return '';
|
|
@@ -390,13 +405,11 @@ export function detectIfFutureDate(regularDate) {
|
|
|
390
405
|
}
|
|
391
406
|
|
|
392
407
|
export function detectIfValidPKDate(regularDate) {
|
|
393
|
-
const year = regularDate.slice(0, 4);
|
|
394
408
|
const month = regularDate.slice(5, 7);
|
|
395
409
|
|
|
396
|
-
//
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
}
|
|
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.
|
|
400
413
|
|
|
401
414
|
// check valid month
|
|
402
415
|
if (parseInt(month) < 1 || (parseInt(month) > 12)) {
|
|
@@ -406,9 +419,72 @@ export function detectIfValidPKDate(regularDate) {
|
|
|
406
419
|
return DateTime.fromISO(regularDate).isValid;
|
|
407
420
|
}
|
|
408
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
|
+
|
|
409
481
|
// shortcut used must belong to the destination formatType.
|
|
410
482
|
// example: 4 digit American format means something different than 4 digit European
|
|
411
|
-
export function convertShortcutDate(
|
|
483
|
+
export function convertShortcutDate(
|
|
484
|
+
formatType: FormatNames,
|
|
485
|
+
date: string,
|
|
486
|
+
daysAheadAllowed?: number,
|
|
487
|
+
) {
|
|
412
488
|
const inputDate = date.toString().replace(/\D/g, '');
|
|
413
489
|
const currentYear = (new Date()).getFullYear();
|
|
414
490
|
const currentYearEnd = currentYear.toString().slice(2);
|
|
@@ -431,9 +507,11 @@ export function convertShortcutDate(formatType: FormatNames, date: string) {
|
|
|
431
507
|
const day = formatType.toUpperCase() === 'EUROPEAN'
|
|
432
508
|
? inputDate.slice(0, 2)
|
|
433
509
|
: inputDate.slice(2, 4);
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
510
|
+
const year = resolveYearForYearLess(
|
|
511
|
+
`${currentYear}-${month}-${day}`,
|
|
512
|
+
daysAheadAllowed,
|
|
513
|
+
);
|
|
514
|
+
return `${inputDate.slice(0, 2)}-${inputDate.slice(2, 4)}-${year}`;
|
|
437
515
|
} else if (inputDate.length === 6) {
|
|
438
516
|
return `${inputDate.slice(0, 2)}-${inputDate.slice(2, 4)}-20${inputDate.slice(4)}`;
|
|
439
517
|
} else if (inputDate.length !== 8) {
|
|
@@ -446,9 +524,8 @@ export function convertShortcutDate(formatType: FormatNames, date: string) {
|
|
|
446
524
|
const paddedDate = `${currentYearEnd}-${padNum(inputDate, 3)}`;
|
|
447
525
|
const regularizedDate = convertDateToFormatType('JULIAN', 'Regular', paddedDate);
|
|
448
526
|
if (detectIfValidPKDate(regularizedDate)) {
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
: `${currentYearEnd}-${padNum(inputDate, 3)}`;
|
|
527
|
+
const year = resolveYearForYearLess(regularizedDate, daysAheadAllowed);
|
|
528
|
+
return `${year.toString().slice(2)}-${padNum(inputDate, 3)}`;
|
|
452
529
|
}
|
|
453
530
|
throw new Error(`${inputDate} does not match 'JULIAN' format or any of its shortcuts.`);
|
|
454
531
|
} else if (inputDate.length === 4) {
|
|
@@ -458,9 +535,16 @@ export function convertShortcutDate(formatType: FormatNames, date: string) {
|
|
|
458
535
|
day: parseInt(inputDate.slice(2)),
|
|
459
536
|
});
|
|
460
537
|
if (luxonDate.isValid) {
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
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')}`;
|
|
464
548
|
}
|
|
465
549
|
throw new Error(`${inputDate} does not match 'JULIAN' format or any of its shortcuts.`);
|
|
466
550
|
} else if (inputDate.length === 5) {
|
|
@@ -487,9 +571,8 @@ export function convertShortcutDate(formatType: FormatNames, date: string) {
|
|
|
487
571
|
// allowed shortcuts: MMdd and yyMMdd
|
|
488
572
|
if (inputDate.length === 4) {
|
|
489
573
|
const date = `${currentYear}-${inputDate.slice(0, 2)}-${inputDate.slice(2)}`;
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
: date;
|
|
574
|
+
const year = resolveYearForYearLess(date, daysAheadAllowed);
|
|
575
|
+
return `${year}-${inputDate.slice(0, 2)}-${inputDate.slice(2)}`;
|
|
493
576
|
} else if (inputDate.length === 6) {
|
|
494
577
|
return `20${inputDate.slice(0, 2)}-${inputDate.slice(2, 4)}-${inputDate.slice(4)}`;
|
|
495
578
|
} else if (inputDate.length !== 8) {
|
|
@@ -501,11 +584,7 @@ export function convertShortcutDate(formatType: FormatNames, date: string) {
|
|
|
501
584
|
const todayNewsham = convertDateToFormatType('REGULAR', 'NEWSHAM', DateTime.local().toFormat('yyyy-MM-dd'));
|
|
502
585
|
const currentNewshamCycle = todayNewsham.slice(0, 2);
|
|
503
586
|
if (inputDate.length < 4) {
|
|
504
|
-
|
|
505
|
-
const regularizedDate = convertDateToFormatType('NEWSHAM', 'REGULAR', paddedDate);
|
|
506
|
-
return detectIfFutureDate(regularizedDate)
|
|
507
|
-
? `${parseInt(currentNewshamCycle) - 1}${padNum(inputDate, 3)}`
|
|
508
|
-
: paddedDate;
|
|
587
|
+
return resolveCycleForDayOnly('NEWSHAM', currentNewshamCycle, padNum(inputDate, 3), daysAheadAllowed);
|
|
509
588
|
} else if (inputDate.length === 4) {
|
|
510
589
|
return `1${inputDate}`;
|
|
511
590
|
} else if (inputDate.length === 6) {
|
|
@@ -523,17 +602,16 @@ export function convertShortcutDate(formatType: FormatNames, date: string) {
|
|
|
523
602
|
const todayThousand = convertDateToFormatType('REGULAR', 'THOUSAND', DateTime.local().toFormat('yyyy-MM-dd'));
|
|
524
603
|
const currentCycle = todayThousand.slice(0, 2);
|
|
525
604
|
if (inputDate.length < 4) {
|
|
526
|
-
|
|
527
|
-
const regularizedDate = convertDateToFormatType('THOUSAND', 'REGULAR', paddedDate);
|
|
528
|
-
return detectIfFutureDate(regularizedDate)
|
|
529
|
-
? `${parseInt(currentCycle) - 1}${padNum(inputDate, 3)}`
|
|
530
|
-
: paddedDate;
|
|
605
|
+
return resolveCycleForDayOnly('THOUSAND', currentCycle, padNum(inputDate, 3), daysAheadAllowed);
|
|
531
606
|
} else if (inputDate.length === 4) {
|
|
532
607
|
const regularDate = `${currentYear}-${inputDate.slice(0, 2)}-${inputDate.slice(2)}`;
|
|
533
608
|
if (detectIfValidPKDate(regularDate)) {
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
609
|
+
const year = resolveYearForYearLess(regularDate, daysAheadAllowed);
|
|
610
|
+
return convertDateToFormatType(
|
|
611
|
+
'REGULAR',
|
|
612
|
+
'THOUSAND',
|
|
613
|
+
regularDate.replace(currentYear.toString(), year.toString()),
|
|
614
|
+
);
|
|
537
615
|
}
|
|
538
616
|
throw new Error(`${inputDate} does not match 'THOUSAND' format or any of its shortcuts.`);
|
|
539
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';
|