Skip to content

Commit

Permalink
Update to actually work with one of the non-browser package.
Browse files Browse the repository at this point in the history
  • Loading branch information
We-Gold committed Jun 21, 2023
1 parent a2b0058 commit abfb7e6
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 34 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ The schema for GPX, a commonly used gps tracking format, can be found here: [GPX

**This library currently does not include support for non-browser execution.**

Right now, to use this in Node.js without a browser, you will need to use `jsdom`'s `DOMParser`.

To use it in something like React Native, use [`advanced-html-parser`](https://github.com/AlenToma/advanced-html-parser/tree/main) instead.
Right now, to use this in Node.js without a browser or in something like React Native, use [`xmldom-qsa`](https://www.npmjs.com/package/xmldom-qsa) instead.

See instructions below on [how to use a custom DOM parser](#using-a-custom-dom-parser).

Expand Down Expand Up @@ -98,11 +96,10 @@ If working in an environment where a custom DOM Parser is required, you can incl

```js
import { parseGPXWithCustomParser } from "@we-gold/gpxjs"
import { DOMParser } from "mycustompackage"
import { DOMParser } from "xmldom-qsa"

const customParseMethod = (myGPXString) => {
const parser = new DOMParser()
return parser.parseFromString(myGPXString, "text/xml")
const customParseMethod = (txt: string): Document | null => {
return new DOMParser().parseFromString(txt, "text/xml")
}

const [parsedFile, error] = parseGPXWithCustomParser(
Expand Down
13 changes: 10 additions & 3 deletions lib/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,17 @@ export const parseGPXWithCustomParser = (
const extensions: any = {}

// Store all available extensions as numbers
for (const extension of extensionsElement.childNodes) {
for (const extension of Array.from(
extensionsElement.childNodes
)) {
const name = extension.nodeName

// Parse the extension data with support for the browser or an xml parser
extensions[name] = parseFloat(
(extension as Element).innerHTML
(extension as Element).innerHTML != undefined
? (extension as Element).innerHTML
: (extension as Element).childNodes[0]
.textContent ?? ""
)
}

Expand Down Expand Up @@ -315,7 +322,7 @@ const queryDirectSelector = (parent: Element, tag: string): Element | null => {
const directChildren = parent.childNodes

// Find all nodes that match the given tag within the parent
for (const child of directChildren) {
for (const child of Array.from(directChildren)) {
if ((child as Element).tagName === tag.toUpperCase()) {
finalElem = child as Element
}
Expand Down
17 changes: 14 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@we-gold/gpxjs",
"author": "Weaver Goldman <[email protected]>",
"description": "GPX.js is a modern library for parsing GPX files and converting them to GeoJSON.",
"version": "1.0.3",
"version": "1.0.4",
"type": "module",
"license": "MIT",
"repository": {
Expand Down Expand Up @@ -42,7 +42,8 @@
"@types/node": "^20.3.1",
"typescript": "^5.0.2",
"vite": "^4.3.9",
"vite-plugin-dts": "^2.3.0"
"vite-plugin-dts": "^2.3.0",
"xmldom-qsa": "^1.1.3"
},
"prettier": {
"trailingComma": "es5",
Expand Down
68 changes: 50 additions & 18 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parseGPX } from "../lib"
import { parseGPX, parseGPXWithCustomParser } from "../lib"
import { DOMParser } from "xmldom-qsa"

fetch("./src/test_files/test.gpx")
.then((response) => {
Expand All @@ -8,26 +9,57 @@ fetch("./src/test_files/test.gpx")
return response.text()
})
.then((textData) => {
let startTime = performance.now()
testBrowserParser(textData)
testNonBrowserParser(textData)
})
.catch((error) => {
console.error("Error:", error)
})

// Parse the test file
const [parsedGPX, error] = parseGPX(textData)
const testBrowserParser = (textData: string) => {
console.log("\nBROWSER MODE")
let startTime = performance.now()

// Verify that the parsing was successful
if (error) throw error
const [parsedGPX, error] = parseGPX(textData)

let endTime = performance.now()
console.log("Execution time:", endTime - startTime, "ms")
console.log(parsedGPX)
// Verify that the parsing was successful
if (error) throw error

startTime = performance.now()
let endTime = performance.now()
console.log("Execution time:", endTime - startTime, "ms")
console.log(parsedGPX)

const GeoJSON = parsedGPX.toGeoJSON()
startTime = performance.now()

endTime = performance.now()
console.log("Execution time:", endTime - startTime, "ms")
console.log(GeoJSON)
})
.catch((error) => {
console.error("Error:", error)
})
const GeoJSON = parsedGPX.toGeoJSON()

endTime = performance.now()
console.log("Execution time:", endTime - startTime, "ms")
console.log(GeoJSON)
}

const testNonBrowserParser = (textData: string) => {
console.log("\nNONBROWSER MODE")
let startTime = performance.now()

const [parsedGPX, error] = parseGPXWithCustomParser(
textData,
(txt: string): Document | null =>
new DOMParser().parseFromString(txt, "text/xml")
)

// Verify that the parsing was successful
if (error) throw error

let endTime = performance.now()
console.log("Execution time:", endTime - startTime, "ms")
console.log(parsedGPX)

startTime = performance.now()

const GeoJSON = parsedGPX.toGeoJSON()

endTime = performance.now()
console.log("Execution time:", endTime - startTime, "ms")
console.log(GeoJSON)
}
6 changes: 5 additions & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ export default defineConfig({
fileName: 'gpxjs',
}
},
plugins: [dts()]
plugins: [dts()],
define: {
// Shim required for using the custom parser
global: {},
},
})

0 comments on commit abfb7e6

Please sign in to comment.