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: fix autocorrections being assigned to the wrong field #46

Closed
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion src/parse/__tests__/swissDrivingLicense.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ describe('parse Swiss Driving License', () => {
{ line: 1, column: 24, original: 'G', corrected: '6' },
],
[],
[],
[
{ line: 2, column: 12, original: '8', corrected: 'B' },
{ line: 2, column: 13, original: '1', corrected: 'I' },
],
[],
]);
});
});
3 changes: 2 additions & 1 deletion src/parse/__tests__/td1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,11 @@ describe('parse TD1', () => {
[
{ line: 2, column: 0, original: '5', corrected: 'S' },
{ line: 2, column: 2, original: '1', corrected: 'I' },
],
[
{ line: 2, column: 8, original: '0', corrected: 'O' },
{ line: 2, column: 14, original: '8', corrected: 'B' },
],
[],
]);
});
});
3 changes: 1 addition & 2 deletions src/parse/__tests__/td2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ describe('parse TD2', () => {
[
{ line: 0, column: 9, original: '5', corrected: 'S' },
{ line: 0, column: 10, original: '5', corrected: 'S' },
{ line: 0, column: 23, original: '1', corrected: 'I' },
],
[],
[{ line: 0, column: 23, original: '1', corrected: 'I' }],
[],
[],
[{ line: 1, column: 12, original: '0', corrected: 'O' }],
Expand Down
32 changes: 13 additions & 19 deletions src/parse/createFieldParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ interface Range {

export interface CreateFieldParserResult {
parser: (lines: string[], autocorrect?: Autocorrect[]) => Details;
autocorrector: (lines: string[]) => {
correctedLines: string[];
autocorrect: Autocorrect[];
};
autocorrector: (lines: string[]) => Autocorrect[];
}

export default function createFieldParser(
Expand Down Expand Up @@ -113,13 +110,23 @@ export default function createFieldParser(
result.start = range.start + parsed.start;
result.end = range.start + parsed.end;
}

// We ignore any autocorrect which applied to a character outside of the
// field's start / end range.
result.autocorrect = result.autocorrect.filter((autoCorrect) => {
return (
autoCorrect.line === result.line &&
autoCorrect.column >= result.start &&
autoCorrect.column < result.end
);
});
} catch (e: any) {
result.error = e.message;
}

return result;
};
const autocorrector = (lines: string[]) => {
let corrected = lines;
let source = getText(lines, fieldOptions);
let autocorrect: Autocorrect[] = [];
const type = fieldOptions.type || fieldTypes.ALPHANUMERIC;
Expand All @@ -128,8 +135,7 @@ export default function createFieldParser(
source = result.correctedLine;
autocorrect = result.autocorrect;
}
corrected = changeText(lines, fieldOptions, source);
return { correctedLines: corrected, autocorrect };
return autocorrect;
};
return { parser, autocorrector };
}
Expand All @@ -142,18 +148,6 @@ function getText(
return line.substring(options.start, options.end);
}

function changeText(
lines: string[],
options: Pick<FieldOptions, 'line' | 'end' | 'start'>,
text: string,
) {
const line = lines[options.line];
const newText =
line.substring(0, options.start) + text + line.substring(options.end);
lines[options.line] = newText;
return lines;
}

function checkType(
options: object,
name: string,
Expand Down
22 changes: 18 additions & 4 deletions src/parse/getResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,24 @@ function getCorrection(
fieldParsers: CreateFieldParserResult[],
autocorrect: boolean,
) {
let corrected = lines;
const autocorrectArray: Autocorrect[][] = [];

if (autocorrect) {
fieldParsers.forEach(({ autocorrector }) => {
const result = autocorrector(corrected);
autocorrectArray.push(result.autocorrect);
corrected = result.correctedLines;
autocorrectArray.push(autocorrector(lines));
});
}
// Apply autocorrect on the whole MRZ
const corrected = lines.slice();
for (let autocorrect of autocorrectArray) {
for (let correction of autocorrect) {
corrected[correction.line] = setCharAt(
corrected[correction.line],
correction.column,
correction.corrected,
);
}
}
return { corrected, autocorrectArray };
}
export function getResult(
Expand All @@ -60,6 +68,7 @@ export function getResult(
fieldParsers,
autocorrect,
);

const details = getDetails(corrected, fieldParsers, autocorrectArray);
const fields = getFields(details);
const result = {
Expand All @@ -70,3 +79,8 @@ export function getResult(
};
return result;
}

function setCharAt(source: string, index: number, replaceWith: string) {
if (index > source.length - 1) return source;
return source.substring(0, index) + replaceWith + source.substring(index + 1);
}