Skip to content

Commit 150eded

Browse files
author
LazyTesting
committed
fixed codeduplication in tests
1 parent 4672b95 commit 150eded

File tree

4 files changed

+27
-37
lines changed

4 files changed

+27
-37
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
const assert = require('assert');
22
const hdistance = require('./app.js');
33

4+
const pos1 = { lat: '46.974974', lon: '10.311162' };
5+
const pos2 = { lat: '46.97495', lon: '10.311144' };
6+
47
describe('calculate distance horizontal', () => {
58
it('should return a positive result', () => {
6-
const pos1 = { lat: '46.974974', lon: '10.311162' };
7-
const pos2 = { lat: '46.97495', lon: '10.311144' };
89
const result = hdistance(pos1, pos2);
910
assert(result > 0);
1011
});
1112

1213
it('should not return a negative value', () => {
13-
const pos1 = { lat: '46.97495', lon: '10.311144' };
14-
const pos2 = { lat: '46.974974', lon: '10.311162' };
15-
const result = hdistance(pos1, pos2);
14+
const result = hdistance(pos2, pos1);
1615
assert(result > 0);
1716
});
1817
});
18+

core/convert-gpx-data/app.js

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,20 @@
1-
const xml2js = require('xml2js');
1+
const gpxParse = require('gpx-parse');
22

33
const get = function(gpxData) {
4-
const json = xml2JSON(gpxData);
4+
let convertedPoints = [];
55

6-
return getDataPoints(json);
7-
};
8-
9-
var xml2JSON = function (xml){
10-
var parser = new xml2js.Parser();
11-
var json;
12-
parser.parseString(xml, function (err, result) {
13-
json = result;
6+
gpxParse.parseGpx(gpxData, (error, data)=>{
7+
const points = data.tracks.map(track => track.segments.map(segment => segment));
8+
convertedPoints = points[0][0].map(point => ({
9+
date: point.time,
10+
ele: point.elevation,
11+
lat: point.lat,
12+
lon: point.lon
13+
}));
1414
});
15-
return json;
16-
}
1715

18-
var getDataPoints = function(json) {
19-
var result = json["gpx"]["trk"][0]["trkseg"][0]["trkpt"];
20-
var formated = result.map(function(obj){
21-
var item = {};
22-
item.date = obj.time[0];
23-
item.ele = obj.ele[0];
24-
item.lat = obj.$.lat;
25-
item.lon = obj.$.lon;
26-
return item;
27-
});
28-
return formated;
29-
}
16+
return convertedPoints;
17+
};
18+
3019

3120
module.exports = get;

core/convert-gpx-data/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"author": "Maarten Groeneweg",
1010
"license": "ISC",
1111
"dependencies": {
12+
"togeojson": "^0.16.0",
1213
"xml2js": "^0.4.17"
1314
},
1415
"devDependencies": {

core/convert-gpx-data/spec.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ describe('Convert GPX data', () => {
99
const content = fs.readFileSync(filePath, 'utf8');
1010

1111
const expected = [
12-
{ date: '2015-01-11T15:10:11.000+01:00',
13-
ele: '2182.0',
14-
lat: '46.974974',
15-
lon: '10.311162' },
16-
{ date: '2015-01-11T15:10:12.000+01:00',
17-
ele: '2185.0',
18-
lat: '46.97495',
19-
lon: '10.311144' }
12+
{ date: '2015-01-11T14:10:11.000Z',
13+
ele: 2182,
14+
lat: 46.974974,
15+
lon: 10.311162 },
16+
{ date: '2015-01-11T14:10:12.000Z',
17+
ele: 2185,
18+
lat: 46.97495,
19+
lon: 10.311144 }
2020
];
2121
const result = gpxData(content);
2222

0 commit comments

Comments
 (0)