Skip to content

Commit

Permalink
parse Canadian licenses
Browse files Browse the repository at this point in the history
  • Loading branch information
nathany committed Aug 10, 2018
1 parent 1c91729 commit df434c9
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 9 deletions.
21 changes: 21 additions & 0 deletions spec/fixtures/canada.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
exports.canada = {
'addressCity': "YOUR CITY",
'addressPostalCode': "A1B 2C3",
'addressState': "AB",
'addressStreet': "2345 ANYWHERE STREET",
'dateOfBirth': '2000/08/31',
'dateOfExpiry': '2022/08/31',
'documentNumber': "123456-123",
'eyeColor': "green",
'hairColor': "blond",
'firstName': "M",
'givenName': "M Motorist",
'height': "175 cm",
'weightLb': "73 kg",
'weightRange': "4",
'jurisdictionVehicleClass': "5",
'jurisdictionRestrictionCodes': "A",
'lastName': "MICHAEL",
'middleName': "Motorist",
'sex': "M",
};
3 changes: 2 additions & 1 deletion spec/fixtures/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
exports.default = require('./default').default;
exports.default = require('./default').default;
exports.canada = require('./canada').canada;
14 changes: 13 additions & 1 deletion spec/parseUsdl.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
const UsdlData1 = require('./sample/index').UsdlData1;
const UsdlData2 = require('./sample/index').UsdlData2;
const UsdlData3 = require('./sample/index').UsdlData3;
const UsdlData4 = require('./sample/index').UsdlData4;
const UsdlData_error = require('./sample/index').UsdlData_error;
const UsdlData_invalid_characters = require('./sample/index').UsdlData_invalid_characters;
const UsdlData_invalid_characters_2 = require('./sample/index').UsdlData_invalid_characters_2;
const default_fixture = require('./fixtures').default;
const canada_fixture = require('./fixtures').canada;

const parse = require("../index").parse;

describe("USDL Parser", () => {
it("should parse correct values", () => {
const parsedData = parse(UsdlData1);
expect(parsedData).toEqual(default_fixture)
expect(parsedData).toEqual(default_fixture);
});

it("should correctly identify female", () => {
const parsedData = parse(UsdlData2);
expect(parsedData.sex).toBe("F");
});

it("should correctly identify unspecified gender", () => {
const parsedData = parse(UsdlData3);
expect(parsedData.sex).toBe("X");
});

it("should parse correct values", () => {
const parsedData = parse(UsdlData4);
expect(parsedData).toEqual(canada_fixture);
});

it("should not throw error if invalid code is passed and warming suppress is on", () => {
Expand Down
26 changes: 26 additions & 0 deletions spec/sample/data_3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
exports.UsdlData3 = `@

ANSI 636001070002DL00410392ZN04330047DLDCANONE
DCBNONE
DCDNONE
DBA08312013
DCSMichael
DACM
DADMotorist
DBD08312013
DBB08312013
DBC9
DAYBRO
DAU064 in
DAG2345 ANYWHERE STREET
DAIYOUR CITY
DAJNY
DAK123450000
DAQNONE
DCFNONE
DCGUSA
DDEN
DDFN
DDGN
ZNZNAMDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5
`;
23 changes: 23 additions & 0 deletions spec/sample/data_4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
exports.UsdlData4 = `@

ANSI 990876030001DLÊDLDCSMICHAEL
DCTM Motorist
DABMICHAEL
DACM
DADMotorist
DAG2345 ANYWHERE STREET
DAQ123456-123
DAKA1B 2C3
DAIYOUR CITY
DAJAB
DBCM
DBB20000831
DAR5
DATA
DBA20220831
DAU175 cm
DAW73 kg
DCE4
DAZblond
DAYgreen
`;
2 changes: 2 additions & 0 deletions spec/sample/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
exports.UsdlData1 = require('./data_1').UsdlData1;
exports.UsdlData2 = require('./data_2').UsdlData2;
exports.UsdlData3 = require('./data_3').UsdlData3;
exports.UsdlData4 = require('./data_4').UsdlData4;
exports.UsdlData_error = require('./data_error').UsdlData_error;
exports.UsdlData_invalid_characters = require('./data_invalid_characters').UsdlData_invalid_characters;
exports.UsdlData_invalid_characters_2 = require('./data_invalid_characters_2').UsdlData_invalid_characters_2;
4 changes: 4 additions & 0 deletions src/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ exports.CodeToKey = {
DCD: 'jurisdictionEndorsementCodes',
DBA: 'dateOfExpiry',
DCS: 'lastName',
DAB: 'lastName',
DAC: 'firstName',
DAD: 'middleName',
DAR: 'jurisdictionVehicleClass', // License Classification Code
DBD: 'dateOfIssue',
DBB: 'dateOfBirth',
DBC: 'sex',
Expand All @@ -17,8 +19,10 @@ exports.CodeToKey = {
DAJ: 'addressState',
DAK: 'addressPostalCode',
DAQ: 'documentNumber',
DAT: 'jurisdictionRestrictionCodes', // License Endorsements Code
DCF: 'documentDiscriminator',
DCG: 'issuer',
DCT: 'givenName',
DDE: 'lastNameTruncated',
DDF: 'firstNameTruncated',
DDG: 'middleNameTruncated',
Expand Down
38 changes: 31 additions & 7 deletions src/parseUsdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const lineSeparator = "\n";

const defaultOptions = {suppressErrors: false};

exports.parse = function parseCode128(str, options = defaultOptions) {
exports.parse = function parseDL(str, options = defaultOptions) {
const props = {};
const rawLines = str.trim().split(lineSeparator);
const lines = rawLines.map(rawLine => sanitizeData(rawLine));
let started;
lines.slice(0, -1).forEach(line => {
lines.forEach(line => {
if (!started) {
if (line.indexOf("ANSI ") === 0) {
started = true;
Expand All @@ -21,18 +21,27 @@ exports.parse = function parseCode128(str, options = defaultOptions) {
let value = getValue(line);
let key = getKey(code);
if (!key) {
if (options.suppressErrors) {
if (options.suppressErrors || code === "ZNZ") {
return;
} else {
throw new Error("unknown code: " + code);
}
}

if (isSexField(code)) value = getSex(code, value);

props[key] = isDateField(key) ? getDateFormat(value) : value;
props[key] = value;
});

// date format depends on issuer
const issuer = props["issuer"] || "CAN";
const getDateFormat = issuer === "USA" ? getDateFormatUSA : getDateFormatCAN;

for (let key in props) {
if (isDateField(key)) {
props[key] = getDateFormat(props[key]);
}
}

return props;
};

Expand All @@ -44,11 +53,26 @@ const getKey = code => CodeToKey[code];

const isSexField = code => code === "DBC";

const getSex = (code, value) => (value === "1" ? "M" : "F");
const getSex = (code, value) => {
if (value === "1" || value === "M") {
return "M";
} else if (value === "2" || value === "F") {
return "F";
}
return "X";
};

const isDateField = key => key.indexOf("date") === 0;

const getDateFormat = value => {
const getDateFormatUSA = value => {
const parts = [value.slice(0, 2), value.slice(2, 4), value.slice(4)];
return parts.join("/");
};

const getDateFormatCAN = value => {
const parts = [value.slice(0, 4), value.slice(4, 6), value.slice(6)];
return parts.join("/");
};



0 comments on commit df434c9

Please sign in to comment.