Skip to content

Commit

Permalink
feat: improve and export types (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
stropitek authored Feb 16, 2024
1 parent 62e3606 commit 66c31bc
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 94 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ import states from './generated/states';
import parse from './parse/parse';

export { formats, states, parse };
export * from './types';
8 changes: 2 additions & 6 deletions src/parse/autoCorrection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

import { Autocorrect } from '../types';

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

const numberToLetterMismatches = {
Expand All @@ -18,12 +20,6 @@ const letterToNumberMismatches = {
S: '5',
Z: '2',
};
export interface Autocorrect {
line: number;
column: number;
original: string;
corrected: string;
}
export function letterToNumber(char: string): string {
if (letterToNumberMismatches[char]) {
return letterToNumberMismatches[char];
Expand Down
22 changes: 3 additions & 19 deletions src/parse/createFieldParser.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
'use strict';

import { Autocorrect, autoCorrection } from './autoCorrection';
import { Autocorrect, Details, FieldName, Range } from '../types';

export interface Details {
label: string;
field: string | null;
value: string | null;
valid: boolean;
ranges: Range[];
line: number;
start: number;
end: number;
error?: string;
autocorrect: Autocorrect[];
}
import { autoCorrection } from './autoCorrection';

interface ParseResult {
value: string;
Expand All @@ -32,19 +21,14 @@ export const fieldTypes = {

export interface FieldOptions {
label: string;
field: string | null;
field: FieldName | null;
line: number;
start: number;
end: number;
parser: Parser;
related?: Range[];
type?: FieldTypes;
}
interface Range {
line: number;
start: number;
end: number;
}

export interface CreateFieldParserResult {
parser: (lines: string[], autocorrect: Autocorrect[]) => Details;
Expand Down
32 changes: 17 additions & 15 deletions src/parse/fieldTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,104 +10,106 @@ import parseLastName from '../parsers/parseLastName';
import parseSex from '../parsers/parseSex';
import parseState from '../parsers/parseState';

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

type FieldOptionTemplate = Partial<FieldOptions>;

const documentNumberTemplate = {
label: 'Document number',
field: 'documentNumber',
parser: parseDocumentNumber,
type: fieldTypes.ALPHANUMERIC,
};
} satisfies FieldOptionTemplate;

const documentNumberCheckDigitTemplate = {
label: 'Document number check digit',
field: 'documentNumberCheckDigit',
parser: parseDocumentNumberCheckDigit,
type: fieldTypes.NUMERIC,
};
} satisfies FieldOptionTemplate;

const documentCodeTemplate = {
label: 'Document code',
field: 'documentCode',
type: fieldTypes.ALPHABETIC,
};
} satisfies FieldOptionTemplate;

const nationalityTemplate = {
label: 'Nationality',
field: 'nationality',
parser: parseState,
type: fieldTypes.ALPHABETIC,
};
} satisfies FieldOptionTemplate;

const sexTemplate = {
label: 'Sex',
field: 'sex',
parser: parseSex,
type: fieldTypes.ALPHABETIC,
};
} satisfies FieldOptionTemplate;

const expirationDateTemplate = {
label: 'Expiration date',
field: 'expirationDate',
parser: parseDate,
type: fieldTypes.NUMERIC,
};
} satisfies FieldOptionTemplate;

const expirationDateCheckDigitTemplate = {
label: 'Expiration date check digit',
field: 'expirationDateCheckDigit',
parser: parseDateCheckDigit,
type: fieldTypes.NUMERIC,
};
} satisfies FieldOptionTemplate;

const compositeCheckDigitTemplate = {
label: 'Composite check digit',
field: 'compositeCheckDigit',
parser: parseCompositeCheckDigit,
type: fieldTypes.NUMERIC,
};
} satisfies FieldOptionTemplate;

const birthDateTemplate = {
label: 'Birth date',
field: 'birthDate',
parser: parseDate,
type: fieldTypes.NUMERIC,
};
} satisfies FieldOptionTemplate;

const birthDateCheckDigitTemplate = {
label: 'Birth date check digit',
field: 'birthDateCheckDigit',
parser: parseDateCheckDigit,
type: fieldTypes.NUMERIC,
};
} satisfies FieldOptionTemplate;

const issueDateTemplate = {
label: 'Issue date',
field: 'issueDate',
parser: parseDate,
type: fieldTypes.NUMERIC,
};
} satisfies FieldOptionTemplate;

const firstNameTemplate = {
label: 'First name',
field: 'firstName',
parser: parseFirstName,
type: fieldTypes.ALPHABETIC,
};
} satisfies FieldOptionTemplate;

const lastNameTemplate = {
label: 'Last name',
field: 'lastName',
parser: parseLastName,
type: fieldTypes.ALPHABETIC,
};
} satisfies FieldOptionTemplate;

const issuingStateTemplate = {
label: 'Issuing state',
field: 'issuingState',
parser: parseState,
type: fieldTypes.ALPHABETIC,
};
} satisfies FieldOptionTemplate;

export {
documentNumberTemplate,
Expand Down
21 changes: 11 additions & 10 deletions src/parse/frenchNationalIdFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ import { parseAlpha } from '../parsers/parseAlpha';
import parseDocumentCode from '../parsers/parseDocumentCodeId';
import { parseOptional } from '../parsers/parseOptional';

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

export default [
const fields: FieldOptions[] = [
{
...documentCodeTemplate,
line: 0,
Expand Down Expand Up @@ -94,4 +94,5 @@ export default [
},
],
},
].map(createFieldParser);
];
export default fields.map(createFieldParser);
8 changes: 4 additions & 4 deletions src/parse/getResult.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

import { FormatType } from '../formats';
import { Autocorrect, Details, FieldRecords, ParseResult } from '../types';

import { Autocorrect } from './autoCorrection';
import { Details, CreateFieldParserResult } from './createFieldParser';
import { CreateFieldParserResult } from './createFieldParser';
import { ParseMRZOptions } from './parse';

function getDetails(
Expand All @@ -19,7 +19,7 @@ function getDetails(
}

function getFields(details: Details[]) {
const fields: Record<string, string | null> = {};
const fields: FieldRecords = {};
let valid = true;
for (const detail of details) {
if (!detail.valid) valid = false;
Expand Down Expand Up @@ -57,7 +57,7 @@ export function getResult(
lines: string[],
fieldParsers: CreateFieldParserResult[],
options: ParseMRZOptions,
) {
): ParseResult {
const { autocorrect = false } = options;

const { corrected, autocorrectArray } = getCorrection(
Expand Down
16 changes: 10 additions & 6 deletions src/parse/swissDrivingLicenseFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ import parseDocumentNumber from '../parsers/swissDrivingLicense/parseDocumentNum
import parseIssuingState from '../parsers/swissDrivingLicense/parseIssuingState';
import parseLanguageCode from '../parsers/swissDrivingLicense/parseLanguageCode';

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

export default [
const fields: FieldOptions[] = [
{
...documentNumberTemplate,
line: 0,
Expand Down Expand Up @@ -85,4 +88,5 @@ export default [
},
{ ...lastNameTemplate, line: 2, start: 0, end: 30 },
{ ...firstNameTemplate, line: 2, start: 0, end: 30 },
].map(createFieldParser);
];
export default fields.map(createFieldParser);
26 changes: 14 additions & 12 deletions src/parse/td1Fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ import parseDocumentCode from '../parsers/parseDocumentCodeId';
import { parseDocumentNumberOptional } from '../parsers/parseDocumentNumberOptional';
import { parseOptional } from '../parsers/parseOptional';

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

export default [
const fields: FieldOptions[] = [
{
...documentCodeTemplate,
line: 0,
Expand Down Expand Up @@ -154,4 +154,6 @@ export default [
},
{ ...lastNameTemplate, line: 2, start: 0, end: 30 },
{ ...firstNameTemplate, line: 2, start: 0, end: 30 },
].map(createFieldParser);
];

export default fields.map(createFieldParser);
24 changes: 13 additions & 11 deletions src/parse/td2Fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
import parseDocumentCode from '../parsers/parseDocumentCodeId';
import { parseOptional } from '../parsers/parseOptional';

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

export default [
const fields: FieldOptions[] = [
{
...documentCodeTemplate,
line: 0,
Expand Down Expand Up @@ -128,4 +128,6 @@ export default [
},
],
},
].map(createFieldParser);
];

export default fields.map(createFieldParser);
Loading

0 comments on commit 66c31bc

Please sign in to comment.