Skip to content

Commit

Permalink
fix(mf-utilities): types returned by preprocessIonizations (#258)
Browse files Browse the repository at this point in the history
migrate to ts

Closes: #243
  • Loading branch information
tpoisseau authored Dec 19, 2024
1 parent f295376 commit 629973d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
7 changes: 7 additions & 0 deletions packages/mf-parser/src/util/flatten.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @param parsed
* @param options
* @return {string[]}
*/
export function flatten(parsed, options = {}) {
const { groupIdentical = false, limit = 100000 } = options;
if (parsed.length === 0) return [''];
Expand Down Expand Up @@ -114,6 +119,8 @@ function createMFs(parts, comment, limit) {
for (let i = 0; i < currents.length; i++) {
currents[i] = parts[i].min;
}

/** @type {string[]} */
const mfs = [];
let position = 0;
while (position < currents.length) {
Expand Down
File renamed without changes.
25 changes: 0 additions & 25 deletions packages/mf-utilities/src/preprocessIonizations.js

This file was deleted.

35 changes: 35 additions & 0 deletions packages/mf-utilities/src/preprocessIonizations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { MF } from 'mf-parser';
import type { AtomsMap } from 'mf-parser';

export interface Ionization {
mf: string;
em: number;
charge: number;
atoms: AtomsMap;
}

export function preprocessIonizations(
ionizationsString: string | Ionization[] = '',
): Ionization[] {
if (Array.isArray(ionizationsString)) return ionizationsString;
const ionizations = ionizationsString.split(/ *[\t\n\r,.;]+ */);

// it is allowed to have ranges in Ionizations. We need to explode them.

const results: Ionization[] = [];

for (const ionization of ionizations) {
const parts = new MF(ionization).flatten();
for (const part of parts) {
const info = new MF(part).getInfo();
results.push({
mf: part,
em: info.monoisotopicMass,
charge: info.charge,
atoms: info.atoms,
});
}
}

return results;
}

0 comments on commit 629973d

Please sign in to comment.