Socket
Socket
Sign inDemoInstall

mrz

Package Overview
Dependencies
0
Maintainers
4
Versions
26
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.2.1 to 3.3.0

lib-esm/parse/autoCorrection.d.ts

14

lib-esm/formats.d.ts

@@ -1,9 +0,9 @@

declare const formats: {
TD1: string;
TD2: string;
TD3: string;
SWISS_DRIVING_LICENSE: string;
FRENCH_NATIONAL_ID: string;
export type FormatType = keyof typeof formats;
export declare const formats: {
readonly TD1: "TD1";
readonly TD2: "TD2";
readonly TD3: "TD3";
readonly SWISS_DRIVING_LICENSE: "SWISS_DRIVING_LICENSE";
readonly FRENCH_NATIONAL_ID: "FRENCH_NATIONAL_ID";
};
export default formats;
//# sourceMappingURL=formats.d.ts.map
'use strict';
const formats = {
export const formats = {
TD1: 'TD1',

@@ -9,4 +9,2 @@ TD2: 'TD2',

};
Object.freeze(formats);
export default formats;
//# sourceMappingURL=formats.js.map

@@ -1,2 +0,2 @@

import formats from './formats';
import { formats } from './formats';
import states from './generated/states';

@@ -3,0 +3,0 @@ import parse from './parse/parse';

'use strict';
import formats from './formats';
import { formats } from './formats';
import states from './generated/states';

@@ -4,0 +4,0 @@ import parse from './parse/parse';

@@ -1,2 +0,2 @@

export default function checkLines(lines: string | string[]): string[];
export declare function checkLines(lines: string | string[]): string[];
//# sourceMappingURL=checkLines.d.ts.map
'use strict';
export default function checkLines(lines) {
export function checkLines(lines) {
if (typeof lines === 'string') {

@@ -4,0 +4,0 @@ lines = lines.split(/[\r\n]+/);

@@ -0,1 +1,2 @@

import { Autocorrect } from './autoCorrection';
export interface Details {

@@ -11,2 +12,3 @@ label: string;

error?: string;
autocorrect: Autocorrect[];
}

@@ -19,3 +21,9 @@ interface ParseResult {

type Parser = (source: string, ...related: string[]) => ParseResult | string;
type FieldOptions = {
type FieldTypes = keyof typeof fieldTypes;
export declare const fieldTypes: {
readonly NUMERIC: "NUMERIC";
readonly ALPHABETIC: "ALPHABETIC";
readonly ALPHANUMERIC: "ALPHANUMERIC";
};
export type FieldOptions = {
label: string;

@@ -28,2 +36,3 @@ field: string | null;

related?: Range[];
type?: FieldTypes;
};

@@ -35,5 +44,11 @@ interface Range {

}
export type ParseFunction = (lines: string | string[]) => Details;
export declare function createFieldParser(fieldOptions: FieldOptions): ParseFunction;
export interface CreateFieldParserResult {
parser: (lines: string[], autocorrect?: Autocorrect[]) => Details;
autocorrector: (lines: string[]) => {
correctedLines: string[];
autocorrect: Autocorrect[];
};
}
export default function createFieldParser(fieldOptions: FieldOptions): CreateFieldParserResult;
export {};
//# sourceMappingURL=createFieldParser.d.ts.map
'use strict';
export function createFieldParser(fieldOptions) {
import { autoCorrection } from './autoCorrection';
export const fieldTypes = {
NUMERIC: 'NUMERIC',
ALPHABETIC: 'ALPHABETIC',
ALPHANUMERIC: 'ALPHANUMERIC',
};
export default function createFieldParser(fieldOptions) {
checkType(fieldOptions, 'label', 'string');

@@ -26,3 +32,3 @@ if (fieldOptions.field !== null) {

}
return function parseField(lines) {
const parser = (lines, autocorrect = []) => {
const source = getText(lines, fieldOptions);

@@ -45,2 +51,3 @@ const related = fieldOptions.related || [];

end: 0,
autocorrect,
};

@@ -65,2 +72,16 @@ const range = result.ranges[0];

};
const autocorrector = (lines) => {
let corrected = lines;
let source = getText(lines, fieldOptions);
let autocorrect = [];
const type = fieldOptions.type || fieldTypes.ALPHANUMERIC;
if (type !== fieldTypes.ALPHANUMERIC) {
const result = autoCorrection(source, fieldOptions);
source = result.correctedLine;
autocorrect = result.autocorrect;
}
corrected = changeText(lines, fieldOptions, source);
return { correctedLines: corrected, autocorrect };
};
return { parser, autocorrector };
}

@@ -71,2 +92,8 @@ function getText(lines, options) {

}
function changeText(lines, options, text) {
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, name, type) {

@@ -73,0 +100,0 @@ if (typeof options[name] !== type) {

@@ -14,2 +14,3 @@ import parseCompositeCheckDigit from '../parsers/parseCompositeCheckDigit';

parser: typeof parseDocumentNumber;
type: "ALPHANUMERIC";
};

@@ -20,2 +21,3 @@ declare const documentNumberCheckDigitTemplate: {

parser: typeof parseDocumentNumberCheckDigit;
type: "NUMERIC";
};

@@ -25,2 +27,3 @@ declare const documentCodeTemplate: {

field: string;
type: "ALPHABETIC";
};

@@ -31,2 +34,3 @@ declare const nationalityTemplate: {

parser: typeof parseState;
type: "ALPHABETIC";
};

@@ -37,2 +41,3 @@ declare const sexTemplate: {

parser: typeof parseSex;
type: "ALPHABETIC";
};

@@ -43,2 +48,3 @@ declare const expirationDateTemplate: {

parser: typeof parseDate;
type: "NUMERIC";
};

@@ -49,2 +55,3 @@ declare const expirationDateCheckDigitTemplate: {

parser: typeof parseDateCheckDigit;
type: "NUMERIC";
};

@@ -55,2 +62,3 @@ declare const compositeCheckDigitTemplate: {

parser: typeof parseCompositeCheckDigit;
type: "NUMERIC";
};

@@ -61,2 +69,3 @@ declare const birthDateTemplate: {

parser: typeof parseDate;
type: "NUMERIC";
};

@@ -67,2 +76,3 @@ declare const birthDateCheckDigitTemplate: {

parser: typeof parseDateCheckDigit;
type: "NUMERIC";
};

@@ -73,2 +83,3 @@ declare const issueDateTemplate: {

parser: typeof parseDate;
type: "NUMERIC";
};

@@ -79,2 +90,3 @@ declare const firstNameTemplate: {

parser: typeof parseFirstName;
type: "ALPHABETIC";
};

@@ -85,2 +97,3 @@ declare const lastNameTemplate: {

parser: typeof parseLastName;
type: "ALPHABETIC";
};

@@ -91,4 +104,5 @@ declare const issuingStateTemplate: {

parser: typeof parseState;
type: "ALPHABETIC";
};
export { documentNumberTemplate, documentNumberCheckDigitTemplate, documentCodeTemplate, nationalityTemplate, sexTemplate, expirationDateTemplate, expirationDateCheckDigitTemplate, birthDateTemplate, birthDateCheckDigitTemplate, issueDateTemplate, compositeCheckDigitTemplate, firstNameTemplate, lastNameTemplate, issuingStateTemplate, };
//# sourceMappingURL=fieldTemplates.d.ts.map

@@ -11,2 +11,3 @@ 'use strict';

import parseState from '../parsers/parseState';
import { fieldTypes } from './createFieldParser';
const documentNumberTemplate = {

@@ -16,2 +17,3 @@ label: 'Document number',

parser: parseDocumentNumber,
type: fieldTypes.ALPHANUMERIC,
};

@@ -22,2 +24,3 @@ const documentNumberCheckDigitTemplate = {

parser: parseDocumentNumberCheckDigit,
type: fieldTypes.NUMERIC,
};

@@ -27,2 +30,3 @@ const documentCodeTemplate = {

field: 'documentCode',
type: fieldTypes.ALPHABETIC,
};

@@ -33,2 +37,3 @@ const nationalityTemplate = {

parser: parseState,
type: fieldTypes.ALPHABETIC,
};

@@ -39,2 +44,3 @@ const sexTemplate = {

parser: parseSex,
type: fieldTypes.ALPHABETIC,
};

@@ -45,2 +51,3 @@ const expirationDateTemplate = {

parser: parseDate,
type: fieldTypes.NUMERIC,
};

@@ -51,2 +58,3 @@ const expirationDateCheckDigitTemplate = {

parser: parseDateCheckDigit,
type: fieldTypes.NUMERIC,
};

@@ -57,2 +65,3 @@ const compositeCheckDigitTemplate = {

parser: parseCompositeCheckDigit,
type: fieldTypes.NUMERIC,
};

@@ -63,2 +72,3 @@ const birthDateTemplate = {

parser: parseDate,
type: fieldTypes.NUMERIC,
};

@@ -69,2 +79,3 @@ const birthDateCheckDigitTemplate = {

parser: parseDateCheckDigit,
type: fieldTypes.NUMERIC,
};

@@ -75,2 +86,3 @@ const issueDateTemplate = {

parser: parseDate,
type: fieldTypes.NUMERIC,
};

@@ -81,2 +93,3 @@ const firstNameTemplate = {

parser: parseFirstName,
type: fieldTypes.ALPHABETIC,
};

@@ -87,2 +100,3 @@ const lastNameTemplate = {

parser: parseLastName,
type: fieldTypes.ALPHABETIC,
};

@@ -93,4 +107,5 @@ const issuingStateTemplate = {

parser: parseState,
type: fieldTypes.ALPHABETIC,
};
export { documentNumberTemplate, documentNumberCheckDigitTemplate, documentCodeTemplate, nationalityTemplate, sexTemplate, expirationDateTemplate, expirationDateCheckDigitTemplate, birthDateTemplate, birthDateCheckDigitTemplate, issueDateTemplate, compositeCheckDigitTemplate, firstNameTemplate, lastNameTemplate, issuingStateTemplate, };
//# sourceMappingURL=fieldTemplates.js.map

@@ -1,3 +0,4 @@

export default function parseFrenchNationalId(lines: string | string[]): {
format: string;
import { ParseMRZOptions } from './parse';
export default function parseFrenchNationalId(lines: string[], options: ParseMRZOptions): {
format: "TD1" | "TD2" | "TD3" | "SWISS_DRIVING_LICENSE" | "FRENCH_NATIONAL_ID";
details: import("./createFieldParser").Details[];

@@ -4,0 +5,0 @@ fields: Record<string, string | null>;

'use strict';
import formats from '../formats';
import checkLines from './checkLines';
import { formats } from '../formats';
import frenchNationalIdFields from './frenchNationalIdFields';
import getResult from './getResult';
import { getResult } from './getResult';
const FRENCH_NATIONAL_ID = formats.FRENCH_NATIONAL_ID;
export default function parseFrenchNationalId(lines) {
const result = checkLines(lines);
if (result.length !== 2) {
throw new Error(`invalid number of lines: ${result.length}: Must be 2 for ${FRENCH_NATIONAL_ID}`);
export default function parseFrenchNationalId(lines, options) {
if (lines.length !== 2) {
throw new Error(`invalid number of lines: ${lines.length}: Must be 2 for ${FRENCH_NATIONAL_ID}`);
}
result.forEach((line, index) => {
lines.forEach((line, index) => {
if (line.length !== 36) {

@@ -17,4 +15,4 @@ throw new Error(`invalid number of characters for line ${index + 1}: ${line.length}. Must be 36 for ${FRENCH_NATIONAL_ID}`);

});
return getResult(FRENCH_NATIONAL_ID, lines, frenchNationalIdFields);
return getResult(FRENCH_NATIONAL_ID, lines, frenchNationalIdFields, options);
}
//# sourceMappingURL=frenchNationalId.js.map

@@ -1,3 +0,3 @@

declare const _default: import("./createFieldParser").ParseFunction[];
declare const _default: import("./createFieldParser").CreateFieldParserResult[];
export default _default;
//# sourceMappingURL=frenchNationalIdFields.d.ts.map

@@ -5,3 +5,3 @@ 'use strict';

import { parseOptional } from '../parsers/parseOptional';
import { createFieldParser } from './createFieldParser';
import createFieldParser from './createFieldParser';
import { documentCodeTemplate, issuingStateTemplate, lastNameTemplate, issueDateTemplate, firstNameTemplate, documentNumberTemplate, documentNumberCheckDigitTemplate, birthDateTemplate, birthDateCheckDigitTemplate, sexTemplate, compositeCheckDigitTemplate, } from './fieldTemplates';

@@ -8,0 +8,0 @@ export default [

@@ -1,4 +0,6 @@

import { ParseFunction, Details } from './createFieldParser';
export default function getResult(format: string, lines: string | string[], fieldParsers: ParseFunction[]): {
format: string;
import { FormatType } from '../formats';
import { Details, CreateFieldParserResult } from './createFieldParser';
import { ParseMRZOptions } from './parse';
export declare function getResult(format: FormatType, lines: string[], fieldParsers: CreateFieldParserResult[], options: ParseMRZOptions): {
format: "TD1" | "TD2" | "TD3" | "SWISS_DRIVING_LICENSE" | "FRENCH_NATIONAL_ID";
details: Details[];

@@ -5,0 +7,0 @@ fields: Record<string, string | null>;

'use strict';
function getDetails(lines, fieldParsers) {
function getDetails(lines, fieldParsers, autocorrectArray) {
const details = [];
for (const parser of fieldParsers) {
details.push(parser(lines));
}
fieldParsers.forEach(({ parser }, i) => {
details.push(parser(lines, autocorrectArray[i]));
});
return details;

@@ -21,4 +21,18 @@ }

}
export default function getResult(format, lines, fieldParsers) {
const details = getDetails(lines, fieldParsers);
function getCorrection(lines, fieldParsers, autocorrect) {
let corrected = lines;
const autocorrectArray = [];
if (autocorrect) {
fieldParsers.forEach(({ autocorrector }) => {
const result = autocorrector(corrected);
autocorrectArray.push(result.autocorrect);
corrected = result.correctedLines;
});
}
return { corrected, autocorrectArray };
}
export function getResult(format, lines, fieldParsers, options) {
const { autocorrect = false } = options;
const { corrected, autocorrectArray } = getCorrection(lines, fieldParsers, autocorrect);
const details = getDetails(corrected, fieldParsers, autocorrectArray);
const fields = getFields(details);

@@ -25,0 +39,0 @@ const result = {

@@ -1,3 +0,6 @@

declare function parseMRZ(lines: string | string[]): {
format: string;
export interface ParseMRZOptions {
autocorrect?: boolean;
}
declare function parseMRZ(inputLines: string | string[], options?: ParseMRZOptions): {
format: "TD1" | "TD2" | "TD3" | "SWISS_DRIVING_LICENSE" | "FRENCH_NATIONAL_ID";
details: import("./createFieldParser").Details[];

@@ -4,0 +7,0 @@ fields: Record<string, string | null>;

'use strict';
import formats from '../formats';
import checkLines from './checkLines';
import { checkLines } from './checkLines';
import { parsers } from './parsers';
function parseMRZ(lines) {
const result = checkLines(lines);
switch (result.length) {
function parseMRZ(inputLines, options = {}) {
const lines = checkLines(inputLines);
switch (lines.length) {
case 2:
case 3: {
switch (result[0].length) {
switch (lines[0].length) {
case 30:
return parsers.td1(lines);
return parsers.td1(lines, options);
case 36: {
const endLine1 = lines[0].substr(30, 36);
const endLine1 = lines[0].substring(30, 66);
if (endLine1.match(/[0-9]/)) {
return parsers.frenchNationalId(lines);
return parsers.frenchNationalId(lines, options);
}
else {
return parsers.td2(lines);
return parsers.td2(lines, options);
}
}
case 44:
return parsers.td3(lines);
return parsers.td3(lines, options);
case 9:
return parsers.swissDrivingLicense(lines);
return parsers.swissDrivingLicense(lines, options);
default:
throw new Error('unrecognized document format. First line of input must have 30 (TD1), 36 (TD2 or French National Id), 44 (TD3) or 9 (Swiss Driving License) characters');
throw new Error(`unrecognized document format. First line of input must have 30 (TD1), 36 (TD2 or French National Id), 44 (TD3) or 9 (Swiss Driving License) characters and it has a length of ${lines[0].length}`);
}
}
default:
throw new Error(`unrecognized document format. Input must have two or three lines, found${lines.length}`);
throw new Error(`unrecognized document format. Input must have two or three lines, found ${lines.length}`);
}
}
for (const format in formats) {
parseMRZ[format] = parsers[format];
}
export default parseMRZ;
//# sourceMappingURL=parse.js.map

@@ -1,3 +0,4 @@

export default function parseSwissDrivingLicense(lines: string | string[]): {
format: string;
import { ParseMRZOptions } from './parse';
export default function parseSwissDrivingLicense(lines: string[], options: ParseMRZOptions): {
format: "TD1" | "TD2" | "TD3" | "SWISS_DRIVING_LICENSE" | "FRENCH_NATIONAL_ID";
details: import("./createFieldParser").Details[];

@@ -4,0 +5,0 @@ fields: Record<string, string | null>;

'use strict';
import formats from '../formats';
import checkLines from './checkLines';
import getResult from './getResult';
import { formats } from '../formats';
import { getResult } from './getResult';
import swissDrivingLicenseFields from './swissDrivingLicenseFields';
const SWISS_DRIVING_LICENSE = formats.SWISS_DRIVING_LICENSE;
export default function parseSwissDrivingLicense(lines) {
const result = checkLines(lines);
if (result.length !== 3) {
throw new Error(`invalid number of lines: ${result.length}: Must be 3 for ${SWISS_DRIVING_LICENSE}`);
export default function parseSwissDrivingLicense(lines, options) {
if (lines.length !== 3) {
throw new Error(`invalid number of lines: ${lines.length}: Must be 3 for ${SWISS_DRIVING_LICENSE}`);
}
if (result[0].length !== 9) {
throw new Error(`invalid number of characters for line 1: ${result[0].length}. Must be 9 for ${SWISS_DRIVING_LICENSE}`);
if (lines[0].length !== 9) {
throw new Error(`invalid number of characters for line 1: ${lines[0].length}. Must be 9 for ${SWISS_DRIVING_LICENSE}`);
}
if (result[1].length !== 30) {
throw new Error(`invalid number of characters for line 2: ${result[1].length}. Must be 30 for ${SWISS_DRIVING_LICENSE}`);
if (lines[1].length !== 30) {
throw new Error(`invalid number of characters for line 2: ${lines[1].length}. Must be 30 for ${SWISS_DRIVING_LICENSE}`);
}
if (result[2].length !== 30) {
throw new Error(`invalid number of characters for line 3: ${result[2].length}. Must be 30 for ${SWISS_DRIVING_LICENSE}`);
if (lines[2].length !== 30) {
throw new Error(`invalid number of characters for line 3: ${lines[2].length}. Must be 30 for ${SWISS_DRIVING_LICENSE}`);
}
return getResult(SWISS_DRIVING_LICENSE, lines, swissDrivingLicenseFields);
return getResult(SWISS_DRIVING_LICENSE, lines, swissDrivingLicenseFields, options);
}
//# sourceMappingURL=swissDrivingLicense.js.map

@@ -1,3 +0,3 @@

declare const _default: import("./createFieldParser").ParseFunction[];
declare const _default: import("./createFieldParser").CreateFieldParserResult[];
export default _default;
//# sourceMappingURL=swissDrivingLicenseFields.d.ts.map

@@ -8,3 +8,3 @@ 'use strict';

import parseLanguageCode from '../parsers/swissDrivingLicense/parseLanguageCode';
import { createFieldParser } from './createFieldParser';
import createFieldParser, { fieldTypes } from './createFieldParser';
import { documentNumberTemplate, documentCodeTemplate, issuingStateTemplate, birthDateTemplate, lastNameTemplate, firstNameTemplate, } from './fieldTemplates';

@@ -26,2 +26,3 @@ export default [

parser: parseLanguageCode,
type: fieldTypes.ALPHABETIC,
},

@@ -49,2 +50,3 @@ {

parser: parseNumber,
type: fieldTypes.NUMERIC,
},

@@ -58,2 +60,3 @@ {

parser: parseNumber,
type: fieldTypes.NUMERIC,
},

@@ -60,0 +63,0 @@ {

@@ -1,3 +0,4 @@

export default function parseTD1(lines: string | string[]): {
format: string;
import { ParseMRZOptions } from './parse';
export default function parseTD1(lines: string[], options: ParseMRZOptions): {
format: "TD1" | "TD2" | "TD3" | "SWISS_DRIVING_LICENSE" | "FRENCH_NATIONAL_ID";
details: import("./createFieldParser").Details[];

@@ -4,0 +5,0 @@ fields: Record<string, string | null>;

'use strict';
import formats from '../formats';
import checkLines from './checkLines';
import getResult from './getResult';
import { formats } from '../formats';
import { getResult } from './getResult';
import TD1Fields from './td1Fields';
const TD1 = formats.TD1;
export default function parseTD1(lines) {
const result = checkLines(lines);
if (result.length !== 3) {
throw new Error(`invalid number of lines: ${result.length}: Must be 3 for ${TD1}`);
export default function parseTD1(lines, options) {
if (lines.length !== 3) {
throw new Error(`invalid number of lines: ${lines.length}: Must be 3 for ${TD1}`);
}
result.forEach((line, index) => {
lines.forEach((line, index) => {
if (line.length !== 30) {

@@ -17,4 +15,4 @@ throw new Error(`invalid number of characters for line ${index + 1}: ${line.length}. Must be 30 for ${TD1}`);

});
return getResult(TD1, lines, TD1Fields);
return getResult(TD1, lines, TD1Fields, options);
}
//# sourceMappingURL=td1.js.map

@@ -1,3 +0,3 @@

declare const _default: import("./createFieldParser").ParseFunction[];
declare const _default: import("./createFieldParser").CreateFieldParserResult[];
export default _default;
//# sourceMappingURL=td1Fields.d.ts.map

@@ -5,3 +5,3 @@ 'use strict';

import { parseOptional } from '../parsers/parseOptional';
import { createFieldParser } from './createFieldParser';
import createFieldParser from './createFieldParser';
import { documentCodeTemplate, issuingStateTemplate, documentNumberTemplate, documentNumberCheckDigitTemplate, birthDateTemplate, birthDateCheckDigitTemplate, sexTemplate, expirationDateTemplate, expirationDateCheckDigitTemplate, nationalityTemplate, compositeCheckDigitTemplate, lastNameTemplate, firstNameTemplate, } from './fieldTemplates';

@@ -35,3 +35,4 @@ export default [

},
Object.assign(documentNumberCheckDigitTemplate, {
{
...documentNumberCheckDigitTemplate,
line: 0,

@@ -52,3 +53,3 @@ start: 14,

],
}),
},
{

@@ -55,0 +56,0 @@ label: 'Optional field 1',

@@ -1,3 +0,4 @@

export default function parseTD2(lines: string | string[]): {
format: string;
import { ParseMRZOptions } from './parse';
export default function parseTD2(lines: string[], options: ParseMRZOptions): {
format: "TD1" | "TD2" | "TD3" | "SWISS_DRIVING_LICENSE" | "FRENCH_NATIONAL_ID";
details: import("./createFieldParser").Details[];

@@ -4,0 +5,0 @@ fields: Record<string, string | null>;

'use strict';
import formats from '../formats';
import checkLines from './checkLines';
import getResult from './getResult';
import { formats } from '../formats';
import { getResult } from './getResult';
import TD2Fields from './td2Fields';
const TD2 = formats.TD2;
export default function parseTD2(lines) {
const result = checkLines(lines);
if (result.length !== 2) {
throw new Error(`invalid number of lines: ${result.length}: Must be 2 for ${TD2}`);
export default function parseTD2(lines, options) {
if (lines.length !== 2) {
throw new Error(`invalid number of lines: ${lines.length}: Must be 2 for ${TD2}`);
}
result.forEach((line, index) => {
lines.forEach((line, index) => {
if (line.length !== 36) {

@@ -17,4 +15,4 @@ throw new Error(`invalid number of characters for line ${index + 1}: ${line.length}. Must be 36 for TD2`);

});
return getResult(TD2, lines, TD2Fields);
return getResult(TD2, lines, TD2Fields, options);
}
//# sourceMappingURL=td2.js.map

@@ -1,3 +0,3 @@

declare const _default: import("./createFieldParser").ParseFunction[];
declare const _default: import("./createFieldParser").CreateFieldParserResult[];
export default _default;
//# sourceMappingURL=td2Fields.d.ts.map
'use strict';
import parseDocumentCode from '../parsers/parseDocumentCodeId';
import { parseOptional } from '../parsers/parseOptional';
import { createFieldParser } from './createFieldParser';
import createFieldParser from './createFieldParser';
import { documentCodeTemplate, issuingStateTemplate, firstNameTemplate, lastNameTemplate, documentNumberTemplate, documentNumberCheckDigitTemplate, nationalityTemplate, birthDateTemplate, birthDateCheckDigitTemplate, sexTemplate, expirationDateTemplate, expirationDateCheckDigitTemplate, compositeCheckDigitTemplate, } from './fieldTemplates';

@@ -6,0 +6,0 @@ export default [

@@ -1,3 +0,4 @@

export default function parseTD3(lines: string | string[]): {
format: string;
import { ParseMRZOptions } from './parse';
export default function parseTD3(lines: string[], options: ParseMRZOptions): {
format: "TD1" | "TD2" | "TD3" | "SWISS_DRIVING_LICENSE" | "FRENCH_NATIONAL_ID";
details: import("./createFieldParser").Details[];

@@ -4,0 +5,0 @@ fields: Record<string, string | null>;

'use strict';
import formats from '../formats';
import checkLines from './checkLines';
import getResult from './getResult';
import { formats } from '../formats';
import { getResult } from './getResult';
import TD3Fields from './td3Fields';
const TD3 = formats.TD3;
export default function parseTD3(lines) {
const result = checkLines(lines);
if (result.length !== 2) {
throw new Error(`invalid number of lines: ${result.length}: Must be 2 for ${TD3}`);
export default function parseTD3(lines, options) {
if (lines.length !== 2) {
throw new Error(`invalid number of lines: ${lines.length}: Must be 2 for ${TD3}`);
}
result.forEach((line, index) => {
lines.forEach((line, index) => {
if (line.length !== 44) {

@@ -17,4 +15,4 @@ throw new Error(`invalid number of characters for line ${index + 1}: ${line.length}. Must be 44 for TD3`);

});
return getResult(TD3, lines, TD3Fields);
return getResult(TD3, lines, TD3Fields, options);
}
//# sourceMappingURL=td3.js.map

@@ -1,3 +0,3 @@

declare const _default: import("./createFieldParser").ParseFunction[];
declare const _default: import("./createFieldParser").CreateFieldParserResult[];
export default _default;
//# sourceMappingURL=td3Fields.d.ts.map

@@ -5,3 +5,3 @@ 'use strict';

import { parsePersonalNumberCheckDigit } from '../parsers/parsePersonalNumberCheckDigit';
import { createFieldParser } from './createFieldParser';
import createFieldParser from './createFieldParser';
import { documentCodeTemplate, issuingStateTemplate, lastNameTemplate, firstNameTemplate, documentNumberTemplate, documentNumberCheckDigitTemplate, nationalityTemplate, birthDateTemplate, birthDateCheckDigitTemplate, sexTemplate, expirationDateTemplate, expirationDateCheckDigitTemplate, compositeCheckDigitTemplate, } from './fieldTemplates';

@@ -8,0 +8,0 @@ export default [

@@ -1,9 +0,9 @@

declare const formats: {
TD1: string;
TD2: string;
TD3: string;
SWISS_DRIVING_LICENSE: string;
FRENCH_NATIONAL_ID: string;
export type FormatType = keyof typeof formats;
export declare const formats: {
readonly TD1: "TD1";
readonly TD2: "TD2";
readonly TD3: "TD3";
readonly SWISS_DRIVING_LICENSE: "SWISS_DRIVING_LICENSE";
readonly FRENCH_NATIONAL_ID: "FRENCH_NATIONAL_ID";
};
export default formats;
//# sourceMappingURL=formats.d.ts.map
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const formats = {
exports.formats = void 0;
exports.formats = {
TD1: 'TD1',

@@ -10,4 +11,2 @@ TD2: 'TD2',

};
Object.freeze(formats);
exports.default = formats;
//# sourceMappingURL=formats.js.map

@@ -1,2 +0,2 @@

import formats from './formats';
import { formats } from './formats';
import states from './generated/states';

@@ -3,0 +3,0 @@ import parse from './parse/parse';

@@ -7,4 +7,4 @@ 'use strict';

exports.parse = exports.states = exports.formats = void 0;
const formats_1 = __importDefault(require("./formats"));
exports.formats = formats_1.default;
const formats_1 = require("./formats");
Object.defineProperty(exports, "formats", { enumerable: true, get: function () { return formats_1.formats; } });
const states_1 = __importDefault(require("./generated/states"));

@@ -11,0 +11,0 @@ exports.states = states_1.default;

@@ -1,2 +0,2 @@

export default function checkLines(lines: string | string[]): string[];
export declare function checkLines(lines: string | string[]): string[];
//# sourceMappingURL=checkLines.d.ts.map
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkLines = void 0;
function checkLines(lines) {

@@ -17,3 +18,3 @@ if (typeof lines === 'string') {

}
exports.default = checkLines;
exports.checkLines = checkLines;
//# sourceMappingURL=checkLines.js.map

@@ -0,1 +1,2 @@

import { Autocorrect } from './autoCorrection';
export interface Details {

@@ -11,2 +12,3 @@ label: string;

error?: string;
autocorrect: Autocorrect[];
}

@@ -19,3 +21,9 @@ interface ParseResult {

type Parser = (source: string, ...related: string[]) => ParseResult | string;
type FieldOptions = {
type FieldTypes = keyof typeof fieldTypes;
export declare const fieldTypes: {
readonly NUMERIC: "NUMERIC";
readonly ALPHABETIC: "ALPHABETIC";
readonly ALPHANUMERIC: "ALPHANUMERIC";
};
export type FieldOptions = {
label: string;

@@ -28,2 +36,3 @@ field: string | null;

related?: Range[];
type?: FieldTypes;
};

@@ -35,5 +44,11 @@ interface Range {

}
export type ParseFunction = (lines: string | string[]) => Details;
export declare function createFieldParser(fieldOptions: FieldOptions): ParseFunction;
export interface CreateFieldParserResult {
parser: (lines: string[], autocorrect?: Autocorrect[]) => Details;
autocorrector: (lines: string[]) => {
correctedLines: string[];
autocorrect: Autocorrect[];
};
}
export default function createFieldParser(fieldOptions: FieldOptions): CreateFieldParserResult;
export {};
//# sourceMappingURL=createFieldParser.d.ts.map
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
exports.createFieldParser = void 0;
exports.fieldTypes = void 0;
const autoCorrection_1 = require("./autoCorrection");
exports.fieldTypes = {
NUMERIC: 'NUMERIC',
ALPHABETIC: 'ALPHABETIC',
ALPHANUMERIC: 'ALPHANUMERIC',
};
function createFieldParser(fieldOptions) {

@@ -28,3 +34,3 @@ checkType(fieldOptions, 'label', 'string');

}
return function parseField(lines) {
const parser = (lines, autocorrect = []) => {
const source = getText(lines, fieldOptions);

@@ -47,2 +53,3 @@ const related = fieldOptions.related || [];

end: 0,
autocorrect,
};

@@ -67,4 +74,18 @@ const range = result.ranges[0];

};
const autocorrector = (lines) => {
let corrected = lines;
let source = getText(lines, fieldOptions);
let autocorrect = [];
const type = fieldOptions.type || exports.fieldTypes.ALPHANUMERIC;
if (type !== exports.fieldTypes.ALPHANUMERIC) {
const result = (0, autoCorrection_1.autoCorrection)(source, fieldOptions);
source = result.correctedLine;
autocorrect = result.autocorrect;
}
corrected = changeText(lines, fieldOptions, source);
return { correctedLines: corrected, autocorrect };
};
return { parser, autocorrector };
}
exports.createFieldParser = createFieldParser;
exports.default = createFieldParser;
function getText(lines, options) {

@@ -74,2 +95,8 @@ const line = lines[options.line];

}
function changeText(lines, options, text) {
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, name, type) {

@@ -76,0 +103,0 @@ if (typeof options[name] !== type) {

@@ -14,2 +14,3 @@ import parseCompositeCheckDigit from '../parsers/parseCompositeCheckDigit';

parser: typeof parseDocumentNumber;
type: "ALPHANUMERIC";
};

@@ -20,2 +21,3 @@ declare const documentNumberCheckDigitTemplate: {

parser: typeof parseDocumentNumberCheckDigit;
type: "NUMERIC";
};

@@ -25,2 +27,3 @@ declare const documentCodeTemplate: {

field: string;
type: "ALPHABETIC";
};

@@ -31,2 +34,3 @@ declare const nationalityTemplate: {

parser: typeof parseState;
type: "ALPHABETIC";
};

@@ -37,2 +41,3 @@ declare const sexTemplate: {

parser: typeof parseSex;
type: "ALPHABETIC";
};

@@ -43,2 +48,3 @@ declare const expirationDateTemplate: {

parser: typeof parseDate;
type: "NUMERIC";
};

@@ -49,2 +55,3 @@ declare const expirationDateCheckDigitTemplate: {

parser: typeof parseDateCheckDigit;
type: "NUMERIC";
};

@@ -55,2 +62,3 @@ declare const compositeCheckDigitTemplate: {

parser: typeof parseCompositeCheckDigit;
type: "NUMERIC";
};

@@ -61,2 +69,3 @@ declare const birthDateTemplate: {

parser: typeof parseDate;
type: "NUMERIC";
};

@@ -67,2 +76,3 @@ declare const birthDateCheckDigitTemplate: {

parser: typeof parseDateCheckDigit;
type: "NUMERIC";
};

@@ -73,2 +83,3 @@ declare const issueDateTemplate: {

parser: typeof parseDate;
type: "NUMERIC";
};

@@ -79,2 +90,3 @@ declare const firstNameTemplate: {

parser: typeof parseFirstName;
type: "ALPHABETIC";
};

@@ -85,2 +97,3 @@ declare const lastNameTemplate: {

parser: typeof parseLastName;
type: "ALPHABETIC";
};

@@ -91,4 +104,5 @@ declare const issuingStateTemplate: {

parser: typeof parseState;
type: "ALPHABETIC";
};
export { documentNumberTemplate, documentNumberCheckDigitTemplate, documentCodeTemplate, nationalityTemplate, sexTemplate, expirationDateTemplate, expirationDateCheckDigitTemplate, birthDateTemplate, birthDateCheckDigitTemplate, issueDateTemplate, compositeCheckDigitTemplate, firstNameTemplate, lastNameTemplate, issuingStateTemplate, };
//# sourceMappingURL=fieldTemplates.d.ts.map

@@ -16,2 +16,3 @@ 'use strict';

const parseState_1 = __importDefault(require("../parsers/parseState"));
const createFieldParser_1 = require("./createFieldParser");
const documentNumberTemplate = {

@@ -21,2 +22,3 @@ label: 'Document number',

parser: parseDocumentNumber_1.default,
type: createFieldParser_1.fieldTypes.ALPHANUMERIC,
};

@@ -28,2 +30,3 @@ exports.documentNumberTemplate = documentNumberTemplate;

parser: parseDocumentNumberCheckDigit_1.default,
type: createFieldParser_1.fieldTypes.NUMERIC,
};

@@ -34,2 +37,3 @@ exports.documentNumberCheckDigitTemplate = documentNumberCheckDigitTemplate;

field: 'documentCode',
type: createFieldParser_1.fieldTypes.ALPHABETIC,
};

@@ -41,2 +45,3 @@ exports.documentCodeTemplate = documentCodeTemplate;

parser: parseState_1.default,
type: createFieldParser_1.fieldTypes.ALPHABETIC,
};

@@ -48,2 +53,3 @@ exports.nationalityTemplate = nationalityTemplate;

parser: parseSex_1.default,
type: createFieldParser_1.fieldTypes.ALPHABETIC,
};

@@ -55,2 +61,3 @@ exports.sexTemplate = sexTemplate;

parser: parseDate_1.default,
type: createFieldParser_1.fieldTypes.NUMERIC,
};

@@ -62,2 +69,3 @@ exports.expirationDateTemplate = expirationDateTemplate;

parser: parseDateCheckDigit_1.default,
type: createFieldParser_1.fieldTypes.NUMERIC,
};

@@ -69,2 +77,3 @@ exports.expirationDateCheckDigitTemplate = expirationDateCheckDigitTemplate;

parser: parseCompositeCheckDigit_1.default,
type: createFieldParser_1.fieldTypes.NUMERIC,
};

@@ -76,2 +85,3 @@ exports.compositeCheckDigitTemplate = compositeCheckDigitTemplate;

parser: parseDate_1.default,
type: createFieldParser_1.fieldTypes.NUMERIC,
};

@@ -83,2 +93,3 @@ exports.birthDateTemplate = birthDateTemplate;

parser: parseDateCheckDigit_1.default,
type: createFieldParser_1.fieldTypes.NUMERIC,
};

@@ -90,2 +101,3 @@ exports.birthDateCheckDigitTemplate = birthDateCheckDigitTemplate;

parser: parseDate_1.default,
type: createFieldParser_1.fieldTypes.NUMERIC,
};

@@ -97,2 +109,3 @@ exports.issueDateTemplate = issueDateTemplate;

parser: parseFirstName_1.default,
type: createFieldParser_1.fieldTypes.ALPHABETIC,
};

@@ -104,2 +117,3 @@ exports.firstNameTemplate = firstNameTemplate;

parser: parseLastName_1.default,
type: createFieldParser_1.fieldTypes.ALPHABETIC,
};

@@ -111,4 +125,5 @@ exports.lastNameTemplate = lastNameTemplate;

parser: parseState_1.default,
type: createFieldParser_1.fieldTypes.ALPHABETIC,
};
exports.issuingStateTemplate = issuingStateTemplate;
//# sourceMappingURL=fieldTemplates.js.map

@@ -1,3 +0,4 @@

export default function parseFrenchNationalId(lines: string | string[]): {
format: string;
import { ParseMRZOptions } from './parse';
export default function parseFrenchNationalId(lines: string[], options: ParseMRZOptions): {
format: "TD1" | "TD2" | "TD3" | "SWISS_DRIVING_LICENSE" | "FRENCH_NATIONAL_ID";
details: import("./createFieldParser").Details[];

@@ -4,0 +5,0 @@ fields: Record<string, string | null>;

@@ -6,13 +6,11 @@ 'use strict';

Object.defineProperty(exports, "__esModule", { value: true });
const formats_1 = __importDefault(require("../formats"));
const checkLines_1 = __importDefault(require("./checkLines"));
const formats_1 = require("../formats");
const frenchNationalIdFields_1 = __importDefault(require("./frenchNationalIdFields"));
const getResult_1 = __importDefault(require("./getResult"));
const FRENCH_NATIONAL_ID = formats_1.default.FRENCH_NATIONAL_ID;
function parseFrenchNationalId(lines) {
const result = (0, checkLines_1.default)(lines);
if (result.length !== 2) {
throw new Error(`invalid number of lines: ${result.length}: Must be 2 for ${FRENCH_NATIONAL_ID}`);
const getResult_1 = require("./getResult");
const FRENCH_NATIONAL_ID = formats_1.formats.FRENCH_NATIONAL_ID;
function parseFrenchNationalId(lines, options) {
if (lines.length !== 2) {
throw new Error(`invalid number of lines: ${lines.length}: Must be 2 for ${FRENCH_NATIONAL_ID}`);
}
result.forEach((line, index) => {
lines.forEach((line, index) => {
if (line.length !== 36) {

@@ -22,5 +20,5 @@ throw new Error(`invalid number of characters for line ${index + 1}: ${line.length}. Must be 36 for ${FRENCH_NATIONAL_ID}`);

});
return (0, getResult_1.default)(FRENCH_NATIONAL_ID, lines, frenchNationalIdFields_1.default);
return (0, getResult_1.getResult)(FRENCH_NATIONAL_ID, lines, frenchNationalIdFields_1.default, options);
}
exports.default = parseFrenchNationalId;
//# sourceMappingURL=frenchNationalId.js.map

@@ -1,3 +0,3 @@

declare const _default: import("./createFieldParser").ParseFunction[];
declare const _default: import("./createFieldParser").CreateFieldParserResult[];
export default _default;
//# sourceMappingURL=frenchNationalIdFields.d.ts.map

@@ -9,3 +9,3 @@ 'use strict';

const parseOptional_1 = require("../parsers/parseOptional");
const createFieldParser_1 = require("./createFieldParser");
const createFieldParser_1 = __importDefault(require("./createFieldParser"));
const fieldTemplates_1 = require("./fieldTemplates");

@@ -87,3 +87,3 @@ exports.default = [

},
].map(createFieldParser_1.createFieldParser);
].map(createFieldParser_1.default);
//# sourceMappingURL=frenchNationalIdFields.js.map

@@ -1,4 +0,6 @@

import { ParseFunction, Details } from './createFieldParser';
export default function getResult(format: string, lines: string | string[], fieldParsers: ParseFunction[]): {
format: string;
import { FormatType } from '../formats';
import { Details, CreateFieldParserResult } from './createFieldParser';
import { ParseMRZOptions } from './parse';
export declare function getResult(format: FormatType, lines: string[], fieldParsers: CreateFieldParserResult[], options: ParseMRZOptions): {
format: "TD1" | "TD2" | "TD3" | "SWISS_DRIVING_LICENSE" | "FRENCH_NATIONAL_ID";
details: Details[];

@@ -5,0 +7,0 @@ fields: Record<string, string | null>;

'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
function getDetails(lines, fieldParsers) {
exports.getResult = void 0;
function getDetails(lines, fieldParsers, autocorrectArray) {
const details = [];
for (const parser of fieldParsers) {
details.push(parser(lines));
}
fieldParsers.forEach(({ parser }, i) => {
details.push(parser(lines, autocorrectArray[i]));
});
return details;

@@ -22,4 +23,18 @@ }

}
function getResult(format, lines, fieldParsers) {
const details = getDetails(lines, fieldParsers);
function getCorrection(lines, fieldParsers, autocorrect) {
let corrected = lines;
const autocorrectArray = [];
if (autocorrect) {
fieldParsers.forEach(({ autocorrector }) => {
const result = autocorrector(corrected);
autocorrectArray.push(result.autocorrect);
corrected = result.correctedLines;
});
}
return { corrected, autocorrectArray };
}
function getResult(format, lines, fieldParsers, options) {
const { autocorrect = false } = options;
const { corrected, autocorrectArray } = getCorrection(lines, fieldParsers, autocorrect);
const details = getDetails(corrected, fieldParsers, autocorrectArray);
const fields = getFields(details);

@@ -34,3 +49,3 @@ const result = {

}
exports.default = getResult;
exports.getResult = getResult;
//# sourceMappingURL=getResult.js.map

@@ -1,3 +0,6 @@

declare function parseMRZ(lines: string | string[]): {
format: string;
export interface ParseMRZOptions {
autocorrect?: boolean;
}
declare function parseMRZ(inputLines: string | string[], options?: ParseMRZOptions): {
format: "TD1" | "TD2" | "TD3" | "SWISS_DRIVING_LICENSE" | "FRENCH_NATIONAL_ID";
details: import("./createFieldParser").Details[];

@@ -4,0 +7,0 @@ fields: Record<string, string | null>;

'use strict';
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const formats_1 = __importDefault(require("../formats"));
const checkLines_1 = __importDefault(require("./checkLines"));
const checkLines_1 = require("./checkLines");
const parsers_1 = require("./parsers");
function parseMRZ(lines) {
const result = (0, checkLines_1.default)(lines);
switch (result.length) {
function parseMRZ(inputLines, options = {}) {
const lines = (0, checkLines_1.checkLines)(inputLines);
switch (lines.length) {
case 2:
case 3: {
switch (result[0].length) {
switch (lines[0].length) {
case 30:
return parsers_1.parsers.td1(lines);
return parsers_1.parsers.td1(lines, options);
case 36: {
const endLine1 = lines[0].substr(30, 36);
const endLine1 = lines[0].substring(30, 66);
if (endLine1.match(/[0-9]/)) {
return parsers_1.parsers.frenchNationalId(lines);
return parsers_1.parsers.frenchNationalId(lines, options);
}
else {
return parsers_1.parsers.td2(lines);
return parsers_1.parsers.td2(lines, options);
}
}
case 44:
return parsers_1.parsers.td3(lines);
return parsers_1.parsers.td3(lines, options);
case 9:
return parsers_1.parsers.swissDrivingLicense(lines);
return parsers_1.parsers.swissDrivingLicense(lines, options);
default:
throw new Error('unrecognized document format. First line of input must have 30 (TD1), 36 (TD2 or French National Id), 44 (TD3) or 9 (Swiss Driving License) characters');
throw new Error(`unrecognized document format. First line of input must have 30 (TD1), 36 (TD2 or French National Id), 44 (TD3) or 9 (Swiss Driving License) characters and it has a length of ${lines[0].length}`);
}
}
default:
throw new Error(`unrecognized document format. Input must have two or three lines, found${lines.length}`);
throw new Error(`unrecognized document format. Input must have two or three lines, found ${lines.length}`);
}
}
for (const format in formats_1.default) {
parseMRZ[format] = parsers_1.parsers[format];
}
exports.default = parseMRZ;
//# sourceMappingURL=parse.js.map

@@ -1,3 +0,4 @@

export default function parseSwissDrivingLicense(lines: string | string[]): {
format: string;
import { ParseMRZOptions } from './parse';
export default function parseSwissDrivingLicense(lines: string[], options: ParseMRZOptions): {
format: "TD1" | "TD2" | "TD3" | "SWISS_DRIVING_LICENSE" | "FRENCH_NATIONAL_ID";
details: import("./createFieldParser").Details[];

@@ -4,0 +5,0 @@ fields: Record<string, string | null>;

@@ -6,24 +6,22 @@ 'use strict';

Object.defineProperty(exports, "__esModule", { value: true });
const formats_1 = __importDefault(require("../formats"));
const checkLines_1 = __importDefault(require("./checkLines"));
const getResult_1 = __importDefault(require("./getResult"));
const formats_1 = require("../formats");
const getResult_1 = require("./getResult");
const swissDrivingLicenseFields_1 = __importDefault(require("./swissDrivingLicenseFields"));
const SWISS_DRIVING_LICENSE = formats_1.default.SWISS_DRIVING_LICENSE;
function parseSwissDrivingLicense(lines) {
const result = (0, checkLines_1.default)(lines);
if (result.length !== 3) {
throw new Error(`invalid number of lines: ${result.length}: Must be 3 for ${SWISS_DRIVING_LICENSE}`);
const SWISS_DRIVING_LICENSE = formats_1.formats.SWISS_DRIVING_LICENSE;
function parseSwissDrivingLicense(lines, options) {
if (lines.length !== 3) {
throw new Error(`invalid number of lines: ${lines.length}: Must be 3 for ${SWISS_DRIVING_LICENSE}`);
}
if (result[0].length !== 9) {
throw new Error(`invalid number of characters for line 1: ${result[0].length}. Must be 9 for ${SWISS_DRIVING_LICENSE}`);
if (lines[0].length !== 9) {
throw new Error(`invalid number of characters for line 1: ${lines[0].length}. Must be 9 for ${SWISS_DRIVING_LICENSE}`);
}
if (result[1].length !== 30) {
throw new Error(`invalid number of characters for line 2: ${result[1].length}. Must be 30 for ${SWISS_DRIVING_LICENSE}`);
if (lines[1].length !== 30) {
throw new Error(`invalid number of characters for line 2: ${lines[1].length}. Must be 30 for ${SWISS_DRIVING_LICENSE}`);
}
if (result[2].length !== 30) {
throw new Error(`invalid number of characters for line 3: ${result[2].length}. Must be 30 for ${SWISS_DRIVING_LICENSE}`);
if (lines[2].length !== 30) {
throw new Error(`invalid number of characters for line 3: ${lines[2].length}. Must be 30 for ${SWISS_DRIVING_LICENSE}`);
}
return (0, getResult_1.default)(SWISS_DRIVING_LICENSE, lines, swissDrivingLicenseFields_1.default);
return (0, getResult_1.getResult)(SWISS_DRIVING_LICENSE, lines, swissDrivingLicenseFields_1.default, options);
}
exports.default = parseSwissDrivingLicense;
//# sourceMappingURL=swissDrivingLicense.js.map

@@ -1,3 +0,3 @@

declare const _default: import("./createFieldParser").ParseFunction[];
declare const _default: import("./createFieldParser").CreateFieldParserResult[];
export default _default;
//# sourceMappingURL=swissDrivingLicenseFields.d.ts.map
'use strict';
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -12,3 +35,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

const parseLanguageCode_1 = __importDefault(require("../parsers/swissDrivingLicense/parseLanguageCode"));
const createFieldParser_1 = require("./createFieldParser");
const createFieldParser_1 = __importStar(require("./createFieldParser"));
const fieldTemplates_1 = require("./fieldTemplates");

@@ -30,2 +53,3 @@ exports.default = [

parser: parseLanguageCode_1.default,
type: createFieldParser_1.fieldTypes.ALPHABETIC,
},

@@ -53,2 +77,3 @@ {

parser: parseNumber_1.parseNumber,
type: createFieldParser_1.fieldTypes.NUMERIC,
},

@@ -62,2 +87,3 @@ {

parser: parseNumber_1.parseNumber,
type: createFieldParser_1.fieldTypes.NUMERIC,
},

@@ -83,3 +109,3 @@ {

{ ...fieldTemplates_1.firstNameTemplate, line: 2, start: 0, end: 30 },
].map(createFieldParser_1.createFieldParser);
].map(createFieldParser_1.default);
//# sourceMappingURL=swissDrivingLicenseFields.js.map

@@ -1,3 +0,4 @@

export default function parseTD1(lines: string | string[]): {
format: string;
import { ParseMRZOptions } from './parse';
export default function parseTD1(lines: string[], options: ParseMRZOptions): {
format: "TD1" | "TD2" | "TD3" | "SWISS_DRIVING_LICENSE" | "FRENCH_NATIONAL_ID";
details: import("./createFieldParser").Details[];

@@ -4,0 +5,0 @@ fields: Record<string, string | null>;

@@ -6,13 +6,11 @@ 'use strict';

Object.defineProperty(exports, "__esModule", { value: true });
const formats_1 = __importDefault(require("../formats"));
const checkLines_1 = __importDefault(require("./checkLines"));
const getResult_1 = __importDefault(require("./getResult"));
const formats_1 = require("../formats");
const getResult_1 = require("./getResult");
const td1Fields_1 = __importDefault(require("./td1Fields"));
const TD1 = formats_1.default.TD1;
function parseTD1(lines) {
const result = (0, checkLines_1.default)(lines);
if (result.length !== 3) {
throw new Error(`invalid number of lines: ${result.length}: Must be 3 for ${TD1}`);
const TD1 = formats_1.formats.TD1;
function parseTD1(lines, options) {
if (lines.length !== 3) {
throw new Error(`invalid number of lines: ${lines.length}: Must be 3 for ${TD1}`);
}
result.forEach((line, index) => {
lines.forEach((line, index) => {
if (line.length !== 30) {

@@ -22,5 +20,5 @@ throw new Error(`invalid number of characters for line ${index + 1}: ${line.length}. Must be 30 for ${TD1}`);

});
return (0, getResult_1.default)(TD1, lines, td1Fields_1.default);
return (0, getResult_1.getResult)(TD1, lines, td1Fields_1.default, options);
}
exports.default = parseTD1;
//# sourceMappingURL=td1.js.map

@@ -1,3 +0,3 @@

declare const _default: import("./createFieldParser").ParseFunction[];
declare const _default: import("./createFieldParser").CreateFieldParserResult[];
export default _default;
//# sourceMappingURL=td1Fields.d.ts.map

@@ -9,3 +9,3 @@ 'use strict';

const parseOptional_1 = require("../parsers/parseOptional");
const createFieldParser_1 = require("./createFieldParser");
const createFieldParser_1 = __importDefault(require("./createFieldParser"));
const fieldTemplates_1 = require("./fieldTemplates");

@@ -39,3 +39,4 @@ exports.default = [

},
Object.assign(fieldTemplates_1.documentNumberCheckDigitTemplate, {
{
...fieldTemplates_1.documentNumberCheckDigitTemplate,
line: 0,

@@ -56,3 +57,3 @@ start: 14,

],
}),
},
{

@@ -146,3 +147,3 @@ label: 'Optional field 1',

{ ...fieldTemplates_1.firstNameTemplate, line: 2, start: 0, end: 30 },
].map(createFieldParser_1.createFieldParser);
].map(createFieldParser_1.default);
//# sourceMappingURL=td1Fields.js.map

@@ -1,3 +0,4 @@

export default function parseTD2(lines: string | string[]): {
format: string;
import { ParseMRZOptions } from './parse';
export default function parseTD2(lines: string[], options: ParseMRZOptions): {
format: "TD1" | "TD2" | "TD3" | "SWISS_DRIVING_LICENSE" | "FRENCH_NATIONAL_ID";
details: import("./createFieldParser").Details[];

@@ -4,0 +5,0 @@ fields: Record<string, string | null>;

@@ -6,13 +6,11 @@ 'use strict';

Object.defineProperty(exports, "__esModule", { value: true });
const formats_1 = __importDefault(require("../formats"));
const checkLines_1 = __importDefault(require("./checkLines"));
const getResult_1 = __importDefault(require("./getResult"));
const formats_1 = require("../formats");
const getResult_1 = require("./getResult");
const td2Fields_1 = __importDefault(require("./td2Fields"));
const TD2 = formats_1.default.TD2;
function parseTD2(lines) {
const result = (0, checkLines_1.default)(lines);
if (result.length !== 2) {
throw new Error(`invalid number of lines: ${result.length}: Must be 2 for ${TD2}`);
const TD2 = formats_1.formats.TD2;
function parseTD2(lines, options) {
if (lines.length !== 2) {
throw new Error(`invalid number of lines: ${lines.length}: Must be 2 for ${TD2}`);
}
result.forEach((line, index) => {
lines.forEach((line, index) => {
if (line.length !== 36) {

@@ -22,5 +20,5 @@ throw new Error(`invalid number of characters for line ${index + 1}: ${line.length}. Must be 36 for TD2`);

});
return (0, getResult_1.default)(TD2, lines, td2Fields_1.default);
return (0, getResult_1.getResult)(TD2, lines, td2Fields_1.default, options);
}
exports.default = parseTD2;
//# sourceMappingURL=td2.js.map

@@ -1,3 +0,3 @@

declare const _default: import("./createFieldParser").ParseFunction[];
declare const _default: import("./createFieldParser").CreateFieldParserResult[];
export default _default;
//# sourceMappingURL=td2Fields.d.ts.map

@@ -8,3 +8,3 @@ 'use strict';

const parseOptional_1 = require("../parsers/parseOptional");
const createFieldParser_1 = require("./createFieldParser");
const createFieldParser_1 = __importDefault(require("./createFieldParser"));
const fieldTemplates_1 = require("./fieldTemplates");

@@ -119,3 +119,3 @@ exports.default = [

},
].map(createFieldParser_1.createFieldParser);
].map(createFieldParser_1.default);
//# sourceMappingURL=td2Fields.js.map

@@ -1,3 +0,4 @@

export default function parseTD3(lines: string | string[]): {
format: string;
import { ParseMRZOptions } from './parse';
export default function parseTD3(lines: string[], options: ParseMRZOptions): {
format: "TD1" | "TD2" | "TD3" | "SWISS_DRIVING_LICENSE" | "FRENCH_NATIONAL_ID";
details: import("./createFieldParser").Details[];

@@ -4,0 +5,0 @@ fields: Record<string, string | null>;

@@ -6,13 +6,11 @@ 'use strict';

Object.defineProperty(exports, "__esModule", { value: true });
const formats_1 = __importDefault(require("../formats"));
const checkLines_1 = __importDefault(require("./checkLines"));
const getResult_1 = __importDefault(require("./getResult"));
const formats_1 = require("../formats");
const getResult_1 = require("./getResult");
const td3Fields_1 = __importDefault(require("./td3Fields"));
const TD3 = formats_1.default.TD3;
function parseTD3(lines) {
const result = (0, checkLines_1.default)(lines);
if (result.length !== 2) {
throw new Error(`invalid number of lines: ${result.length}: Must be 2 for ${TD3}`);
const TD3 = formats_1.formats.TD3;
function parseTD3(lines, options) {
if (lines.length !== 2) {
throw new Error(`invalid number of lines: ${lines.length}: Must be 2 for ${TD3}`);
}
result.forEach((line, index) => {
lines.forEach((line, index) => {
if (line.length !== 44) {

@@ -22,5 +20,5 @@ throw new Error(`invalid number of characters for line ${index + 1}: ${line.length}. Must be 44 for TD3`);

});
return (0, getResult_1.default)(TD3, lines, td3Fields_1.default);
return (0, getResult_1.getResult)(TD3, lines, td3Fields_1.default, options);
}
exports.default = parseTD3;
//# sourceMappingURL=td3.js.map

@@ -1,3 +0,3 @@

declare const _default: import("./createFieldParser").ParseFunction[];
declare const _default: import("./createFieldParser").CreateFieldParserResult[];
export default _default;
//# sourceMappingURL=td3Fields.d.ts.map

@@ -9,3 +9,3 @@ 'use strict';

const parsePersonalNumberCheckDigit_1 = require("../parsers/parsePersonalNumberCheckDigit");
const createFieldParser_1 = require("./createFieldParser");
const createFieldParser_1 = __importDefault(require("./createFieldParser"));
const fieldTemplates_1 = require("./fieldTemplates");

@@ -113,3 +113,3 @@ exports.default = [

},
].map(createFieldParser_1.createFieldParser);
].map(createFieldParser_1.default);
//# sourceMappingURL=td3Fields.js.map
{
"name": "mrz",
"version": "3.2.1",
"version": "3.3.0",
"description": "Parse MRZ (Machine Readable Zone) from identity documents",

@@ -52,11 +52,11 @@ "main": "./lib/index.js",

"@babel/preset-typescript": "^7.18.6",
"@types/jest": "^29.2.5",
"@types/jest": "^29.2.6",
"cheminfo-build": "^1.1.11",
"eslint": "^8.31.0",
"eslint": "^8.32.0",
"eslint-config-cheminfo-typescript": "^11.2.2",
"jest": "^29.3.1",
"prettier": "^2.8.1",
"rimraf": "^3.0.2",
"jest": "^29.4.0",
"prettier": "^2.8.3",
"rimraf": "^4.1.2",
"typescript": "^4.9.4"
}
}
'use strict';
const formats = {
export type FormatType = keyof typeof formats;
export const formats = {
TD1: 'TD1',

@@ -9,5 +11,2 @@ TD2: 'TD2',

FRENCH_NATIONAL_ID: 'FRENCH_NATIONAL_ID',
};
Object.freeze(formats);
export default formats;
} as const;
'use strict';
import formats from './formats';
import { formats } from './formats';
import states from './generated/states';

@@ -5,0 +5,0 @@ import parse from './parse/parse';

@@ -11,3 +11,4 @@ 'use strict';

];
let result = parse(MRZ);
const result = parse(MRZ);
expect(result.format).toBe('FRENCH_NATIONAL_ID');

@@ -32,2 +33,42 @@ // expect(result.valid).toStrictEqual(true);

});
it('Use autocorrect', () => {
const MRZ = [
'IDFRATEST<NAME<<<<<<<<<<<<<<<<0CHE02',
'1710GVA123451ROBERTA<<<<<<<9112311F2',
];
const falseMRZ = [
'1DFRATE5T<NAME<<<<<<<<<<<<<<<<0CHE02',
'I7I0GVA123451RO8ERTA<<<<<<<9IIZ3IIF2',
];
const result = parse(MRZ);
const correctedResult = parse(falseMRZ, { autocorrect: true });
expect(result.fields).toStrictEqual(correctedResult.fields);
expect(
correctedResult.details.map(({ autocorrect }) => autocorrect),
).toStrictEqual([
[{ line: 0, column: 0, original: '1', corrected: 'I' }],
[],
[{ line: 0, column: 7, original: '5', corrected: 'S' }],
[],
[
{ line: 1, column: 0, original: 'I', corrected: '1' },
{ line: 1, column: 2, original: 'I', corrected: '1' },
],
[],
[],
[],
[{ line: 1, column: 15, original: '8', corrected: 'B' }],
[
{ line: 1, column: 28, original: 'I', corrected: '1' },
{ line: 1, column: 29, original: 'I', corrected: '1' },
{ line: 1, column: 30, original: 'Z', corrected: '2' },
{ line: 1, column: 32, original: 'I', corrected: '1' },
],
[{ line: 1, column: 33, original: 'I', corrected: '1' }],
[],
[],
]);
});
});

@@ -12,3 +12,4 @@ 'use strict';

];
let result = parse(MRZ);
const result = parse(MRZ);
expect(result.format).toBe('SWISS_DRIVING_LICENSE');

@@ -26,2 +27,3 @@ expect(result.valid).toBe(true);

valid: true,
autocorrect: [],
});

@@ -44,2 +46,3 @@ expect(result.details[result.details.length - 1]).toStrictEqual({

end: 18,
autocorrect: [],
});

@@ -58,2 +61,46 @@ expect(result.fields).toStrictEqual({

});
it('Use autocorrect', () => {
const MRZ = [
'AAA001D<<',
'FACHE305142128097<<800126<<<<<',
'MARCHAND<<FABIENNE<<<<<<<<<<<<',
];
const falseMRZ = [
'AAA001D<<',
'FACHE30S142IZBO97<<8OO12G<<<<<',
'MARCHAND<<FA81ENNE<<<<<<<<<<<<',
];
const result = parse(MRZ);
const correctedResult = parse(falseMRZ, { autocorrect: true });
expect(result.fields).toStrictEqual(correctedResult.fields);
expect(
correctedResult.details.map(({ autocorrect }) => 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' },
],
[],
]);
});
});

@@ -120,2 +120,3 @@ 'use strict';

end: 18,
autocorrect: [],
});

@@ -148,2 +149,54 @@ expect(result.fields.documentNumber).toBe('D23145890124');

});
it('Use autocorrection', () => {
const data = [
'IDCHEA1234567<6<<<<<<<<<<<<<<<',
'7510256M2009018CHE<<<<<<<<<<<8',
'SMITH<<JOHN<ALBERT<<<<<<<<<<<<',
];
const falseData = [
'IDCHEA1234567<6<<<<<<<<<<<<<<<',
'7SIOZSGMZOO90IBCHE<<<<<<<<<<<B',
'5M1TH<<J0HN<AL8ERT<<<<<<<<<<<<',
];
const result = parse(data);
const correctedResult = parse(falseData, { autocorrect: true });
expect(result.fields).toStrictEqual(correctedResult.fields);
expect(
correctedResult.details.map(({ autocorrect }) => autocorrect),
).toStrictEqual([
[],
[],
[],
[],
[],
[
{ line: 1, column: 1, original: 'S', corrected: '5' },
{ line: 1, column: 2, original: 'I', corrected: '1' },
{ line: 1, column: 3, original: 'O', corrected: '0' },
{ line: 1, column: 4, original: 'Z', corrected: '2' },
{ line: 1, column: 5, original: 'S', corrected: '5' },
],
[{ line: 1, column: 6, original: 'G', corrected: '6' }],
[],
[
{ line: 1, column: 8, original: 'Z', corrected: '2' },
{ line: 1, column: 9, original: 'O', corrected: '0' },
{ line: 1, column: 10, original: 'O', corrected: '0' },
{ line: 1, column: 13, original: 'I', corrected: '1' },
],
[{ line: 1, column: 14, original: 'B', corrected: '8' }],
[],
[],
[{ line: 1, column: 29, original: 'B', corrected: '8' }],
[
{ 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' },
],
[],
]);
});
});

@@ -37,2 +37,47 @@ 'use strict';

});
it('Use autocorrect', () => {
const MRZ = [
'I<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<',
'D231458907UTO7408122F1204159<<<<<<<6',
];
const falseMRZ = [
'I<UTOERIK55ON<<ANNA<MAR1A<<<<<<<<<<<',
'D231458907UT0740BIZZF12O4IS9<<<<<<<G',
];
const result = parse(MRZ);
const correctedResult = parse(falseMRZ, { autocorrect: true });
expect(result.fields).toStrictEqual(correctedResult.fields);
expect(
correctedResult.details.map(({ autocorrect }) => autocorrect),
).toStrictEqual([
[],
[],
[
{ 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: 1, column: 12, original: '0', corrected: 'O' }],
[
{ line: 1, column: 16, original: 'B', corrected: '8' },
{ line: 1, column: 17, original: 'I', corrected: '1' },
{ line: 1, column: 18, original: 'Z', corrected: '2' },
],
[{ line: 1, column: 19, original: 'Z', corrected: '2' }],
[],
[
{ line: 1, column: 23, original: 'O', corrected: '0' },
{ line: 1, column: 25, original: 'I', corrected: '1' },
{ line: 1, column: 26, original: 'S', corrected: '5' },
],
[],
[],
[{ line: 1, column: 35, original: 'G', corrected: '6' }],
]);
});
});

@@ -50,2 +50,3 @@ 'use strict';

end: 37,
autocorrect: [],
});

@@ -63,2 +64,3 @@

error: 'invalid state code: UTO',
autocorrect: [],
});

@@ -119,2 +121,99 @@ });

});
it('CHN PO', () => {
// found on https://www.rfa.org/english/news/uyghur/return-09252019172016.html/uyghur-ablikim-abla-passport-crop.jpg/@@images/image
const MRZ = [
'POCHNABULIKEMU<<ABULA<<<<<<<<<<<<<<<<<<<<<<<',
'E596593216CHN9701078M2510077LAKCLCLMMBKGG932',
];
const result = parse(MRZ);
expect(result.valid).toBe(true);
expect(result.fields).toStrictEqual({
documentCode: 'PO',
issuingState: 'CHN',
lastName: 'ABULIKEMU',
firstName: 'ABULA',
documentNumber: 'E59659321',
documentNumberCheckDigit: '6',
nationality: 'CHN',
birthDate: '970107',
birthDateCheckDigit: '8',
sex: 'male',
expirationDate: '251007',
expirationDateCheckDigit: '7',
personalNumber: 'LAKCLCLMMBKGG9',
personalNumberCheckDigit: '3',
compositeCheckDigit: '2',
});
});
it('CHN PT', () => {
// found on https://upload.wikimedia.org/wikipedia/commons/a/a6/People%27s_Republic_of_China_Passport_%2897-2_version_for_Single_Exit_and_Entry%29.png
const MRZ = [
'PTCHNCESHI<<YANGBEN<<<<<<<<<<<<<<<<<<<<<<<<<',
'G622925996CHN8310291F1904220LCOCMKNENBPJB984',
];
const result = parse(MRZ);
expect(result.valid).toBe(true);
expect(result.fields).toStrictEqual({
documentCode: 'PT',
issuingState: 'CHN',
lastName: 'CESHI',
firstName: 'YANGBEN',
documentNumber: 'G62292599',
documentNumberCheckDigit: '6',
nationality: 'CHN',
birthDate: '831029',
birthDateCheckDigit: '1',
sex: 'female',
expirationDate: '190422',
expirationDateCheckDigit: '0',
personalNumber: 'LCOCMKNENBPJB9',
personalNumberCheckDigit: '8',
compositeCheckDigit: '4',
});
});
it('Use autocorrection', () => {
const MRZ = [
'P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<',
'L898902C36UTO7408122F1204159ZE184226B<<<<<10',
];
const falseMRZ = [
'P<UT0ERIK55ON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<',
'L898902C36UTO740BIZZF12041S9ZE184226B<<<<<1O',
];
const result = parse(MRZ);
const correctedResult = parse(falseMRZ, { autocorrect: true });
expect(result.fields).toStrictEqual(correctedResult.fields);
expect(
correctedResult.details.map(({ autocorrect }) => autocorrect),
).toStrictEqual([
[],
[{ line: 0, column: 4, original: '0', corrected: 'O' }],
[
{ line: 0, column: 9, original: '5', corrected: 'S' },
{ line: 0, column: 10, original: '5', corrected: 'S' },
],
[],
[],
[],
[],
[
{ line: 1, column: 16, original: 'B', corrected: '8' },
{ line: 1, column: 17, original: 'I', corrected: '1' },
{ line: 1, column: 18, original: 'Z', corrected: '2' },
],
[{ line: 1, column: 19, original: 'Z', corrected: '2' }],
[],
[{ line: 1, column: 26, original: 'S', corrected: '5' }],
[],
[],
[],
[{ line: 1, column: 43, original: 'O', corrected: '0' }],
]);
});
});
'use strict';
export default function checkLines(lines: string | string[]) {
export function checkLines(lines: string | string[]) {
if (typeof lines === 'string') {

@@ -5,0 +5,0 @@ lines = lines.split(/[\r\n]+/);

'use strict';
import { Autocorrect, autoCorrection } from './autoCorrection';
export interface Details {

@@ -13,2 +15,3 @@ label: string;

error?: string;
autocorrect: Autocorrect[];
}

@@ -24,3 +27,10 @@

type FieldOptions = {
type FieldTypes = keyof typeof fieldTypes;
export const fieldTypes = {
NUMERIC: 'NUMERIC',
ALPHABETIC: 'ALPHABETIC',
ALPHANUMERIC: 'ALPHANUMERIC',
} as const;
export type FieldOptions = {
label: string;

@@ -33,2 +43,3 @@ field: string | null;

related?: Range[];
type?: FieldTypes;
};

@@ -40,4 +51,14 @@ interface Range {

}
export type ParseFunction = (lines: string | string[]) => Details;
export function createFieldParser(fieldOptions: FieldOptions): ParseFunction {
export interface CreateFieldParserResult {
parser: (lines: string[], autocorrect?: Autocorrect[]) => Details;
autocorrector: (lines: string[]) => {
correctedLines: string[];
autocorrect: Autocorrect[];
};
}
export default function createFieldParser(
fieldOptions: FieldOptions,
): CreateFieldParserResult {
checkType(fieldOptions, 'label', 'string');

@@ -67,4 +88,3 @@ if (fieldOptions.field !== null) {

}
return function parseField(lines: string | string[]) {
const parser = (lines: string[], autocorrect: Autocorrect[] = []) => {
const source = getText(lines, fieldOptions);

@@ -87,2 +107,3 @@ const related = fieldOptions.related || [];

end: 0,
autocorrect,
};

@@ -106,5 +127,22 @@ const range = result.ranges[0];

};
const autocorrector = (lines: string[]) => {
let corrected = lines;
let source = getText(lines, fieldOptions);
let autocorrect: Autocorrect[] = [];
const type = fieldOptions.type || fieldTypes.ALPHANUMERIC;
if (type !== fieldTypes.ALPHANUMERIC) {
const result = autoCorrection(source, fieldOptions);
source = result.correctedLine;
autocorrect = result.autocorrect;
}
corrected = changeText(lines, fieldOptions, source);
return { correctedLines: corrected, autocorrect };
};
return { parser, autocorrector };
}
function getText(lines: string | string[], options) {
function getText(
lines: string | string[],
options: Pick<FieldOptions, 'line' | 'end' | 'start'>,
) {
const line = lines[options.line];

@@ -114,2 +152,14 @@ 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(

@@ -116,0 +166,0 @@ options: object,

@@ -13,2 +13,4 @@ 'use strict';

import { fieldTypes } from './createFieldParser';
const documentNumberTemplate = {

@@ -18,2 +20,3 @@ label: 'Document number',

parser: parseDocumentNumber,
type: fieldTypes.ALPHANUMERIC,
};

@@ -25,2 +28,3 @@

parser: parseDocumentNumberCheckDigit,
type: fieldTypes.NUMERIC,
};

@@ -31,2 +35,3 @@

field: 'documentCode',
type: fieldTypes.ALPHABETIC,
};

@@ -38,2 +43,3 @@

parser: parseState,
type: fieldTypes.ALPHABETIC,
};

@@ -45,2 +51,3 @@

parser: parseSex,
type: fieldTypes.ALPHABETIC,
};

@@ -52,2 +59,3 @@

parser: parseDate,
type: fieldTypes.NUMERIC,
};

@@ -59,2 +67,3 @@

parser: parseDateCheckDigit,
type: fieldTypes.NUMERIC,
};

@@ -66,2 +75,3 @@

parser: parseCompositeCheckDigit,
type: fieldTypes.NUMERIC,
};

@@ -73,2 +83,3 @@

parser: parseDate,
type: fieldTypes.NUMERIC,
};

@@ -80,2 +91,3 @@

parser: parseDateCheckDigit,
type: fieldTypes.NUMERIC,
};

@@ -87,2 +99,3 @@

parser: parseDate,
type: fieldTypes.NUMERIC,
};

@@ -94,2 +107,3 @@

parser: parseFirstName,
type: fieldTypes.ALPHABETIC,
};

@@ -101,2 +115,3 @@

parser: parseLastName,
type: fieldTypes.ALPHABETIC,
};

@@ -108,2 +123,3 @@

parser: parseState,
type: fieldTypes.ALPHABETIC,
};

@@ -110,0 +126,0 @@

'use strict';
import formats from '../formats';
import { formats } from '../formats';
import checkLines from './checkLines';
import frenchNationalIdFields from './frenchNationalIdFields';
import getResult from './getResult';
import { getResult } from './getResult';
import { ParseMRZOptions } from './parse';
const FRENCH_NATIONAL_ID = formats.FRENCH_NATIONAL_ID;
export default function parseFrenchNationalId(lines: string | string[]) {
const result = checkLines(lines);
if (result.length !== 2) {
export default function parseFrenchNationalId(
lines: string[],
options: ParseMRZOptions,
) {
if (lines.length !== 2) {
throw new Error(
`invalid number of lines: ${result.length}: Must be 2 for ${FRENCH_NATIONAL_ID}`,
`invalid number of lines: ${lines.length}: Must be 2 for ${FRENCH_NATIONAL_ID}`,
);
}
result.forEach((line, index) => {
lines.forEach((line, index) => {
if (line.length !== 36) {

@@ -26,3 +28,3 @@ throw new Error(

});
return getResult(FRENCH_NATIONAL_ID, lines, frenchNationalIdFields);
return getResult(FRENCH_NATIONAL_ID, lines, frenchNationalIdFields, options);
}

@@ -7,3 +7,3 @@ 'use strict';

import { createFieldParser } from './createFieldParser';
import createFieldParser from './createFieldParser';
import {

@@ -10,0 +10,0 @@ documentCodeTemplate,

'use strict';
import { ParseFunction, Details } from './createFieldParser';
import { FormatType } from '../formats';
function getDetails(lines: string | string[], fieldParsers: ParseFunction[]) {
import { Autocorrect } from './autoCorrection';
import { Details, CreateFieldParserResult } from './createFieldParser';
import { ParseMRZOptions } from './parse';
function getDetails(
lines: string[],
fieldParsers: CreateFieldParserResult[],
autocorrectArray: Autocorrect[][],
) {
const details: Details[] = [];
for (const parser of fieldParsers) {
details.push(parser(lines));
}
fieldParsers.forEach(({ parser }, i) => {
details.push(parser(lines, autocorrectArray[i]));
});
return details;

@@ -25,8 +33,33 @@ }

export default function getResult(
format: string,
lines: string | string[],
fieldParsers: ParseFunction[],
function getCorrection(
lines: string[],
fieldParsers: CreateFieldParserResult[],
autocorrect: boolean,
) {
const details = getDetails(lines, fieldParsers);
let corrected = lines;
const autocorrectArray: Autocorrect[][] = [];
if (autocorrect) {
fieldParsers.forEach(({ autocorrector }) => {
const result = autocorrector(corrected);
autocorrectArray.push(result.autocorrect);
corrected = result.correctedLines;
});
}
return { corrected, autocorrectArray };
}
export function getResult(
format: FormatType,
lines: string[],
fieldParsers: CreateFieldParserResult[],
options: ParseMRZOptions,
) {
const { autocorrect = false } = options;
const { corrected, autocorrectArray } = getCorrection(
lines,
fieldParsers,
autocorrect,
);
const details = getDetails(corrected, fieldParsers, autocorrectArray);
const fields = getFields(details);

@@ -33,0 +66,0 @@ const result = {

'use strict';
import formats from '../formats';
import checkLines from './checkLines';
import { checkLines } from './checkLines';
import { parsers } from './parsers';
function parseMRZ(lines: string | string[]) {
const result = checkLines(lines);
switch (result.length) {
export interface ParseMRZOptions {
autocorrect?: boolean;
}
function parseMRZ(
inputLines: string | string[],
options: ParseMRZOptions = {},
) {
const lines = checkLines(inputLines);
switch (lines.length) {
case 2:
case 3: {
switch (result[0].length) {
switch (lines[0].length) {
case 30:
return parsers.td1(lines);
return parsers.td1(lines, options);
case 36: {
const endLine1 = lines[0].substr(30, 36);
const endLine1 = lines[0].substring(30, 66);
if (endLine1.match(/[0-9]/)) {
return parsers.frenchNationalId(lines);
return parsers.frenchNationalId(lines, options);
} else {
return parsers.td2(lines);
return parsers.td2(lines, options);
}
}
case 44:
return parsers.td3(lines);
return parsers.td3(lines, options);
case 9:
return parsers.swissDrivingLicense(lines);
return parsers.swissDrivingLicense(lines, options);
default:
throw new Error(
'unrecognized document format. First line of input must have 30 (TD1), 36 (TD2 or French National Id), 44 (TD3) or 9 (Swiss Driving License) characters',
`unrecognized document format. First line of input must have 30 (TD1), 36 (TD2 or French National Id), 44 (TD3) or 9 (Swiss Driving License) characters and it has a length of ${lines[0].length}`,
);

@@ -36,3 +40,3 @@ }

throw new Error(
`unrecognized document format. Input must have two or three lines, found${lines.length}`,
`unrecognized document format. Input must have two or three lines, found ${lines.length}`,
);

@@ -42,6 +46,2 @@ }

for (const format in formats) {
parseMRZ[format] = parsers[format];
}
export default parseMRZ;
'use strict';
import formats from '../formats';
import { formats } from '../formats';
import checkLines from './checkLines';
import getResult from './getResult';
import { getResult } from './getResult';
import { ParseMRZOptions } from './parse';
import swissDrivingLicenseFields from './swissDrivingLicenseFields';
const SWISS_DRIVING_LICENSE = formats.SWISS_DRIVING_LICENSE;
export default function parseSwissDrivingLicense(lines: string | string[]) {
const result = checkLines(lines);
if (result.length !== 3) {
export default function parseSwissDrivingLicense(
lines: string[],
options: ParseMRZOptions,
) {
if (lines.length !== 3) {
throw new Error(
`invalid number of lines: ${result.length}: Must be 3 for ${SWISS_DRIVING_LICENSE}`,
`invalid number of lines: ${lines.length}: Must be 3 for ${SWISS_DRIVING_LICENSE}`,
);
}
if (result[0].length !== 9) {
if (lines[0].length !== 9) {
throw new Error(
`invalid number of characters for line 1: ${result[0].length}. Must be 9 for ${SWISS_DRIVING_LICENSE}`,
`invalid number of characters for line 1: ${lines[0].length}. Must be 9 for ${SWISS_DRIVING_LICENSE}`,
);
}
if (result[1].length !== 30) {
if (lines[1].length !== 30) {
throw new Error(
`invalid number of characters for line 2: ${result[1].length}. Must be 30 for ${SWISS_DRIVING_LICENSE}`,
`invalid number of characters for line 2: ${lines[1].length}. Must be 30 for ${SWISS_DRIVING_LICENSE}`,
);
}
if (result[2].length !== 30) {
if (lines[2].length !== 30) {
throw new Error(
`invalid number of characters for line 3: ${result[2].length}. Must be 30 for ${SWISS_DRIVING_LICENSE}`,
`invalid number of characters for line 3: ${lines[2].length}. Must be 30 for ${SWISS_DRIVING_LICENSE}`,
);
}
return getResult(SWISS_DRIVING_LICENSE, lines, swissDrivingLicenseFields);
return getResult(
SWISS_DRIVING_LICENSE,
lines,
swissDrivingLicenseFields,
options,
);
}

@@ -10,3 +10,3 @@ 'use strict';

import { createFieldParser } from './createFieldParser';
import createFieldParser, { fieldTypes } from './createFieldParser';
import {

@@ -36,2 +36,3 @@ documentNumberTemplate,

parser: parseLanguageCode,
type: fieldTypes.ALPHABETIC,
},

@@ -59,2 +60,3 @@ {

parser: parseNumber,
type: fieldTypes.NUMERIC,
},

@@ -68,2 +70,3 @@ {

parser: parseNumber,
type: fieldTypes.NUMERIC,
},

@@ -70,0 +73,0 @@ {

'use strict';
import formats from '../formats';
import { formats } from '../formats';
import checkLines from './checkLines';
import getResult from './getResult';
import { getResult } from './getResult';
import { ParseMRZOptions } from './parse';
import TD1Fields from './td1Fields';
const TD1 = formats.TD1;
export default function parseTD1(lines: string | string[]) {
const result = checkLines(lines);
if (result.length !== 3) {
export default function parseTD1(lines: string[], options: ParseMRZOptions) {
if (lines.length !== 3) {
throw new Error(
`invalid number of lines: ${result.length}: Must be 3 for ${TD1}`,
`invalid number of lines: ${lines.length}: Must be 3 for ${TD1}`,
);
}
result.forEach((line: string, index) => {
lines.forEach((line: string, index) => {
if (line.length !== 30) {

@@ -26,3 +25,3 @@ throw new Error(

});
return getResult(TD1, lines, TD1Fields);
return getResult(TD1, lines, TD1Fields, options);
}

@@ -7,3 +7,3 @@ 'use strict';

import { createFieldParser } from './createFieldParser';
import createFieldParser from './createFieldParser';
import {

@@ -52,3 +52,4 @@ documentCodeTemplate,

},
Object.assign(documentNumberCheckDigitTemplate, {
{
...documentNumberCheckDigitTemplate,
line: 0,

@@ -69,3 +70,3 @@ start: 14,

],
}),
},
{

@@ -72,0 +73,0 @@ label: 'Optional field 1',

'use strict';
import formats from '../formats';
import { formats } from '../formats';
import checkLines from './checkLines';
import getResult from './getResult';
import { getResult } from './getResult';
import { ParseMRZOptions } from './parse';
import TD2Fields from './td2Fields';
const TD2 = formats.TD2;
export default function parseTD2(lines: string | string[]) {
const result = checkLines(lines);
if (result.length !== 2) {
export default function parseTD2(lines: string[], options: ParseMRZOptions) {
if (lines.length !== 2) {
throw new Error(
`invalid number of lines: ${result.length}: Must be 2 for ${TD2}`,
`invalid number of lines: ${lines.length}: Must be 2 for ${TD2}`,
);
}
result.forEach((line, index) => {
lines.forEach((line, index) => {
if (line.length !== 36) {

@@ -26,3 +25,3 @@ throw new Error(

});
return getResult(TD2, lines, TD2Fields);
return getResult(TD2, lines, TD2Fields, options);
}

@@ -6,3 +6,3 @@ 'use strict';

import { createFieldParser } from './createFieldParser';
import createFieldParser from './createFieldParser';
import {

@@ -9,0 +9,0 @@ documentCodeTemplate,

'use strict';
import formats from '../formats';
import { formats } from '../formats';
import checkLines from './checkLines';
import getResult from './getResult';
import { getResult } from './getResult';
import { ParseMRZOptions } from './parse';
import TD3Fields from './td3Fields';
const TD3 = formats.TD3;
export default function parseTD3(lines: string | string[]) {
const result = checkLines(lines);
if (result.length !== 2) {
export default function parseTD3(lines: string[], options: ParseMRZOptions) {
if (lines.length !== 2) {
throw new Error(
`invalid number of lines: ${result.length}: Must be 2 for ${TD3}`,
`invalid number of lines: ${lines.length}: Must be 2 for ${TD3}`,
);
}
result.forEach((line, index) => {
lines.forEach((line, index) => {
if (line.length !== 44) {

@@ -26,3 +25,3 @@ throw new Error(

});
return getResult(TD3, lines, TD3Fields);
return getResult(TD3, lines, TD3Fields, options);
}

@@ -7,3 +7,3 @@ 'use strict';

import { createFieldParser } from './createFieldParser';
import createFieldParser from './createFieldParser';
import {

@@ -10,0 +10,0 @@ documentCodeTemplate,

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc