skiz-parser 1.3.0 → 1.6.1

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 (3) hide show
  1. package/README.md +4 -4
  2. package/index.js +46 -19
  3. package/package.json +7 -11
package/README.md CHANGED
@@ -14,8 +14,8 @@ $ npm install skiz-parser
14
14
  import { promises as fsAsync } from 'fs';
15
15
  import { parseSkizFile } from 'skiz-parser';
16
16
 
17
- const file = await fsAsync.readFile('./example.skiz');
18
- const result = await parseSkizFile(file);
17
+ const contents = await fsAsync.readFile('./example.skiz');
18
+ const result = await parseSkizFile(contents);
19
19
 
20
20
  console.log(result);
21
21
  //=> { name: 'Day 1 - 2018/2019', … }
@@ -23,13 +23,13 @@ console.log(result);
23
23
 
24
24
  ## API
25
25
 
26
- ### parseSkizFile(file, callback?)
26
+ ### parseSkizFile(contents, callback?)
27
27
 
28
28
  Returns a `Promise<object>` with the parsed .skiz file.
29
29
 
30
30
  Optionally a callback function may be used instead.
31
31
 
32
- #### file
32
+ #### contents
33
33
 
34
34
  Type: `Buffer`
35
35
 
package/index.js CHANGED
@@ -1,8 +1,13 @@
1
1
  import csv from 'csv-parser';
2
- import xmlParser from 'fast-xml-parser';
2
+ import { XMLParser } from 'fast-xml-parser';
3
3
  import { promisify } from 'util';
4
4
  import yauzl from 'yauzl';
5
5
 
6
+ const parser = new XMLParser({
7
+ attributeNamePrefix: '',
8
+ ignoreAttributes: false,
9
+ });
10
+
6
11
  const convertReadStreamToBuffer = (readStream) => {
7
12
  return new Promise((resolve, reject) => {
8
13
  const chunks = [];
@@ -18,6 +23,28 @@ const convertReadStreamToBuffer = (readStream) => {
18
23
  });
19
24
  };
20
25
 
26
+ const parseBatteryCsvFile = (readStream) => {
27
+ return new Promise((resolve, reject) => {
28
+ const batteryUsage = [];
29
+
30
+ readStream
31
+ .pipe(csv({ headers: false }))
32
+ .on('error', reject)
33
+ .on('data', (data) => {
34
+ const values = Object.values(data);
35
+
36
+ batteryUsage.push({
37
+ timestamp: new Date(values[0]),
38
+ status: values[1],
39
+ level: parseFloat(values[2]),
40
+ });
41
+ })
42
+ .once('end', () => {
43
+ resolve({ batteryUsage });
44
+ });
45
+ });
46
+ };
47
+
21
48
  const parseNodeCsvFile = (readStream) => {
22
49
  return new Promise((resolve, reject) => {
23
50
  const trackNodes = [];
@@ -87,13 +114,8 @@ const parseSegmentCsvFile = (readStream) => {
87
114
  };
88
115
 
89
116
  const parseTrackXmlFile = async (readStream) => {
90
- const options = {
91
- attributeNamePrefix: '',
92
- ignoreAttributes: false,
93
- };
94
-
95
117
  const buffer = await convertReadStreamToBuffer(readStream);
96
- const parsed = xmlParser.parse(buffer.toString('utf-8'), options);
118
+ const parsed = parser.parse(buffer.toString('utf-8'));
97
119
 
98
120
  const track = parsed.track;
99
121
  const metrics = track.metrics;
@@ -149,13 +171,8 @@ const parseTrackXmlFile = async (readStream) => {
149
171
  };
150
172
 
151
173
  const parseEventsXmlFile = async (readStream) => {
152
- const options = {
153
- attributeNamePrefix: '',
154
- ignoreAttributes: false,
155
- };
156
-
157
174
  const buffer = await convertReadStreamToBuffer(readStream);
158
- const parsed = xmlParser.parse(buffer.toString('utf-8'), options);
175
+ const parsed = parser.parse(buffer.toString('utf-8'));
159
176
 
160
177
  const events = parsed.events.event || [];
161
178
  const trackEvents = events.map((event) => ({
@@ -167,9 +184,13 @@ const parseEventsXmlFile = async (readStream) => {
167
184
  return { trackEvents };
168
185
  };
169
186
 
170
- export const parseSkizFile = (file, callback) => {
187
+ export const parseSkizFile = (contents, callback) => {
171
188
  const parse = (resolve, reject) => {
172
- yauzl.fromBuffer(file, { lazyEntries: true }, (err, zipFile) => {
189
+ if (contents instanceof ArrayBuffer) {
190
+ contents = Buffer.from(contents);
191
+ }
192
+
193
+ yauzl.fromBuffer(contents, { lazyEntries: true }, (err, zipFile) => {
173
194
  if (err) {
174
195
  return reject(err);
175
196
  }
@@ -184,9 +205,13 @@ export const parseSkizFile = (file, callback) => {
184
205
  .on('error', reject)
185
206
  .on('entry', async (entry) => {
186
207
  if (
187
- !['Events.xml', 'Nodes.csv', 'Segment.csv', 'Track.xml'].includes(
188
- entry.fileName
189
- )
208
+ ![
209
+ 'Battery.csv',
210
+ 'Events.xml',
211
+ 'Nodes.csv',
212
+ 'Segment.csv',
213
+ 'Track.xml',
214
+ ].includes(entry.fileName)
190
215
  ) {
191
216
  return zipFile.readEntry();
192
217
  }
@@ -194,7 +219,9 @@ export const parseSkizFile = (file, callback) => {
194
219
  const readStream = await openReadStream(entry);
195
220
 
196
221
  let result;
197
- if (entry.fileName === 'Events.xml') {
222
+ if (entry.fileName === 'Battery.csv') {
223
+ result = await parseBatteryCsvFile(readStream);
224
+ } else if (entry.fileName === 'Events.xml') {
198
225
  result = await parseEventsXmlFile(readStream);
199
226
  } else if (entry.fileName === 'Nodes.csv') {
200
227
  result = await parseNodeCsvFile(readStream);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "skiz-parser",
3
3
  "description": "Parse a .skiz file exported from Ski Tracks",
4
- "version": "1.3.0",
4
+ "version": "1.6.1",
5
5
  "author": "Kirk Eaton <contact@kirkeaton.ca>",
6
6
  "ava": {
7
7
  "files": [
@@ -14,13 +14,13 @@
14
14
  },
15
15
  "dependencies": {
16
16
  "csv-parser": "^3.0.0",
17
- "fast-xml-parser": "^3.19.0",
17
+ "fast-xml-parser": "^4.0.1",
18
18
  "yauzl": "^2.10.0"
19
19
  },
20
20
  "devDependencies": {
21
- "ava": "^3.15.0",
22
- "husky": "^6.0.0",
23
- "lint-staged": "^10.5.4",
21
+ "ava": "^4.0.1",
22
+ "husky": "^7.0.0",
23
+ "lint-staged": "^12.1.4",
24
24
  "prettier": "^2.2.1"
25
25
  },
26
26
  "engines": {
@@ -31,11 +31,6 @@
31
31
  "index.js"
32
32
  ],
33
33
  "homepage": "https://github.com/kirkeaton/skiz-parser#readme",
34
- "husky": {
35
- "hooks": {
36
- "pre-commit": "lint-staged"
37
- }
38
- },
39
34
  "keywords": [
40
35
  "file",
41
36
  "parse",
@@ -44,7 +39,7 @@
44
39
  ],
45
40
  "license": "BSD-3-Clause",
46
41
  "lint-staged": {
47
- "*.{ts,tsx,json,md}": "prettier --write"
42
+ "*.{js,json,md}": "prettier --write"
48
43
  },
49
44
  "prettier": {
50
45
  "printWidth": 80,
@@ -62,6 +57,7 @@
62
57
  "url": "git+https://github.com/kirkeaton/skiz-parser.git"
63
58
  },
64
59
  "scripts": {
60
+ "prepare": "husky install",
65
61
  "test": "ava -v"
66
62
  },
67
63
  "type": "module"