Skip to content

Commit

Permalink
feat: toText deals correctly with normalization preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed May 7, 2024
1 parent 24711a2 commit 97b26a2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface AnalysisOptions {
label?: string;
spectrumCallback?: SpectrumCallback;
}
interface NormalizedOptions {
export interface NormalizedOptions {
normalization?: NormalizedSpectrumOptions;
selector?: SpectrumSelector;
}
Expand Down
9 changes: 9 additions & 0 deletions src/to/__tests__/toText.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,13 @@ describe('toCvs', () => {
const result = toText(new Analysis());
expect(result).toHaveLength(0);
});

it('select variables with normalizaiton', () => {
const result = toText(analysis, {
selector: { xLabel: 'seconds', yLabel: 'voltage' },
normalization: { from: 5, to: 6 },
});
expect(result).toHaveLength(1);
expect(result[0]).toBe('seconds,voltage\n5,1\n6,2');
});
});
13 changes: 6 additions & 7 deletions src/to/toText.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { Analysis } from '../Analysis';
import { Analysis, NormalizedOptions } from '../Analysis';
import type { Spectrum } from '../types/Cheminfo';
import { SpectrumSelector } from '../types/SpectrumSelector';

interface ToTextOptions {
selector?: SpectrumSelector;
interface ToTextOptions extends NormalizedOptions {
endOfLine?: string;
fieldSeparator?: string;
}

export function toText(analysis: Analysis, options: ToTextOptions = {}) {
// Export all the data to Csv
if (!options.selector) {
if (!options.selector && !options.normalization) {
return exportText(analysis.spectra, options);
}

// Export selected variables
const spectra = analysis.getXYSpectrum(options.selector);
return exportText(spectra ? [spectra] : [], options);
const spectra = analysis.getNormalizedSpectra(options);
// const spectra = analysis.getXYSpectrum(options.selector);
return exportText(spectra, options);
}

function exportText(spectrums: Spectrum[], options: ToTextOptions) {
Expand Down

0 comments on commit 97b26a2

Please sign in to comment.