Skip to content

Commit

Permalink
#4 update code to use ECMAScript 6 syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenquan committed Jun 14, 2019
1 parent 965197d commit 97fa72e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 73 deletions.
66 changes: 32 additions & 34 deletions AAMVAtoJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,46 @@ function AAMVAtoJSON(data) {
// 2. Third character should be 0x1e but we ignore because of South Carolina 0x1c edge condition
// 3. Next 5 characters either "ANSI " or "AAMVA"
// 4. Next 12 characters: IIN, AAMVAVersion, JurisdictionVersion, numberOfEntries
var m = data.match(/^@\n.\r(ANSI |AAMVA)(\d{6})(\d{2})(\d{2})(\d{2})/);
if (!m) {
return null
}
const [ __data,
AAMVAType,
IIN,
AAMVAVersion,
jurisdictionVersion,
numberOfEntries ] = data.match(/^@\n.\r(ANSI |AAMVA)(\d{6})(\d{2})(\d{2})(\d{2})/);
if (!__data) return null

var obj = {
header: {
IIN: m[2],
AAMVAVersion: parseInt(m[3]),
jurisdictionVersion: parseInt(m[4]),
numberOfEntries: parseInt(m[5])
IIN: IIN,
AAMVAVersion: +AAMVAVersion,
jurisdictionVersion: +jurisdictionVersion,
numberOfEntries: +numberOfEntries
}
};
}

for (var i = 0; i < obj.header.numberOfEntries; i++) {
var offset = 21 + i * 10
m = data.substring(offset, offset + 10).match(/(.{2})(\d{4})(\d{4})/)
var subfileType = m[1]
var offset = parseInt(m[2])
var length = parseInt(m[3])
if (i === 0) {
obj.files = [ subfileType ]
} else {
obj.files.push(subfileType)
}
obj[subfileType] = data.substring(offset + 2, offset + length).trim().split(/\n\r?/).reduce(function (p, c) {
p[c.substring(0,3)] = c.substring(3);
return p;
for (let i = 0; i < obj.header.numberOfEntries; i++) {
let entryOffset = 21 + i * 10
let [ __entry, subfileType, offset, length ]
= data.substring(entryOffset, entryOffset + 10).match(/(.{2})(\d{4})(\d{4})/)
if (i === 0) obj.files = [ ]
obj.files.push(subfileType)
obj[subfileType] = data.substring(+offset + 2, +offset + +length).trim().split(/\n\r?/).reduce((p, c) => {
p[c.substring(0,3)] = c.substring(3)
return p
}, { } )
}

// Convert date string (in local timezone) into Javascript UTC time
function convertAAMVADate(str, country) {
function convertAAMVADateUSA(str) {
var m = str.match(/(\d{2})(\d{2})(\d{4})/)
if (!m) return null
return new Date(parseInt(m[3]), parseInt(m[1]) - 1, parseInt(m[2])).getTime()
const [ __str, month, day, year ] = str.match(/(\d{2})(\d{2})(\d{4})/)
if (!__str) return null
return new Date(year, month-1, day).getTime()
}
function convertAAMVADateCAN(str) {
var m = str.match(/(\d{4})(\d{2})(\d{2})/)
if (!m) return null
return new Date(parseInt(m[1]), parseInt(m[2]) - 1, parseInt(m[3])).getTime()
const [ __str, year, month, day ] = str.match(/(\d{4})(\d{2})(\d{2})/)
if (!__str) return null
return new Date(year, month-1, day).getTime()
}
switch (country) {
case "USA": return convertAAMVADateUSA(str)
Expand All @@ -55,12 +53,12 @@ function AAMVAtoJSON(data) {
}

if (obj.DL) {
["DBA", "DBB", "DBD", "DDB", "DDC", "DDH", "DDI", "DDJ"].forEach(function (k) {
if (!obj.DL[k]) return
var t = convertAAMVADate(obj.DL[k], obj.DL.DCG)
if (!t) return
for (let k of ["DBA", "DBB", "DBD", "DDB", "DDC", "DDH", "DDI", "DDJ"]) {
if (!obj.DL[k]) continue
const t = convertAAMVADate(obj.DL[k], obj.DL.DCG)
if (!t) continue
obj.DL[k] = t
} )
}
}

return obj
Expand Down
12 changes: 6 additions & 6 deletions test-output.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"header": {
"AAMVAVersion": 8,
"IIN": "636000",
"AAMVAVersion": 8,
"jurisdictionVersion": 0,
"numberOfEntries": 2
},
Expand All @@ -21,9 +21,9 @@
"DCA": "D",
"DCB": "K",
"DCD": "PH",
"DBD": 1212710400000,
"DBB": 518400000000,
"DBA": 1386633600000,
"DBD": 1212674400000,
"DBB": 518364000000,
"DBA": 1386594000000,
"DBC": "1",
"DAU": "068 in",
"DAY": "BRO",
Expand All @@ -35,8 +35,8 @@
"DCG": "USA",
"DCK": "123456789",
"DDA": "M",
"DDB": 1212710400000,
"DDC": 1244246400000,
"DDB": 1212674400000,
"DDC": 1244210400000,
"DDD": "1"
},
"ZV": {
Expand Down
63 changes: 30 additions & 33 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,34 @@
// 2013 AAMVA DL/ID Card Design Standard
// D.13 Example of raw PDF417 data

var data = [
"@\n\u001e\r",
"ANSI 636000080002DL00410278ZV03190008DLDAQT64235789L\n",
"DCSSAMPLE\n",
"DDENL\n",
"DACMICHAEL\n",
"DDFN\n",
"DADJOHN\n",
"DDGN\n",
"DCUJR\n",
"DCAD\n",
"DCBK\n",
"DCDPH\n",
"DBD06062008\n",
"DBB06061986\n",
"DBA12102013\n",
"DBC1\n",
"DAU068 in\n",
"DAYBRO\n",
"DAG2300 WEST BROAD STREET\n",
"DAIRICHMOND\n",
"DAJVA\n",
"DAK232690000\n",
"DCF2424244747474786102204\n",
"DCGUSA\n",
"DCK123456789\n",
"DDAM\n",
"DDB06062008\n",
"DDC06062009\n",
"DDD1\r",
"ZVZVA01\r"
].join("");
data = `@
\u001e\rANSI 636000080002DL00410278ZV03190008DLDAQT64235789L
DCSSAMPLE
DDENL
DACMICHAEL
DDFN
DADJOHN
DDGN
DCUJR
DCAD
DCBK
DCDPH
DBD06062008
DBB06061986
DBA12102013
DBC1
DAU068 in
DAYBRO
DAG2300 WEST BROAD STREET
DAIRICHMOND
DAJVA
DAK232690000
DCF2424244747474786102204
DCGUSA
DCK123456789
DDAM
DDB06062008
DDC06062009
DDD1\rZVZVA01\r`

AAMVAtoJSON(data);
AAMVAtoJSON(data)

0 comments on commit 97fa72e

Please sign in to comment.