fit-file-parser 4.1.0 → 5.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +59 -0
- package/README.md +90 -1
- package/dist/binary.d.ts +2 -3
- package/dist/binary.js +23 -40
- package/dist/cjs/binary.d.ts +2 -3
- package/dist/cjs/binary.js +23 -40
- package/dist/cjs/fit-parser.js +4 -2
- package/dist/cjs/fit.d.ts +11 -9
- package/dist/cjs/fit.js +123 -9187
- package/dist/cjs/fit_types.d.ts +551 -555
- package/dist/cjs/garmin_profile.generated.js +479 -479
- package/dist/cjs/helper.js +22 -12
- package/dist/cjs/messages.d.ts +2 -3
- package/dist/cjs/type_generator.js +36 -35
- package/dist/fit-parser.js +4 -2
- package/dist/fit.d.ts +11 -9
- package/dist/fit.js +122 -9186
- package/dist/fit_types.d.ts +551 -555
- package/dist/garmin_profile.generated.js +479 -479
- package/dist/helper.js +22 -12
- package/dist/messages.d.ts +2 -3
- package/dist/type_generator.js +36 -35
- package/package.json +1 -1
package/dist/helper.js
CHANGED
|
@@ -1,18 +1,27 @@
|
|
|
1
|
+
function timestampValue(value) {
|
|
2
|
+
if (value === undefined || value === null) {
|
|
3
|
+
return undefined;
|
|
4
|
+
}
|
|
5
|
+
const timestamp = value instanceof Date
|
|
6
|
+
? value.getTime()
|
|
7
|
+
: new Date(value).getTime();
|
|
8
|
+
return Number.isFinite(timestamp) ? timestamp : undefined;
|
|
9
|
+
}
|
|
1
10
|
export function mapDataIntoLap(inputLaps, lapKey, data) {
|
|
11
|
+
var _a;
|
|
2
12
|
const laps = [...inputLaps];
|
|
3
13
|
let index = 0;
|
|
4
14
|
for (let i = 0; i < laps.length; i++) {
|
|
5
15
|
const nextLap = laps[i + 1];
|
|
6
16
|
const tempData = [];
|
|
7
|
-
const nextLapStartTime = nextLap
|
|
8
|
-
? new Date(nextLap.start_time).getTime()
|
|
9
|
-
: null;
|
|
17
|
+
const nextLapStartTime = timestampValue(nextLap === null || nextLap === void 0 ? void 0 : nextLap.start_time);
|
|
10
18
|
for (let j = index; j < data.length; j++) {
|
|
11
19
|
const row = data[j];
|
|
12
|
-
if (nextLap && nextLapStartTime) {
|
|
13
|
-
const timestamp =
|
|
14
|
-
if (nextLapStartTime > timestamp) {
|
|
20
|
+
if (nextLap && nextLapStartTime !== undefined) {
|
|
21
|
+
const timestamp = timestampValue((_a = row.timestamp) !== null && _a !== void 0 ? _a : row.start_time);
|
|
22
|
+
if (timestamp === undefined || nextLapStartTime > timestamp) {
|
|
15
23
|
tempData.push(row);
|
|
24
|
+
index = j + 1;
|
|
16
25
|
}
|
|
17
26
|
else if (nextLapStartTime <= timestamp) {
|
|
18
27
|
index = j;
|
|
@@ -21,6 +30,7 @@ export function mapDataIntoLap(inputLaps, lapKey, data) {
|
|
|
21
30
|
}
|
|
22
31
|
else {
|
|
23
32
|
tempData.push(row);
|
|
33
|
+
index = j + 1;
|
|
24
34
|
}
|
|
25
35
|
}
|
|
26
36
|
if (!laps[i][lapKey]) {
|
|
@@ -35,15 +45,14 @@ export function mapDataIntoSession(inputSessions, laps) {
|
|
|
35
45
|
for (let i = 0; i < sessions.length; i++) {
|
|
36
46
|
const nextSession = sessions[i + 1];
|
|
37
47
|
const tempLaps = [];
|
|
38
|
-
const nextSessionStartTime = nextSession
|
|
39
|
-
? new Date(nextSession.start_time).getTime()
|
|
40
|
-
: null;
|
|
48
|
+
const nextSessionStartTime = timestampValue(nextSession === null || nextSession === void 0 ? void 0 : nextSession.start_time);
|
|
41
49
|
for (let j = lapIndex; j < laps.length; j++) {
|
|
42
50
|
const lap = laps[j];
|
|
43
|
-
if (nextSession && nextSessionStartTime) {
|
|
44
|
-
const lapStartTime =
|
|
45
|
-
if (nextSessionStartTime > lapStartTime) {
|
|
51
|
+
if (nextSession && nextSessionStartTime !== undefined) {
|
|
52
|
+
const lapStartTime = timestampValue(lap.start_time);
|
|
53
|
+
if (lapStartTime === undefined || nextSessionStartTime > lapStartTime) {
|
|
46
54
|
tempLaps.push(lap);
|
|
55
|
+
lapIndex = j + 1;
|
|
47
56
|
}
|
|
48
57
|
else if (nextSessionStartTime <= lapStartTime) {
|
|
49
58
|
lapIndex = j;
|
|
@@ -52,6 +61,7 @@ export function mapDataIntoSession(inputSessions, laps) {
|
|
|
52
61
|
}
|
|
53
62
|
else {
|
|
54
63
|
tempLaps.push(lap);
|
|
64
|
+
lapIndex = j + 1;
|
|
55
65
|
}
|
|
56
66
|
}
|
|
57
67
|
if (!sessions[i].laps) {
|
package/dist/messages.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type { MessageObject } from './fit.js';
|
|
2
|
-
import type { MesgNum } from './fit_types.js';
|
|
1
|
+
import type { MessageName, MessageObject } from './fit.js';
|
|
3
2
|
export declare function getFitMessage(messageNum: number): {
|
|
4
|
-
name:
|
|
3
|
+
name: MessageName | '';
|
|
5
4
|
getAttributes: (fieldNum: number) => MessageObject;
|
|
6
5
|
};
|
|
7
6
|
export declare function getFitMessageBaseType(inp: any): any;
|
package/dist/type_generator.js
CHANGED
|
@@ -38,9 +38,15 @@ export function generateProperty(name, type, optional = true) {
|
|
|
38
38
|
export function generateArrayProperty(name, type, optional = true) {
|
|
39
39
|
return generateProperty(name, ts.factory.createArrayTypeNode(type), optional);
|
|
40
40
|
}
|
|
41
|
+
function generateNullableArrayType(type) {
|
|
42
|
+
return ts.factory.createArrayTypeNode(ts.factory.createParenthesizedType(ts.factory.createUnionTypeNode([
|
|
43
|
+
type,
|
|
44
|
+
ts.factory.createLiteralTypeNode(ts.factory.createNull()),
|
|
45
|
+
])));
|
|
46
|
+
}
|
|
41
47
|
export function generateTypeFromField(def) {
|
|
42
48
|
if (def.array === true) {
|
|
43
|
-
return
|
|
49
|
+
return generateNullableArrayType(generateTypeFromField(Object.assign(Object.assign({}, def), { array: false })));
|
|
44
50
|
}
|
|
45
51
|
switch (def.type) {
|
|
46
52
|
case 'uint32_array':
|
|
@@ -50,14 +56,15 @@ export function generateTypeFromField(def) {
|
|
|
50
56
|
case 'sint16_array':
|
|
51
57
|
case 'sint8_array':
|
|
52
58
|
case 'byte_array':
|
|
53
|
-
return
|
|
59
|
+
return generateNullableArrayType(ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword));
|
|
54
60
|
case 'exercise_category_array':
|
|
55
|
-
return
|
|
56
|
-
case 'bool':
|
|
57
|
-
return ts.factory.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword);
|
|
61
|
+
return generateNullableArrayType(ts.factory.createTypeReferenceNode('ExerciseCategory'));
|
|
58
62
|
case 'date_time':
|
|
63
|
+
case 'local_date_time':
|
|
64
|
+
return ts.factory.createTypeReferenceNode('Date');
|
|
59
65
|
case 'string':
|
|
60
66
|
return ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword);
|
|
67
|
+
case 'bool':
|
|
61
68
|
case 'uint32':
|
|
62
69
|
case 'uint64':
|
|
63
70
|
case 'uint16':
|
|
@@ -87,20 +94,30 @@ export function generateTypes(types) {
|
|
|
87
94
|
const nodes = [];
|
|
88
95
|
const typeNames = Object.keys(types);
|
|
89
96
|
typeNames.forEach((name) => {
|
|
90
|
-
if (name === 'message_index') {
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
97
|
const type = types[name];
|
|
94
|
-
const names = Object.values(type);
|
|
98
|
+
const names = [...new Set(Object.values(type))];
|
|
99
|
+
const isMask = names.includes('mask');
|
|
100
|
+
const typeNode = isMask
|
|
101
|
+
? ts.factory.createTypeLiteralNode([
|
|
102
|
+
generateProperty('value', ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword), false),
|
|
103
|
+
...names
|
|
104
|
+
.filter(value => value !== 'mask')
|
|
105
|
+
.map(value => generateProperty(String(value), ts.factory.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword), false)),
|
|
106
|
+
])
|
|
107
|
+
: names.length === 0
|
|
108
|
+
? ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
|
|
109
|
+
: ts.factory.createUnionTypeNode([
|
|
110
|
+
...names.map(n => ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(String(n)))),
|
|
111
|
+
...(name === 'mesg_num'
|
|
112
|
+
? [
|
|
113
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral('definition')),
|
|
114
|
+
]
|
|
115
|
+
: []),
|
|
116
|
+
ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
|
|
117
|
+
]);
|
|
95
118
|
const typeAlias = ts.factory.createTypeAliasDeclaration([
|
|
96
119
|
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword),
|
|
97
|
-
], snakeToCamel(name), undefined,
|
|
98
|
-
? ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
|
|
99
|
-
: ts.factory.createUnionTypeNode([...names.map(n => ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(String(n)))), ...(name === 'mesg_num'
|
|
100
|
-
? [
|
|
101
|
-
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral('definition')),
|
|
102
|
-
]
|
|
103
|
-
: [])]));
|
|
120
|
+
], snakeToCamel(name), undefined, typeNode);
|
|
104
121
|
nodes.push(typeAlias);
|
|
105
122
|
});
|
|
106
123
|
return nodes;
|
|
@@ -137,13 +154,6 @@ export function generateUtilities() {
|
|
|
137
154
|
]),
|
|
138
155
|
]));
|
|
139
156
|
nodes.push(unitType);
|
|
140
|
-
const messageIndex = ts.factory.createInterfaceDeclaration([ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)], 'MessageIndex', undefined, undefined, [
|
|
141
|
-
generateProperty('0', ts.factory.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword), false),
|
|
142
|
-
generateProperty('value', ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword), false),
|
|
143
|
-
generateProperty('reserved', ts.factory.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword), false),
|
|
144
|
-
generateProperty('selected', ts.factory.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword), false),
|
|
145
|
-
]);
|
|
146
|
-
nodes.push(messageIndex);
|
|
147
157
|
return nodes;
|
|
148
158
|
}
|
|
149
159
|
function generateAdditionalFields(msg) {
|
|
@@ -241,22 +251,13 @@ export function generateMessages(messages) {
|
|
|
241
251
|
const nodes = [];
|
|
242
252
|
Object.keys(messages).forEach((name) => {
|
|
243
253
|
const msg = FIT.messages[Number(name)];
|
|
244
|
-
const usedFields = new Set();
|
|
245
254
|
const messageType = ts.factory.createInterfaceDeclaration([
|
|
246
255
|
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword),
|
|
247
256
|
], snakeToCamel(`parsed_${msg.name}`), undefined, undefined, [
|
|
248
|
-
...Object.keys(msg).filter(n => n !== 'name').
|
|
249
|
-
var _a;
|
|
257
|
+
...Object.keys(msg).filter(n => n !== 'name').map((id) => {
|
|
250
258
|
const def = msg[Number(id)];
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
if (!usedFields.has(definition.field)) {
|
|
254
|
-
usedFields.add(definition.field);
|
|
255
|
-
acc.push(generateProperty(definition.field, generateTypeFromField(definition), !['start_time', 'timestamp'].includes(definition.field)));
|
|
256
|
-
}
|
|
257
|
-
});
|
|
258
|
-
return acc;
|
|
259
|
-
}, []),
|
|
259
|
+
return generateProperty(def.field, generateTypeFromField(def));
|
|
260
|
+
}),
|
|
260
261
|
...generateAdditionalFields(msg),
|
|
261
262
|
]);
|
|
262
263
|
nodes.push(messageType);
|