Skip to content

Commit

Permalink
add initial parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
pirxpilot committed Sep 24, 2023
1 parent 8b74f73 commit 336a028
Show file tree
Hide file tree
Showing 6 changed files with 639 additions and 6 deletions.
18 changes: 17 additions & 1 deletion lib/import.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
const makeStream = require('./stream');

module.exports = importNmea;

function importNmea() {
/* global WritableStream */

/**
* Parses ASCII stream containing NMEA messages into Furkot's trip
* @param {ReadableStream} from
*/
async function importNmea(from) {
const stream = makeStream();
let trip = [];

await from.pipeThrough(stream).pipeTo(new WritableStream({
write: packet => trip.push(packet)
}));

return trip;
}
49 changes: 49 additions & 0 deletions lib/stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const nmea = require("nmea-simple");

module.exports = makeStream;

/* global TransformStream */

function makeStream() {
return new TransformStream(makeSource());
}

function makeSource() {
let prefix = '';
return {
transform,
flush
};

/**
* @param {String} chunk
* @param {TransformStreamDefaultController} controller
*/
function transform(chunk, controller) {
const lines = chunk.split(/[\n\l]/);
if (prefix) {
lines[0] = prefix + lines[0];
}
prefix = lines.length > 1 ? lines.pop() : '';
for (const line of lines) {
if (line[0] === '$') {
const packet = nmea.parseNmeaSentence(line);
controller.enqueue(packet);
}
}
}

/**
* @param {TransformStreamDefaultController} controller
*/
function flush(controller) {
if (prefix[0] === '$') {
const packet = nmea.parseNmeaSentence(prefix);
controller.enqueue(packet);
}
}
}




4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"nmea",
"furkot"
],
"dependencies": {},
"dependencies": {
"nmea-simple": "^3.3.0"
},
"devDependencies": {
"@pirxpilot/jshint": "^3.0.1"
},
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]

0 comments on commit 336a028

Please sign in to comment.