Skip to content

Commit 056bacd

Browse files
committed
adds field canonocalization to ensure field order on export
1 parent deec129 commit 056bacd

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

src/js/editView.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,37 @@ function ensureProfileDefinitions(mesgDefinitions) {
788788
mesgDefinitions.forEach(onMesgDefinition);
789789
}
790790

791+
// Reorders a message's fields by their Profile field number.
792+
//
793+
// This bites swim lengths: editing the stroke of a length that had no
794+
// swim_stroke field (`{ ...entry, swimStroke }`) appends the key last, while
795+
// lengths that already carried a stroke keep it earlier. The drill lengths
796+
// then decode with length_type/swim_stroke shifted (e.g. drill -> backstroke,
797+
// active -> a stray cadence value). Forcing a canonical field order makes all
798+
// messages of a type share one consistent definition.
799+
800+
function canonicalizeFields(mesgNum, fields) {
801+
const mesgProfile = Profile.messages[mesgNum];
802+
if (!mesgProfile) return fields;
803+
804+
const numByName = {};
805+
for (const key in mesgProfile.fields) {
806+
const f = mesgProfile.fields[key];
807+
numByName[f.name] = f.num;
808+
}
809+
810+
const keys = Object.keys(fields);
811+
const known = keys
812+
.filter(k => k in numByName)
813+
.sort((a, b) => numByName[a] - numByName[b]);
814+
const rest = keys.filter(k => !(k in numByName)); // e.g. developerFields
815+
816+
const ordered = {};
817+
for (const k of known) ordered[k] = fields[k];
818+
for (const k of rest) ordered[k] = fields[k];
819+
return ordered;
820+
}
821+
791822
// Converts modifiedData into a flat message list for export
792823
function prepareExportData(modifiedData) {
793824

@@ -808,7 +839,9 @@ function prepareExportData(modifiedData) {
808839
for (const [messageName, messages] of messageGroups) {
809840
const mesgNum = getMesgNumByMessagesKey(messageName);
810841
if (mesgNum == null) continue;
811-
for (const fields of messages) allMessages.push({ mesgNum, ...fields });
842+
for (const fields of messages) {
843+
allMessages.push({ mesgNum, ...canonicalizeFields(mesgNum, fields) });
844+
}
812845
}
813846
return allMessages;
814847
}

0 commit comments

Comments
 (0)