UNPKG

@sports-alliance/sports-lib

Version:

A Library to for importing / exporting and processing GPX, TCX, FIT and JSON files from services such as Strava, Movescount, Garmin, Polar etc

88 lines (87 loc) 3.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Stream = void 0; var helpers_1 = require("../events/utilities/helpers"); var data_store_1 = require("../data/data.store"); var Stream = /** @class */ (function () { function Stream(type, data) { this.data = []; this.filter = null; this.type = type; if (data) { this.data = data; } } Stream.prototype.clearFilters = function () { this.filter = null; return this; }; Stream.prototype.useFilter = function (filter) { this.filter = filter; return this; }; Stream.prototype.hasFilter = function () { return !!this.filter; }; Stream.prototype.getData = function (onlyNumeric, filterInfinity) { var _this = this; if (onlyNumeric === void 0) { onlyNumeric = false; } if (filterInfinity === void 0) { filterInfinity = false; } var data = this.filter ? this.filter.filterData(this.data) : this.data; if (!onlyNumeric && !filterInfinity) { return data; } return data.filter(function (dataItem) { return !_this.shouldDataBeFiltered(dataItem, onlyNumeric, filterInfinity); }); }; Stream.prototype.setData = function (data) { this.data = data; return this; }; Stream.prototype.getStreamDataByTime = function (startDate, onlyNumeric, filterInfinity) { var _this = this; if (onlyNumeric === void 0) { onlyNumeric = false; } if (filterInfinity === void 0) { filterInfinity = false; } return this.getData().reduce(function (accu, dataItem, index) { if (_this.shouldDataBeFiltered(dataItem, onlyNumeric, filterInfinity)) { return accu; } accu.push({ time: startDate.getTime() + index * 1000, value: dataItem }); return accu; }, []); }; Stream.prototype.getStreamDataByDuration = function (offset, onlyNumeric, filterInfinity) { var _this = this; if (offset === void 0) { offset = 0; } if (onlyNumeric === void 0) { onlyNumeric = false; } if (filterInfinity === void 0) { filterInfinity = false; } return this.getData().reduce(function (accu, dataItem, index) { if (_this.shouldDataBeFiltered(dataItem, onlyNumeric, filterInfinity)) { return accu; } accu.push({ time: index * 1000 + (offset || 0), value: dataItem }); return accu; }, []); }; Stream.prototype.isExportable = function () { return (!data_store_1.DynamicDataLoader.isUnitDerivedDataType(this.type) && !data_store_1.DynamicDataLoader.isSpeedDerivedDataType(this.type) && !data_store_1.DynamicDataLoader.isBlackListedStream(this.type)); }; Stream.prototype.toJSON = function () { return { type: this.type, data: this.data // Exporting does/ should not use a filter }; }; Stream.prototype.shouldDataBeFiltered = function (data, onlyNumeric, filterInfinity) { return (onlyNumeric && !helpers_1.isNumber(data)) || (filterInfinity && (data === Infinity || data === -Infinity)); }; return Stream; }()); exports.Stream = Stream;