Comparing version 3.2.0 to 3.2.1
{ | ||
"name": "mf-parser", | ||
"version": "3.2.0", | ||
"version": "3.2.1", | ||
"description": "Parse a molecular formula", | ||
"main": "lib/index.js", | ||
"main": "lib/src/index.js", | ||
"module": "src/index.js", | ||
@@ -23,6 +23,5 @@ "files": [ | ||
"dependencies": { | ||
"atom-sorter": "^2.1.0", | ||
"chemical-elements": "^2.1.0", | ||
"chemical-groups": "^2.2.0", | ||
"mf-utilities": "^3.2.0" | ||
"atom-sorter": "^2.1.1", | ||
"chemical-elements": "^2.1.1", | ||
"chemical-groups": "^2.2.1" | ||
}, | ||
@@ -32,3 +31,3 @@ "devDependencies": { | ||
}, | ||
"gitHead": "28dae91d3b42556a23097ee08acfe4061f276ed0" | ||
"gitHead": "838f98a30846d4b1721b8ed7aa94a55e854d7521" | ||
} |
import { elementsObject } from 'chemical-elements'; | ||
const elements = Object.keys(elementsObject).sort( | ||
(a, b) => b.length - a.length, | ||
const elements = new Set( | ||
Object.keys(elementsObject).sort((a, b) => b.length - a.length), | ||
); | ||
@@ -15,2 +15,3 @@ | ||
for (let i = 0; i < mf.length; i++) { | ||
// eslint-disable-next-line unicorn/prefer-code-point | ||
if (mf.charCodeAt(i) > 64 && mf.charCodeAt(i) < 91) { | ||
@@ -20,3 +21,3 @@ return mf; | ||
} | ||
let parts = mf.replace(/([a-z]*)([^a-z]*)/g, '$1 $2 ').split(/ +/); | ||
let parts = mf.replaceAll(/([a-z]*)([^a-z]*)/g, '$1 $2 ').split(/ +/); | ||
for (let i = 0; i < parts.length; i++) { | ||
@@ -28,3 +29,3 @@ if (parts[i].match(/^[a-z]$/)) { | ||
for (let j = 0; j < parts[i].length; j++) { | ||
let two = parts[i].substr(j, 2); | ||
let two = parts[i].slice(j, j + 2); | ||
let one = parts[i].charAt(j).toUpperCase(); | ||
@@ -39,6 +40,6 @@ if ( | ||
two = two.charAt(0).toUpperCase() + two.charAt(1); | ||
if (elements.includes(two)) { | ||
if (elements.has(two)) { | ||
newPart += two; | ||
j++; | ||
} else if (elements.includes(one)) { | ||
} else if (elements.has(one)) { | ||
newPart += one; | ||
@@ -45,0 +46,0 @@ } else { |
@@ -15,2 +15,6 @@ import { ensureCase } from './ensureCase'; | ||
/** @typedef {import('./util/getIsotopesInfo.types').IsotopesInfo} IsotopesInfo */ | ||
/** @typedef {import('./util/getInfo.types').PartInfo} PartInfo */ | ||
/** @typedef {import('./util/getInfo.types').PartInfoWithParts} PartInfoWithParts */ | ||
/** | ||
@@ -70,2 +74,3 @@ * Class allowing to deal with molecular formula and derived information | ||
* @param {string} [options.msemFieldName='observedMonoisotopicMass'] name of the observed monoisotopic mass field | ||
* @returns {PartInfo|PartInfoWithParts} | ||
*/ | ||
@@ -105,2 +110,3 @@ getInfo(options = {}) { | ||
* Returns an array with each atom and isotopic composition | ||
* @returns {IsotopesInfo} | ||
*/ | ||
@@ -107,0 +113,0 @@ getIsotopesInfo(options = {}) { |
@@ -0,1 +1,3 @@ | ||
/* eslint-disable unicorn/prefer-code-point */ | ||
import { Kind } from './Kind'; | ||
@@ -21,7 +23,4 @@ import { parseCharge } from './util/parseCharge'; | ||
while (this.i < mf.length) { | ||
if ( | ||
this.result.length > 0 && | ||
this.result[this.result.length - 1].kind !== Kind.TEXT | ||
) { | ||
lastKind = this.result[this.result.length - 1].kind; | ||
if (this.result.length > 0 && this.result.at(-1).kind !== Kind.TEXT) { | ||
lastKind = this.result.at(-1).kind; | ||
} | ||
@@ -56,3 +55,3 @@ let char = mf.charAt(this.i); | ||
} | ||
this.result[this.result.length - 1].value = value.from; | ||
this.result.at(-1).value = value.from; | ||
} else if (value.to) { | ||
@@ -115,3 +114,3 @@ this.result.push({ | ||
if (lastKind === Kind.ATOM) { | ||
let lastResult = this.result[this.result.length - 1]; | ||
let lastResult = this.result.at(-1); | ||
lastResult.kind = Kind.ISOTOPE_RATIO; | ||
@@ -147,3 +146,3 @@ lastResult.value = { | ||
kind: Kind.COMMENT, | ||
value: this.mf.substring(this.i + 1), | ||
value: this.mf.slice(this.i + 1), | ||
}); | ||
@@ -196,4 +195,4 @@ break; | ||
return { | ||
from: parseNumberWithDivision(number.substr(0, indexOfDash)), | ||
to: parseNumberWithDivision(number.substr(indexOfDash + 1)), | ||
from: parseNumberWithDivision(number.slice(0, indexOfDash)), | ||
to: parseNumberWithDivision(number.slice(indexOfDash + 1)), | ||
}; | ||
@@ -223,4 +222,4 @@ } | ||
let atom = substring.replace(/[^a-zA-Z]/g, ''); | ||
let isotope = Number(substring.replace(/[^0-9]/g, '')); | ||
let atom = substring.replaceAll(/[^A-Za-z]/g, ''); | ||
let isotope = Number(substring.replaceAll(/\D/g, '')); | ||
return { atom, isotope }; | ||
@@ -241,4 +240,4 @@ } | ||
} while (ascii !== 125 && this.i <= this.mf.length); // closing curly bracket | ||
if (substring.match(/^[0-9,]+$/)) { | ||
return substring.split(',').map((a) => Number(a)); | ||
if (substring.match(/^[\d,]+$/)) { | ||
return substring.split(',').map(Number); | ||
} | ||
@@ -260,4 +259,4 @@ throw new MFError( | ||
} while (ascii !== 41 && this.i <= this.mf.length); // closing parenthesis | ||
if (substring.match(/^\([0-9+-]+$/)) { | ||
return parseCharge(substring.substring(1)); | ||
if (substring.match(/^\([\d+-]+$/)) { | ||
return parseCharge(substring.slice(1)); | ||
} else { | ||
@@ -264,0 +263,0 @@ this.i = begin; |
@@ -11,3 +11,3 @@ import { elementsObject } from 'chemical-elements'; | ||
export function capitalizeMF(mf) { | ||
if (!mf.match(/^[0-9a-zA-Z ]+/)) return mf; | ||
if (!mf.match(/^[\d A-Za-z]+/)) return mf; | ||
if (isMF(mf)) return mf; | ||
@@ -18,3 +18,3 @@ let oldMF = mf | ||
.replace(/cd/, ' C D ') | ||
.replace(/([0-9])([a-zA-Z])/, '$1 $2'); | ||
.replace(/(\d)([A-Za-z])/, '$1 $2'); | ||
let newMF = ''; | ||
@@ -25,4 +25,4 @@ let parts = oldMF.split(/ +/); | ||
if (parts[i] === '') continue; | ||
let label = parts[i].replace(/[0-9]*$/, ''); | ||
let number = parts[i].replace(/^[a-zA-Z]*/, ''); | ||
let label = parts[i].replace(/\d*$/, ''); | ||
let number = parts[i].replace(/^[A-Za-z]*/, ''); | ||
@@ -47,3 +47,3 @@ let casedLabel = getCasedLabel(label); | ||
let casedLabel = | ||
label.substr(0, 1).toUpperCase() + label.substr(1).toLowerCase(); | ||
label.slice(0, 1).toUpperCase() + label.slice(1).toLowerCase(); | ||
if (elementsObject[label]) return casedLabel; | ||
@@ -55,16 +55,16 @@ if (groupsObject[label]) return casedLabel; | ||
.toLowerCase() | ||
.replace(/cl/g, 'Cl') | ||
.replace(/br/g, 'Br') | ||
.replace(/na/g, 'Na') | ||
.replace(/li/g, 'Li') | ||
.replace(/si/g, 'Si') | ||
.replace(/cr/g, 'Cr') | ||
.replace(/c/g, 'C') | ||
.replace(/o/g, 'O') | ||
.replace(/n/g, 'N') | ||
.replace(/h/g, 'H') | ||
.replace(/k/g, 'K') | ||
.replace(/s/g, 'S'); | ||
.replaceAll('cl', 'Cl') | ||
.replaceAll('br', 'Br') | ||
.replaceAll('na', 'Na') | ||
.replaceAll('li', 'Li') | ||
.replaceAll('si', 'Si') | ||
.replaceAll('cr', 'Cr') | ||
.replaceAll('c', 'C') | ||
.replaceAll('o', 'O') | ||
.replaceAll('n', 'N') | ||
.replaceAll('h', 'H') | ||
.replaceAll('k', 'K') | ||
.replaceAll('s', 'S'); | ||
if (isMF(newLabel)) return newLabel; | ||
return label; | ||
} |
@@ -129,3 +129,5 @@ export function flatten(parsed, options = {}) { | ||
if (mfs.length > limit) { | ||
throw Error(`MF.flatten generates too many fragments (over ${limit})`); | ||
throw new Error( | ||
`MF.flatten generates too many fragments (over ${limit})`, | ||
); | ||
} | ||
@@ -132,0 +134,0 @@ } |
@@ -46,3 +46,3 @@ import { | ||
element = groups[line.value]; | ||
if (!element) throw Error(`Unknown element: ${line.value}`); | ||
if (!element) throw new Error(`Unknown element: ${line.value}`); | ||
// need to explode group ???? | ||
@@ -72,5 +72,5 @@ } | ||
eas.forEach((ea) => { | ||
for (const ea of eas) { | ||
ea.ratio = ea.mass / sum; | ||
}); | ||
} | ||
return eas; | ||
@@ -77,0 +77,0 @@ } |
@@ -30,5 +30,5 @@ import { elementsObject, elementsAndIsotopesObject } from 'chemical-elements'; | ||
} | ||
let isotope = element.isotopes.filter( | ||
let isotope = element.isotopes.find( | ||
(a) => a.nominal === line.value.isotope, | ||
)[0]; | ||
); | ||
if (!isotope) { | ||
@@ -35,0 +35,0 @@ throw new Error(`isotope unknown: ${line.value.isotope} - ${line}`); |
@@ -15,2 +15,5 @@ import { | ||
/** @typedef {import('./getInfo.types').PartInfo} PartInfo */ | ||
/** @typedef {import('./getInfo.types').PartInfoWithParts} PartInfoWithParts */ | ||
/** | ||
@@ -20,2 +23,3 @@ * | ||
* @param {*} [options={}] | ||
* @returns {object|PartInfo|PartInfoWithParts} | ||
*/ | ||
@@ -72,2 +76,3 @@ export function getInfo(parts, options = {}) { | ||
/** @type {PartInfo} */ | ||
let currentPart = { | ||
@@ -95,3 +100,3 @@ mass: 0, | ||
element = groups[line.value]; | ||
if (!element) throw Error(`Unknown element: ${line.value}`); | ||
if (!element) throw new Error(`Unknown element: ${line.value}`); | ||
if (!customUnsaturations[line.value]) { | ||
@@ -98,0 +103,0 @@ customUnsaturations[line.value] = element.unsaturation; |
@@ -8,6 +8,8 @@ import { | ||
/** @typedef {import('./getIsotopesInfo.types').IsotopesInfo} IsotopesInfo | ||
/** | ||
* | ||
* @param {*} parts | ||
* @param {*} options | ||
* @returns {[]|IsotopesInfo} | ||
*/ | ||
@@ -23,3 +25,7 @@ export function getIsotopesInfo(parts) { | ||
/** | ||
* @returns {IsotopesInfo} | ||
*/ | ||
function getProcessedPart(part) { | ||
/** @type {IsotopesInfo} */ | ||
let result = { | ||
@@ -34,3 +40,7 @@ charge: 0, | ||
if (!isotope) { | ||
throw Error('unknown isotope:', line.value.atom, line.value.isotope); | ||
throw new Error( | ||
'unknown isotope:', | ||
line.value.atom, | ||
line.value.isotope, | ||
); | ||
} | ||
@@ -37,0 +47,0 @@ result.isotopes.push({ |
@@ -5,4 +5,4 @@ import { elementsObject } from 'chemical-elements'; | ||
export function isMF(mf) { | ||
let tmpMF = mf.replace(/[^a-zA-Z]/g, ''); | ||
let parts = tmpMF.replace(/([A-Za-z])(?=[A-Z])/g, '$1 ').split(' '); | ||
let tmpMF = mf.replaceAll(/[^A-Za-z]/g, ''); | ||
let parts = tmpMF.replaceAll(/([A-Za-z])(?=[A-Z])/g, '$1 ').split(' '); | ||
for (let i = 0; i < parts.length; i++) { | ||
@@ -9,0 +9,0 @@ if (!elementsObject[parts[i]] && !groupsObject[parts[i]]) { |
/** | ||
* Parse a string to extract the charge | ||
* Parse a string to extract the charge. | ||
* The charge may be in the form --, +++, +3, -2, 4+, 2- | ||
@@ -8,3 +8,3 @@ * @param {*} charge | ||
export function parseCharge(charge) { | ||
charge = charge.replace(/[()]/g, ''); | ||
charge = charge.replaceAll(/[()]/g, ''); | ||
let chargeNumber = 0; | ||
@@ -16,5 +16,6 @@ if (charge.match(/^[+-]+$/)) { | ||
} | ||
} else if (charge.match(/^[0-9]+[+-]$/)) { | ||
} else if (charge.match(/^\d+[+-]$/)) { | ||
chargeNumber = Number( | ||
charge.charAt(charge.length - 1) + charge.substring(0, charge.length - 1), | ||
// eslint-disable-next-line unicorn/prefer-at | ||
charge.charAt(charge.length - 1) + charge.slice(0, -1), | ||
); | ||
@@ -21,0 +22,0 @@ } else { |
import { Kind } from '../Kind.js'; | ||
/** @typedef {Record<string, number>} AtomsMap */ | ||
/** | ||
* Convert a MF part to an array of atoms | ||
* Convert a MF part to a map of atoms | ||
* This procedure will suppress the isotopes ! | ||
* This is mainly used to make queries | ||
* @returns {AtomsMap} | ||
*/ | ||
export function partToAtoms(part) { | ||
let atoms = {}; | ||
/** @type {AtomsMap} */ | ||
const atoms = {}; | ||
for (let line of part) { | ||
@@ -12,0 +15,0 @@ switch (line.kind) { |
@@ -15,10 +15,8 @@ import { Format } from '../Format'; | ||
case Format.SUPERIMPOSE: | ||
html.push(`<span style="${Style.SUPERIMPOSE}">`); | ||
html.push( | ||
`<span style="${Style.SUPERIMPOSE}">`, | ||
`<sup style="${Style.SUPERIMPOSE_SUP_SUB}">${line.over}</sup>`, | ||
); | ||
html.push( | ||
`<sub style="${Style.SUPERIMPOSE_SUP_SUB}">${line.under}</sub>`, | ||
'</span>', | ||
); | ||
html.push('</span>'); | ||
break; | ||
@@ -25,0 +23,0 @@ default: |
@@ -112,3 +112,3 @@ import { atomSorter } from 'atom-sorter'; | ||
// just applies to the previous element | ||
currentPart.lines[currentPart.lines.length - 1].multiplier *= value; | ||
currentPart.lines.at(-1).multiplier *= value; | ||
} | ||
@@ -146,3 +146,3 @@ } | ||
} | ||
if (expanded) part.lines = part.lines.filter((a) => a); | ||
if (expanded) part.lines = part.lines.filter(Boolean); | ||
} | ||
@@ -167,4 +167,3 @@ } | ||
} else { | ||
result[result.length - 1].value += | ||
key.value.value * key.value.multiplier; | ||
result.at(-1).value += key.value.value * key.value.multiplier; | ||
} | ||
@@ -174,3 +173,3 @@ } else if (currentKey !== key.key) { | ||
} else { | ||
result[result.length - 1].multiplier += key.value.multiplier; | ||
result.at(-1).multiplier += key.value.multiplier; | ||
} | ||
@@ -177,0 +176,0 @@ currentKey = key.key; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
157576
3
91
3733
2
- Removedmf-utilities@^3.2.0
- Removedmf-parser@3.3.0(transitive)
- Removedmf-utilities@3.3.4(transitive)
Updatedatom-sorter@^2.1.1
Updatedchemical-elements@^2.1.1
Updatedchemical-groups@^2.2.1