Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve autocorrect reported ranges #45

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/lactame.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ on:
types: [published]

env:
NODE_VERSION: 16.x
NODE_VERSION: 18.x

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Get package name
run: echo "PACKAGE_NAME=$(jq .name package.json | tr -d '"')" >> $GITHUB_ENV
- uses: actions/setup-node@v2
- uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: Install dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nodejs-ts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Node.js CI
on:
push:
branches:
- "main"
- main
pull_request:

jobs:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Release
on:
push:
branches:
- 'main'
- main

jobs:
release:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/typedoc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ on:
types: [published]

env:
NODE_VERSION: 16.x
NODE_VERSION: 18.x
ENTRY_FILE: 'src/index.ts'

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: Install dependencies
Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@
},
"homepage": "https://github.com/cheminfo/mrz#readme",
"devDependencies": {
"@babel/plugin-transform-modules-commonjs": "^7.20.11",
"@babel/preset-typescript": "^7.18.6",
"@types/jest": "^29.2.6",
"cheminfo-build": "^1.1.11",
"eslint": "^8.32.0",
"eslint-config-cheminfo-typescript": "^11.2.2",
"jest": "^29.4.0",
"prettier": "^2.8.3",
"rimraf": "^4.1.2",
"typescript": "^4.9.4"
"@babel/plugin-transform-modules-commonjs": "^7.23.0",
"@babel/preset-typescript": "^7.23.2",
"@types/jest": "^29.5.6",
"cheminfo-build": "^1.2.0",
"eslint": "^8.52.0",
"eslint-config-cheminfo-typescript": "^12.0.4",
"jest": "^29.7.0",
"prettier": "^3.0.3",
"rimraf": "^5.0.5",
"typescript": "^5.2.2"
}
}
2 changes: 1 addition & 1 deletion src/parse/__tests__/swissDrivingLicense.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ describe('parse Swiss Driving License', () => {
{ line: 1, column: 24, original: 'G', corrected: '6' },
],
[],
[],
[
{ line: 2, column: 12, original: '8', corrected: 'B' },
{ line: 2, column: 13, original: '1', corrected: 'I' },
],
[],
]);
});
});
3 changes: 2 additions & 1 deletion src/parse/__tests__/td1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,11 @@ describe('parse TD1', () => {
[
{ line: 2, column: 0, original: '5', corrected: 'S' },
{ line: 2, column: 2, original: '1', corrected: 'I' },
],
[
{ line: 2, column: 8, original: '0', corrected: 'O' },
{ line: 2, column: 14, original: '8', corrected: 'B' },
],
[],
]);
});
});
3 changes: 1 addition & 2 deletions src/parse/__tests__/td2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ describe('parse TD2', () => {
[
{ line: 0, column: 9, original: '5', corrected: 'S' },
{ line: 0, column: 10, original: '5', corrected: 'S' },
{ line: 0, column: 23, original: '1', corrected: 'I' },
],
[],
[{ line: 0, column: 23, original: '1', corrected: 'I' }],
[],
[],
[{ line: 1, column: 12, original: '0', corrected: 'O' }],
Expand Down
4 changes: 2 additions & 2 deletions src/parse/__tests__/td3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe('parse TD3', () => {
'L898902C36UTO7408122F1204159ZE184226B<<<<<10',
];
const falseMRZ = [
'P<UT0ERIK55ON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<',
'P<UT0ERIK55ON<<ANNA<MAR1A<<<<<<<<<<<<<<<<<<<',
'L898902C36UTO740BIZZF12041S9ZE184226B<<<<<1O',
];

Expand All @@ -195,7 +195,7 @@ describe('parse TD3', () => {
{ line: 0, column: 9, original: '5', corrected: 'S' },
{ line: 0, column: 10, original: '5', corrected: 'S' },
],
[],
[{ line: 0, column: 23, original: '1', corrected: 'I' }],
[],
[],
[],
Expand Down
10 changes: 5 additions & 5 deletions src/parse/autoCorrection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function autoCorrection(
source: string,
fieldOptions: Pick<FieldOptions, 'line' | 'type' | 'start'>,
) {
let correctedLine = '';
let correctedText = '';
const autocorrect: Autocorrect[] = [];
const chars = source.split('');
chars.forEach((char, i) => {
Expand All @@ -56,7 +56,7 @@ export function autoCorrection(
corrected: correctedChar,
});
}
correctedLine += correctedChar;
correctedText += correctedChar;
} else if (fieldOptions.type === fieldTypes.NUMERIC) {
const correctedChar = letterToNumber(char);
if (correctedChar !== char) {
Expand All @@ -67,10 +67,10 @@ export function autoCorrection(
corrected: correctedChar,
});
}
correctedLine += correctedChar;
correctedText += correctedChar;
} else {
correctedLine += char;
correctedText += char;
}
});
return { correctedLine, autocorrect };
return { correctedText, autocorrect };
}
87 changes: 42 additions & 45 deletions src/parse/createFieldParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const fieldTypes = {
ALPHANUMERIC: 'ALPHANUMERIC',
} as const;

export type FieldOptions = {
export interface FieldOptions {
label: string;
field: string | null;
line: number;
Expand All @@ -39,18 +39,19 @@ export type FieldOptions = {
parser: Parser;
related?: Range[];
type?: FieldTypes;
};
}
interface Range {
line: number;
start: number;
end: number;
}

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

Expand All @@ -66,13 +67,13 @@ export default function createFieldParser(
checkType(fieldOptions, 'end', 'number');
checkType(fieldOptions, 'parser', 'function');

const ranges: Range[] = [
{
line: fieldOptions.line,
start: fieldOptions.start,
end: fieldOptions.end,
},
];
const mainRange: Range = {
line: fieldOptions.line,
start: fieldOptions.start,
end: fieldOptions.end,
};

const ranges: Range[] = [mainRange];
if (Array.isArray(fieldOptions.related)) {
for (const related of fieldOptions.related) {
checkType(related, 'line', 'number');
Expand All @@ -81,7 +82,10 @@ export default function createFieldParser(
ranges.push(related);
}
}
const parser = (lines: string[], autocorrect: Autocorrect[] = []) => {
const parser: CreateFieldParserResult['parser'] = (
lines: string[],
autocorrect: Autocorrect[],
) => {
const source = getText(lines, fieldOptions);
const related = fieldOptions.related || [];
const textRelated = related.map((r) => getText(lines, r));
Expand All @@ -91,16 +95,15 @@ export default function createFieldParser(
value: null,
valid: false,
ranges: ranges.map((range) => ({
line: range.line,
start: range.start,
end: range.end,
...range,
raw: getText(lines, range),
})),
line: 0,
start: 0,
end: 0,
autocorrect,
autocorrect: [],
};

const range = result.ranges[0];
result.line = range.line;
result.start = range.start;
Expand All @@ -113,47 +116,41 @@ export default function createFieldParser(
result.start = range.start + parsed.start;
result.end = range.start + parsed.end;
}
} catch (e: any) {
} catch (e) {
result.error = e.message;
}

for (const autocorrectElement of autocorrect) {
if (
autocorrectElement.line === result.line &&
autocorrectElement.column >= result.start &&
autocorrectElement.column < result.end
) {
result.autocorrect.push(autocorrectElement);
}
}

return result;
};
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 };

const autocorrector: CreateFieldParserResult['autocorrector'] = (
lines: string[],
) => {
const originalText = getText(lines, fieldOptions);
return {
...autoCorrection(originalText, fieldOptions),
range: mainRange,
};
};

return { parser, autocorrector };
}

function getText(
lines: string | string[],
options: Pick<FieldOptions, 'line' | 'end' | 'start'>,
) {
function getText(lines: string | string[], options: Range) {
const line = lines[options.line];
return line.substring(options.start, options.end);
}

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

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

if (autocorrect) {
fieldParsers.forEach(({ autocorrector }) => {
const result = autocorrector(corrected);
autocorrectArray.push(result.autocorrect);
corrected = result.correctedLines;
const { autocorrect, correctedText, range } = autocorrector(lines);
autocorrectArray.push(autocorrect);
const line = corrected[range.line];
corrected[range.line] =
line.slice(0, range.start) + correctedText + line.slice(range.end);
});
} else {
autocorrectArray = new Array(fieldParsers.length).fill([]);
}
return { corrected, autocorrectArray };
}

export function getResult(
format: FormatType,
lines: string[],
Expand All @@ -62,11 +67,10 @@ export function getResult(
);
const details = getDetails(corrected, fieldParsers, autocorrectArray);
const fields = getFields(details);
const result = {
return {
format,
details,
fields: fields.fields,
valid: fields.valid,
};
return result;
}
2 changes: 1 addition & 1 deletion src/parsers/parseState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { cleanText } from './cleanText';

export default function parseState(source: string) {
source = cleanText(source);
let state = STATES[source];
const state = STATES[source];
if (!state) {
throw new Error(`invalid state code: ${source}`);
}
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2020"
"target": "es2020",
"useUnknownInCatchVariables": false
},
"include": ["./src/**/*"]
}