@infect/infect-rda-sample-importer
Version:
INFECT Sample Data Importer
88 lines (59 loc) • 1.75 kB
JavaScript
/**
* The sample processor is responsible for processing samples that are passed to it. It normalizes
* values by using fieldProcessors that may convert incoming values into a standardized format. It
* the makes sure that all required data is present
*
* @class SampleProcessor (name)
*/
export default class SampleProcessor {
constructor({
config,
}) {
this.config = config;
this.fieldProcessors = new Set();
this.isLoaded = false;
}
async load() {
this.isLoaded = true;
}
/**
* batch process samples
*
* @param {Array} samples the samples to process
*/
async processSamples(samples) {
if (!this.isLoaded) {
throw new Error(`The sample processor is not loaded!`);
}
const validSamples = [];
const invalidSamples = [];
await Promise.all(samples.map(async (sample) => {
await this.process(sample);
if (sample.isValid()) {
validSamples.push(sample);
} else {
invalidSamples.push(sample);
}
}));
return {
invalidSamples,
validSamples,
}
}
/**
* process one sample, process each field separately and check for field requirements thereafter
*
* @param {Object} sample instance of the Sample Class
*/
async process(sample) {
await sample.process(this.fieldProcessors);
}
/**
* adds a field processor to the sample processor
*
* @param {object} processor The processor
*/
registerFieldProcessor(processor) {
this.fieldProcessors.add(processor);
}
}