Skip to content

Commit

Permalink
Order matters on properties too
Browse files Browse the repository at this point in the history
  • Loading branch information
dwayneparton committed Jun 10, 2023
1 parent 58c4dd4 commit c7553b7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dwayneparton/geojson-to-gpx",
"version": "0.0.25",
"version": "0.0.26",
"description": "Converts GeoJson to GPX",
"main": "dist/index.js",
"module": "dist/index.js",
Expand Down
18 changes: 9 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ export default function GeoJsonToGpx(geoJson: Feature | FeatureCollection, optio
*/
function createTrk(properties?: GeoJsonProperties): Element {
const el = doc.createElement('trk');
if (properties) {
Object.keys(properties).forEach((key) => {
if (properties && typeof properties === 'object') {
const supports = ['name', 'desc', 'src', 'type'];
supports.forEach((key) => {
const value = properties[key];
const supports = ['name', 'desc', 'src', 'type'];
if (typeof value === 'string' && supports.includes(key)) {
createTagInParentElement(el, key, value);
if (value && typeof value === 'string' && supports.includes(key)) {
createTagInParentElement(el, key, value);
}
});
}
Expand All @@ -156,11 +156,11 @@ export default function GeoJsonToGpx(geoJson: Feature | FeatureCollection, optio
el.setAttribute('lon', String(lon));
createTagInParentElement(el, 'ele', ele);
createTagInParentElement(el, 'time', time);
if (properties) {
Object.keys(properties).forEach((key) => {
if (properties && typeof properties === 'object') {
const supports = ['name', 'desc', 'src', 'type'];
supports.forEach((key) => {
const value = properties[key];
const supports = ['name', 'desc', 'src', 'type'];
if (typeof value === 'string' && supports.includes(key)) {
if (value && typeof value === 'string' && supports.includes(key)) {
createTagInParentElement(el, key, value);
}
});
Expand Down
21 changes: 21 additions & 0 deletions tests/geojson-to-gpx-linestring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { Feature, LineString } from "geojson";
const geojson: Feature<LineString> = {
type: "Feature",
properties : {
desc : 'This is a description',
src : 'Test',
type : 'Race',
name : 'Slow journey from null island'
},
geometry: {
Expand All @@ -29,6 +32,24 @@ test('should have correct trk count', () => {
expect(trk).toHaveLength(1);
});

test('should have correct trk properties and be in correct order', () => {
const trk = gpx.querySelector('trk');
const name = trk?.querySelector('name');
expect(name?.innerHTML).toBe(geojson?.properties?.name);
// desc should be after name
const desc = trk?.querySelector('desc');
expect(name?.nextSibling).toBe(desc);
expect(desc?.innerHTML).toBe(geojson?.properties?.desc);
// src should be after desc
const src = trk?.querySelector('src');
expect(desc?.nextSibling).toBe(src);
expect(src?.innerHTML).toBe(geojson?.properties?.src);
// Type should be after src
const type = trk?.querySelector('type');
expect(src?.nextSibling).toBe(type);
expect(type?.innerHTML).toBe(geojson?.properties?.type);
});

test('should have correct trkpt count', () => {
const trkpt = gpx.querySelectorAll('trkpt');
expect(trkpt).toHaveLength(geojson.geometry.coordinates.length);
Expand Down

0 comments on commit c7553b7

Please # to comment.