-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* /!\ target es2021 (instead es2020) to be able to use Intl.ListFormat * add documentCodeAlphaNumTemplate * add tests and fix errors.test.ts * without proper resources, I'm not sure about the real existence of A1, A2 or B1, but I let the parser flexible Closes: #54
- Loading branch information
Showing
11 changed files
with
188 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
import parse from '../parse'; | ||
|
||
describe('parse French Driving License', () => { | ||
it('valid MRZ', () => { | ||
const MRZ = 'D1FRA13AA000026181231MARTIN<<9'; | ||
|
||
const result = parse(MRZ); | ||
|
||
expect(result.format).toBe('FRENCH_DRIVING_LICENSE'); | ||
expect(result.valid).toBe(true); | ||
expect(result.details.filter((a) => !a.valid)).toHaveLength(0); | ||
expect(result.fields).toStrictEqual({ | ||
documentCode: 'D1', | ||
issuingState: 'FRA', | ||
documentNumber: '13AA00002', | ||
documentNumberCheckDigit: '6', | ||
expirationDate: '181231', | ||
lastName: 'MARTIN', | ||
compositeCheckDigit: '9', | ||
}); | ||
}); | ||
|
||
it('Use autocorrect', () => { | ||
const MRZok = 'D1FRA13AA000026181231MARTIN<<9'; | ||
const MRZko = 'D1FRA13AA0000261B1231MART1N<<9'; | ||
|
||
const result = parse(MRZok); | ||
const correctedResult = parse(MRZko, { autocorrect: true }); | ||
expect(correctedResult.fields).toStrictEqual(result.fields); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
import { formats } from '../formats'; | ||
|
||
import frenchDrivingLicenceFields from './frenchDrivingLicenceFields'; | ||
import { getResult } from './getResult'; | ||
import { ParseMRZOptions } from './parse'; | ||
|
||
const FRENCH_DRIVING_LICENSE = formats.FRENCH_DRIVING_LICENSE; | ||
export default function parseFrenchDrivingLicense( | ||
lines: string[], | ||
options: ParseMRZOptions, | ||
) { | ||
if (lines.length !== 1) { | ||
throw new Error( | ||
`invalid number of lines: ${lines.length}: Must be 1 for ${FRENCH_DRIVING_LICENSE}`, | ||
); | ||
} | ||
if (lines[0].length !== 30) { | ||
throw new Error( | ||
`invalid number of characters for line 1: ${lines[0].length}. Must be 30 for ${FRENCH_DRIVING_LICENSE}`, | ||
); | ||
} | ||
|
||
return getResult( | ||
FRENCH_DRIVING_LICENSE, | ||
lines, | ||
frenchDrivingLicenceFields, | ||
options, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict'; | ||
|
||
import parseDocumentCode from '../parsers/frenchDrivingLicence/parseDocumentCode'; | ||
import { parseAlpha } from '../parsers/parseAlpha'; | ||
|
||
import createFieldParser, { FieldOptions } from './createFieldParser'; | ||
import { | ||
compositeCheckDigitTemplate, | ||
documentCodeAlphaNumTemplate, | ||
documentNumberCheckDigitTemplate, | ||
documentNumberTemplate, | ||
expirationDateTemplate, | ||
issuingStateTemplate, | ||
lastNameTemplate, | ||
} from './fieldTemplates'; | ||
|
||
const fields: FieldOptions[] = [ | ||
{ | ||
...documentCodeAlphaNumTemplate, | ||
line: 0, | ||
start: 0, | ||
end: 2, | ||
parser: parseDocumentCode, | ||
}, | ||
{ ...issuingStateTemplate, line: 0, start: 2, end: 5 }, | ||
{ ...documentNumberTemplate, line: 0, start: 5, end: 14 }, | ||
{ | ||
...documentNumberCheckDigitTemplate, | ||
line: 0, | ||
start: 14, | ||
end: 15, | ||
related: [ | ||
{ | ||
line: 0, | ||
start: 0, | ||
end: 14, | ||
}, | ||
], | ||
}, | ||
{ ...expirationDateTemplate, line: 0, start: 15, end: 21 }, | ||
{ ...lastNameTemplate, line: 0, start: 21, end: 29, parser: parseAlpha }, | ||
{ | ||
...compositeCheckDigitTemplate, | ||
line: 0, | ||
start: 29, | ||
end: 30, | ||
related: [ | ||
{ | ||
line: 0, | ||
start: 0, | ||
end: 29, | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
export default fields.map(createFieldParser); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict'; | ||
|
||
/** | ||
* D1 seems to be the most common, | ||
* but with Google image search we can find some B1, A1, A2 | ||
*/ | ||
const knownPrefixCode = ['A', 'B', 'D']; | ||
|
||
export default function parseDocumentCode(source: string) { | ||
if (source.length !== 2) { | ||
throw new Error(`invalid document code: ${source}. must be 2 char length`); | ||
} | ||
|
||
const [first, second] = source; | ||
|
||
validateFirstChar(first, source); | ||
validateSecondChar(second, source); | ||
|
||
return `${first}${second}`; | ||
} | ||
|
||
function validateFirstChar(char: string, source: string) { | ||
if (!knownPrefixCode.includes(char)) { | ||
const formatter = new Intl.ListFormat('en-US', { | ||
style: 'short', | ||
type: 'disjunction', | ||
}); | ||
throw new Error( | ||
`invalid document code: ${source}. First character must be ${formatter.format(knownPrefixCode)}`, | ||
); | ||
} | ||
} | ||
|
||
function validateSecondChar(char: string, source: string) { | ||
const num = parseInt(char, 10); | ||
|
||
if (Number.isNaN(num)) { | ||
throw new Error( | ||
`invalid document code number: ${source}. Second character must be a number`, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters