-
Notifications
You must be signed in to change notification settings - Fork 8
/
examples.js
210 lines (108 loc) · 4.69 KB
/
examples.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
This purpose of this file is to test the generated tcx.js file, outside of jasmine,
before deployment to npm. It (accurately) generates Examples section of the README.md
file based on actual working code and output.
Copyright 2014, Christopher Joakim, JoakimSoftware LLC <[email protected]>
*/
(function() {
var a2, activity, author, creator, filename, fs, m26, opts, p2, parser, t2, tcx, trackpoints;
fs = require('fs');
m26 = require("m26-js");
tcx = require("./lib/tcx.js");
console.log('');
console.log('### Examples');
console.log('');
console.log('#### Setup');
console.log('');
console.log('Add tcx-js to your project or package.json file:');
console.log('```');
console.log('npm install tcx-js');
console.log('```');
console.log('');
console.log('Require tcx-js in your code:');
console.log('```');
console.log('tcx = require("tcx-js")');
console.log('```');
console.log('');
console.log('#### Parse a TCX file from Garmin Connect');
console.log('');
console.log('Parsing elapsed time is typically sub-second, even for a marathon run.');
console.log("The tcx-js Parser uses the 'node-expat' library, and the SAX API, for speed and performance.");
console.log('');
console.log('Note: this library is implemented with CoffeeScript, and these examples are also in CoffeeScript.');
console.log('');
console.log('```');
parser = new tcx.Parser();
parser.parse_file("data/activity_twin_cities_marathon.tcx");
activity = parser.activity;
author = activity.author;
creator = activity.creator;
trackpoints = activity.trackpoints;
console.log('parser = new tcx.Parser()');
console.log('parser.parse_file("data/activity_twin_cities_marathon.tcx")');
console.log('activity = parser.activity');
console.log('creator = activity.creator');
console.log('author = activity.author');
console.log('trackpoints = activity.trackpoints');
console.log('```');
console.log('');
console.log('"creator" is the device that recorded the data');
console.log('');
console.log('```');
console.log("console.log(JSON.stringify(creator, null, 2)) -> \n" + JSON.stringify(creator, null, 2));
console.log('```');
console.log('');
console.log('"author" is what created the tcx/xml file');
console.log('');
console.log('```');
console.log("console.log(JSON.stringify(author, null, 2)) -> \n" + JSON.stringify(author, null, 2));
console.log('```');
console.log('');
console.log('"trackpoints" is an Array of the recorded data points');
console.log('');
console.log('```');
console.log("console.log(trackpoints.length) -> " + trackpoints.length);
console.log('');
console.log("console.log(JSON.stringify(trackpoints[0], null, 2)) -> \n" + JSON.stringify(trackpoints[0], null, 2));
console.log('');
console.log("console.log(JSON.stringify(trackpoints[trackpoints.length - 1], null, 2)) -> \n" + JSON.stringify(trackpoints[trackpoints.length - 1], null, 2));
console.log('```');
console.log('');
console.log('#### Parse, with Augmented Calculated fields');
console.log('');
console.log("tcx-js will optionally calculate and add the 'alt_feet', 'dist_miles', ");
console.log("'elapsed_sec', and 'elapsed_hhmmss' fields to each trackpoint if you configure ");
console.log('the parser as follows: ');
console.log('');
opts = {};
opts.alt_feet = true;
opts.dist_miles = true;
opts.elapsed = true;
p2 = new tcx.Parser(opts);
p2.parse_file("data/activity_twin_cities_marathon.tcx");
a2 = p2.activity;
t2 = a2.trackpoints;
console.log('```');
console.log('opts = {}');
console.log('opts.alt_feet = true');
console.log('opts.dist_miles = true');
console.log('opts.elapsed = true # this will add two fields - elapsed_sec and elapsed_hhmmss');
console.log('');
console.log('p2 = new tcx.Parser(opts)');
console.log('p2.parse_file("data/activity_twin_cities_marathon.tcx")');
console.log('a2 = p2.activity');
console.log('t2 = a2.trackpoints');
console.log("console.log(JSON.stringify(t2[t2.length - 1], null, 2)) -> \n" + JSON.stringify(t2[t2.length - 1], null, 2));
console.log('```');
console.log('');
console.log('The version number of this library, and other constant values, can be determined at runtime.');
console.log('');
console.log('```');
console.log('Parser.VERSION -> ' + tcx.Parser.VERSION);
console.log('Parser.FEET_PER_METER -> ' + tcx.Parser.FEET_PER_METER);
console.log('Parser.METERS_PER_MILE -> ' + tcx.Parser.METERS_PER_MILE);
console.log('```');
console.log('');
filename = 'data/activity_twin_cities_marathon.json';
fs.writeFileSync(filename, JSON.stringify(a2, null, 2));
}).call(this);