Skip to content
Open
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
10 changes: 9 additions & 1 deletion src/fit2json.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
const constants = require('./constants');
const { StringDecoder } = require('node:string_decoder');

/**
* Parses the buffer as a UTF-8 string.
*/
function decodeString(buffer) {
return new StringDecoder('utf8').end(Buffer.from(buffer));
}

/**
* Parses one record of a FIT file
Expand Down Expand Up @@ -169,7 +177,7 @@ function readDataField (baseType, fit, pointer, size, littleEndian) {
result.push(fit.getUint8(pointer + i));
}
}
return String.fromCharCode(...result);
return decodeString(result);
}
}

Expand Down
7 changes: 7 additions & 0 deletions test/fit2json.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ describe('/src/fit2json', () => {
const result = fit2json.readDataField(0x07, fit, 0, 6, false);
expect(result).to.equal('string');
});

it('should decode the string base type (0x07) as UTF-8', () => {
const fit = new DataView(new Uint8Array([0xf0, 0x9f, 0x92, 0xaa]).buffer);
const result = fit2json.readDataField(0x07, fit, 0, 4, false);
expect(result).to.equal('\uD83D\uDCAA');
});

it('should read a byte base type (0x0D) in big endian notation', () => {
const fit = new DataView(new Uint8Array([115,116,114,105,110,103]).buffer);
const result = fit2json.readDataField(0x0D, fit, 0, 6, false);
Expand Down