skiz-parser 4.1.1 → 4.2.0

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.
Files changed (2) hide show
  1. package/index.js +89 -67
  2. package/package.json +7 -7
package/index.js CHANGED
@@ -1,11 +1,11 @@
1
- import csv from 'csv-parser';
1
+ import { parse as CSVParser } from 'csv-parse';
2
2
  import { XMLParser } from 'fast-xml-parser';
3
3
  import yauzl from 'yauzl';
4
4
 
5
- const parser = new XMLParser({
5
+ const xmlParserOptions = {
6
6
  attributeNamePrefix: '',
7
7
  ignoreAttributes: false,
8
- });
8
+ };
9
9
 
10
10
  const convertReadStreamToBuffer = (readStream) => {
11
11
  return new Promise((resolve, reject) => {
@@ -31,69 +31,84 @@ const openReadStream = (zipFile, entry) => {
31
31
  };
32
32
 
33
33
  const parseBatteryCsvFile = (readStream) => {
34
+ const parser = CSVParser();
35
+
34
36
  return new Promise((resolve, reject) => {
35
37
  const batteryUsage = [];
36
38
 
37
- readStream
38
- .pipe(csv({ headers: false }))
39
+ parser
39
40
  .on('error', reject)
40
- .on('data', (data) => {
41
- const values = Object.values(data);
42
-
43
- batteryUsage.push({
44
- timestamp: new Date(values[0]),
45
- status: values[1],
46
- level: parseFloat(values[2]),
47
- });
41
+ .on('readable', () => {
42
+ let record;
43
+
44
+ while ((record = parser.read()) !== null) {
45
+ batteryUsage.push({
46
+ timestamp: new Date(record[0]),
47
+ status: record[1],
48
+ level: parseFloat(record[2]),
49
+ });
50
+ }
48
51
  })
49
52
  .once('end', () => {
50
53
  resolve({ batteryUsage });
51
54
  });
55
+
56
+ readStream.pipe(parser);
52
57
  });
53
58
  };
54
59
 
55
60
  const parseNodeCsvFile = (readStream) => {
61
+ const parser = CSVParser();
62
+
56
63
  return new Promise((resolve, reject) => {
57
64
  const trackNodes = [];
58
65
 
59
- readStream
60
- .pipe(csv({ headers: false }))
66
+ parser
61
67
  .on('error', reject)
62
- .on('data', (data) => {
63
- const values = Object.values(data);
64
-
65
- trackNodes.push({
66
- timestamp: new Date(parseFloat(values[0]) * 1000.0),
67
- latitude: parseFloat(values[1]),
68
- longitude: parseFloat(values[2]),
69
- altitude: parseFloat(values[3]),
70
- heading: parseFloat(values[4]),
71
- velocity: parseFloat(values[5]),
72
- hAccuracy: parseFloat(values[6]),
73
- vAccuracy: parseFloat(values[7]),
74
- });
68
+ .on('readable', () => {
69
+ let record;
70
+
71
+ while ((record = parser.read()) !== null) {
72
+ trackNodes.push({
73
+ timestamp: new Date(parseFloat(record[0]) * 1000.0),
74
+ latitude: parseFloat(record[1]),
75
+ longitude: parseFloat(record[2]),
76
+ altitude: parseFloat(record[3]),
77
+ heading: parseFloat(record[4]),
78
+ velocity: parseFloat(record[5]),
79
+ hAccuracy: parseFloat(record[6]),
80
+ vAccuracy: parseFloat(record[7]),
81
+ });
82
+ }
75
83
  })
76
84
  .once('end', () => {
77
85
  resolve({ trackNodes });
78
86
  });
87
+
88
+ readStream.pipe(parser);
79
89
  });
80
90
  };
81
91
 
82
92
  const parseRelativeAltitudeSensorCsvFile = (readStream) => {
93
+ const parser = CSVParser();
94
+
83
95
  return new Promise((resolve, reject) => {
84
96
  const relativeAltitude = [];
85
97
 
86
- readStream
87
- .pipe(csv({ headers: false }))
88
- .on('error', reject)
89
- .on('data', (data) => {
90
- const values = Object.values(data);
98
+ readStream.pipe(parser);
91
99
 
92
- relativeAltitude.push({
93
- timestamp: new Date(values[0]),
94
- pressure: parseFloat(values[1]),
95
- relativeAltitude: parseFloat(values[2]),
96
- });
100
+ parser
101
+ .on('error', reject)
102
+ .on('readable', (data) => {
103
+ let record;
104
+
105
+ while ((record = parser.read()) !== null) {
106
+ relativeAltitude.push({
107
+ timestamp: new Date(record[0]),
108
+ pressure: parseFloat(record[1]),
109
+ relativeAltitude: parseFloat(record[2]),
110
+ });
111
+ }
97
112
  })
98
113
  .once('end', () => {
99
114
  resolve({ relativeAltitude });
@@ -102,48 +117,54 @@ const parseRelativeAltitudeSensorCsvFile = (readStream) => {
102
117
  };
103
118
 
104
119
  const parseSegmentCsvFile = (readStream) => {
120
+ const parser = CSVParser({ fromLine: 2 });
121
+
105
122
  return new Promise((resolve, reject) => {
106
123
  const trackSegments = [];
107
124
 
108
- readStream
109
- .pipe(csv({ headers: false, skipLines: 1 }))
125
+ parser
110
126
  .on('error', reject)
111
- .on('data', (data) => {
112
- const values = Object.values(data);
113
-
114
- trackSegments.push({
115
- startTime: new Date(parseFloat(values[0]) * 1000),
116
- endTime: new Date(parseFloat(values[1]) * 1000),
117
- number: parseInt(values[4], 10),
118
- name: values[5],
119
- comment: values[6],
120
- type: values[7],
121
- category: values[8],
122
- link: values[9],
123
- uuid: values[10],
124
- metrics: {
125
- time: parseFloat(values[11]),
126
- speed: parseFloat(values[12]),
127
- distance: parseFloat(values[13]),
128
- vertical: parseFloat(values[14]),
129
- maxSpeed: parseFloat(values[15]),
130
- slope: parseFloat(values[16]),
131
- maxSlope: parseFloat(values[17]),
132
- minAltitude: parseFloat(values[18]),
133
- maxAltitude: parseFloat(values[19]),
134
- startAltitude: parseFloat(values[20]),
135
- finishAltitude: parseFloat(values[21]),
136
- },
137
- });
127
+ .on('readable', () => {
128
+ let record;
129
+
130
+ while ((record = parser.read()) !== null) {
131
+ trackSegments.push({
132
+ startTime: new Date(parseFloat(record[0]) * 1000),
133
+ endTime: new Date(parseFloat(record[1]) * 1000),
134
+ number: parseInt(record[4], 10),
135
+ name: record[5],
136
+ comment: record[6],
137
+ type: record[7],
138
+ category: record[8],
139
+ link: record[9],
140
+ uuid: record[10],
141
+ metrics: {
142
+ time: parseFloat(record[11]),
143
+ speed: parseFloat(record[12]),
144
+ distance: parseFloat(record[13]),
145
+ vertical: parseFloat(record[14]),
146
+ maxSpeed: parseFloat(record[15]),
147
+ slope: parseFloat(record[16]),
148
+ maxSlope: parseFloat(record[17]),
149
+ minAltitude: parseFloat(record[18]),
150
+ maxAltitude: parseFloat(record[19]),
151
+ startAltitude: parseFloat(record[20]),
152
+ finishAltitude: parseFloat(record[21]),
153
+ },
154
+ });
155
+ }
138
156
  })
139
157
  .once('end', () => {
140
158
  resolve({ trackSegments });
141
159
  });
160
+
161
+ readStream.pipe(parser);
142
162
  });
143
163
  };
144
164
 
145
165
  const parseTrackXmlFile = async (readStream) => {
146
166
  const buffer = await convertReadStreamToBuffer(readStream);
167
+ const parser = new XMLParser(xmlParserOptions);
147
168
  const parsed = parser.parse(buffer.toString('utf-8'));
148
169
 
149
170
  const track = parsed.track;
@@ -201,6 +222,7 @@ const parseTrackXmlFile = async (readStream) => {
201
222
 
202
223
  const parseEventsXmlFile = async (readStream) => {
203
224
  const buffer = await convertReadStreamToBuffer(readStream);
225
+ const parser = new XMLParser(xmlParserOptions);
204
226
  const parsed = parser.parse(buffer.toString('utf-8'));
205
227
 
206
228
  const events = parsed.events.event || [];
package/package.json CHANGED
@@ -1,23 +1,23 @@
1
1
  {
2
2
  "name": "skiz-parser",
3
3
  "description": "Parse a .skiz file exported from Ski Tracks",
4
- "version": "4.1.1",
4
+ "version": "4.2.0",
5
5
  "author": "Kirk Eaton <contact@kirkeaton.ca>",
6
6
  "bugs": {
7
7
  "url": "https://github.com/kirkeaton/skiz-parser/issues"
8
8
  },
9
9
  "dependencies": {
10
- "csv-parser": "3.0.0",
10
+ "csv-parse": "5.5.6",
11
11
  "fast-xml-parser": "4.4.0",
12
12
  "yauzl": "3.1.3"
13
13
  },
14
14
  "devDependencies": {
15
15
  "@kirkeaton/prettier-config": "1.0.2",
16
16
  "@kirkeaton/semantic-release-config": "1.0.1",
17
- "@types/node": "20.12.13",
18
- "prettier": "3.2.5",
19
- "semantic-release": "23.1.1",
20
- "tsd": "0.31.0"
17
+ "@types/node": "20.14.9",
18
+ "prettier": "3.3.2",
19
+ "semantic-release": "24.0.0",
20
+ "tsd": "0.31.1"
21
21
  },
22
22
  "engines": {
23
23
  "node": ">=18"
@@ -38,7 +38,7 @@
38
38
  "skiz"
39
39
  ],
40
40
  "license": "BSD-3-Clause",
41
- "packageManager": "pnpm@9.1.4",
41
+ "packageManager": "pnpm@9.4.0",
42
42
  "repository": {
43
43
  "type": "git",
44
44
  "url": "git+https://github.com/kirkeaton/skiz-parser.git"