Skip to content

Commit

Permalink
Parse individual lines from HH Timing replay files.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesremuscat committed Jul 26, 2024
1 parent 798e69c commit ab5be9b
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 1 deletion.
8 changes: 7 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ module.exports = {
],
rules: {
'brace-style': ['error', 'stroustrup'],
'comma-dangle': ['warn', 'only-multiline'],
'dot-notation': 'off',
'operator-linebreak': ['warn', 'after'],
'import/extensions': ['error', 'ignorePackages'],
'padded-blocks': 'off',
'quote-props': 'off',
'semi': ['warn', 'always'],
'space-before-function-paren': ['error', 'never']
'space-before-function-paren': ['error', {
'anonymous': 'always',
'named': 'never',
'asyncArrow': 'always'
}]
}
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@
"jest": "^29.0.0"
},
"dependencies": {
"xml2js": "^0.6.2"
}
}
42 changes: 42 additions & 0 deletions src/hhtiming/__tests__/index.js
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);
});
});
});
35 changes: 35 additions & 0 deletions src/hhtiming/index.js
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);
}
};
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3082,6 +3082,11 @@ safe-regex-test@^1.0.3:
es-errors "^1.3.0"
is-regex "^1.1.4"

sax@>=0.6.0:
version "1.4.1"
resolved "https://registry.yarnpkg.com/sax/-/sax-1.4.1.tgz#44cc8988377f126304d3b3fc1010c733b929ef0f"
integrity sha512-+aWOz7yVScEGoKNd4PA10LZ8sk0A/z5+nXQG5giUO5rprX9jgYsTdov9qCchZiPIZezbZH+jRut8nPodFAX4Jg==

semver@^6.3.0, semver@^6.3.1:
version "6.3.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.1.tgz#556d2ef8689146e46dcea4bfdd095f3434dffcb4"
Expand Down Expand Up @@ -3495,6 +3500,19 @@ write-file-atomic@^4.0.2:
imurmurhash "^0.1.4"
signal-exit "^3.0.7"

xml2js@^0.6.2:
version "0.6.2"
resolved "https://registry.yarnpkg.com/xml2js/-/xml2js-0.6.2.tgz#dd0b630083aa09c161e25a4d0901e2b2a929b499"
integrity sha512-T4rieHaC1EXcES0Kxxj4JWgaUQHDk+qwHcYOCFHfiwKz7tOVPLq7Hjq9dM1WCMhylqMEfP7hMcOIChvotiZegA==
dependencies:
sax ">=0.6.0"
xmlbuilder "~11.0.0"

xmlbuilder@~11.0.0:
version "11.0.1"
resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-11.0.1.tgz#be9bae1c8a046e76b31127726347d0ad7002beb3"
integrity sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==

y18n@^5.0.5:
version "5.0.8"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-5.0.8.tgz#7f4934d0f7ca8c56f95314939ddcd2dd91ce1d55"
Expand Down

0 comments on commit ab5be9b

Please sign in to comment.