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

chore(typedefs): added typedefs #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cSpell.words": ["autocorrect", "autocorrector"]
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,15 @@
"@babel/plugin-transform-modules-commonjs": "^7.20.11",
"@babel/preset-typescript": "^7.18.6",
"@types/jest": "^29.2.6",
"@typescript-eslint/eslint-plugin": "^5.55.0",
"@typescript-eslint/parser": "^5.55.0",
"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",
"eslint-plugin-import": "^2.27.5",
"typescript": "^4.9.4"
}
}
30 changes: 28 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@

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

export { formats, states, parse };
/* Exporting the default export from the parse module. */
export { default as parse } from './parse/parse';

/* Exporting the `formats` and `states` variables from the `index.ts` file. */
export { formats, states };

// Types
export {
ParseMRZOptions,
Formats,
DefaultRecord,
FrenchNationalRecord,
SwissDrivingLicense,
Td1Record,
Td2Record,
Td3Record,
CombinedRecord,
Autocorrect,
Details,
Record,
FieldTypes,
TemplateDetails,
ParseResult,
FieldOptions,
Range,
Results,
CreateFieldParserResult,
} from './types';
30 changes: 21 additions & 9 deletions src/parse/autoCorrection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

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

const numberToLetterMismatches = {
'8': 'B',
Expand All @@ -18,26 +18,38 @@ const letterToNumberMismatches = {
S: '5',
Z: '2',
};
export interface Autocorrect {
line: number;
column: number;
original: string;
corrected: string;
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove those (autogenerated?) comments all over the PR.

They add a lot of unnecessary noise.

* It takes a string and returns a string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This description is not helpful (just describes what already is understandable from the type anotations)

* @param {string} char - The character to convert.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • The params don't need type annotations in JS doc since they are available from the TS types.
  • The param description doesn't add helpful context (the function name letterToNumber already says it converts a char.

* @returns A function that takes a string and returns a string.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • The return value does to need type annotation with JS doc since already available from TS
  • The description is not helpful

*/
export function letterToNumber(char: string): string {
if (letterToNumberMismatches[char]) {
return letterToNumberMismatches[char];
}
return char;
}

/**
* It takes a string and returns a string
* @param {string} char - The character to convert.
* @returns A function that takes a string and returns a string.
*/
export function numberToLetter(char: string): string {
if (numberToLetterMismatches[char]) {
return numberToLetterMismatches[char];
}
return char;
}

/**
* It takes a string and a fieldOptions object, and returns an object with a correctedLine string and
* an autocorrect array
* @param {string} source - The string to be corrected
* @param fieldOptions - Pick<FieldOptions, 'line' | 'type' | 'start'>
* @returns An object with two properties: correctedLine and autocorrect.
*/
export function autoCorrection(
source: string,
fieldOptions: Pick<FieldOptions, 'line' | 'type' | 'start'>,
Expand All @@ -46,7 +58,7 @@ export function autoCorrection(
const autocorrect: Autocorrect[] = [];
const chars = source.split('');
chars.forEach((char, i) => {
if (fieldOptions.type === fieldTypes.ALPHABETIC) {
if (fieldOptions.type === 'ALPHABETIC') {
const correctedChar = numberToLetter(char);
if (correctedChar !== char) {
autocorrect.push({
Expand All @@ -57,7 +69,7 @@ export function autoCorrection(
});
}
correctedLine += correctedChar;
} else if (fieldOptions.type === fieldTypes.NUMERIC) {
} else if (fieldOptions.type === 'NUMERIC') {
const correctedChar = letterToNumber(char);
if (correctedChar !== char) {
autocorrect.push({
Expand Down
5 changes: 5 additions & 0 deletions src/parse/checkLines.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
'use strict';

/**
* It takes a string or an array of strings, and throws an error if the input is invalid
* @param {string | string[]} lines - The lines of the program.
* @returns A function that takes a string or array of strings and returns an array of strings.
*/
export function checkLines(lines: string | string[]) {
if (typeof lines === 'string') {
lines = lines.split(/[\r\n]+/);
Expand Down
107 changes: 45 additions & 62 deletions src/parse/createFieldParser.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,20 @@
'use strict';

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

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[];
}

interface ParseResult {
value: string;
start: number;
end: number;
}

type Parser = (source: string, ...related: string[]) => ParseResult | string;

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

export type FieldOptions = {
label: string;
field: string | 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;
autocorrector: (lines: string[]) => {
correctedLines: string[];
autocorrect: Autocorrect[];
};
}

export default function createFieldParser(
/**
* It takes a fieldOptions object and returns a parser function and an autocorrector function
* @param {FieldOptions} fieldOptions - This is an object that contains the following properties:
* @returns A function that takes in a string and returns a string.
*/
export function createFieldParser(
fieldOptions: FieldOptions,
): CreateFieldParserResult {
checkType(fieldOptions, 'label', 'string');
Expand Down Expand Up @@ -106,12 +66,14 @@ export default function createFieldParser(
result.start = range.start;
result.end = range.end;
try {
const parsed = fieldOptions.parser(source, ...textRelated);
result.value = typeof parsed === 'object' ? parsed.value : parsed;
result.valid = true;
if (typeof parsed === 'object') {
result.start = range.start + parsed.start;
result.end = range.start + parsed.end;
if (fieldOptions.parser !== undefined) {
const parsed = fieldOptions.parser(source, ...textRelated);
result.value = typeof parsed === 'object' ? parsed.value : parsed;
result.valid = true;
if (typeof parsed === 'object') {
result.start = range.start + parsed.start;
result.end = range.start + parsed.end;
}
}
} catch (e: any) {
result.error = e.message;
Expand All @@ -122,8 +84,8 @@ export default function createFieldParser(
let corrected = lines;
let source = getText(lines, fieldOptions);
let autocorrect: Autocorrect[] = [];
const type = fieldOptions.type || fieldTypes.ALPHANUMERIC;
if (type !== fieldTypes.ALPHANUMERIC) {
const type = fieldOptions.type || 'ALPHANUMERIC';
if (type !== 'ALPHANUMERIC') {
const result = autoCorrection(source, fieldOptions);
source = result.correctedLine;
autocorrect = result.autocorrect;
Expand All @@ -134,6 +96,13 @@ export default function createFieldParser(
return { parser, autocorrector };
}

/**
* `getText` takes a string or an array of strings and an object with `line`, `end`, and `start`
* properties and returns a string
* @param {string | string[]} lines - string | string[]
* @param options - Pick<FieldOptions, 'line' | 'end' | 'start'>
* @returns The text between the start and end of the line.
*/
function getText(
lines: string | string[],
options: Pick<FieldOptions, 'line' | 'end' | 'start'>,
Expand All @@ -142,6 +111,13 @@ function getText(
return line.substring(options.start, options.end);
}

/**
* It takes a line of text, and replaces a portion of it with some new text
* @param {string[]} lines - The lines of the file.
* @param options - This is the object that contains the line, start, and end properties.
* @param {string} text - The text to be inserted
* @returns The lines array with the new text inserted.
*/
function changeText(
lines: string[],
options: Pick<FieldOptions, 'line' | 'end' | 'start'>,
Expand All @@ -154,6 +130,13 @@ function changeText(
return lines;
}

/**
* If the type of the property named name on the object options is not the type type, throw a
* TypeError.
* @param {object} options - object - The object to check.
* @param {string} name - The name of the option to check.
* @param {'string' | 'number' | 'function'} type - 'string' | 'number' | 'function'
*/
function checkType(
options: object,
name: string,
Expand Down
Loading