-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parse individual lines from HH Timing replay files.
- Loading branch information
1 parent
798e69c
commit ab5be9b
Showing
5 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,5 +35,6 @@ | |
"jest": "^29.0.0" | ||
}, | ||
"dependencies": { | ||
"xml2js": "^0.6.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { DecodeError, decodeLine } from '../index.js'; | ||
|
||
const VALID_B64_ENCODED_DATA = 'PFBBU1M+DQogIDxJRD5mMGZmMzk4Zi00OWNhLTQ3MzItODA2Ni0wZTY1YzZlMTBlZGQ8L0lEPg0KICA8RFQ+MDIuMDcuMjAyMyAwMjo1MDozOC4wOTY8L0RUPg0KICA8QkI+OTExPC9CQj4NCiAgPElUPjM5ODExPC9JVD4NCiAgPExQPjIzNjwvTFA+DQogIDxTVD5JMTwvU1Q+DQogIDxWRT4yNjIuNzczPC9WRT4NCiAgPERSPjQ8L0RSPg0KPC9QQVNTPg=='; | ||
|
||
const DECODED_DATA = { | ||
'PASS': { | ||
'BB': '911', | ||
'DR': '4', | ||
'DT': '02.07.2023 02:50:38.096', | ||
'ID': 'f0ff398f-49ca-4732-8066-0e65c6e10edd', | ||
'IT': '39811', | ||
'LP': '236', | ||
'ST': 'I1', | ||
'VE': '262.773', | ||
} | ||
}; | ||
|
||
describe('HH Timing', () => { | ||
describe('#decodeLine', () => { | ||
|
||
it('decodes a line', async () => { | ||
const source = `2023-07-02 09:38:22.306395|${VALID_B64_ENCODED_DATA}`; | ||
const decoded = await decodeLine(source); | ||
|
||
expect(decoded.timestamp).toEqual(new Date('2023-07-02 09:38:22.306395')); | ||
expect(decoded.data).toEqual(DECODED_DATA); | ||
}); | ||
|
||
it('throws exception if no pipe separator is present', async () => { | ||
expect(decodeLine('no pipe here sir')).rejects.toThrow(DecodeError); | ||
}); | ||
|
||
it('throws exception if the timestamp is junk', async () => { | ||
expect(decodeLine(`not a timestamp|${VALID_B64_ENCODED_DATA}`)).rejects.toThrow(DecodeError); | ||
}); | ||
|
||
it('throws exception if decoded XML is junk', () => { | ||
const invalidXML = btoa('<foo><bar></foos>'); | ||
expect(decodeLine(`2023-07-02 09:38:22.306395|${invalidXML}`)).rejects.toThrow(DecodeError); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { parseStringPromise } from 'xml2js'; | ||
|
||
export class DecodeError extends Error {}; | ||
|
||
export class HHTimingConverter { | ||
convert(hhFile) { | ||
|
||
} | ||
} | ||
|
||
export const decodeLine = async (lineText) => { | ||
const parts = lineText.split('|'); | ||
|
||
if (parts.length !== 2) { | ||
throw new DecodeError('Line did not contain pipe separator (|)'); | ||
} | ||
|
||
const timestamp = new Date(parts[0]); | ||
|
||
if (isNaN(timestamp)) { | ||
throw new DecodeError(`Timestamp '${parts[0]}' was not valid`); | ||
} | ||
|
||
const xmlData = atob(parts[1]); | ||
try { | ||
const data = await parseStringPromise(xmlData, { explicitArray: false }); | ||
return { | ||
timestamp, | ||
data | ||
}; | ||
} | ||
catch (e) { | ||
throw new DecodeError(e); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters