Comparing version 3.4.0 to 3.4.1
@@ -11,5 +11,5 @@ import { FieldOptions } from './createFieldParser'; | ||
export declare function autoCorrection(source: string, fieldOptions: Pick<FieldOptions, 'line' | 'type' | 'start'>): { | ||
correctedLine: string; | ||
correctedText: string; | ||
autocorrect: Autocorrect[]; | ||
}; | ||
//# sourceMappingURL=autoCorrection.d.ts.map |
@@ -32,3 +32,3 @@ 'use strict'; | ||
export function autoCorrection(source, fieldOptions) { | ||
let correctedLine = ''; | ||
let correctedText = ''; | ||
const autocorrect = []; | ||
@@ -47,3 +47,3 @@ const chars = source.split(''); | ||
} | ||
correctedLine += correctedChar; | ||
correctedText += correctedChar; | ||
} | ||
@@ -60,10 +60,10 @@ else if (fieldOptions.type === fieldTypes.NUMERIC) { | ||
} | ||
correctedLine += correctedChar; | ||
correctedText += correctedChar; | ||
} | ||
else { | ||
correctedLine += char; | ||
correctedText += char; | ||
} | ||
}); | ||
return { correctedLine, autocorrect }; | ||
return { correctedText, autocorrect }; | ||
} | ||
//# sourceMappingURL=autoCorrection.js.map |
@@ -26,3 +26,3 @@ import { Autocorrect } from './autoCorrection'; | ||
}; | ||
export type FieldOptions = { | ||
export interface FieldOptions { | ||
label: string; | ||
@@ -36,3 +36,3 @@ field: string | null; | ||
type?: FieldTypes; | ||
}; | ||
} | ||
interface Range { | ||
@@ -44,6 +44,7 @@ line: 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; | ||
}; | ||
@@ -50,0 +51,0 @@ } |
@@ -17,9 +17,8 @@ 'use strict'; | ||
checkType(fieldOptions, 'parser', 'function'); | ||
const ranges = [ | ||
{ | ||
line: fieldOptions.line, | ||
start: fieldOptions.start, | ||
end: fieldOptions.end, | ||
}, | ||
]; | ||
const mainRange = { | ||
line: fieldOptions.line, | ||
start: fieldOptions.start, | ||
end: fieldOptions.end, | ||
}; | ||
const ranges = [mainRange]; | ||
if (Array.isArray(fieldOptions.related)) { | ||
@@ -33,3 +32,3 @@ for (const related of fieldOptions.related) { | ||
} | ||
const parser = (lines, autocorrect = []) => { | ||
const parser = (lines, autocorrect) => { | ||
const source = getText(lines, fieldOptions); | ||
@@ -44,5 +43,3 @@ const related = fieldOptions.related || []; | ||
ranges: ranges.map((range) => ({ | ||
line: range.line, | ||
start: range.start, | ||
end: range.end, | ||
...range, | ||
raw: getText(lines, range), | ||
@@ -53,3 +50,3 @@ })), | ||
end: 0, | ||
autocorrect, | ||
autocorrect: [], | ||
}; | ||
@@ -72,16 +69,17 @@ const range = result.ranges[0]; | ||
} | ||
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) => { | ||
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 }; | ||
const originalText = getText(lines, fieldOptions); | ||
return { | ||
...autoCorrection(originalText, fieldOptions), | ||
range: mainRange, | ||
}; | ||
}; | ||
@@ -94,8 +92,2 @@ return { parser, autocorrector }; | ||
} | ||
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) { | ||
@@ -102,0 +94,0 @@ if (typeof options[name] !== type) { |
@@ -22,11 +22,16 @@ 'use strict'; | ||
function getCorrection(lines, fieldParsers, autocorrect) { | ||
let corrected = lines; | ||
const autocorrectArray = []; | ||
const corrected = lines.slice(); | ||
let autocorrectArray = []; | ||
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 }; | ||
@@ -39,3 +44,3 @@ } | ||
const fields = getFields(details); | ||
const result = { | ||
return { | ||
format, | ||
@@ -46,4 +51,3 @@ details, | ||
}; | ||
return result; | ||
} | ||
//# sourceMappingURL=getResult.js.map |
@@ -6,3 +6,3 @@ 'use strict'; | ||
source = cleanText(source); | ||
let state = STATES[source]; | ||
const state = STATES[source]; | ||
if (!state) { | ||
@@ -9,0 +9,0 @@ throw new Error(`invalid state code: ${source}`); |
@@ -1,2 +0,2 @@ | ||
export default function parseIssuingState(source: string): "LIE" | "CHE"; | ||
export default function parseIssuingState(source: string): "CHE" | "LIE"; | ||
//# sourceMappingURL=parseIssuingState.d.ts.map |
@@ -11,5 +11,5 @@ import { FieldOptions } from './createFieldParser'; | ||
export declare function autoCorrection(source: string, fieldOptions: Pick<FieldOptions, 'line' | 'type' | 'start'>): { | ||
correctedLine: string; | ||
correctedText: string; | ||
autocorrect: Autocorrect[]; | ||
}; | ||
//# sourceMappingURL=autoCorrection.d.ts.map |
@@ -36,3 +36,3 @@ 'use strict'; | ||
function autoCorrection(source, fieldOptions) { | ||
let correctedLine = ''; | ||
let correctedText = ''; | ||
const autocorrect = []; | ||
@@ -51,3 +51,3 @@ const chars = source.split(''); | ||
} | ||
correctedLine += correctedChar; | ||
correctedText += correctedChar; | ||
} | ||
@@ -64,11 +64,11 @@ else if (fieldOptions.type === createFieldParser_1.fieldTypes.NUMERIC) { | ||
} | ||
correctedLine += correctedChar; | ||
correctedText += correctedChar; | ||
} | ||
else { | ||
correctedLine += char; | ||
correctedText += char; | ||
} | ||
}); | ||
return { correctedLine, autocorrect }; | ||
return { correctedText, autocorrect }; | ||
} | ||
exports.autoCorrection = autoCorrection; | ||
//# sourceMappingURL=autoCorrection.js.map |
@@ -26,3 +26,3 @@ import { Autocorrect } from './autoCorrection'; | ||
}; | ||
export type FieldOptions = { | ||
export interface FieldOptions { | ||
label: string; | ||
@@ -36,3 +36,3 @@ field: string | null; | ||
type?: FieldTypes; | ||
}; | ||
} | ||
interface Range { | ||
@@ -44,6 +44,7 @@ line: 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; | ||
}; | ||
@@ -50,0 +51,0 @@ } |
@@ -19,9 +19,8 @@ 'use strict'; | ||
checkType(fieldOptions, 'parser', 'function'); | ||
const ranges = [ | ||
{ | ||
line: fieldOptions.line, | ||
start: fieldOptions.start, | ||
end: fieldOptions.end, | ||
}, | ||
]; | ||
const mainRange = { | ||
line: fieldOptions.line, | ||
start: fieldOptions.start, | ||
end: fieldOptions.end, | ||
}; | ||
const ranges = [mainRange]; | ||
if (Array.isArray(fieldOptions.related)) { | ||
@@ -35,3 +34,3 @@ for (const related of fieldOptions.related) { | ||
} | ||
const parser = (lines, autocorrect = []) => { | ||
const parser = (lines, autocorrect) => { | ||
const source = getText(lines, fieldOptions); | ||
@@ -46,5 +45,3 @@ const related = fieldOptions.related || []; | ||
ranges: ranges.map((range) => ({ | ||
line: range.line, | ||
start: range.start, | ||
end: range.end, | ||
...range, | ||
raw: getText(lines, range), | ||
@@ -55,3 +52,3 @@ })), | ||
end: 0, | ||
autocorrect, | ||
autocorrect: [], | ||
}; | ||
@@ -74,16 +71,17 @@ const range = result.ranges[0]; | ||
} | ||
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) => { | ||
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 }; | ||
const originalText = getText(lines, fieldOptions); | ||
return { | ||
...(0, autoCorrection_1.autoCorrection)(originalText, fieldOptions), | ||
range: mainRange, | ||
}; | ||
}; | ||
@@ -97,8 +95,2 @@ return { parser, autocorrector }; | ||
} | ||
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) { | ||
@@ -105,0 +97,0 @@ if (typeof options[name] !== type) { |
@@ -24,11 +24,16 @@ 'use strict'; | ||
function getCorrection(lines, fieldParsers, autocorrect) { | ||
let corrected = lines; | ||
const autocorrectArray = []; | ||
const corrected = lines.slice(); | ||
let autocorrectArray = []; | ||
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 }; | ||
@@ -41,3 +46,3 @@ } | ||
const fields = getFields(details); | ||
const result = { | ||
return { | ||
format, | ||
@@ -48,5 +53,4 @@ details, | ||
}; | ||
return result; | ||
} | ||
exports.getResult = getResult; | ||
//# sourceMappingURL=getResult.js.map |
@@ -10,3 +10,3 @@ 'use strict'; | ||
source = (0, cleanText_1.cleanText)(source); | ||
let state = states_1.default[source]; | ||
const state = states_1.default[source]; | ||
if (!state) { | ||
@@ -13,0 +13,0 @@ throw new Error(`invalid state code: ${source}`); |
@@ -1,2 +0,2 @@ | ||
export default function parseIssuingState(source: string): "LIE" | "CHE"; | ||
export default function parseIssuingState(source: string): "CHE" | "LIE"; | ||
//# sourceMappingURL=parseIssuingState.d.ts.map |
{ | ||
"name": "mrz", | ||
"version": "3.4.0", | ||
"version": "3.4.1", | ||
"description": "Parse MRZ (Machine Readable Zone) from identity documents", | ||
@@ -50,13 +50,13 @@ "main": "./lib/index.js", | ||
"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" | ||
} | ||
} |
@@ -95,2 +95,3 @@ 'use strict'; | ||
[], | ||
[], | ||
[ | ||
@@ -100,5 +101,4 @@ { line: 2, column: 12, original: '8', corrected: 'B' }, | ||
], | ||
[], | ||
]); | ||
}); | ||
}); |
@@ -194,8 +194,9 @@ 'use strict'; | ||
{ line: 2, column: 2, original: '1', corrected: 'I' }, | ||
], | ||
[ | ||
{ line: 2, column: 8, original: '0', corrected: 'O' }, | ||
{ line: 2, column: 14, original: '8', corrected: 'B' }, | ||
], | ||
[], | ||
]); | ||
}); | ||
}); |
@@ -59,7 +59,6 @@ 'use strict'; | ||
{ 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' }], | ||
@@ -66,0 +65,0 @@ [ |
@@ -181,3 +181,3 @@ 'use strict'; | ||
const falseMRZ = [ | ||
'P<UT0ERIK55ON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<', | ||
'P<UT0ERIK55ON<<ANNA<MAR1A<<<<<<<<<<<<<<<<<<<', | ||
'L898902C36UTO740BIZZF12041S9ZE184226B<<<<<1O', | ||
@@ -199,6 +199,6 @@ ]; | ||
], | ||
[{ line: 0, column: 23, original: '1', corrected: 'I' }], | ||
[], | ||
[], | ||
[], | ||
[], | ||
[ | ||
@@ -205,0 +205,0 @@ { line: 1, column: 16, original: 'B', corrected: '8' }, |
@@ -45,3 +45,3 @@ 'use strict'; | ||
) { | ||
let correctedLine = ''; | ||
let correctedText = ''; | ||
const autocorrect: Autocorrect[] = []; | ||
@@ -60,3 +60,3 @@ const chars = source.split(''); | ||
} | ||
correctedLine += correctedChar; | ||
correctedText += correctedChar; | ||
} else if (fieldOptions.type === fieldTypes.NUMERIC) { | ||
@@ -72,8 +72,8 @@ const correctedChar = letterToNumber(char); | ||
} | ||
correctedLine += correctedChar; | ||
correctedText += correctedChar; | ||
} else { | ||
correctedLine += char; | ||
correctedText += char; | ||
} | ||
}); | ||
return { correctedLine, autocorrect }; | ||
return { correctedText, autocorrect }; | ||
} |
@@ -33,3 +33,3 @@ 'use strict'; | ||
export type FieldOptions = { | ||
export interface FieldOptions { | ||
label: string; | ||
@@ -43,3 +43,3 @@ field: string | null; | ||
type?: FieldTypes; | ||
}; | ||
} | ||
interface Range { | ||
@@ -52,6 +52,7 @@ line: 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; | ||
}; | ||
@@ -72,9 +73,9 @@ } | ||
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)) { | ||
@@ -88,3 +89,6 @@ for (const related of fieldOptions.related) { | ||
} | ||
const parser = (lines: string[], autocorrect: Autocorrect[] = []) => { | ||
const parser: CreateFieldParserResult['parser'] = ( | ||
lines: string[], | ||
autocorrect: Autocorrect[], | ||
) => { | ||
const source = getText(lines, fieldOptions); | ||
@@ -99,5 +103,3 @@ const related = fieldOptions.related || []; | ||
ranges: ranges.map((range) => ({ | ||
line: range.line, | ||
start: range.start, | ||
end: range.end, | ||
...range, | ||
raw: getText(lines, range), | ||
@@ -108,4 +110,5 @@ })), | ||
end: 0, | ||
autocorrect, | ||
autocorrect: [], | ||
}; | ||
const range = result.ranges[0]; | ||
@@ -123,27 +126,33 @@ result.line = range.line; | ||
} | ||
} 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]; | ||
@@ -153,14 +162,2 @@ 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( | ||
@@ -167,0 +164,0 @@ options: object, |
@@ -38,14 +38,19 @@ 'use strict'; | ||
) { | ||
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( | ||
@@ -66,3 +71,3 @@ format: FormatType, | ||
const fields = getFields(details); | ||
const result = { | ||
return { | ||
format, | ||
@@ -73,3 +78,2 @@ details, | ||
}; | ||
return result; | ||
} |
@@ -9,3 +9,3 @@ 'use strict'; | ||
source = cleanText(source); | ||
let state = STATES[source]; | ||
const state = STATES[source]; | ||
if (!state) { | ||
@@ -12,0 +12,0 @@ throw new Error(`invalid state code: ${source}`); |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
336046
7058