Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix autocorrect range #44

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 94 additions & 28 deletions src/parse/__tests__/swissDrivingLicense.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ describe('parse Swiss Driving License', () => {
ranges: [
{
line: 2,
start: 0,
end: 30,
raw: 'MARCHAND<<FABIENNE<<<<<<<<<<<<',
start: 10,
end: 18,
raw: 'FABIENNE',
},
],
line: 2,
Expand All @@ -55,7 +55,7 @@ describe('parse Swiss Driving License', () => {
lastName: 'MARCHAND',
});
});
it('Use autocorrect', () => {
it('Use autocorrect 1', () => {
const MRZ = [
'AAA001D<<',
'FACHE305142128097<<800126<<<<<',
Expand All @@ -72,31 +72,97 @@ describe('parse Swiss Driving License', () => {

expect(result.fields).toStrictEqual(correctedResult.fields);
expect(
correctedResult.details.map(({ autocorrect }) => autocorrect),
correctedResult.details.map(({ value, autocorrect }) => ({
value,
autocorrect,
})),
).toStrictEqual([
[],
[],
[],
[],
[
{ line: 1, column: 7, original: 'S', corrected: '5' },
{ line: 1, column: 11, original: 'I', corrected: '1' },
{ line: 1, column: 12, original: 'Z', corrected: '2' },
{ line: 1, column: 13, original: 'B', corrected: '8' },
],
[{ line: 1, column: 14, original: 'O', corrected: '0' }],
[],
[
{ line: 1, column: 20, original: 'O', corrected: '0' },
{ line: 1, column: 21, original: 'O', corrected: '0' },
{ line: 1, column: 24, original: 'G', corrected: '6' },
],
[],
[
{ line: 2, column: 12, original: '8', corrected: 'B' },
{ line: 2, column: 13, original: '1', corrected: 'I' },
],
[],
{ value: 'AAA001D', autocorrect: [] },
{ value: 'D', autocorrect: [] },
{ value: 'FA', autocorrect: [] },
{ value: 'CHE', autocorrect: [] },
{
value: '305142128',
autocorrect: [
{ line: 1, column: 7, original: 'S', corrected: '5' },
{ line: 1, column: 11, original: 'I', corrected: '1' },
{ line: 1, column: 12, original: 'Z', corrected: '2' },
{ line: 1, column: 13, original: 'B', corrected: '8' },
],
},
{
value: '097',
autocorrect: [{ line: 1, column: 14, original: 'O', corrected: '0' }],
},
{ value: '<<', autocorrect: [] },
{
value: '800126',
autocorrect: [
{ line: 1, column: 20, original: 'O', corrected: '0' },
{ line: 1, column: 21, original: 'O', corrected: '0' },
{ line: 1, column: 24, original: 'G', corrected: '6' },
],
},
{ value: '<<<<<', autocorrect: [] },
{
value: 'MARCHAND',
autocorrect: [],
},
{
value: 'FABIENNE',
autocorrect: [
{ line: 2, column: 12, original: '8', corrected: 'B' },
{ line: 2, column: 13, original: '1', corrected: 'I' },
],
},
]);
});

it('Use autocorrect 2', () => {
const MRZ = [
'AKU735D<<',
'FACHE000231268003<<530727<<<<<',
'AELLEN<<ROLAND<<<<<<<<<<<<<<<<',
];
const falseMRZ = [
'AKU735D<<',
'FACHE000231268OO3<<53O727<<<<<',
'AELLEN<<R0LAND<<<<<<<<<<<<<<<<',
];

const result = parse(MRZ);
const correctedResult = parse(falseMRZ, { autocorrect: true });
const corrections = correctedResult.details.map(
({ value, autocorrect }) => ({ value, autocorrect }),
);
expect(result.fields).toStrictEqual(correctedResult.fields);
expect(corrections).toStrictEqual([
{ value: 'AKU735D', autocorrect: [] },
{ value: 'D', autocorrect: [] },
{ value: 'FA', autocorrect: [] },
{ value: 'CHE', autocorrect: [] },
{ value: '000231268', autocorrect: [] },
{
value: '003',
autocorrect: [
{ line: 1, column: 14, original: 'O', corrected: '0' },
{ line: 1, column: 15, original: 'O', corrected: '0' },
],
},
{ value: '<<', autocorrect: [] },
{
value: '530727',
autocorrect: [{ line: 1, column: 21, original: 'O', corrected: '0' }],
},
{ value: '<<<<<', autocorrect: [] },
{
value: 'AELLEN',
autocorrect: [],
},
{
value: 'ROLAND',
autocorrect: [{ line: 2, column: 9, original: '0', corrected: 'O' }],
},
]);
});
});
3 changes: 2 additions & 1 deletion src/parse/swissDrivingLicense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ export default function parseSwissDrivingLicense(
`invalid number of characters for line 3: ${lines[2].length}. Must be 30 for ${SWISS_DRIVING_LICENSE}`,
);
}

return getResult(
SWISS_DRIVING_LICENSE,
lines,
swissDrivingLicenseFields,
swissDrivingLicenseFields(lines),
options,
);
}
149 changes: 80 additions & 69 deletions src/parse/swissDrivingLicenseFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,72 +17,83 @@ import {
firstNameTemplate,
} from './fieldTemplates';

export default [
{
...documentNumberTemplate,
line: 0,
start: 0,
end: 9,
parser: parseDocumentNumber,
},
{
label: 'Language code',
field: 'languageCode',
line: 0,
start: 6,
end: 7,
parser: parseLanguageCode,
type: fieldTypes.ALPHABETIC,
},
{
...documentCodeTemplate,
line: 1,
start: 0,
end: 2,
parser: parseDocumentCode,
},
{
...issuingStateTemplate,
line: 1,
start: 2,
end: 5,
parser: parseIssuingState,
},
{
label: 'PIN code',
field: 'pinCode',
line: 1,
start: 5,
end: 14,
parser: parseNumber,
type: fieldTypes.NUMERIC,
},
{
label: 'Version number',
field: 'versionNumber',
line: 1,
start: 14,
end: 17,
parser: parseNumber,
type: fieldTypes.NUMERIC,
},
{
label: 'Separator 1',
field: null,
line: 1,
start: 17,
end: 19,
parser: checkSeparator,
},
{ ...birthDateTemplate, line: 1, start: 19, end: 25 },
{
label: 'Separator 2',
field: null,
line: 1,
start: 25,
end: 30,
parser: checkSeparator,
},
{ ...lastNameTemplate, line: 2, start: 0, end: 30 },
{ ...firstNameTemplate, line: 2, start: 0, end: 30 },
].map(createFieldParser);
export default function swissDrivingLicenseFields(lines: string[]) {
const names = lines[2].replace(/^<+|<+$/g, '').split('<<');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very risky as it only works if the line is formatted correctly

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@targos It is acceptable to use "<<" as the delimiter between the first and last name?

Copy link
Member

@targos targos Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but the parsing should be resilient to lines that don't follow the standard.

  • What about lines without any <<?
  • What about lines with multiple names separated by <<?

I would prefer not to fundamentally change the parser to accomodate an edge case related to autocorrect only.

Also, this pattern isn't specific to the Swiss driving license. Don't we need similar code changes for other MRZ formats?

const lastNameEndIndex = names[0].length;
const firstNameStartIndex = lastNameEndIndex + 2;
const firstNameEndIndex = firstNameStartIndex + names[1].length;
return [
{
...documentNumberTemplate,
line: 0,
start: 0,
end: 9,
parser: parseDocumentNumber,
},
{
label: 'Language code',
field: 'languageCode',
line: 0,
start: 6,
end: 7,
parser: parseLanguageCode,
type: fieldTypes.ALPHABETIC,
},
{
...documentCodeTemplate,
line: 1,
start: 0,
end: 2,
parser: parseDocumentCode,
},
{
...issuingStateTemplate,
line: 1,
start: 2,
end: 5,
parser: parseIssuingState,
},
{
label: 'PIN code',
field: 'pinCode',
line: 1,
start: 5,
end: 14,
parser: parseNumber,
type: fieldTypes.NUMERIC,
},
{
label: 'Version number',
field: 'versionNumber',
line: 1,
start: 14,
end: 17,
parser: parseNumber,
type: fieldTypes.NUMERIC,
},
{
label: 'Separator 1',
field: null,
line: 1,
start: 17,
end: 19,
parser: checkSeparator,
},
{ ...birthDateTemplate, line: 1, start: 19, end: 25 },
{
label: 'Separator 2',
field: null,
line: 1,
start: 25,
end: 30,
parser: checkSeparator,
},
{ ...lastNameTemplate, line: 2, start: 0, end: lastNameEndIndex },
{
...firstNameTemplate,
line: 2,
start: firstNameStartIndex,
end: firstNameEndIndex,
},
].map(createFieldParser);
}
Loading