-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.ts
69 lines (56 loc) · 1.73 KB
/
utilities.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import type { DocketDateString } from './types.js'
const monthNumbers = {
JAN: '01',
FEB: '02',
MAR: '03',
APR: '04',
MAY: '05',
JUN: '06',
JUL: '07',
AUG: '08',
SEP: '09',
OCT: '10',
NOV: '11',
DEC: '12'
}
/**
* Converts the court date on the docket into a standardized format.
* @param {string} courtDateString ex. "10 JAN 2024"
* @returns {DocketDateString} ex. "2024/01/10"
*/
export function normalizeCourtDate(courtDateString: string): DocketDateString {
const courtDatePieces = courtDateString.padStart(11, '0').split(' ')
const monthString = monthNumbers[courtDatePieces[1]]
return `${courtDatePieces[2]}/${monthString}/${courtDatePieces[0]}` as DocketDateString
}
/**
* Converts the line item dates on the docket into a standardized format.
* @param {string} itemDateString ex. "1AUG23"
* @returns {DocketDateString} ex. "2023/08/01"
*/
export function normalizeItemDate(
itemDateString: string
): DocketDateString | undefined {
if (itemDateString === '00') {
return undefined
}
const currentDate = new Date()
const currentCentury =
currentDate.getFullYear() - (currentDate.getFullYear() % 100)
const itemDateStringPadded = itemDateString.padStart(7, '0')
let itemYear =
currentCentury + Number.parseInt(itemDateStringPadded.slice(5), 10)
const itemMonthString = monthNumbers[
itemDateStringPadded.slice(2, 5)
] as string
const itemDayString = itemDateStringPadded.slice(0, 2)
const itemDate = new Date(
itemYear,
Number.parseInt(itemMonthString, 10) - 1,
Number.parseInt(itemDayString, 10)
)
if (itemDate.getTime() > currentDate.getTime()) {
itemYear -= 100
}
return `${itemYear.toString()}/${itemMonthString}/${itemDayString}` as DocketDateString
}