Skip to content

Commit fd8d889

Browse files
committed
feat: allow parsing readonly arrays
1 parent 594619c commit fd8d889

File tree

10 files changed

+31
-17
lines changed

10 files changed

+31
-17
lines changed

src/parse/checkLines.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export function checkLines(lines: string | string[]) {
1+
export function checkLines(
2+
lines: string | readonly string[],
3+
): readonly string[] {
24
if (typeof lines === 'string') {
35
lines = lines.split(/[\r\n]+/);
46
}

src/parse/createFieldParser.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ export interface FieldOptions {
3030
}
3131

3232
export interface CreateFieldParserResult {
33-
parser: (lines: string[], autocorrect: Autocorrect[]) => Details;
34-
autocorrector: (lines: string[]) => {
33+
parser: (
34+
lines: readonly string[],
35+
autocorrect: readonly Autocorrect[],
36+
) => Details;
37+
autocorrector: (lines: readonly string[]) => {
3538
autocorrect: Autocorrect[];
3639
correctedText: string;
3740
range: Range;
@@ -66,8 +69,8 @@ export default function createFieldParser(
6669
}
6770
}
6871
const parser: CreateFieldParserResult['parser'] = (
69-
lines: string[],
70-
autocorrect: Autocorrect[],
72+
lines: readonly string[],
73+
autocorrect: readonly Autocorrect[],
7174
) => {
7275
const source = getText(lines, fieldOptions);
7376
const related = fieldOptions.related || [];
@@ -122,7 +125,7 @@ export default function createFieldParser(
122125
};
123126

124127
const autocorrector: CreateFieldParserResult['autocorrector'] = (
125-
lines: string[],
128+
lines: readonly string[],
126129
) => {
127130
const originalText = getText(lines, fieldOptions);
128131
return {
@@ -134,7 +137,7 @@ export default function createFieldParser(
134137
return { parser, autocorrector };
135138
}
136139

137-
function getText(lines: string | string[], options: Range) {
140+
function getText(lines: readonly string[], options: Range) {
138141
const line = lines[options.line];
139142
return line.slice(options.start, options.end);
140143
}

src/parse/frenchDrivingLicence.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { ParseMRZOptions } from './parse.ts';
66

77
const FRENCH_DRIVING_LICENSE = formats.FRENCH_DRIVING_LICENSE;
88
export default function parseFrenchDrivingLicense(
9-
lines: string[],
9+
lines: readonly string[],
1010
options: ParseMRZOptions,
1111
) {
1212
if (lines.length !== 1) {

src/parse/frenchNationalId.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { ParseMRZOptions } from './parse.ts';
66

77
const FRENCH_NATIONAL_ID = formats.FRENCH_NATIONAL_ID;
88
export default function parseFrenchNationalId(
9-
lines: string[],
9+
lines: readonly string[],
1010
options: ParseMRZOptions,
1111
) {
1212
if (lines.length !== 2) {

src/parse/getResult.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ function getFields(details: Details[]) {
3737
}
3838

3939
function getCorrection(
40-
lines: string[],
41-
fieldParsers: CreateFieldParserResult[],
40+
lines: readonly string[],
41+
fieldParsers: readonly CreateFieldParserResult[],
4242
autocorrect: boolean,
4343
) {
4444
const corrected = lines.slice();
@@ -60,7 +60,7 @@ function getCorrection(
6060

6161
export function getResult(
6262
format: FormatType,
63-
lines: string[],
63+
lines: readonly string[],
6464
fieldParsers: CreateFieldParserResult[],
6565
options: ParseMRZOptions,
6666
): ParseResult {

src/parse/parse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface ParseMRZOptions {
88
}
99

1010
function parseMRZ(
11-
inputLines: string | string[],
11+
inputLines: string | readonly string[],
1212
options: ParseMRZOptions = {},
1313
): ParseResult {
1414
const lines = checkLines(inputLines);

src/parse/swissDrivingLicense.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import swissDrivingLicenseFields from './swissDrivingLicenseFields.ts';
66

77
const SWISS_DRIVING_LICENSE = formats.SWISS_DRIVING_LICENSE;
88
export default function parseSwissDrivingLicense(
9-
lines: string[],
9+
lines: readonly string[],
1010
options: ParseMRZOptions,
1111
) {
1212
if (lines.length !== 3) {

src/parse/td1.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import type { ParseMRZOptions } from './parse.ts';
55
import TD1Fields from './td1Fields.ts';
66

77
const TD1 = formats.TD1;
8-
export default function parseTD1(lines: string[], options: ParseMRZOptions) {
8+
export default function parseTD1(
9+
lines: readonly string[],
10+
options: ParseMRZOptions,
11+
) {
912
if (lines.length !== 3) {
1013
throw new Error(
1114
`invalid number of lines: ${lines.length}: Must be 3 for ${TD1}`,

src/parse/td2.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import type { ParseMRZOptions } from './parse.ts';
55
import TD2Fields from './td2Fields.ts';
66

77
const TD2 = formats.TD2;
8-
export default function parseTD2(lines: string[], options: ParseMRZOptions) {
8+
export default function parseTD2(
9+
lines: readonly string[],
10+
options: ParseMRZOptions,
11+
) {
912
if (lines.length !== 2) {
1013
throw new Error(
1114
`invalid number of lines: ${lines.length}: Must be 2 for ${TD2}`,

src/parse/td3.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import type { ParseMRZOptions } from './parse.ts';
55
import TD3Fields from './td3Fields.ts';
66

77
const TD3 = formats.TD3;
8-
export default function parseTD3(lines: string[], options: ParseMRZOptions) {
8+
export default function parseTD3(
9+
lines: readonly string[],
10+
options: ParseMRZOptions,
11+
) {
912
if (lines.length !== 2) {
1013
throw new Error(
1114
`invalid number of lines: ${lines.length}: Must be 2 for ${TD3}`,

0 commit comments

Comments
 (0)