@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
77 lines (76 loc) • 4.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SwimLength = void 0;
const data_cadence_1 = require("../data/data.cadence");
const data_distance_1 = require("../data/data.distance");
const data_duration_1 = require("../data/data.duration");
const data_energy_1 = require("../data/data.energy");
const data_heart_rate_1 = require("../data/data.heart-rate");
const data_speed_1 = require("../data/data.speed");
class SwimLength {
constructor(init) {
Object.assign(this, init);
}
static fromJSON(json) {
var _a;
const nullableNumber = (value) => {
if (value === null || value === undefined) {
return null;
}
if (typeof value === 'number' && Number.isFinite(value)) {
return value;
}
if (typeof value === 'string' && value.trim().length > 0) {
const parsedValue = Number(value);
return Number.isFinite(parsedValue) ? parsedValue : null;
}
return null;
};
const nullableData = (value, dataConstructor) => {
const numericValue = nullableNumber(value);
return numericValue === null ? null : new dataConstructor(numericValue);
};
return new SwimLength({
index: json.index,
lapIndex: nullableNumber(json.lapIndex),
startDate: new Date(json.startDate),
endDate: new Date(json.endDate),
type: json.type,
stroke: (_a = json.stroke) !== null && _a !== void 0 ? _a : null,
strokes: nullableNumber(json.strokes),
elapsedTime: nullableData(json.elapsedTime, data_duration_1.DataDuration),
timerTime: nullableData(json.timerTime, data_duration_1.DataDuration),
distance: nullableData(json.distance, data_distance_1.DataDistance),
poolLength: nullableData(json.poolLength, data_distance_1.DataDistance),
avgSpeed: nullableData(json.avgSpeed, data_speed_1.DataSpeed),
avgCadence: nullableData(json.avgCadence, data_cadence_1.DataCadence),
avgHeartRate: nullableData(json.avgHeartRate, data_heart_rate_1.DataHeartRate),
maxHeartRate: nullableData(json.maxHeartRate, data_heart_rate_1.DataHeartRate),
swolf: nullableNumber(json.swolf),
calories: nullableData(json.calories, data_energy_1.DataEnergy)
});
}
toJSON() {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
return {
index: this.index,
lapIndex: this.lapIndex,
startDate: this.startDate.getTime(),
endDate: this.endDate.getTime(),
type: this.type,
stroke: this.stroke,
strokes: this.strokes,
elapsedTime: (_b = (_a = this.elapsedTime) === null || _a === void 0 ? void 0 : _a.getValue()) !== null && _b !== void 0 ? _b : null,
timerTime: (_d = (_c = this.timerTime) === null || _c === void 0 ? void 0 : _c.getValue()) !== null && _d !== void 0 ? _d : null,
distance: (_f = (_e = this.distance) === null || _e === void 0 ? void 0 : _e.getValue()) !== null && _f !== void 0 ? _f : null,
poolLength: (_h = (_g = this.poolLength) === null || _g === void 0 ? void 0 : _g.getValue()) !== null && _h !== void 0 ? _h : null,
avgSpeed: (_k = (_j = this.avgSpeed) === null || _j === void 0 ? void 0 : _j.getValue()) !== null && _k !== void 0 ? _k : null,
avgCadence: (_m = (_l = this.avgCadence) === null || _l === void 0 ? void 0 : _l.getValue()) !== null && _m !== void 0 ? _m : null,
avgHeartRate: (_p = (_o = this.avgHeartRate) === null || _o === void 0 ? void 0 : _o.getValue()) !== null && _p !== void 0 ? _p : null,
maxHeartRate: (_r = (_q = this.maxHeartRate) === null || _q === void 0 ? void 0 : _q.getValue()) !== null && _r !== void 0 ? _r : null,
swolf: this.swolf,
calories: (_t = (_s = this.calories) === null || _s === void 0 ? void 0 : _s.getValue()) !== null && _t !== void 0 ? _t : null
};
}
}
exports.SwimLength = SwimLength;