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

Handle digits that are in names. #21

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
60 changes: 60 additions & 0 deletions src/parse/__tests__/td3.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,64 @@ describe('parse TD3', () => {
compositeCheckDigit: '0',
});
});

it('digits in name', () => {
//Notice how we put a number (0) as part of the surname - In cases where we have misinterpreted an O as a 0
const MRZ = [
'P<UTOERIKSS0N<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<',
'L898902C36UTO7408122F1204159ZE184226B<<<<<10',
];

const result = parse(MRZ);
expect(result).toMatchObject({
valid: false,
format: 'TD3',
});
expect(result.valid).toBe(false);
const errors = result.details.filter((a) => !a.valid);
expect(errors).toHaveLength(2);
expect(result.fields).toStrictEqual({
documentCode: 'P',
firstName: 'ANNA MARIA',
lastName: 'ERIKSSON',
documentNumber: 'L898902C3',
documentNumberCheckDigit: '6',
nationality: null,
sex: 'female',
expirationDate: '120415',
expirationDateCheckDigit: '9',
personalNumber: 'ZE184226B',
personalNumberCheckDigit: '1',
birthDate: '740812',
birthDateCheckDigit: '2',
issuingState: null,
compositeCheckDigit: '0',
});

const personalNumberDetails = result.details.find(
(d) => d.field === 'personalNumber',
);
expect(personalNumberDetails).toStrictEqual({
label: 'Personal number',
field: 'personalNumber',
value: 'ZE184226B',
valid: true,
ranges: [{ line: 1, start: 28, end: 42, raw: 'ZE184226B<<<<<' }],
line: 1,
start: 28,
end: 37,
});

expect(errors[0]).toStrictEqual({
label: 'Issuing state',
field: 'issuingState',
value: null,
valid: false,
ranges: [{ line: 0, start: 2, end: 5, raw: 'UTO' }],
line: 0,
start: 2,
end: 5,
error: 'invalid state code: UTO',
});
});
});
24 changes: 24 additions & 0 deletions src/parse/getResult.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const formats = require('../formats');

function getDetails(lines, fieldParsers) {
const details = [];
for (const parser of fieldParsers) {
Expand All @@ -21,6 +23,7 @@ function getFields(details) {
}

function getResult(format, lines, fieldParsers) {
lines = cleanInvalidCharactersInName(format, lines);
const details = getDetails(lines, fieldParsers);
const fields = getFields(details);
const result = {
Expand All @@ -32,4 +35,25 @@ function getResult(format, lines, fieldParsers) {
return result;
}

function cleanInvalidCharactersInName(format, lines) {
const commonNumberToLetterMismatches = {
'8':'B', '6':'G', '0':'O', '3':'E', '1':'I', '5':'S', '2':'Z'
}

const keys = Object.keys(commonNumberToLetterMismatches);

switch (format) {
case formats.TD3:
let topLine = lines[0].split("");
lines[0] = topLine.map(v => {
if (keys.includes(v)) {
v = commonNumberToLetterMismatches[v] === undefined ? v : commonNumberToLetterMismatches[v];
}
return v;
}).join(',').replaceAll(',','');
break;
}
return lines;
}

module.exports = getResult;