Skip to content

Commit

Permalink
RF: Packet type assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
becseya committed Feb 10, 2023
1 parent 5f7e496 commit 5221ca4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
5 changes: 5 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import { validNmeaChecksum } from "./helpers";
export type Packet = APBPacket | BWCPacket | DBTPacket | DTMPacket | GGAPacket | GLLPacket | GNSPacket | GSAPacket | GSTPacket | GSVPacket | HDGPacket | HDMPacket | HDTPacket | MTKPacket | MWVPacket | RDIDPacket | RMCPacket | VHWPacket | VTGPacket | ZDAPacket;
export { APBPacket, BWCPacket, DBTPacket, DTMPacket, GGAPacket, GLLPacket, GNSPacket, GSAPacket, GSTPacket, GSVPacket, HDGPacket, HDMPacket, HDTPacket, MTKPacket, MWVPacket, RDIDPacket, RMCPacket, VHWPacket, VTGPacket, ZDAPacket };

export function assertPacketIs<IdType extends string, PacketType extends PacketStub = Packet>(packetId: IdType, packet: PacketType): asserts packet is (PacketType & { sentenceId: IdType }) {
if (packet.sentenceId !== packetId) {
throw new Error(`Was expecting packetId of '${packetId}'. Found '${packet.sentenceId}' instead.`);
}
}

type Decoder = (stub: PacketStub, parts: string[]) => Packet;

Expand Down
10 changes: 3 additions & 7 deletions tests/CustomPacketsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "should";

import { initStubFields, PacketStub } from "../codecs/PacketStub";
import { appendChecksumFooter } from "../helpers";
import { parseGenericPacket, DefaultPacketFactory } from "../index";
import { assertPacketIs, parseGenericPacket, DefaultPacketFactory } from "../index";



Expand Down Expand Up @@ -61,9 +61,7 @@ describe("CustomPackets", (): void => {
it("Log", (): void => {
const packet = parseGenericPacket(appendChecksumFooter("$P_LOG,5,everything is ok"), CUSTOM_PACKET_FACTORY);

if (packet.sentenceId !== logSentenceId) {
throw Error("Bad packet type");
}
assertPacketIs(logSentenceId, packet);

packet.logNum.should.equal(5);
packet.logMsg.should.equal("everything is ok");
Expand All @@ -72,9 +70,7 @@ describe("CustomPackets", (): void => {
it("Btn", (): void => {
const packet = parseGenericPacket(appendChecksumFooter("$P_BTN,0,L"), CUSTOM_PACKET_FACTORY);

if (packet.sentenceId !== buttonPressSentenceId) {
throw Error("Bad packet type");
}
assertPacketIs(buttonPressSentenceId, packet);

packet.buttonId.should.equal(0);
packet.longPress.should.equal(true);
Expand Down
10 changes: 3 additions & 7 deletions tests/UnsafePacketsTest.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import "should";

import { getUnsafePacketId, parseUnsafeNmeaSentence } from "../index";
import { assertPacketIs, getUnsafePacketId, parseUnsafeNmeaSentence } from "../index";

describe("UnsafeParsing", (): void => {
it("Built in NMEA sentence", (): void => {
const packet = parseUnsafeNmeaSentence("$GPZDA,160012.71,11,03,2004,-1,00*7D");

if (packet.sentenceId !== "ZDA") {
throw Error("sentenceId should be \"ZDA\"");
}
assertPacketIs("ZDA", packet);

packet.localZoneHours.should.equal(-1);
});

it("Unknown sentence", (): void => {
const packet = parseUnsafeNmeaSentence("$GPTXT,01,01,02,ANTSTATUS=OPEN*2B");

if (packet.sentenceId !== "?") {
throw Error("sentenceId should be \"?\"");
}
assertPacketIs("?", packet);

packet.dataFields.length.should.equal(4);
(packet.talkerId === "GP").should.equal(true);
Expand Down

0 comments on commit 5221ca4

Please sign in to comment.