UNPKG

extended-nmea

Version:

A TypeScript library for parsing NMEA0183-like sentences with support for custom and proprietary sentences.

67 lines 2.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NmeaSentence = void 0; class NmeaSentence { /** * Create a NMEA0183 sentence from a string. * * @param data The line to interpret as an NMEA0183 sentence. Can also be an existing Sentence. * @param type The type of this sentence. * @param prefix The prefix to use when validating the sentence. * @param suffix The suffix to use when validating the sentence. */ constructor(data, type, prefix = NmeaSentence.Prefix, suffix = NmeaSentence.Suffix) { if (typeof data === 'string') this.raw = data; else this.raw = data.raw; this.type = type; this.prefix = prefix; this.suffix = suffix; } /** * Whether or not this sentence is a valid NMEA0183 sentence. * * All data is transmitted in the form of sentences. Only printable ASCII characters are allowed, plus CR (carriage * return) and LF (line feed). Each sentence starts with a "$" sign and ends with <CR><LF>. * * This does _not_ validate the checksum, regardless of if one is contained in the sentence. */ get valid() { // TODO: check if all characters are printable ASCII characters (except <CR><LF>). // MAYBE: add an option for sentences to be able to omit <CR><LF> (e.g. when parsing a string from a readline which automatically removes the line delimiters). return this.raw.startsWith(this.prefix) && this.raw.endsWith(this.suffix); } get invalidReason() { if (!this.raw.startsWith(this.prefix)) { return 'The sentence does not start with the expected prefix.'; } if (!this.raw.endsWith(this.suffix)) { return 'The sentence does not end with the expected suffix.'; } return null; } /** * Returns all characters between "$" and "<CR><LF>". */ get dataNoFixtures() { return this.raw.slice(this.prefix.length, -this.suffix.length); } /** * Returns all fields (including the first field in the sentence) separated by comma. */ get fields() { return this.dataNoFixtures.split(','); } } exports.NmeaSentence = NmeaSentence; /** * Each NMEA0183 sentence starts with a "$" sign. */ NmeaSentence.Prefix = "$"; /** * Each NMEA0183 sentence ends with <CR><LF>. */ NmeaSentence.Suffix = "\r\n"; //# sourceMappingURL=NmeaSentence.js.map