Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions _tests/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ const simpleDescriptorOutput = [
'0xC0, // End Collection',
].join('\n') + '\n';

const noSeparatorDescriptorOutput = [
'\\x05\\x01' + ' '.repeat(16) + '// Usage Page (Generic Desktop Page)',
'\\x09\\x02' + ' '.repeat(16) + '// Usage (Mouse)',
'\\xA1\\x01' + ' '.repeat(16) + '// Collection (Application)',
'\\x81\\x02' + ' '.repeat(16) + '// Input (Data,Variable,Absolute,No Wrap,Linear,Preferred State,No Null position,Bit Field)',
'\\xC0' + ' '.repeat(20) + '// End Collection',
].join('\n') + '\n';

test('stripComments removes C-style, C++-style and hash comments', () => {
const input = `0x01, 0x02 // comment\n# another\n/* block\ncomment */\n0x03`;
const result = stripComments(input);
Expand All @@ -36,6 +44,11 @@ test('parseDescriptor falls back to default formatting options', () => {
assert.strictEqual(output, simpleDescriptorOutput);
});

test('parseDescriptor supports empty string separator', () => {
const output = parseDescriptor(simpleDescriptorBytes, { separator: '', prefix: '\\x' });
assert.strictEqual(output, noSeparatorDescriptorOutput);
});

test('parseDescriptor reports parsing errors in the output', () => {
const originalError = console.error;
let loggedMessage = '';
Expand Down
10 changes: 7 additions & 3 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ if (typeof document !== "undefined") {
.split(/[\s,]+/)
.map(b => parseInt(b, 16))
.filter(n => !isNaN(n));
const separatorInput = document.getElementById("separator");
const prefixInput = document.getElementById("prefix");
const commentInput = document.getElementById("commentSymbol");

const opts = {
separator: (document.getElementById("separator")?.value) || ",",
prefix: (document.getElementById("prefix")?.value) || "0x",
comment: (document.getElementById("commentSymbol")?.value) || "//",
separator: separatorInput?.value ?? ",",
prefix: prefixInput?.value ?? "0x",
comment: commentInput?.value ?? "//",
};

const parsed = parseDescriptor(bytes, opts);
Expand Down