Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "mrz", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Create and parse MRZ (Machine Readable Zone) in TD1 and TD3 format", | ||
@@ -11,8 +11,8 @@ "main": "./src/index.js", | ||
"scripts": { | ||
"eslint": "eslint src test", | ||
"eslint": "eslint src __tests__", | ||
"eslint-fix": "npm run eslint -- --fix", | ||
"test": "npm run test-mocha && npm run eslint", | ||
"test-cov": "istanbul cover node_modules/.bin/_mocha -- --require should --reporter dot --recursive", | ||
"test-travis": "istanbul cover node_modules/.bin/_mocha --report lcovonly -- --require should --reporter dot --recursive", | ||
"test-mocha": "mocha --require should --reporter mocha-better-spec-reporter", | ||
"test": "npm run test-only && npm run eslint", | ||
"test-coverage": "jest --coverage", | ||
"test-travis": "npm run test-coverage && npm run eslint", | ||
"test-only": "jest", | ||
"build": "npm run buildCountries && cheminfo build", | ||
@@ -40,11 +40,9 @@ "prepublish": "npm run buildCountries", | ||
"devDependencies": { | ||
"cheminfo-tools": "^1.5.0", | ||
"eslint": "^3.4.0", | ||
"eslint-config-cheminfo": "^1.2.0", | ||
"eslint-plugin-no-only-tests": "^1.1.0", | ||
"istanbul": "^0.4.4", | ||
"mocha": "^3.1.2", | ||
"mocha-better-spec-reporter": "^3.0.2", | ||
"cheminfo-tools": "^1.20.2", | ||
"eslint": "^4.15.0", | ||
"eslint-config-cheminfo": "^1.12.0", | ||
"eslint-plugin-jest": "^21.6.2", | ||
"jest": "^22.1.1", | ||
"should": "^11.1.0" | ||
} | ||
} |
# mrz | ||
[![NPM version][npm-image]][npm-url] | ||
[![build status][travis-image]][travis-url] | ||
[![Test coverage][coveralls-image]][coveralls-url] | ||
[![David deps][david-image]][david-url] | ||
[![npm download][download-image]][download-url] | ||
[![NPM version][npm-image]][npm-url] | ||
[![build status][travis-image]][travis-url] | ||
[![Test coverage][coveralls-image]][coveralls-url] | ||
[![David deps][david-image]][david-url] | ||
[![npm download][download-image]][download-url] | ||
Parse MRZ (Machine Readable Zone) in TD1, TD2, TD3 or CH driving licence format | ||
@@ -18,3 +18,2 @@ | ||
```js | ||
const parse = require('mrz').parse; | ||
@@ -28,3 +27,2 @@ | ||
console.log(result); | ||
``` | ||
@@ -34,2 +32,12 @@ | ||
## Specifications | ||
## TD1, TD2 and TD3 | ||
https://www.icao.int/publications/pages/publication.aspx?docnum=9303 | ||
## Swiss driving license | ||
<!-- TODO add link --> | ||
## License | ||
@@ -36,0 +44,0 @@ |
@@ -9,4 +9,4 @@ 'use strict'; | ||
module.exports = { | ||
COUNTRIES, | ||
parse | ||
COUNTRIES, | ||
parse | ||
}; |
@@ -8,25 +8,44 @@ 'use strict'; | ||
module.exports = function parse(text) { | ||
var lines = text.split(/[\r\n]+/); | ||
var result = {logs: []}; | ||
switch (lines.length) { | ||
case 2: | ||
if (lines[0].length < 41) { | ||
result = parseTD2(lines); | ||
} else { | ||
result = parseTD3(lines); | ||
} | ||
break; | ||
case 3: | ||
if (lines[0].length < 15) { // in fact it should be 9 | ||
result = parsePCC(lines); | ||
} else { | ||
result = parseTD1(lines); | ||
} | ||
module.exports = function parse(text, options = {}) { | ||
const lines = text.split(/[\r\n]+/); | ||
let result = { logs: [] }; | ||
switch (lines.length) { | ||
case 2: | ||
if (lines[0].length < 41) { | ||
result = parseTD2(lines); | ||
} else { | ||
result = parseTD3(lines); | ||
} | ||
break; | ||
case 3: | ||
if (lines[0].length < 15) { | ||
// in fact it should be 9 | ||
result = parsePCC(lines); | ||
} else { | ||
result = parseTD1(lines); | ||
} | ||
break; | ||
default: | ||
result.logs.push('We need 2 or 3 lines'); | ||
break; | ||
default: | ||
result.logs.push('We need 2 or 3 lines'); | ||
} | ||
if (options.debug) { | ||
return result; | ||
} | ||
const simpleResult = { | ||
values: {}, | ||
errors: [] | ||
}; | ||
for (let key in result) { | ||
if (result[key].error) simpleResult.errors.push(...result[key].error); | ||
if (result[key].value !== undefined) { | ||
simpleResult.values[key] = result[key].value; | ||
} | ||
return result; | ||
} | ||
simpleResult.isValid = result.isValid; | ||
return simpleResult; | ||
}; |
@@ -14,32 +14,32 @@ 'use strict'; | ||
module.exports = function parseTD1(lines) { | ||
var result = { | ||
format: 'PCC', | ||
error: [] | ||
}; | ||
var first = lines[0]; | ||
if (first.length !== 9) { | ||
result.error.push('First line does not have 9 symbols'); | ||
} | ||
var second = lines[1]; | ||
if (second.length !== 30) { | ||
result.error.push('Second line does not have 30 symbols'); | ||
} | ||
var third = lines[2]; | ||
if (third.length !== 30) { | ||
result.error.push('Third line does not have 30 symbols'); | ||
} | ||
var result = { | ||
format: 'PCC', | ||
error: [] | ||
}; | ||
var first = lines[0]; | ||
if (first.length !== 9) { | ||
result.error.push('First line does not have 9 symbols'); | ||
} | ||
var second = lines[1]; | ||
if (second.length !== 30) { | ||
result.error.push('Second line does not have 30 symbols'); | ||
} | ||
var third = lines[2]; | ||
if (third.length !== 30) { | ||
result.error.push('Third line does not have 30 symbols'); | ||
} | ||
result.documentNumber = parsePCCDocumentNumber(first); | ||
result.documentType = parsePCCDocumentType(second.substring(0, 2)); | ||
result.issuingCountry = parseCountry(second.substring(2, 5)); | ||
result.nipCode = parseNumber('NIP code', second.substring(5, 14)); | ||
result.version = parseNumber('Version', second.substring(14, 17)); | ||
result.separator1 = checkSeparator('Separator second line 18-19', second.substring(17, 19)); | ||
result.birthDate = parseBirthdayDate(second.substring(19, 25), false); | ||
result.separator1 = checkSeparator('Separator second line 26-30', second.substring(25, 30)); | ||
result.lastname = parseFirstname('Lastname', third.substring(0, 30)); | ||
result.firstname = parseLastname('Firstname', third.substring(0, 30)); | ||
finalAnalysis(result); | ||
result.documentNumber = parsePCCDocumentNumber(first); | ||
result.documentType = parsePCCDocumentType(second.substring(0, 2)); | ||
result.issuingCountry = parseCountry(second.substring(2, 5)); | ||
result.nipCode = parseNumber('NIP code', second.substring(5, 14)); | ||
result.version = parseNumber('Version', second.substring(14, 17)); | ||
result.separator1 = checkSeparator('Separator second line 18-19', second.substring(17, 19)); | ||
result.birthDate = parseBirthdayDate(second.substring(19, 25), false); | ||
result.separator1 = checkSeparator('Separator second line 26-30', second.substring(25, 30)); | ||
result.lastname = parseFirstname('Lastname', third.substring(0, 30)); | ||
result.firstname = parseLastname('Firstname', third.substring(0, 30)); | ||
finalAnalysis(result); | ||
return result; | ||
return result; | ||
}; |
@@ -16,36 +16,36 @@ 'use strict'; | ||
module.exports = function parseTD1(lines) { | ||
var result = { | ||
format: 'TD1', | ||
error: [] | ||
}; | ||
var first = lines[0]; | ||
var second = lines[1]; | ||
var third = lines[2]; | ||
var result = { | ||
format: 'TD1', | ||
error: [] | ||
}; | ||
var first = lines[0]; | ||
var second = lines[1]; | ||
var third = lines[2]; | ||
if (first.length !== 30) { | ||
result.error.push('First line does not have 30 symbols'); | ||
} | ||
result.documentType = parseDocumentType(first.substring(0, 2)); | ||
result.issuingCountry = parseIssuingCountry(first.substring(2, 5)); | ||
result.optional1 = parseText('Optional 1', first.substring(15, 30)); | ||
result.documentNumber = parseDocumentNumber(first.substring(5, 14), first.substr(14, 1), result.optional1.value); | ||
if (first.length !== 30) { | ||
result.error.push('First line does not have 30 symbols'); | ||
} | ||
result.documentType = parseDocumentType(first.substring(0, 2)); | ||
result.issuingCountry = parseIssuingCountry(first.substring(2, 5)); | ||
result.optional1 = parseText('Optional 1', first.substring(15, 30)); | ||
result.documentNumber = parseDocumentNumber(first.substring(5, 14), first.substr(14, 1), result.optional1.value); | ||
if (second.length !== 30) { | ||
result.error.push('Second line does not have 30 symbols'); | ||
} | ||
result.birthDate = parseBirthdayDate(second.substring(0, 6), second.substr(6, 1)); | ||
result.sex = parseSex(second.substr(7, 1)); | ||
result.expirationDate = parseBirthdayDate(second.substring(8, 14), second.substr(14, 1)); | ||
result.nationality = parseNationality(second.substring(15, 18), second.substr(18, 1)); | ||
result.optional2 = parseText('Optional 2', second.substring(18, 29)); | ||
if (second.length !== 30) { | ||
result.error.push('Second line does not have 30 symbols'); | ||
} | ||
result.birthDate = parseBirthdayDate(second.substring(0, 6), second.substr(6, 1)); | ||
result.sex = parseSex(second.substr(7, 1)); | ||
result.expirationDate = parseBirthdayDate(second.substring(8, 14), second.substr(14, 1)); | ||
result.nationality = parseNationality(second.substring(15, 18), second.substr(18, 1)); | ||
result.optional2 = parseText('Optional 2', second.substring(18, 29)); | ||
if (third.length !== 30) { | ||
result.error.push('Third line does not have 30 symbols'); | ||
} | ||
result.lastname = parseFirstname('Lastname', third.substring(0, 30)); | ||
result.firstname = parseLastname('Firstname', third.substring(0, 30)); | ||
result.globalCheck = globalCheck(first.substring(5, 30) + second.substring(0, 7) + second.substring(8, 15) + second.substring(18, 29), second.substr(29, 1)); | ||
if (third.length !== 30) { | ||
result.error.push('Third line does not have 30 symbols'); | ||
} | ||
result.lastname = parseFirstname('Lastname', third.substring(0, 30)); | ||
result.firstname = parseLastname('Firstname', third.substring(0, 30)); | ||
result.globalCheck = globalCheck(first.substring(5, 30) + second.substring(0, 7) + second.substring(8, 15) + second.substring(18, 29), second.substr(29, 1)); | ||
finalAnalysis(result); | ||
return result; | ||
finalAnalysis(result); | ||
return result; | ||
}; |
@@ -16,31 +16,31 @@ 'use strict'; | ||
module.exports = function parseTD3(lines) { | ||
var result = { | ||
error: [], | ||
format: 'TD2' | ||
}; | ||
var result = { | ||
error: [], | ||
format: 'TD2' | ||
}; | ||
var first = lines[0]; | ||
var second = lines[1]; | ||
var first = lines[0]; | ||
var second = lines[1]; | ||
if (first.length !== 36) { | ||
result.error.push('First line does not have 36 symbols'); | ||
} | ||
result.documentType = parseDocumentType(first.substring(0, 2)); | ||
result.issuingCountry = parseIssuingCountry(first.substring(2, 5)); | ||
result.lastname = parseFirstname('Lastname', first.substring(5, 36)); | ||
result.firstname = parseLastname('Firstname', first.substring(5, 36)); | ||
if (first.length !== 36) { | ||
result.error.push('First line does not have 36 symbols'); | ||
} | ||
result.documentType = parseDocumentType(first.substring(0, 2)); | ||
result.issuingCountry = parseIssuingCountry(first.substring(2, 5)); | ||
result.lastname = parseFirstname('Lastname', first.substring(5, 36)); | ||
result.firstname = parseLastname('Firstname', first.substring(5, 36)); | ||
if (second.length !== 36) { | ||
result.error.push('Second line does not have 36 symbols'); | ||
} | ||
result.documentNumber = parseDocumentNumber(second.substring(0, 9), second.substr(9, 1), second.substr(28, 35)); | ||
result.nationality = parseNationality(second.substring(10, 13)); | ||
result.birthDate = parseBirthdayDate(second.substring(13, 19), second.substr(19, 1)); | ||
result.sex = parseSex(second.substring(20, 21)); | ||
result.expirationDate = parseExpirationDate(second.substring(21, 27), second.substr(27, 1)); | ||
if (second.length !== 36) { | ||
result.error.push('Second line does not have 36 symbols'); | ||
} | ||
result.documentNumber = parseDocumentNumber(second.substring(0, 9), second.substr(9, 1), second.substr(28, 35)); | ||
result.nationality = parseNationality(second.substring(10, 13)); | ||
result.birthDate = parseBirthdayDate(second.substring(13, 19), second.substr(19, 1)); | ||
result.sex = parseSex(second.substring(20, 21)); | ||
result.expirationDate = parseExpirationDate(second.substring(21, 27), second.substr(27, 1)); | ||
result.globalCheck = globalCheck(second.substring(0, 10) + second.substring(13, 20) + second.substring(21, 35), second.substr(35, 1)); | ||
finalAnalysis(result); | ||
result.globalCheck = globalCheck(second.substring(0, 10) + second.substring(13, 20) + second.substring(21, 35), second.substr(35, 1)); | ||
finalAnalysis(result); | ||
return result; | ||
return result; | ||
}; |
@@ -16,31 +16,31 @@ 'use strict'; | ||
module.exports = function parseTD3(lines) { | ||
var result = { | ||
error: [], | ||
format: 'TD3' | ||
}; | ||
var result = { | ||
error: [], | ||
format: 'TD3' | ||
}; | ||
var first = lines[0]; | ||
var second = lines[1]; | ||
var first = lines[0]; | ||
var second = lines[1]; | ||
if (first.length !== 44) { | ||
result.error.push('First line does not have 44 symbols'); | ||
} | ||
result.documentType = parseDocumentType(first.substring(0, 2)); | ||
result.issuingCountry = parseCountry(first.substring(2, 5)); | ||
result.lastname = parseFirstname('Lastname', first.substring(5, 50)); | ||
result.firstname = parseLastname('Firstname', first.substring(5, 50)); | ||
result.documentNumber = parseDocumentNumber(second.substring(0, 9), second.substr(9, 1)); | ||
result.nationality = parseCountry(second.substring(10, 13)); | ||
result.birthDate = parseBirthdayDate(second.substring(13, 19), second.substr(19, 1)); | ||
if (first.length !== 44) { | ||
result.error.push('First line does not have 44 symbols'); | ||
} | ||
result.documentType = parseDocumentType(first.substring(0, 2)); | ||
result.issuingCountry = parseCountry(first.substring(2, 5)); | ||
result.lastname = parseFirstname('Lastname', first.substring(5, 50)); | ||
result.firstname = parseLastname('Firstname', first.substring(5, 50)); | ||
result.documentNumber = parseDocumentNumber(second.substring(0, 9), second.substr(9, 1)); | ||
result.nationality = parseCountry(second.substring(10, 13)); | ||
result.birthDate = parseBirthdayDate(second.substring(13, 19), second.substr(19, 1)); | ||
if (second.length !== 44) { | ||
result.error.push('Second line does not have 44 symbols'); | ||
} | ||
result.sex = parseSex(second.substring(20, 21)); | ||
result.expirationDate = parseExpirationDate(second.substring(21, 27), second.substr(27, 1)); | ||
result.personalNumber = parsePersonalNumber(second.substring(28, 42)); | ||
result.globalCheck = globalCheck(second.substring(0, 10) + second.substring(13, 20) + second.substring(21, 43), second.substr(43, 1)); | ||
finalAnalysis(result); | ||
if (second.length !== 44) { | ||
result.error.push('Second line does not have 44 symbols'); | ||
} | ||
result.sex = parseSex(second.substring(20, 21)); | ||
result.expirationDate = parseExpirationDate(second.substring(21, 27), second.substr(27, 1)); | ||
result.personalNumber = parsePersonalNumber(second.substring(28, 42)); | ||
result.globalCheck = globalCheck(second.substring(0, 10) + second.substring(13, 20) + second.substring(21, 43), second.substr(43, 1)); | ||
finalAnalysis(result); | ||
return result; | ||
return result; | ||
}; |
'use strict'; | ||
module.exports = function check(string, value) { | ||
var code = 0; | ||
var factors = [7, 3, 1]; | ||
for (var i = 0; i < string.length; i++) { | ||
var charCode = string.charCodeAt(i); | ||
if (charCode === 60) charCode = 0; | ||
if (charCode >= 65) charCode -= 55; | ||
if (charCode >= 48) charCode -= 48; | ||
charCode *= factors[i % 3]; | ||
code += charCode; | ||
} | ||
return code % 10 === Number(value); | ||
var code = 0; | ||
var factors = [7, 3, 1]; | ||
for (var i = 0; i < string.length; i++) { | ||
var charCode = string.charCodeAt(i); | ||
if (charCode === 60) charCode = 0; | ||
if (charCode >= 65) charCode -= 55; | ||
if (charCode >= 48) charCode -= 48; | ||
charCode *= factors[i % 3]; | ||
code += charCode; | ||
} | ||
return code % 10 === Number(value); | ||
}; |
'use strict'; | ||
module.exports = function checkSeparator(label, source) { | ||
var result = { | ||
source, | ||
error: [], | ||
label | ||
}; | ||
if (!source.match(/^<*$/)) { | ||
result.error.push('The separator must be composed only by "<"'); | ||
} | ||
return result; | ||
var result = { | ||
source, | ||
error: [], | ||
label | ||
}; | ||
if (!source.match(/^<*$/)) { | ||
result.error.push('The separator must be composed only by "<"'); | ||
} | ||
return result; | ||
}; |
'use strict'; | ||
module.exports = function cleanText(string) { | ||
return string.replace(/<+$/g, '').replace(/</g, ' '); | ||
return string.replace(/<+$/g, '').replace(/</g, ' '); | ||
}; |
@@ -6,14 +6,14 @@ 'use strict'; | ||
module.exports = function globalCheck(source, value) { | ||
var checkResult = check(source, value); | ||
var error = []; | ||
if (!checkResult) { | ||
error.push('Check digit error.'); | ||
} | ||
return { | ||
ifValid: checkResult, | ||
source, | ||
value: (checkResult) ? 'valid' : 'non valid', | ||
label: 'Global check digit', | ||
error | ||
}; | ||
var checkResult = check(source, value); | ||
var error = []; | ||
if (!checkResult) { | ||
error.push('Check digit error.'); | ||
} | ||
return { | ||
ifValid: checkResult, | ||
source, | ||
value: checkResult ? 'valid' : 'non valid', | ||
label: 'Global check digit', | ||
error | ||
}; | ||
}; |
@@ -6,5 +6,5 @@ 'use strict'; | ||
module.exports = function parseBirthdayDateDate(value, checkDigit) { | ||
var result = parseDate(value, checkDigit); | ||
result.label = 'Birthday date'; | ||
return result; | ||
var result = parseDate(value, checkDigit); | ||
result.label = 'Birthday date'; | ||
return result; | ||
}; |
@@ -6,13 +6,13 @@ 'use strict'; | ||
module.exports = function parseCountry(source) { | ||
var country = COUNTRIES[source]; | ||
var result = { | ||
source, | ||
value: country || source, | ||
label: 'Country', | ||
error: [] | ||
}; | ||
if (!country) { | ||
result.error.push('The country code "' + source + '" is unknown'); | ||
} | ||
return result; | ||
var country = COUNTRIES[source]; | ||
var result = { | ||
source, | ||
value: country || source, | ||
label: 'Country', | ||
error: [] | ||
}; | ||
if (!country) { | ||
result.error.push(`The country code "${source}" is unknown`); | ||
} | ||
return result; | ||
}; |
@@ -6,20 +6,20 @@ 'use strict'; | ||
module.exports = function parseDate(value, checkDigit) { | ||
var result = { | ||
error: [], | ||
source: value | ||
}; | ||
result.year = value.substring(0, 2); | ||
result.month = value.substring(2, 4); | ||
result.day = value.substring(4, 6); | ||
result.value = result.day + '.' + result.month + '.' + result.year; | ||
if (checkDigit !== false && !check(value, checkDigit)) { | ||
result.error.push('Check digit "' + checkDigit + '" not valid'); | ||
} | ||
if (result.month < 1 || result.month > 12) { | ||
result.error.push('Month "' + result.month + '" not valid'); | ||
} | ||
if (result.day < 1 || result.day > 31) { | ||
result.error.push('Day "' + result.day + '" not valid'); | ||
} | ||
return result; | ||
var result = { | ||
error: [], | ||
source: value | ||
}; | ||
result.year = value.substring(0, 2); | ||
result.month = value.substring(2, 4); | ||
result.day = value.substring(4, 6); | ||
result.value = `${result.day}.${result.month}.${result.year}`; | ||
if (checkDigit !== false && !check(value, checkDigit)) { | ||
result.error.push(`Check digit "${checkDigit}" not valid`); | ||
} | ||
if (result.month < 1 || result.month > 12) { | ||
result.error.push(`Month "${result.month}" not valid`); | ||
} | ||
if (result.day < 1 || result.day > 31) { | ||
result.error.push(`Day "${result.day}" not valid`); | ||
} | ||
return result; | ||
}; |
@@ -11,17 +11,18 @@ 'use strict'; | ||
module.exports = function parseDocumentNumber(source, checkDigit, optional) { | ||
if (checkDigit === '<' && optional) { | ||
optional = optional.replace(/<.*/, ''); | ||
source += optional.substring(0, optional.length - 1); | ||
checkDigit = optional.charAt(optional.length - 1); | ||
} | ||
var result = { | ||
source, | ||
label: 'Document number', | ||
value: source.replace(/<*$/, ''), | ||
error: [] | ||
}; | ||
if (!check(source, checkDigit)) { | ||
result.error.push('Check digit "' + checkDigit + '" not valid'); | ||
} | ||
return result; | ||
if (checkDigit === '<' && optional) { | ||
optional = optional.replace(/<.*/, ''); | ||
source += checkDigit + optional.substring(0, optional.length - 1); | ||
checkDigit = optional.charAt(optional.length - 1); | ||
} | ||
var result = { | ||
source, | ||
label: 'Document number', | ||
value: source.replace(/</g, ''), | ||
error: [] | ||
}; | ||
if (!check(source, checkDigit)) { | ||
result.error.push(`Check digit "${checkDigit}" not valid`); | ||
} | ||
return result; | ||
}; |
@@ -5,31 +5,31 @@ 'use strict'; | ||
module.exports = function parseDocumentType(source) { | ||
var code = source.substring(0, 1); | ||
var type = source.substring(1, 2).replace('<', ''); | ||
var code = source.substring(0, 1); | ||
var type = source.substring(1, 2).replace('<', ''); | ||
var result = { | ||
source, | ||
label: 'Document type', | ||
error: [] | ||
}; | ||
var result = { | ||
source, | ||
label: 'Document type', | ||
error: [] | ||
}; | ||
switch (code) { | ||
case 'P': | ||
result.value = 'Passport'; | ||
break; | ||
case 'I': | ||
result.value = 'Identity card'; | ||
break; | ||
case 'A': | ||
result.value = ''; | ||
break; | ||
case 'C': | ||
result.value = ''; | ||
break; | ||
default: | ||
result.error.push('Document type must be either P, I, A or C'); | ||
} | ||
if (type === 'V') { | ||
result.error.push('Document type (second symbol) may not be V'); | ||
} | ||
return result; | ||
switch (code) { | ||
case 'P': | ||
result.value = 'Passport'; | ||
break; | ||
case 'I': | ||
result.value = 'Identity card'; | ||
break; | ||
case 'A': | ||
result.value = ''; | ||
break; | ||
case 'C': | ||
result.value = ''; | ||
break; | ||
default: | ||
result.error.push('Document type must be either P, I, A or C'); | ||
} | ||
if (type === 'V') { | ||
result.error.push('Document type (second symbol) may not be V'); | ||
} | ||
return result; | ||
}; |
@@ -6,5 +6,5 @@ 'use strict'; | ||
module.exports = function parseExpirationDate(value, checkDigit) { | ||
var result = parseDate(value, checkDigit); | ||
result.label = 'Expiration date'; | ||
return result; | ||
var result = parseDate(value, checkDigit); | ||
result.label = 'Expiration date'; | ||
return result; | ||
}; |
@@ -6,4 +6,4 @@ 'use strict'; | ||
module.exports = function parseFirstname(label, source) { | ||
var result = parseText('Firstname', source.replace(/<{2}.*/, ''), /^[A-Z<]+<*$/); | ||
return result; | ||
var result = parseText('Firstname', source.replace(/<{2}.*/, ''), /^[A-Z<]+<*$/); | ||
return result; | ||
}; |
@@ -6,5 +6,5 @@ 'use strict'; | ||
module.exports = function parseIssuingCountry(value) { | ||
var result = parseCountry(value); | ||
result.label = 'Issuing country'; | ||
return result; | ||
var result = parseCountry(value); | ||
result.label = 'Issuing country'; | ||
return result; | ||
}; |
@@ -6,4 +6,4 @@ 'use strict'; | ||
module.exports = function parseFirstname(label, source) { | ||
var result = parseText('Lastname', source.replace(/.*?<{2}/, ''), /^[A-Z<]+<*$/); | ||
return result; | ||
var result = parseText('Lastname', source.replace(/.*?<{2}/, ''), /^[A-Z<]+<*$/); | ||
return result; | ||
}; |
@@ -6,5 +6,5 @@ 'use strict'; | ||
module.exports = function parseIssuingCountry(value) { | ||
var result = parseCountry(value); | ||
result.label = 'Nationality'; | ||
return result; | ||
var result = parseCountry(value); | ||
result.label = 'Nationality'; | ||
return result; | ||
}; |
'use strict'; | ||
module.exports = function parseNumber(label, source) { | ||
var result = { | ||
error: [], | ||
label, | ||
source | ||
}; | ||
if (!source.match(/^[0-9]+$/)) { | ||
result.error.push('It may only be composed of numbers'); | ||
} | ||
var result = { | ||
error: [], | ||
label, | ||
source | ||
}; | ||
if (!source.match(/^[0-9]+$/)) { | ||
result.error.push('It may only be composed of numbers'); | ||
} | ||
return result; | ||
return result; | ||
}; |
@@ -7,7 +7,7 @@ 'use strict'; | ||
module.exports = function parseExpirationDate(value, checkDigit) { | ||
var result = parseText('Personal number', value, /^[A-Z0-9<]+<*$/); | ||
if (checkDigit && !check(value, checkDigit)) { | ||
result.error.push('Check digit "' + checkDigit + '" not valid'); | ||
} | ||
return result; | ||
var result = parseText('Personal number', value, /^[A-Z0-9<]+<*$/); | ||
if (checkDigit && !check(value, checkDigit)) { | ||
result.error.push(`Check digit "${checkDigit}" not valid`); | ||
} | ||
return result; | ||
}; |
'use strict'; | ||
module.exports = function parseSex(source) { | ||
var result = { | ||
source, | ||
label: 'Sex', | ||
error: [] | ||
}; | ||
switch (source) { | ||
case '<': | ||
result.value = 'Unknown'; | ||
break; | ||
case 'M': | ||
result.value = 'Male'; | ||
break; | ||
case 'F': | ||
result.value = 'Female'; | ||
break; | ||
default: | ||
result.error.push(`The sex "${source}" is incorrect. Allowed values: M, F or <.`); | ||
} | ||
var result = { | ||
source, | ||
label: 'Sex', | ||
error: [] | ||
}; | ||
switch (source) { | ||
case '<': | ||
result.value = 'Unknown'; | ||
break; | ||
case 'M': | ||
result.value = 'Male'; | ||
break; | ||
case 'F': | ||
result.value = 'Female'; | ||
break; | ||
default: | ||
result.error.push(`The sex "${source}" is incorrect. Allowed values: M, F or <.`); | ||
} | ||
return result; | ||
return result; | ||
}; |
@@ -6,12 +6,12 @@ 'use strict'; | ||
module.exports = function parseText(label, source, regexp = /^[0-9A-Z<]+$/) { | ||
var result = { | ||
source, | ||
label, | ||
value: cleanText(source), | ||
error: [] | ||
}; | ||
if (!source.match(regexp)) { | ||
result.error.push('It must match the following regexp: ' + regexp); | ||
} | ||
return result; | ||
var result = { | ||
source, | ||
label, | ||
value: cleanText(source), | ||
error: [] | ||
}; | ||
if (!source.match(regexp)) { | ||
result.error.push(`It must match the following regexp: ${regexp}`); | ||
} | ||
return result; | ||
}; |
@@ -5,40 +5,40 @@ 'use strict'; | ||
module.exports = function parseDocumentNumber(source) { // swiss driving license number | ||
var first = source.substring(0, 3); | ||
var second = source.substring(3, 6); | ||
var language = source.charAt(6); | ||
var end = source.substring(7); | ||
var first = source.substring(0, 3); | ||
var second = source.substring(3, 6); | ||
var language = source.charAt(6); | ||
var end = source.substring(7); | ||
var result = { | ||
label: 'Document number', | ||
source, | ||
error: [] | ||
}; | ||
if (!first.match(/^[A-Z]{3}$/)) { | ||
result.error.push(`The document number "${source}" is incorrect. Need to start by 3 uppercase letters.`); | ||
} | ||
if (!second.match(/^[0-9]{3}$/)) { | ||
result.error.push(`The document number "${source}" is incorrect. Need to have 3 digits in position 3, 4 and 5.`); | ||
} | ||
if (end !== '<<') { | ||
result.error.push(`The document number "${source}" is incorrect. Need to end with <<.`); | ||
} | ||
var languageDescription; | ||
switch (language) { | ||
case 'D': | ||
languageDescription = 'German'; | ||
break; | ||
case 'F': | ||
languageDescription = 'French'; | ||
break; | ||
case 'I': | ||
languageDescription = 'Italian'; | ||
break; | ||
case 'R': | ||
languageDescription = 'Romansh'; | ||
break; | ||
default: | ||
result.error.push(`The document number "${source}" is incorrect. Language ${language} unknown.`); | ||
} | ||
result.value = first + second + ' - language: ' + languageDescription; | ||
return result; | ||
var result = { | ||
label: 'Document number', | ||
source, | ||
error: [] | ||
}; | ||
if (!first.match(/^[A-Z]{3}$/)) { | ||
result.error.push(`The document number "${source}" is incorrect. Need to start by 3 uppercase letters.`); | ||
} | ||
if (!second.match(/^[0-9]{3}$/)) { | ||
result.error.push(`The document number "${source}" is incorrect. Need to have 3 digits in position 3, 4 and 5.`); | ||
} | ||
if (end !== '<<') { | ||
result.error.push(`The document number "${source}" is incorrect. Need to end with <<.`); | ||
} | ||
var languageDescription; | ||
switch (language) { | ||
case 'D': | ||
languageDescription = 'German'; | ||
break; | ||
case 'F': | ||
languageDescription = 'French'; | ||
break; | ||
case 'I': | ||
languageDescription = 'Italian'; | ||
break; | ||
case 'R': | ||
languageDescription = 'Romansh'; | ||
break; | ||
default: | ||
result.error.push(`The document number "${source}" is incorrect. Language ${language} unknown.`); | ||
} | ||
result.value = `${first + second} - language: ${languageDescription}`; | ||
return result; | ||
}; |
'use strict'; | ||
module.exports = function parseDocumentType(source) { | ||
var result = { | ||
label: 'Document type', | ||
source, | ||
error: [] | ||
}; | ||
switch (result.source) { | ||
case 'FA': | ||
result.value = 'Swiss driving license'; | ||
break; | ||
default: | ||
result.error.push('Swiss driving license must have a document type "FA"'); | ||
} | ||
return result; | ||
var result = { | ||
label: 'Document type', | ||
source, | ||
error: [] | ||
}; | ||
switch (result.source) { | ||
case 'FA': | ||
result.value = 'Swiss driving license'; | ||
break; | ||
default: | ||
result.error.push('Swiss driving license must have a document type "FA"'); | ||
} | ||
return result; | ||
}; |
@@ -10,24 +10,24 @@ 'use strict'; | ||
module.exports = function globalCheck(result) { | ||
result.isValid = true; | ||
result.logs = []; | ||
for (var key of Object.keys(result)) { | ||
if (result[key] instanceof Object && !Array.isArray(result[key])) { | ||
if (result[key].error && result[key].error.length > 0) { | ||
result[key].isValid = false; | ||
result.isValid = false; | ||
for (var err of result[key].error) { | ||
result.error.push(result[key].label + ': ' + err); | ||
} | ||
} else { | ||
result[key].isValid = true; | ||
} | ||
result.logs.push( | ||
Object.assign({}, result[key], {field: key}) | ||
); | ||
result.isValid = true; | ||
result.logs = []; | ||
for (var key of Object.keys(result)) { | ||
if (result[key] instanceof Object && !Array.isArray(result[key])) { | ||
if (result[key].error && result[key].error.length > 0) { | ||
result[key].isValid = false; | ||
result.isValid = false; | ||
for (var err of result[key].error) { | ||
result.error.push(`${result[key].label}: ${err}`); | ||
} | ||
} else { | ||
result[key].isValid = true; | ||
} | ||
result.logs.push( | ||
Object.assign({}, result[key], { field: key }) | ||
); | ||
} | ||
} | ||
// we will also create the result as a table call 'logs' | ||
// we will also create the result as a table call 'logs' | ||
}; |
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
6
33
557
54
27364