-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
206 lines (162 loc) · 5.78 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable security/detect-object-injection, sonarjs/cognitive-complexity */
import { actions, appTypes, finds, pleas } from './lookups.js'
import type { Docket, DocketItem, DocketTimeString } from './types.js'
import { normalizeCourtDate, normalizeItemDate } from './utilities.js'
const spacePaddingLength = 130
/**
* Parses a docket print file into an array of Docket objects.
* @param {string} docketFileText - The docket text
* @returns {Docket[]} - An array of dockets
*/
export function parseDockets(docketFileText: string): Docket[] {
const dockets: Docket[] = []
const docketLines = docketFileText.replaceAll('\f', ' \n').split(/[\n\f]/)
let docketLineIndex = 0
for (
docketLineIndex = 0;
docketLineIndex < docketLines.length;
docketLineIndex += 1
) {
let docketLine = docketLines.at(docketLineIndex) ?? ''
/*
* Check if the line is the start of a page.
*/
if (
docketLine.length >= 51 &&
docketLine.slice(0, 48).trim() === '' &&
docketLine.slice(48, 51) !== 'XXX' &&
docketLine.slice(48, 53) !== 'DATE '
) {
const docketDescription = docketLine.trim()
// Skip two blank lines
docketLineIndex += 2
// First header line
docketLineIndex += 1
docketLine = (docketLines.at(docketLineIndex) ?? '').padEnd(
spacePaddingLength,
' '
)
const court = docketLine.slice(1, 6).trim()
const room = docketLine.slice(31, 36).trim()
const prosecutor = docketLine.slice(73, 103).trim()
const courtTimeString = docketLine.slice(103, 108) as DocketTimeString
// One blank line
docketLineIndex += 1
// Second header line
docketLineIndex += 1
docketLine = (docketLines.at(docketLineIndex) ?? '').padEnd(
spacePaddingLength,
' '
)
const courtDateString = docketLine.slice(7, 18).trim()
const pageNumber = docketLine.slice(34, 35).trim()
const justiceOfThePeace = docketLine.slice(43, 73).trim()
// One blank line
docketLineIndex += 1
// Third header line
docketLineIndex += 1
docketLine = (docketLines.at(docketLineIndex) ?? '').padEnd(
spacePaddingLength,
' '
)
const clerk = docketLine.slice(7, 44).trim()
const docket: Docket = {
docketDescription,
pageNumber: Number.parseInt(pageNumber, 10),
court,
room,
prosecutor,
courtDate: normalizeCourtDate(courtDateString),
courtTime: courtTimeString,
justiceOfThePeace,
clerk,
docketItems: []
}
// Three blank lines
docketLineIndex += 3
while (docketLineIndex < docketLines.length) {
docketLineIndex += 1
const itemLine1 = (docketLines.at(docketLineIndex) ?? '').padEnd(
spacePaddingLength,
' '
)
// Blank first line, items complete
if (itemLine1.trim() === '') {
break
}
docketLineIndex += 1
const itemLine2 = (docketLines.at(docketLineIndex) ?? '').padEnd(
spacePaddingLength,
' '
)
const lineNumber = itemLine1.slice(0, 4).trim()
if (lineNumber === '') {
// Likely a comment overflowing to next two lines
const extendedComment = `${itemLine1
.slice(112, 131)
.trim()}\n${itemLine2.slice(112, 131)}`.trim()
try {
;(
docket.docketItems.at(-1) as DocketItem
).comment += `\n${extendedComment}`
} catch {}
continue
}
const informationNumber = `${itemLine1
.slice(5, 16)
.trim()} ${itemLine2.slice(5, 16)}`.trim()
const birthDate = itemLine1.slice(17, 24).trim()
const counts = itemLine1.slice(25, 27).trim()
const appType = itemLine1.slice(28, 31).trim()
const appTypeNumber = itemLine2.slice(28, 31).trim()
const appTypeDescription = appTypes[appType]
const compBadgeNumber = `${itemLine1
.slice(32, 40)
.trim()} ${itemLine2.slice(32, 40)}`.trim()
const offenceDate = itemLine1.slice(41, 48).trim()
const arrestDate = itemLine2.slice(41, 48).trim()
const defendantName = itemLine1.slice(53, 78).trim()
const offenceDescription = itemLine1.slice(78, 99).trim()
const action = itemLine1.slice(100, 103).trim()
const actionDescription = actions[action]
// eslint-disable-next-line unicorn/prevent-abbreviations
const crEl = itemLine1.slice(104, 105).trim()
const plea = itemLine1.slice(106, 108).trim()
const pleaDescription = pleas[plea]
const find = itemLine1.slice(109, 111).trim()
const findDescription = finds[find]
const comment = `${itemLine1.slice(112, 131).trim()}\n${itemLine2.slice(
112,
131
)}`.trim()
const docketItem: DocketItem = {
lineNumber: Number.parseInt(lineNumber, 10),
informationNumber,
defendantBirthDate: normalizeItemDate(birthDate),
counts: Number.parseInt(counts, 10),
appTypeNumber: `${appType} ${appTypeNumber}`.trim(),
appTypeDescription,
compBadgeNumber,
offenceDate: normalizeItemDate(offenceDate),
arrestDate: normalizeItemDate(arrestDate),
defendantName,
offenceDescription,
action,
actionDescription,
crEl,
plea,
pleaDescription,
find,
findDescription,
comment
}
docket.docketItems.push(docketItem)
}
dockets.push(docket)
}
}
return dockets
}
export * as lookups from './lookups.js'
export type * as types from './types.js'