From 97fa72eace0d09d15afcde424e09fccec7a63a44 Mon Sep 17 00:00:00 2001 From: Stephen Quan Date: Sat, 15 Jun 2019 07:13:17 +1000 Subject: [PATCH] #4 update code to use ECMAScript 6 syntax --- AAMVAtoJSON.js | 66 +++++++++++++++++++++++------------------------- test-output.json | 12 ++++----- test.js | 63 ++++++++++++++++++++++----------------------- 3 files changed, 68 insertions(+), 73 deletions(-) diff --git a/AAMVAtoJSON.js b/AAMVAtoJSON.js index 2bc2af5..c4519f5 100644 --- a/AAMVAtoJSON.js +++ b/AAMVAtoJSON.js @@ -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) @@ -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 diff --git a/test-output.json b/test-output.json index 3650939..4139128 100644 --- a/test-output.json +++ b/test-output.json @@ -1,7 +1,7 @@ { "header": { - "AAMVAVersion": 8, "IIN": "636000", + "AAMVAVersion": 8, "jurisdictionVersion": 0, "numberOfEntries": 2 }, @@ -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", @@ -35,8 +35,8 @@ "DCG": "USA", "DCK": "123456789", "DDA": "M", - "DDB": 1212710400000, - "DDC": 1244246400000, + "DDB": 1212674400000, + "DDC": 1244210400000, "DDD": "1" }, "ZV": { diff --git a/test.js b/test.js index 26641f6..567fffe 100644 --- a/test.js +++ b/test.js @@ -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)