From 629973d8175d3d3b0c75d1a488cfc83261c578f4 Mon Sep 17 00:00:00 2001 From: tpoisseau <22891227+tpoisseau@users.noreply.github.com> Date: Thu, 19 Dec 2024 11:27:52 +0100 Subject: [PATCH] fix(mf-utilities): types returned by preprocessIonizations (#258) migrate to ts Closes: https://github.com/cheminfo/mass-tools/issues/243 --- packages/mf-parser/src/util/flatten.js | 7 ++++ .../mf-utilities/src/{index.js => index.ts} | 0 .../mf-utilities/src/preprocessIonizations.js | 25 ------------- .../mf-utilities/src/preprocessIonizations.ts | 35 +++++++++++++++++++ 4 files changed, 42 insertions(+), 25 deletions(-) rename packages/mf-utilities/src/{index.js => index.ts} (100%) delete mode 100644 packages/mf-utilities/src/preprocessIonizations.js create mode 100644 packages/mf-utilities/src/preprocessIonizations.ts diff --git a/packages/mf-parser/src/util/flatten.js b/packages/mf-parser/src/util/flatten.js index 1fd037f8..37c5502c 100644 --- a/packages/mf-parser/src/util/flatten.js +++ b/packages/mf-parser/src/util/flatten.js @@ -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 ['']; @@ -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) { diff --git a/packages/mf-utilities/src/index.js b/packages/mf-utilities/src/index.ts similarity index 100% rename from packages/mf-utilities/src/index.js rename to packages/mf-utilities/src/index.ts diff --git a/packages/mf-utilities/src/preprocessIonizations.js b/packages/mf-utilities/src/preprocessIonizations.js deleted file mode 100644 index d62c1789..00000000 --- a/packages/mf-utilities/src/preprocessIonizations.js +++ /dev/null @@ -1,25 +0,0 @@ -import { MF } from 'mf-parser'; - -export function preprocessIonizations(ionizationsString = '') { - if (Array.isArray(ionizationsString)) return ionizationsString; - let ionizations = ionizationsString.split(/ *[\t\n\r,.;]+ */); - - // it is allowed to have ranges in Ionizations. We need to explode them. - - let results = []; - - for (let ionization of ionizations) { - let parts = new MF(ionization).flatten(); - for (let part of parts) { - let info = new MF(part).getInfo(); - results.push({ - mf: part, - em: info.monoisotopicMass, - charge: info.charge, - atoms: info.atoms, - }); - } - } - - return results; -} diff --git a/packages/mf-utilities/src/preprocessIonizations.ts b/packages/mf-utilities/src/preprocessIonizations.ts new file mode 100644 index 00000000..b18577ee --- /dev/null +++ b/packages/mf-utilities/src/preprocessIonizations.ts @@ -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; +}