UNPKG

gnss_solutions

Version:

Javascript GNSS solution analysis library

124 lines (96 loc) 9.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TimeMismatchError = exports.EmptyTimeRangeError = exports.InvalidTimestampError = undefined; exports.timestampFromArg = timestampFromArg; exports.mergeEvents = mergeEvents; var _moment = require("moment"); var moment = _interopRequireWildcard(_moment); var _errors = require("./errors"); var err = _interopRequireWildcard(_errors); var _lodash = require("lodash"); var _ = _interopRequireWildcard(_lodash); var _pondjs = require("pondjs"); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /* * Copyright (c) 2016 Swift Navigation Inc. * Contact: engineering@swiftnav.com * * This source is subject to the license found in the file 'LICENSE' which must * be be distributed together with this source. All other rights reserved. * * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. */ function timestampFromArg(arg) { if (_.isNumber(arg)) { return new Date(arg); } else if (_.isDate(arg)) { return new Date(arg.getTime()); } else if (_.isString(arg)) { return new Date(arg); } else if (moment.isMoment(arg)) { return new Date(arg.valueOf()); } else { throw new InvalidTimestampError(arg); } } var InvalidTimestampError = exports.InvalidTimestampError = function (_err$CustomException) { _inherits(InvalidTimestampError, _err$CustomException); function InvalidTimestampError(arg) { _classCallCheck(this, InvalidTimestampError); return _possibleConstructorReturn(this, (InvalidTimestampError.__proto__ || Object.getPrototypeOf(InvalidTimestampError)).call(this, "Unable to get timestamp from ${arg}. Should be a number, date, or moment.")); } return InvalidTimestampError; }(err.CustomException); var EmptyTimeRangeError = exports.EmptyTimeRangeError = function (_err$CustomException2) { _inherits(EmptyTimeRangeError, _err$CustomException2); function EmptyTimeRangeError(arg) { _classCallCheck(this, EmptyTimeRangeError); return _possibleConstructorReturn(this, (EmptyTimeRangeError.__proto__ || Object.getPrototypeOf(EmptyTimeRangeError)).call(this, "TimeRange for table ${arg} is empty!")); } return EmptyTimeRangeError; }(err.CustomException); var TimeMismatchError = exports.TimeMismatchError = function (_err$CustomException3) { _inherits(TimeMismatchError, _err$CustomException3); function TimeMismatchError(time1, time2) { _classCallCheck(this, TimeMismatchError); return _possibleConstructorReturn(this, (TimeMismatchError.__proto__ || Object.getPrototypeOf(TimeMismatchError)).call(this, "Items require the same timestamp! ${time1} ${time2}")); } return TimeMismatchError; }(err.CustomException); // TODO(mookerji): replace with something using mergeDeep from Immutable /** * Merge an array of events, with possibly overlapping keys, but keeping the * last non-nil value. * * @param {[Events]} events - Array of events * @return {Event} - Merged event */ function mergeEvents(events) { if (events.length == 1) { return events[0]; } var t = events[0].timestamp(); var data = {}; _.each(events, function (event) { if (t.getTime() !== event.timestamp().getTime()) { throw new TimeMismatchError(t.getTime(), event.timestamp().getTime()); } var d = event.data().toJSON(); _.each(d, function (val, key) { if (key in data) { if (!isNaN(val)) { data[key] = val; } } else { data[key] = val; } }); }); return new _pondjs.Event(t.getTime(), data); }