UNPKG

@signalk/nmea0183-signalk

Version:

A node.js/javascript parser for NMEA0183 sentences. Sentences are parsed to Signal K format.

100 lines 3.33 kB
"use strict"; /** * NMEA-field → typed-union boundary helpers. * * Raw NMEA fields are `string` at parse time; TypeScript can't narrow them * to the tight `Pole` / `UnitFormat` unions that `@signalk/nmea0183-utilities` * exposes without runtime validation. Each helper below does that validation * and returns `null` when the field doesn't match the spec letter set, so * malformed sentences produce a `null` delta value instead of bubbling a * `TypeError` up through the Parser. */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.toPole = toPole; exports.toUnit = toUnit; exports.coord = coord; exports.magVar = magVar; const utils = __importStar(require("@signalk/nmea0183-utilities")); const POLES = new Set(['N', 'S', 'E', 'W']); /** Narrow a raw NMEA cardinal-letter field to `Pole`, or `null` if invalid. */ function toPole(raw) { if (raw === undefined || raw === null) return null; return POLES.has(raw) ? raw : null; } const UNITS = new Set([ 'km', 'nm', 'm', 'ft', 'fa', 'knots', 'kph', 'ms', 'mph', 'deg', 'rad', 'c', 'k', 'f' ]); /** Narrow a raw NMEA unit-letter field to `UnitFormat`, or `null` if invalid. */ function toUnit(raw) { if (raw === undefined || raw === null) return null; return UNITS.has(raw) ? raw : null; } /** * `utils.coordinate` with pole-field validation. Returns `null` when the * pole letter isn't `N` / `S` / `E` / `W` rather than throwing. */ function coord(value, pole) { const p = toPole(pole); if (p === null) return null; return utils.coordinate(value, p); } /** * `utils.magneticVariation` with pole-field validation. Returns `null` * when the pole letter isn't valid rather than throwing. */ function magVar(degrees, pole) { const p = toPole(pole); if (p === null) return null; return utils.magneticVariation(degrees, p); } //# sourceMappingURL=nmea-casts.js.map