@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
87 lines (86 loc) • 3.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getStreamSelectionFromOptions = getStreamSelectionFromOptions;
exports.isStreamTypeAllowedForImport = isStreamTypeAllowedForImport;
exports.pruneActivityStreamsBySelection = pruneActivityStreamsBySelection;
const data_store_1 = require("../data/data.store");
const parsing_event_lib_error_1 = require("../errors/parsing-event-lib.error");
const stream_derivation_registry_1 = require("./stream.derivation.registry");
function validateAndNormalizeRequestedTypes(requestedTypes) {
const normalizedTypes = new Set();
const unknownTypes = [];
requestedTypes.forEach(type => {
const normalizedType = typeof type === 'string' ? type.trim() : String(type);
try {
const dataClass = data_store_1.DynamicDataLoader.getDataClassFromDataType(normalizedType);
normalizedTypes.add(dataClass.type);
}
catch (_error) {
unknownTypes.push(type);
}
});
if (unknownTypes.length > 0) {
throw new parsing_event_lib_error_1.ParsingEventLibError(`Unknown stream includeTypes: ${unknownTypes.join(', ')}`);
}
return normalizedTypes;
}
function getUnitDependencyTypes(dataType) {
return Object.entries(data_store_1.DynamicDataLoader.dataTypeUnitGroups).reduce((dependencies, [baseDataType, group]) => {
if (Object.keys(group).indexOf(dataType) !== -1) {
dependencies.push(baseDataType);
}
return dependencies;
}, []);
}
function getDependencyTypes(dataType) {
const explicitDependencies = (0, stream_derivation_registry_1.getDependencyTypesForResolution)(dataType);
const unitDependencies = getUnitDependencyTypes(dataType);
return Array.from(new Set([...explicitDependencies, ...unitDependencies]));
}
function expandToImportAllowSet(outputAllowSet) {
const importAllowSet = new Set(outputAllowSet);
const queue = [...outputAllowSet];
while (queue.length) {
const currentType = queue.shift();
if (!currentType) {
continue;
}
getDependencyTypes(currentType).forEach(dependencyType => {
if (importAllowSet.has(dependencyType)) {
return;
}
importAllowSet.add(dependencyType);
queue.push(dependencyType);
});
}
return importAllowSet;
}
function getStreamSelectionFromOptions(options) {
var _a;
const requestedTypes = (_a = options.streams) === null || _a === void 0 ? void 0 : _a.includeTypes;
if (!requestedTypes || requestedTypes.length === 0) {
return null;
}
const outputAllowSet = validateAndNormalizeRequestedTypes(requestedTypes);
const importAllowSet = expandToImportAllowSet(outputAllowSet);
return { importAllowSet, outputAllowSet };
}
function isStreamTypeAllowedForImport(dataType, streamSelection) {
if (!streamSelection) {
return true;
}
return streamSelection.importAllowSet.has(dataType);
}
function pruneActivityStreamsBySelection(activity, streamSelection) {
if (!streamSelection) {
return;
}
activity
.getAllStreams()
.slice()
.forEach(stream => {
if (!streamSelection.outputAllowSet.has(stream.type)) {
activity.removeStream(stream);
}
});
}