FIT Javascript SDK - Profile.js implementation

Hi all, I would like to ask a question about the implementation of Profile.js generated by FitGen. 

In the object types of Profile.js key/values are described like below:

 

gender: {
   0: "female",
   1: "male",
},

I generally use the structure types to making comparisons. 

This is an example of an userProfile parsed:

"userProfile": {
  "friendlyName":"Example",
  "gender": 1,
  "age": 34,
  "height": 1.8,
  "weight": 75,
  "language": 0,
  "elevSetting": 0,
  "weightSetting": 0,
  "restingHeartRate": 0,
  "defaultMaxBikingHeartRate": 0,
  "defaultMaxHeartRate": 0,
  "speedSetting": 0,
  "distSetting": 0,
  "powerSetting": 0,
  "temperatureSetting": 0,
  "localId": 0,
  "globalId": [0, 0, 0, 0, 0, 0],
  "heightSetting": 0,
  "positionSetting": 2
},

My question would be: Which is the best approach to compare the gender in userProfile?

I would expect something like:

import { Profile } from '@garmin/fitsdk';

...
const decoder = new Decoder(stream);
const { messages, errors } = decoder.read();
...

// isFemale check
messages?.userProfile?.gender === Profile.types.gender.female

But I couldn't  access the object gender like that, only by numerical value.

The only way, I found is:

const genderName = Profile.types.gender[messages?.userProfile?.gender]

// isFemale check
genderName === Profile.types.gender[0]

Would not be better to organise the objects in types, like this?

 

gender: {
   female: 0,
   male: 1,
},

Am I missing something? 

Thanks so much for the help, as always 

  • The Profile object was designed to facilitate the decoding of files, which is why the keys are integers and the values are the strings. The binary file will contain the integer values for enum field. So during decoding 0 is read from the file and then 0 is used as a key into the gender enum so that "female" can be used as the value in the output message. If the key/values were the other way around the decoding would be slower. 

    The values in the profile do not change, if they changed it would break a lot of code, so for your specific example you could say genderName === "female". The value "female" is not going to change. 

    Depending on your use case, you can also look at the `convertTypesToStrings` decoder option, then you are not doing string comparison.   

  • The Profile object was designed to facilitate the decoding of files, which is why the keys are integers and the values are the strings. The binary file will contain the integer values for enum field. So during decoding 0 is read from the file and then 0 is used as a key into the gender enum so that "female" can be used as the value in the output message. If the key/values were the other way around the decoding would be slower. 

    Make sense.

    Thanks for the help!