@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.
@@ -0,0 +1,670 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertPercentDateFormat = exports.convertShortcutDate = exports.detectIfValidPKDate = exports.detectIfFutureDate = exports.convertDateToFormatType = exports.convertDateToRegularString = exports.getConflictingFormatsForType = exports.detectFormatType = void 0;
4
+ const luxon_1 = require("luxon");
5
+ const NEWSHAM_DAY_ZERO = '1985-06-05';
6
+ const THOUSAND_DAY_ZERO = '1971-09-27';
7
+ // reference: https://bitbucket.org/pigknowsdev/documentation/src/master/product%20specs/DATE_FORMATS.md
8
+ /* CONTENT:
9
+ - padNum (helper function to add leading zeros to numbers)
10
+ - dateFormatsMap (object mapper that takes format type and returns either luxon format string or conversion function)
11
+ - convertDateToRegularString
12
+ - converDateToFormatType
13
+ - convertShortcutDate
14
+ */
15
+ function padNum(input, places = 2) {
16
+ let temp = input.toString();
17
+ while (temp.length < places) {
18
+ temp = `0${temp}`;
19
+ }
20
+ return temp;
21
+ }
22
+ // dateFormatsMap requires Regular-like input
23
+ // convert to regular first using helper function below
24
+ const dateFormatsMap = {
25
+ AMERICAN: 'MM-dd-yyyy',
26
+ EUROPEAN: 'dd-MM-yyyy',
27
+ ISO: 'yyyy-MM-dd',
28
+ JULIAN: 'yy-ooo',
29
+ NEWSHAM: (date) => {
30
+ const temp = date.toString().replace(/\D/g, '');
31
+ const luxonDate = luxon_1.DateTime.fromObject({
32
+ day: parseInt(temp.slice(6, 8)),
33
+ month: parseInt(temp.slice(4, 6)),
34
+ year: parseInt(temp.slice(0, 4)),
35
+ });
36
+ const dayZero = NEWSHAM_DAY_ZERO.toString().replace(/\D/g, '');
37
+ const luxonZeroDay = luxon_1.DateTime.fromObject({
38
+ day: parseInt(dayZero.slice(6, 8)),
39
+ month: parseInt(dayZero.slice(4, 6)),
40
+ year: parseInt(dayZero.slice(0, 4)),
41
+ });
42
+ const diffInDays = Math.abs(luxonZeroDay.diff(luxonDate, 'days').as('days'));
43
+ const thousands = Math.floor(diffInDays / 1000);
44
+ const dayInYear = Math.floor(diffInDays - (thousands * 1000));
45
+ return `${padNum(thousands)}${padNum(dayInYear, 3)}`;
46
+ },
47
+ REGULAR: 'yyyy-MM-dd',
48
+ THOUSAND: (date) => {
49
+ const temp = date.toString().replace(/\D/g, '');
50
+ const luxonDate = luxon_1.DateTime.fromObject({
51
+ day: parseInt(temp.slice(6, 8)),
52
+ month: parseInt(temp.slice(4, 6)),
53
+ year: parseInt(temp.slice(0, 4)),
54
+ });
55
+ const dayZero = THOUSAND_DAY_ZERO.toString().replace(/\D/g, '');
56
+ const luxonZeroDay = luxon_1.DateTime.fromObject({
57
+ day: parseInt(dayZero.slice(6)),
58
+ month: parseInt(dayZero.slice(4, 6)),
59
+ year: parseInt(dayZero.slice(0, 4)),
60
+ });
61
+ const diffInDays = Math.abs(luxonZeroDay.diff(luxonDate, 'days').as('days'));
62
+ const thousands = Math.floor(diffInDays / 1000);
63
+ const dayInYear = Math.floor(diffInDays - (thousands * 1000));
64
+ return `${padNum(thousands)}${padNum(dayInYear, 3)}`;
65
+ },
66
+ };
67
+ function detectFormatType(date, preferStandard = true, ignoreFormats = []) {
68
+ if (!date) {
69
+ // console.error('Input date required.');
70
+ return '';
71
+ }
72
+ const standardFormatMap = {
73
+ 'AMERICAN': /^\d{2}-\d{2}-\d{4}$/,
74
+ 'REGULAR': /^\d{4}-\d{2}-\d{2}$/,
75
+ 'EUROPEAN': /^\d{2}-\d{2}-\d{4}$/,
76
+ 'THOUSAND': /^\d{5}$/,
77
+ 'JULIAN': /^\d{2}-\d{3}$/,
78
+ 'NEWSHAM': /^\d{5}$/,
79
+ };
80
+ const extraFormatMap = {
81
+ '%b%d': /^([a-zA-Z]){3}\d{2}$/,
82
+ '%b%d%y': /^([a-zA-Z]){3}\d{2}\d{2}$/,
83
+ '%b%d%Y': /^([a-zA-Z]){3}\d{2}\d{4}$/,
84
+ '%d%b': /^\d{2}([a-zA-Z]){3}$/,
85
+ '%d%b%y': /^\d{2}([a-zA-Z]){3}\d{2}$/,
86
+ '%d%b%Y': /^\d{2}([a-zA-Z]){3}\d{4}$/,
87
+ '%d-%m-%y': /^\d{2}-\d{2}-\d{2}$/,
88
+ '%d-%m-%Y': /^\d{2}-\d{2}-\d{4}$/,
89
+ '%m-%d': /^\d{2}-\d{2}$/,
90
+ '%m-%d-%y': /^\d{2}-\d{2}-\d{2}$/,
91
+ '%m-%d-%Y': /^\d{2}-\d{2}-\d{4}$/,
92
+ '%j': /^\d{3}$/,
93
+ '%J': /^\d{2}-\d{3}$/,
94
+ '%N': /^\d{5}$/,
95
+ '%n': /^\d{3}$/,
96
+ '%t': /^\d{3}$/,
97
+ '%T': /^\d{5}$/,
98
+ '%y-%m-%d': /^\d{2}-\d{2}-\d{2}$/,
99
+ '%Y-%m-%d': /^\d{4}-\d{2}-\d{2}$/,
100
+ };
101
+ const fullFormatMap = preferStandard ? standardFormatMap : extraFormatMap;
102
+ const formatMap = {};
103
+ Object.keys(fullFormatMap).forEach(format => {
104
+ // remove unneeded formats and set preference for conflicting formats
105
+ if (!ignoreFormats.includes(format))
106
+ formatMap[format] = fullFormatMap[format];
107
+ });
108
+ const standardizedDate = date.toString().replace(/\//g, '-').split('-').map(x => x.replace(/[^a-zA-z0-9]/g, '')).join('-');
109
+ if (standardizedDate.replace(/\D/g, '').length === 6) { // 2 digits for month, day, year
110
+ if (formatMap.AMERICAN && !formatMap.EUROPEAN && !formatMap.REGULAR) {
111
+ return 'AMERICAN';
112
+ }
113
+ else if (!formatMap.AMERICAN && formatMap.EUROPEAN && !formatMap.REGULAR) {
114
+ return 'EUROPEAN';
115
+ }
116
+ else if (!formatMap.AMERICAN && !formatMap.EUROPEAN && formatMap.REGULAR) {
117
+ return 'REGULAR';
118
+ }
119
+ else if (preferStandard) { // 6 digits with Julian, Thousand, or Newsham
120
+ throw 'INVALID DATE';
121
+ }
122
+ }
123
+ for (const formatName in formatMap) {
124
+ if (formatMap[formatName].test(standardizedDate)) {
125
+ return formatName;
126
+ }
127
+ }
128
+ if (preferStandard) { // still no match? check other supported shortcuts
129
+ // order preference: THOUSAND, JULIAN, NEWSHAM
130
+ if (standardizedDate.length <= 3) {
131
+ if (!ignoreFormats.includes('THOUSAND')) {
132
+ return 'THOUSAND';
133
+ }
134
+ else if (!ignoreFormats.includes('JULIAN')) {
135
+ return 'JULIAN';
136
+ }
137
+ else if (!ignoreFormats.includes('NEWSHAM')) {
138
+ return 'NEWSHAM';
139
+ }
140
+ }
141
+ else if (standardizedDate.replace(/\D/g, '').length === 5) {
142
+ if (!ignoreFormats.includes('THOUSAND')) {
143
+ return 'THOUSAND';
144
+ }
145
+ else if (!ignoreFormats.includes('JULIAN')) {
146
+ return 'JULIAN';
147
+ }
148
+ else if (!ignoreFormats.includes('NEWSHAM')) {
149
+ return 'NEWSHAM';
150
+ }
151
+ }
152
+ }
153
+ // console.error(`Unknown format type for date: ${date.toString()}`);
154
+ return '';
155
+ }
156
+ exports.detectFormatType = detectFormatType;
157
+ function getConflictingFormatsForType(formatType) {
158
+ if (!formatType || typeof formatType !== 'string') {
159
+ // console.error('Please use an existing format type supported by PigKnows.');
160
+ return [];
161
+ }
162
+ const conflictsMap = {
163
+ 'AMERICAN': ['EUROPEAN', 'REGULAR'],
164
+ 'EUROPEAN': ['AMERICAN', 'REGULAR'],
165
+ 'JULIAN': ['THOUSAND', 'NEWSHAM'],
166
+ 'NEWSHAM': ['THOUSAND', 'JULIAN'],
167
+ 'REGULAR': ['AMERICAN', 'EUROPEAN'],
168
+ 'THOUSAND': ['NEWSHAM', 'JULIAN'],
169
+ '%b%d': [],
170
+ '%b%d%y': [],
171
+ '%b%d%Y': [],
172
+ '%d%b': [],
173
+ '%d%b%y': [],
174
+ '%d%b%Y': [],
175
+ '%d-%m-%y': ['%m-%d-%y', '%y-%m-%d'],
176
+ '%d-%m-%Y': ['%m-%d-%Y'],
177
+ '%m-%d': [],
178
+ '%m-%d-%y': ['%d-%m-%y', '%y-%m-%d'],
179
+ '%m-%d-%Y': ['%d-%m-%Y'],
180
+ '%j': ['%n', '%t'],
181
+ '%J': [],
182
+ '%N': ['%T'],
183
+ '%n': ['%j', '%t'],
184
+ '%t': ['%j', '%n'],
185
+ '%T': ['%N'],
186
+ '%y-%m-%d': ['%d-%m-%y', '%m-%d-%y'],
187
+ '%Y-%m-%d': [],
188
+ };
189
+ if (conflictsMap[formatType]) {
190
+ return conflictsMap[formatType];
191
+ }
192
+ // console.error(`Unknown format type: ${formatType}`);
193
+ return [];
194
+ }
195
+ exports.getConflictingFormatsForType = getConflictingFormatsForType;
196
+ function convertDateToRegularString(inputFormat, date, daysAheadAllowed) {
197
+ // if (typeof date !== 'string') {
198
+ // console.error('Date must be a string.');
199
+ // }
200
+ if (!date) {
201
+ return '';
202
+ }
203
+ let dateStr = date.toString().replace(/[^a-zA-Z0-9]/g, '');
204
+ const dateConfigObj = {};
205
+ switch (inputFormat) {
206
+ case 'AMERICAN':
207
+ case 'American':
208
+ case 'american':
209
+ if (dateStr.length !== 8) {
210
+ dateStr = convertShortcutDate('AMERICAN', dateStr, daysAheadAllowed).replace(/\D/g, '');
211
+ }
212
+ else if (date.length === 10 && (date[4] === '-')) {
213
+ // regular format was entered. convert.
214
+ dateConfigObj.month = parseInt(date.slice(5, 7));
215
+ dateConfigObj.day = parseInt(date.slice(8, 10));
216
+ dateConfigObj.year = parseInt(date.slice(0, 4));
217
+ break;
218
+ }
219
+ dateConfigObj.month = parseInt(dateStr.slice(0, 2));
220
+ dateConfigObj.day = parseInt(dateStr.slice(2, 4));
221
+ dateConfigObj.year = parseInt(dateStr.slice(4, 8));
222
+ break;
223
+ case 'EUROPEAN':
224
+ case 'European':
225
+ case 'european':
226
+ if (dateStr.length !== 8) {
227
+ dateStr = convertShortcutDate('European', dateStr, daysAheadAllowed).replace(/\D/g, '');
228
+ }
229
+ else if (date.length === 10 && (date[4] === '-')) {
230
+ // regular format was entered. convert.
231
+ dateConfigObj.month = parseInt(date.slice(5, 7));
232
+ dateConfigObj.day = parseInt(date.slice(8, 10));
233
+ dateConfigObj.year = parseInt(date.slice(0, 4));
234
+ break;
235
+ }
236
+ dateConfigObj.month = parseInt(dateStr.slice(2, 4));
237
+ dateConfigObj.day = parseInt(dateStr.slice(0, 2));
238
+ dateConfigObj.year = parseInt(dateStr.slice(4, 8));
239
+ break;
240
+ case 'ISO':
241
+ case 'iso':
242
+ case 'REGULAR':
243
+ case 'Regular':
244
+ case 'regular':
245
+ if (dateStr.length !== 8) {
246
+ dateStr = convertShortcutDate('REGULAR', dateStr, daysAheadAllowed).replace(/\D/g, '');
247
+ }
248
+ dateConfigObj.year = parseInt(dateStr.slice(0, 4));
249
+ dateConfigObj.month = parseInt(dateStr.slice(4, 6));
250
+ dateConfigObj.day = parseInt(dateStr.slice(6, 8));
251
+ break;
252
+ case '%J':
253
+ case 'JULIAN':
254
+ case 'Julian':
255
+ case 'julian':
256
+ if (dateStr.length !== 5) {
257
+ dateStr = convertShortcutDate('JULIAN', dateStr, daysAheadAllowed).replace(/\D/g, '');
258
+ }
259
+ dateConfigObj.year = parseInt(`20${dateStr.slice(0, 2)}`);
260
+ dateConfigObj.ordinal = parseInt(dateStr.slice(2, 6));
261
+ break;
262
+ case 'NEWSHAM':
263
+ case 'Newsham':
264
+ case 'newsham':
265
+ case 'THOUSAND':
266
+ case 'Thousand':
267
+ case 'thousand':
268
+ if (dateStr.length !== 5) {
269
+ dateStr = convertShortcutDate(inputFormat, dateStr, daysAheadAllowed).replace(/\D/g, '');
270
+ }
271
+ const cycles = parseInt(dateStr.slice(0, 2));
272
+ const leftover = parseInt(dateStr.slice(2));
273
+ const dayZero = inputFormat.toUpperCase() === 'NEWSHAM'
274
+ ? NEWSHAM_DAY_ZERO
275
+ : THOUSAND_DAY_ZERO;
276
+ return luxon_1.DateTime.fromISO(dayZero).plus({ days: (cycles * 1000) + leftover }).toFormat('yyyy-MM-dd');
277
+ case '%b%d':
278
+ return luxon_1.DateTime.fromFormat(dateStr, 'MMMdd').toFormat('yyyy-MM-dd');
279
+ case '%b%d%y':
280
+ return luxon_1.DateTime.fromFormat(dateStr, 'MMMddyy').toFormat('yyyy-MM-dd');
281
+ case '%b%d%Y':
282
+ return luxon_1.DateTime.fromFormat(dateStr, 'MMMddyyyy').toFormat('yyyy-MM-dd');
283
+ case '%d%b':
284
+ return luxon_1.DateTime.fromFormat(dateStr, 'ddMMM').toFormat('yyyy-MM-dd');
285
+ case '%d%b%y':
286
+ return luxon_1.DateTime.fromFormat(dateStr, 'ddMMMyy').toFormat('yyyy-MM-dd');
287
+ case '%d%b%Y':
288
+ return luxon_1.DateTime.fromFormat(dateStr, 'ddMMMyyyy').toFormat('yyyy-MM-dd');
289
+ case '%d-%m-%y':
290
+ case '%d/%m/%y':
291
+ return luxon_1.DateTime.fromFormat(dateStr, 'ddMMyy').toFormat('yyyy-MM-dd');
292
+ case '%d-%m-%Y':
293
+ case '%d/%m/%Y':
294
+ return luxon_1.DateTime.fromFormat(dateStr, 'ddMMyyyy').toFormat('yyyy-MM-dd');
295
+ case '%m-%d':
296
+ case '%m/%d':
297
+ return luxon_1.DateTime.fromFormat(dateStr, 'MMdd').toFormat('yyyy-MM-dd');
298
+ case '%m-%d-%y':
299
+ case '%m/%d/%y':
300
+ return luxon_1.DateTime.fromFormat(dateStr, 'MMddyy').toFormat('yyyy-MM-dd');
301
+ case '%m-%d-%Y':
302
+ case '%m/%d/%Y':
303
+ return luxon_1.DateTime.fromFormat(dateStr, 'MMddyyyy').toFormat('yyyy-MM-dd');
304
+ case '%j':
305
+ return convertDateToRegularString('JULIAN', convertShortcutDate('JULIAN', dateStr, daysAheadAllowed), daysAheadAllowed);
306
+ case '%N':
307
+ return convertDateToRegularString('NEWSHAM', dateStr, daysAheadAllowed);
308
+ case '%n':
309
+ return convertDateToRegularString('NEWSHAM', convertShortcutDate('NEWSHAM', dateStr, daysAheadAllowed), daysAheadAllowed);
310
+ case '%t':
311
+ const shortcutToFull = convertShortcutDate('THOUSAND', dateStr, daysAheadAllowed);
312
+ return convertDateToRegularString('THOUSAND', shortcutToFull, daysAheadAllowed);
313
+ case '%T':
314
+ return convertDateToRegularString('THOUSAND', dateStr, daysAheadAllowed);
315
+ case '%y-%m-%d':
316
+ case '%y/%m/%d':
317
+ return luxon_1.DateTime.fromFormat(dateStr, 'yyMMdd').toFormat('yyyy-MM-dd');
318
+ case '%Y-%m-%d':
319
+ case '%Y/%m/%d':
320
+ return luxon_1.DateTime.fromFormat(dateStr, 'yyyyMMdd').toFormat('yyyy-MM-dd');
321
+ default:
322
+ return dateStr;
323
+ }
324
+ return luxon_1.DateTime.fromObject(dateConfigObj).toLocal().toFormat('yyyy-MM-dd');
325
+ }
326
+ exports.convertDateToRegularString = convertDateToRegularString;
327
+ function convertDateToFormatType(inputFormatType, destinationFormatType, date, daysAheadAllowed) {
328
+ if (!date || !inputFormatType || !destinationFormatType) {
329
+ // console.error('Trying to convert with missing fields.');
330
+ return '';
331
+ }
332
+ if (typeof date !== 'string') {
333
+ throw new Error('Date must be a string.');
334
+ }
335
+ const isInRegular = /^\d{4}-\d{2}-\d{2}$/.test(date.toString());
336
+ let dateConvertedToRegular = isInRegular
337
+ ? date.toString()
338
+ : convertDateToRegularString(inputFormatType, date, daysAheadAllowed);
339
+ if (!dateConvertedToRegular) {
340
+ return '';
341
+ }
342
+ if (dateConvertedToRegular.replace(/\D/g, '').length !== 8 ||
343
+ !(/^\d{8}$/).test(dateConvertedToRegular.replace(/\D/g, ''))) {
344
+ // console.warn(`input '${date}' does not match inputFormat '${inputFormatType}' or any of its shortcuts.`);
345
+ return 'INVALID DATE';
346
+ }
347
+ const formatter = dateFormatsMap[destinationFormatType.toUpperCase()];
348
+ if (!formatter) {
349
+ // console.warn(`${destinationFormatType} is not a valid date format.`);
350
+ return 'INVALID DATE';
351
+ }
352
+ if (/^\d{4}-\d{2}-\d{2}$/.test(dateConvertedToRegular)) {
353
+ return typeof formatter === 'string'
354
+ ? luxon_1.DateTime.fromISO(dateConvertedToRegular).toFormat(formatter)
355
+ : formatter(dateConvertedToRegular);
356
+ }
357
+ else {
358
+ return date.toString();
359
+ }
360
+ }
361
+ exports.convertDateToFormatType = convertDateToFormatType;
362
+ ;
363
+ function detectIfFutureDate(regularDate) {
364
+ const currentDateTime = luxon_1.DateTime.local().plus({ days: 1 });
365
+ const comparedDateTime = luxon_1.DateTime.fromObject({
366
+ day: parseInt(regularDate.slice(8, 10)),
367
+ month: parseInt(regularDate.slice(5, 7)),
368
+ year: parseInt(regularDate.slice(0, 4)),
369
+ });
370
+ return currentDateTime < comparedDateTime;
371
+ }
372
+ exports.detectIfFutureDate = detectIfFutureDate;
373
+ function detectIfValidPKDate(regularDate) {
374
+ const month = regularDate.slice(5, 7);
375
+ // The year is not limited, as in 1.0.5. Reports format historic and future dates through
376
+ // convertPercentDateFormat, which throws when this returns false, and batch uploads call this
377
+ // directly. Whether an event date may be in the future is for the caller to decide.
378
+ // check valid month
379
+ if (parseInt(month) < 1 || (parseInt(month) > 12)) {
380
+ return false;
381
+ }
382
+ return luxon_1.DateTime.fromISO(regularDate).isValid;
383
+ }
384
+ exports.detectIfValidPKDate = detectIfValidPKDate;
385
+ /**
386
+ * Which occurrence a shortcut with no year (or no cycle) means, given its date in the current
387
+ * year or cycle (yyyy-MM-dd): 0 is that date, -1 the occurrence before it, 1 the one after it.
388
+ * No daysAheadAllowed → that date, or the one before it when it is more than a day ahead.
389
+ * With N → dates up to N days ahead stay; later ones fall back; a date already past moves
390
+ * forward when its next occurrence is inside the window (New Year, or the start of a cycle) (F198).
391
+ */
392
+ function resolveShortcutOffset(thisYmd, daysAheadAllowed, toNextOccurrence) {
393
+ if (daysAheadAllowed === undefined || daysAheadAllowed === null) {
394
+ return detectIfFutureDate(thisYmd) ? -1 : 0;
395
+ }
396
+ const candidateThis = luxon_1.DateTime.fromISO(thisYmd).startOf('day');
397
+ if (!candidateThis.isValid) {
398
+ return 0;
399
+ }
400
+ const now = luxon_1.DateTime.local().startOf('day');
401
+ const n = Number(daysAheadAllowed);
402
+ const maxAllowed = now.plus({ days: Number.isFinite(n) ? Math.max(0, n) : 1 });
403
+ if (candidateThis > maxAllowed) {
404
+ return -1;
405
+ }
406
+ if (candidateThis < now && toNextOccurrence(candidateThis) <= maxAllowed) {
407
+ return 1;
408
+ }
409
+ return 0;
410
+ }
411
+ // Year for a year-less shortcut assumed as thisYearYmd (yyyy-MM-dd): this, last or next year.
412
+ function resolveYearForYearLess(thisYearYmd, daysAheadAllowed) {
413
+ const year = parseInt(thisYearYmd.slice(0, 4), 10);
414
+ return year + resolveShortcutOffset(thisYearYmd, daysAheadAllowed, (date) => date.plus({ years: 1 }));
415
+ }
416
+ // Thousand and Newsham count days in 1000-day cycles, so a 3-digit shortcut picks a cycle, not a
417
+ // year. A cycle runs across calendar years, so the year of the date says nothing about the cycle.
418
+ function resolveCycleForDayOnly(formatType, currentCycle, day, daysAheadAllowed) {
419
+ const thisCycle = `${currentCycle}${day}`;
420
+ const offset = resolveShortcutOffset(convertDateToFormatType(formatType, 'REGULAR', thisCycle), daysAheadAllowed, (date) => date.plus({ days: 1000 }));
421
+ return offset === 0 ? thisCycle : `${parseInt(currentCycle) + offset}${day}`;
422
+ }
423
+ // shortcut used must belong to the destination formatType.
424
+ // example: 4 digit American format means something different than 4 digit European
425
+ function convertShortcutDate(formatType, date, daysAheadAllowed) {
426
+ const inputDate = date.toString().replace(/\D/g, '');
427
+ const currentYear = (new Date()).getFullYear();
428
+ const currentYearEnd = currentYear.toString().slice(2);
429
+ // all formats are allowed to convert from regular to their desired format
430
+ if (/^\d{4}-\d{2}-\d{2}$/.test(date.toString())) {
431
+ return convertDateToFormatType('REGULAR', formatType, inputDate);
432
+ }
433
+ /* all year-less shortcuts should be converted to previous year if future date */
434
+ switch (formatType.toUpperCase()) {
435
+ case 'AMERICAN':
436
+ case 'EUROPEAN':
437
+ // allowed shortcuts: MMdd and MMddyy (American)
438
+ // allowed shortcuts: ddMM and ddMMyy (European)
439
+ if (inputDate.length === 4) {
440
+ const month = formatType.toUpperCase() === 'EUROPEAN'
441
+ ? inputDate.slice(2, 4)
442
+ : inputDate.slice(0, 2);
443
+ const day = formatType.toUpperCase() === 'EUROPEAN'
444
+ ? inputDate.slice(0, 2)
445
+ : inputDate.slice(2, 4);
446
+ const year = resolveYearForYearLess(`${currentYear}-${month}-${day}`, daysAheadAllowed);
447
+ return `${inputDate.slice(0, 2)}-${inputDate.slice(2, 4)}-${year}`;
448
+ }
449
+ else if (inputDate.length === 6) {
450
+ return `${inputDate.slice(0, 2)}-${inputDate.slice(2, 4)}-20${inputDate.slice(4)}`;
451
+ }
452
+ else if (inputDate.length !== 8) {
453
+ throw new Error(`${inputDate} does not match '${formatType.toUpperCase()}' format or any of its shortcuts.`);
454
+ }
455
+ else {
456
+ return inputDate;
457
+ }
458
+ case 'JULIAN':
459
+ if (inputDate.length < 4) {
460
+ const paddedDate = `${currentYearEnd}-${padNum(inputDate, 3)}`;
461
+ const regularizedDate = convertDateToFormatType('JULIAN', 'Regular', paddedDate);
462
+ if (detectIfValidPKDate(regularizedDate)) {
463
+ const year = resolveYearForYearLess(regularizedDate, daysAheadAllowed);
464
+ return `${year.toString().slice(2)}-${padNum(inputDate, 3)}`;
465
+ }
466
+ throw new Error(`${inputDate} does not match 'JULIAN' format or any of its shortcuts.`);
467
+ }
468
+ else if (inputDate.length === 4) {
469
+ const luxonDate = luxon_1.DateTime.fromObject({
470
+ year: currentYear,
471
+ month: parseInt(inputDate.slice(0, 2)),
472
+ day: parseInt(inputDate.slice(2)),
473
+ });
474
+ if (luxonDate.isValid) {
475
+ const year = resolveYearForYearLess(luxonDate.toFormat('yyyy-MM-dd'), daysAheadAllowed);
476
+ const ordinalDate = luxon_1.DateTime.fromObject({
477
+ year,
478
+ month: parseInt(inputDate.slice(0, 2)),
479
+ day: parseInt(inputDate.slice(2)),
480
+ });
481
+ return `${year.toString().slice(2)}-${ordinalDate.toFormat('ooo')}`;
482
+ }
483
+ throw new Error(`${inputDate} does not match 'JULIAN' format or any of its shortcuts.`);
484
+ }
485
+ else if (inputDate.length === 5) {
486
+ return `${inputDate.slice(0, 2)}-${inputDate.slice(2)}`;
487
+ }
488
+ else if (inputDate.length === 6) {
489
+ const luxonDate = luxon_1.DateTime.fromObject({
490
+ year: parseInt(inputDate.slice(0, 2)),
491
+ month: parseInt(inputDate.slice(2, 4)),
492
+ day: parseInt(inputDate.slice(4)),
493
+ });
494
+ if (luxonDate.isValid) {
495
+ return `${luxonDate.toFormat('yy')}-${luxonDate.toFormat('ooo')}`;
496
+ }
497
+ throw new Error(`${inputDate} does not match 'JULIAN' format or any of its shortcuts.`);
498
+ }
499
+ else if (inputDate.length === 8) {
500
+ return convertDateToFormatType('REGULAR', 'JULIAN', inputDate);
501
+ }
502
+ else if (inputDate.length !== 5) {
503
+ throw new Error(`${inputDate} does not match 'JULIAN' format or any of its shortcuts.`);
504
+ }
505
+ else {
506
+ return inputDate;
507
+ }
508
+ case 'ISO':
509
+ case 'REGULAR':
510
+ // allowed shortcuts: MMdd and yyMMdd
511
+ if (inputDate.length === 4) {
512
+ const date = `${currentYear}-${inputDate.slice(0, 2)}-${inputDate.slice(2)}`;
513
+ const year = resolveYearForYearLess(date, daysAheadAllowed);
514
+ return `${year}-${inputDate.slice(0, 2)}-${inputDate.slice(2)}`;
515
+ }
516
+ else if (inputDate.length === 6) {
517
+ return `20${inputDate.slice(0, 2)}-${inputDate.slice(2, 4)}-${inputDate.slice(4)}`;
518
+ }
519
+ else if (inputDate.length !== 8) {
520
+ throw new Error(`${inputDate} does not match 'REGULAR' format or any of its shortcuts.`);
521
+ }
522
+ else {
523
+ return inputDate;
524
+ }
525
+ case 'NEWSHAM':
526
+ const todayNewsham = convertDateToFormatType('REGULAR', 'NEWSHAM', luxon_1.DateTime.local().toFormat('yyyy-MM-dd'));
527
+ const currentNewshamCycle = todayNewsham.slice(0, 2);
528
+ if (inputDate.length < 4) {
529
+ return resolveCycleForDayOnly('NEWSHAM', currentNewshamCycle, padNum(inputDate, 3), daysAheadAllowed);
530
+ }
531
+ else if (inputDate.length === 4) {
532
+ return `1${inputDate}`;
533
+ }
534
+ else if (inputDate.length === 6) {
535
+ const regularDate = `20${inputDate.slice(0, 2)}-${inputDate.slice(2, 4)}-${inputDate.slice(4)}`;
536
+ if (detectIfValidPKDate(regularDate)) {
537
+ return convertDateToFormatType('REGULAR', 'NEWSHAM', regularDate);
538
+ }
539
+ throw new Error(`${inputDate} does not match 'NEWSHAM' format or any of its shortcuts.`);
540
+ }
541
+ else if (inputDate.length !== 5) {
542
+ throw new Error(`${inputDate} does not match 'NEWSHAM' format or any of its shortcuts.`);
543
+ }
544
+ else {
545
+ return inputDate;
546
+ }
547
+ case 'THOUSAND':
548
+ const todayThousand = convertDateToFormatType('REGULAR', 'THOUSAND', luxon_1.DateTime.local().toFormat('yyyy-MM-dd'));
549
+ const currentCycle = todayThousand.slice(0, 2);
550
+ if (inputDate.length < 4) {
551
+ return resolveCycleForDayOnly('THOUSAND', currentCycle, padNum(inputDate, 3), daysAheadAllowed);
552
+ }
553
+ else if (inputDate.length === 4) {
554
+ const regularDate = `${currentYear}-${inputDate.slice(0, 2)}-${inputDate.slice(2)}`;
555
+ if (detectIfValidPKDate(regularDate)) {
556
+ const year = resolveYearForYearLess(regularDate, daysAheadAllowed);
557
+ return convertDateToFormatType('REGULAR', 'THOUSAND', regularDate.replace(currentYear.toString(), year.toString()));
558
+ }
559
+ throw new Error(`${inputDate} does not match 'THOUSAND' format or any of its shortcuts.`);
560
+ }
561
+ else if (inputDate.length === 6) {
562
+ const regularDate = `20${inputDate.slice(0, 2)}-${inputDate.slice(2, 4)}-${inputDate.slice(4)}`;
563
+ if (detectIfValidPKDate(regularDate)) {
564
+ return convertDateToFormatType('REGULAR', 'THOUSAND', regularDate);
565
+ }
566
+ throw new Error(`${inputDate} does not match 'THOUSAND' format or any of its shortcuts.`);
567
+ }
568
+ else if (inputDate.length !== 5) {
569
+ throw new Error(`${inputDate} does not match 'THOUSAND' format or any of its shortcuts.`);
570
+ }
571
+ else {
572
+ return inputDate;
573
+ }
574
+ default:
575
+ return inputDate;
576
+ }
577
+ }
578
+ exports.convertShortcutDate = convertShortcutDate;
579
+ function convertPercentDateFormat(destinationFormat, date) {
580
+ // if (destinationFormat === 'default') {
581
+ // console.error('"default" format entered for percent date format. Please provide fallback.');
582
+ // }
583
+ // if (destinationFormat.indexOf('%') === -1) {
584
+ // console.error(`${destinationFormat} does not match percentage format type.`);
585
+ // }
586
+ let regularDate = date;
587
+ if (regularDate.length !== 10) {
588
+ // check for shortcut
589
+ switch (regularDate.length) {
590
+ case 1:
591
+ case 2:
592
+ case 3:
593
+ case 5: {
594
+ regularDate = convertDateToFormatType('THOUSAND', 'REGULAR', regularDate);
595
+ break;
596
+ }
597
+ case 4:
598
+ case 6:
599
+ case 8: {
600
+ regularDate = convertShortcutDate('REGULAR', regularDate);
601
+ break;
602
+ }
603
+ default:
604
+ break;
605
+ }
606
+ }
607
+ if (regularDate.length === 8) {
608
+ regularDate = `${regularDate.slice(0, 4)}-${regularDate.slice(4, 6)}-${regularDate.slice(6)}`;
609
+ }
610
+ if (!detectIfValidPKDate(regularDate)) {
611
+ throw new Error(`${date} does not match ${destinationFormat} format or any of its shortcuts.`);
612
+ }
613
+ switch (destinationFormat.replace(/\s/g, '').replace(/\,/g, '')) {
614
+ case '%b%d':
615
+ return luxon_1.DateTime.fromFormat(regularDate, 'yyyy-MM-dd').toFormat('MMMdd');
616
+ case '%b%d%y':
617
+ return luxon_1.DateTime.fromFormat(regularDate, 'yyyy-MM-dd').toFormat('MMMdd,yy');
618
+ case '%b%d%Y':
619
+ return luxon_1.DateTime.fromFormat(regularDate, 'yyyy-MM-dd').toFormat('MMMdd,yyyy');
620
+ case '%d%b':
621
+ return luxon_1.DateTime.fromFormat(regularDate, 'yyyy-MM-dd').toFormat('ddMMM');
622
+ case '%d%b%y':
623
+ return luxon_1.DateTime.fromFormat(regularDate, 'yyyy-MM-dd').toFormat('ddMMMyy');
624
+ case '%d%b%Y':
625
+ return luxon_1.DateTime.fromFormat(regularDate, 'yyyy-MM-dd').toFormat('ddMMMyyyy');
626
+ case '%d-%m-%y':
627
+ return convertDateToFormatType('REGULAR', 'EUROPEAN', regularDate).slice(0, 9);
628
+ case '%d/%m/%y':
629
+ return convertDateToFormatType('REGULAR', 'EUROPEAN', regularDate).slice(0, 9).replace(/\-/g, '/');
630
+ case '%d-%m-%Y':
631
+ return convertDateToFormatType('REGULAR', 'EUROPEAN', regularDate);
632
+ case '%d/%m/%Y':
633
+ return convertDateToFormatType('REGULAR', 'EUROPEAN', regularDate).replace(/\-/g, '/');
634
+ case '%m-%d':
635
+ return convertDateToFormatType('REGULAR', 'AMERICAN', regularDate).slice(0, 5);
636
+ case '%m/%d':
637
+ return convertDateToFormatType('REGULAR', 'AMERICAN', regularDate).slice(0, 5).replace(/\-/g, '/');
638
+ case '%m-%d-%y':
639
+ return convertDateToFormatType('REGULAR', 'AMERICAN', regularDate).slice(0, 9);
640
+ case '%m/%d/%y':
641
+ return convertDateToFormatType('REGULAR', 'AMERICAN', regularDate).slice(0, 9).replace(/\-/g, '/');
642
+ case '%m-%d-%Y':
643
+ return convertDateToFormatType('REGULAR', 'AMERICAN', regularDate);
644
+ case '%m/%d/%Y':
645
+ return convertDateToFormatType('REGULAR', 'AMERICAN', regularDate).replace(/\-/g, '/');
646
+ case '%j': // 3-digit julian
647
+ return convertDateToFormatType('REGULAR', 'JULIAN', regularDate).slice(3);
648
+ case '%J': // full julian
649
+ return convertDateToFormatType('REGULAR', 'JULIAN', regularDate);
650
+ case '%N': // full newsham
651
+ return convertDateToFormatType('REGULAR', 'NEWSHAM', regularDate);
652
+ case '%n':
653
+ case '%t': // 3-digit thousand or 3-digit newsham (same because cycle is removed)
654
+ return convertDateToFormatType('REGULAR', 'THOUSAND', regularDate).slice(2);
655
+ case '%T': // full thousand
656
+ return convertDateToFormatType('REGULAR', 'THOUSAND', regularDate);
657
+ case '%y-%m-%d': // 2-digit year - month - day
658
+ return regularDate.slice(2);
659
+ case '%y/%m/%d': // 2-digit year / month / day
660
+ return regularDate.slice(2).replace(/\-/g, '/');
661
+ case '%Y-%m-%d':
662
+ return regularDate;
663
+ case '%Y/%m/%d':
664
+ return regularDate.replace(/\-/g, '/');
665
+ default:
666
+ return regularDate;
667
+ }
668
+ }
669
+ exports.convertPercentDateFormat = convertPercentDateFormat;
670
+ //# sourceMappingURL=dateHelpers.js.map