jcampconverter 9.2.2 → 9.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,8 @@
1
- "use strict";var he=Object.create;var P=Object.defineProperty;var de=Object.getOwnPropertyDescriptor;var ge=Object.getOwnPropertyNames;var be=Object.getPrototypeOf,xe=Object.prototype.hasOwnProperty;var Ae=(e,t)=>{for(var r in t)P(e,r,{get:t[r],enumerable:!0})},le=(e,t,r,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of ge(t))!xe.call(e,s)&&s!==r&&P(e,s,{get:()=>t[s],enumerable:!(a=de(t,s))||a.enumerable});return e};var Te=(e,t,r)=>(r=e!=null?he(be(e)):{},le(t||!e||!e.__esModule?P(r,"default",{value:e,enumerable:!0}):r,e)),Se=e=>le(P({},"__esModule",{value:!0}),e);var Fe={};Ae(Fe,{convert:()=>ue,createTree:()=>se});module.exports=Se(Fe);var ie=require("ensure-string");function se(e,t={}){e=(0,ie.ensureString)(e);let{flatten:r=!1}=t;if(typeof e!="string")throw new TypeError("the JCAMP should be a string");let a=e.split(/[\r\n]+/),s=[],l=[],p=[],i,f=0,m=e.includes("## ");for(let c=0;c<a.length;c++){let b=a[c],n=m?b.replace(/ /g,""):b;if(n.substring(0,9)==="##NTUPLES"&&f++,n.substring(0,7)==="##TITLE"){let o=[n.substring(8).trim()];for(let u=c+1;u<a.length&&!a[u].startsWith("##");u++)o.push(a[u].trim());l.push({title:o.join(`
1
+ "use strict";var he=Object.create;var P=Object.defineProperty;var ge=Object.getOwnPropertyDescriptor;var de=Object.getOwnPropertyNames;var be=Object.getPrototypeOf,xe=Object.prototype.hasOwnProperty;var Ae=(e,t)=>{for(var r in t)P(e,r,{get:t[r],enumerable:!0})},le=(e,t,r,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of de(t))!xe.call(e,s)&&s!==r&&P(e,s,{get:()=>t[s],enumerable:!(a=ge(t,s))||a.enumerable});return e};var Te=(e,t,r)=>(r=e!=null?he(be(e)):{},le(t||!e||!e.__esModule?P(r,"default",{value:e,enumerable:!0}):r,e)),Se=e=>le(P({},"__esModule",{value:!0}),e);var Fe={};Ae(Fe,{convert:()=>ue,createTree:()=>se});module.exports=Se(Fe);var ie=require("ensure-string");function se(e,t={}){e=(0,ie.ensureString)(e);let{flatten:r=!1}=t;if(typeof e!="string")throw new TypeError("the JCAMP should be a string");let a=e.split(/[\r\n]+/),s=[],l=[],p=[],i,f=0,u=e.includes("## ");for(let c=0;c<a.length;c++){let b=a[c],n=u?b.replace(/ /g,""):b;if(n.substring(0,9)==="##NTUPLES"&&f++,n.substring(0,7)==="##TITLE"){let o=[n.substring(8).trim()];for(let m=c+1;m<a.length&&!a[m].startsWith("##");m++)o.push(a[m].trim());l.push({title:o.join(`
2
2
  `),jcamp:`${b}
3
3
  `,children:[]}),i=l[l.length-1],s.push(i)}else if(n.substring(0,5)==="##END"&&f===0){i.jcamp+=`${b}
4
4
  `;let o=l.pop();l.length!==0?(i=l[l.length-1],i.children.push(o)):(i=void 0,p.push(o))}else if(i&&i.jcamp){i.jcamp+=`${b}
5
- `;let o=n.match(/^##(.*?)=(.+)/);if(o){let u=o[1].replace(/[ _-]/g,"").toUpperCase();u==="DATATYPE"&&(i.dataType=o[2].trim()),u==="DATACLASS"&&(i.dataClass=o[2].trim())}}n.substring(0,5)==="##END"&&f>0&&f--}return r?(s.forEach(c=>{c.children=void 0}),s):p}var oe=require("dynamic-typing"),pe=require("ensure-string");var $=["TIC",".RIC","SCANNUMBER"];function ae(e){let t=e.spectra,r=t.length,a={times:new Array(r),series:{ms:{dimension:2,data:new Array(r)}}},s=[];for(let l=0;l<$.length;l++){let p=U($[l]);t[0][p]&&(s.push(p),a.series[p]={dimension:1,data:new Array(r)})}for(let l=0;l<r;l++){let p=t[l];a.times[l]=p.pageValue;for(let i=0;i<s.length;i++)a.series[s[i]].data[l]=Number(p[s[i]]);p.data&&(a.series.ms.data[l]=[p.data.x,p.data.y])}e.chromatogram=a}function re(e){return $.indexOf(e)!==-1}function U(e){return e.toLowerCase().replace(/[^a-z0-9]/g,"")}function Y(e){let t=[];for(let r=0;r<e.length;r++)t.push(Number(e[r]));return t}function V(e,t){let r=e.yFactor,a=e.deltaX;e.isXYdata=!0;let s={x:[],y:[]};e.data=s;let l=e.firstX,p=e.firstY,i=!1,f,m=0;for(;m<t.length;m++)if(f=t.charCodeAt(m),f===13||f===10)i=!0;else if(i)break;let c=!0,b=!1,n=!1,o=0,u=!1,d=!1,h=0,T=0,A=!1,N=!1,y=!1,S=0;for(;m<=t.length;m++)if(m===t.length?f=13:f=t.charCodeAt(m),d)(f===13||f===10)&&(c=!0,d=!1);else if(f<=57&&f>=48)N=!0,S>0?h+=(f-48)/Math.pow(10,S++):(h*=10,h+=f-48);else if(f===44||f===46)N=!0,S++;else{if(N){if(c)c=!1,n&&(y=!0);else if(y)y=!1;else{b?(o=A?0-h:h,n=!0,b=!1):u||(T=A?0-h:h);let O=u?h-1:1;for(let F=0;F<O;F++)n?p+=o:p=T,s.x.push(l),s.y.push(p*r),l+=a}A=!1,h=0,S=0,N=!1,u=!1}if(f<74&&f>63)N=!0,n=!1,h=f-64;else if(f>96&&f<106)N=!0,n=!1,h=f-96,A=!0;else if(f===115)N=!0,u=!0,h=9;else if(f>82&&f<91)N=!0,u=!0,h=f-82;else if(f>73&&f<83)N=!0,b=!0,h=f-73;else if(f>105&&f<115)N=!0,b=!0,h=f-105,A=!0;else if(f===36&&t.charCodeAt(m+1)===36)N=!0,d=!0;else if(f===37)N=!0,b=!0,h=0,A=!1;else if(f===45){let O=t.charCodeAt(m+1);(O>=48&&O<=57||O===44||O===46)&&(N=!0,c||(n=!1),A=!0)}else(f===13||f===10)&&(c=!0,d=!1)}}var fe=/\$\$.*/,ne=/[,\t ]+/;function w(e,t,r){if(e.isPeaktable=!0,!e.variables||Object.keys(e.variables)===2?Xe(e,t,r):Ne(e,t,r),e.variables)for(let a in e.variables)e.variables[a].data=e.data[a]}function Xe(e,t,r){let a={x:[],y:[]};e.data=a;let s=t.split(/,? *,?[;\r\n]+ */);for(let l=1;l<s.length;l++){let p=s[l].trim().replace(fe,"").split(ne);if(p.length%2===0)for(let i=0;i<p.length;i=i+2)a.x.push(Number(p[i])*e.xFactor),a.y.push(Number(p[i+1])*e.yFactor);else r.logs.push(`Format error: ${p}`)}}function Ne(e,t,r){let a={},s=Object.keys(e.variables),l=s.length;s.forEach(i=>a[i]=[]),e.data=a;let p=t.split(/,? *,?[;\r\n]+ */);for(let i=1;i<p.length;i++){let f=p[i].trim().replace(fe,"").split(ne);if(f.length%l===0)for(let m=0;m<f.length;m++)a[s[m%l]].push(Number(f[m]));else r.logs.push(`Format error: ${f}`)}}function I(e,t){e.isXYAdata=!0;let r={};e.data=r;let a=t.split(/\r?\n/),s=a[0].replace(/^.*?([A-Z]+).*$/,"$1").split("").map(l=>l.toLowerCase());for(let l=1;l<a.length;l++){let p=a[l].replace(/^\((.*)\)$/,"$1").split(/ *, */);for(let i=0;i<s.length;i++){let f=p[i];switch(s[i]){case"x":case"y":case"w":f=Number.parseFloat(f);break;case"a":f=f.replace(/^<(.*)>$/,"$1");break;case"m":break;default:continue}r[s[i]]||(r[s[i]]=[]),r[s[i]].push(f)}}}var G=require("dynamic-typing");var E=Te(require("ml-array-median"));function k(e){let t=e[0].data.y[0],r=t,a=e.length,s=e[0].data.x.length,l=new Array(a);for(let n=0;n<a;n++){l[n]=e[n].data.y;for(let o=0;o<s;o++){let u=l[n][o];u<t&&(t=u),u>r&&(r=u)}}let p=e[0].data.x[0],i=e[0].data.x[e[0].data.x.length-1],f=e[0].pageValue,m=e[a-1].pageValue;if(p>i)for(let n of l)n.reverse();f>m&&l.reverse();let c=[];for(let n=0;n<l.length;n++){let o=Float64Array.from(l[n]);for(let u=0;u<o.length;u++)o[u]<0&&(o[u]=-o[u]);c.push((0,E.default)(o))}let b=(0,E.default)(c);return{z:l,minX:Math.min(p,i),maxX:Math.max(p,i),minY:Math.min(f,m),maxY:Math.max(f,m),minZ:t,maxZ:r,noise:b}}function Z(e,t){let r=e.noise,a=e.z,s,l,p,i,f,m,c,b,n=a.length,o=a[0].length,u,d,h,T,A=e.minX,y=(e.maxX-A)/(o-1),S=e.minY,F=(e.maxY-S)/(n-1),me=e.minZ,z=e.maxZ,W=t.nbContourLevels*2,J=new Array(W),x;for(let M=0;M<W;M++){let R={};J[M]=R;let ce=M%2,Q=(z-t.noiseMultiplier*r)*Math.exp((M>>1)-t.nbContourLevels);ce===0?x=Q+t.noiseMultiplier*r:x=0-Q-t.noiseMultiplier*r;let g=[];if(R.zValue=x,R.lines=g,!(x<=me||x>=z))for(let v=0;v<n-1;v++){let ee=a[v],te=a[v+1];for(let X=0;X<o-1;X++)s=ee[X],l=ee[X+1],p=te[X],i=te[X+1],f=s>x,m=l>x,c=p>x,b=i>x,f!==m&&f!==c&&(u=X+(x-s)/(l-s),d=v,h=X,T=v+(x-s)/(p-s),g.push(u*y+A),g.push(d*F+S),g.push(h*y+A),g.push(T*F+S)),b!==m&&b!==c&&(u=X+1,d=v+1-(x-i)/(l-i),h=X+1-(x-i)/(p-i),T=v+1,g.push(u*y+A),g.push(d*F+S),g.push(h*y+A),g.push(T*F+S)),m!==c&&(u=(X+1-(x-l)/(p-l))*y+A,d=(v+(x-l)/(p-l))*F+S,m!==f&&(h=X+1-(x-l)/(s-l),T=v,g.push(u),g.push(d),g.push(h*y+A),g.push(T*F+S)),c!==f&&(h=X,T=v+1-(x-p)/(s-p),g.push(u),g.push(d),g.push(h*y+A),g.push(T*F+S)),m!==b&&(h=X+1,T=v+(x-l)/(i-l),g.push(u),g.push(d),g.push(h*y+A),g.push(T*F+S)),c!==b&&(h=X+(x-p)/(i-p),T=v+1,g.push(u),g.push(d),g.push(h*y+A),g.push(T*F+S)))}}return{minX:e.minX,maxX:e.maxX,minY:e.minY,maxY:e.maxY,segments:J}}function j(e,t){let r=k(e.spectra);t.noContour||(e.contourLines=Z(r,t),delete r.z),e.minMax=r}var B=require("gyromagnetic-ratio");function q(e){for(let t of e){let r=0,a=0;for(let s of t.spectra){if(t.ntuples&&t.ntuples.symbol?(!r&&s.observeFrequency&&(r=s.observeFrequency),!a&&s.shiftOffsetVal&&(a=s.shiftOffsetVal)):(r=s.observeFrequency,a=s.shiftOffsetVal),r&&s.xUnits&&s.xUnits.toUpperCase().includes("HZ")){s.xUnits="PPM",s.xFactor=s.xFactor/r,s.firstX=s.firstX/r,s.lastX=s.lastX/r,s.deltaX=s.deltaX/r;for(let l=0;l<s.data.x.length;l++)s.data.x[l]/=r}if(a&&s.xUnits.toLowerCase().includes("ppm")){let l=s.firstX-a;s.firstX=s.firstX-l,s.lastX=s.lastX-l;for(let p=0;p<s.data.x.length;p++)s.data.x[p]-=l}if(t.ntuples&&t.ntuples.nucleus&&t.ntuples.symbol)for(let l=0;l<t.ntuples.nucleus.length;l++){let p=t.ntuples.symbol[l],i=t.ntuples.nucleus[l];if(p.match(/^[F|T]/)&&!i){if(p.match(/[F|T]1/))if(t.tmp.$NUC2)t.ntuples.nucleus[l]=t.tmp.$NUC2;else{let f=t.ntuples.symbol.indexOf(p.replace(/^([F|T]).*/,"$12"));f&&t.ntuples.nucleus[f]&&(t.ntuples.nucleus[l]=t.ntuples.nucleus[f])}p.match(/[F|T]2/)&&(t.ntuples.nucleus[l]=t.tmp.$NUC1)}p.match(/[F|T]2/)&&(t.yType=t.ntuples.nucleus[0])}if(r&&t.ntuples&&t.ntuples.symbol&&t.ntuples.nucleus){let l="",p=t.ntuples.symbol.indexOf(s.pageSymbol);if(t.ntuples.units&&t.ntuples.units[p]&&(l=t.ntuples.units[p]),l!=="PPM"){if(p!==0)throw Error("Not sure about this ntuples format");let i=B.gyromagneticRatio[t.ntuples.nucleus[0]],f=B.gyromagneticRatio[t.ntuples.nucleus[1]];if(!i||!f)throw Error("Problem with determination of gyromagnetic ratio");let m=i/f*r;s.pageValue/=m}}}}}function L(e,t,r={}){r.logger?.trace(t),e.profiling&&e.profiling.push({action:t,time:Date.now()-r.start})}function H(e){let t=e.spectra[0].data;e.chromatogram={times:t.x.slice(),series:{intensity:{dimension:1,data:t.y.slice()}}}}function K(e,t,r){q(e),ye(e,r);for(let a of e){if(Object.keys(a.ntuples).length>0){let s=[],l=Object.keys(a.ntuples);for(let p=0;p<l.length;p++){let i=l[p],f=a.ntuples[i];for(let m=0;m<f.length;m++)s[m]||(s[m]={}),s[m][i]=f[m]}a.ntuples=s}a.twoD&&r.wantXY&&(j(a,r),L(t,"Finished countour plot calculation",r),r.keepSpectra||delete a.spectra),r.chromatogram&&(a.spectra.length>1?ae(a):H(a),L(t,"Finished chromatogram calculation",r)),delete a.tmp}}function ye(e,t){for(let r of e)for(let a in r.meta){let s=r.meta[a];if(typeof s=="string"){if(s[0]==="{"){if(s[s.length-1]==="}"){let l=s.slice(1,-1).split(/[,; ]+/).filter(p=>p);for(let p=0;p<l.length;p++)r.meta[a+String(p)]=t.dynamicTyping?(0,G.parseString)(l[p]):l[p]}}else if(s[0]==="("){let l=s.split(/\r?\n/),p=/^\((?<from>\d+)\.\.(?<to>\d+)\)$/;if(p.test(l[0])){let[i,f]=l[0].match(p).slice(1).map(Number),m=l.slice(1).join(" ").split(/[,; ]+/).filter(c=>c);for(let c=i;c<=f;c++)r.meta[a+String(c)]=t.dynamicTyping?(0,G.parseString)(m[c-i]):m[c-i]}}}}}function _(e,t,r){let a=-1,s=-1,l="",p="";if(r.indexOf("++")>0)l=r.replace(/.*\(([a-zA-Z0-9]+)\+\+.*/,"$1"),p=r.replace(/.*\.\.([a-zA-Z0-9]+).*/,"$1");else{r=r.replace(/[^a-zA-Z]/g,""),l=r.charAt(0),p=r.charAt(1),t.variables={};for(let i of r){let f=i.toLowerCase(),m=e.ntuples.symbol.indexOf(i);if(m===-1)throw Error(`Symbol undefined: ${i}`);t.variables[f]={};for(let c in e.ntuples)e.ntuples[c][m]&&(t.variables[f][c.replace(/^var/,"")]=e.ntuples[c][m])}}a=e.ntuples.symbol.indexOf(l),s=e.ntuples.symbol.indexOf(p),a===-1&&(a=0),s===-1&&(s=0),e.ntuples.first&&(e.ntuples.first.length>a&&(t.firstX=e.ntuples.first[a]),e.ntuples.first.length>s&&(t.firstY=e.ntuples.first[s])),e.ntuples.last&&(e.ntuples.last.length>a&&(t.lastX=e.ntuples.last[a]),e.ntuples.last.length>s&&(t.lastY=e.ntuples.last[s])),e.ntuples.vardim&&e.ntuples.vardim.length>a&&(t.nbPoints=e.ntuples.vardim[a]),e.ntuples.factor&&(e.ntuples.factor.length>a&&(t.xFactor=e.ntuples.factor[a]),e.ntuples.factor.length>s&&(t.yFactor=e.ntuples.factor[s])),e.ntuples.units&&(e.ntuples.units.length>a&&(e.ntuples.varname&&e.ntuples.varname[a]?t.xUnits=`${e.ntuples.varname[a]} [${e.ntuples.units[a]}]`:t.xUnits=e.ntuples.units[a]),e.ntuples.units.length>s&&(e.ntuples.varname&&e.ntuples.varname[s]?t.yUnits=`${e.ntuples.varname[s]} [${e.ntuples.units[s]}]`:t.yUnits=e.ntuples.units[s]))}function D(e){e.xFactor||(e.xFactor=1),e.yFactor||(e.yFactor=1)}var C=/[ \t]*,[ \t]*/,ve={keepRecordsRegExp:/^$/,canonicDataLabels:!0,canonicMetadataLabels:!1,dynamicTyping:!0,withoutXY:!1,chromatogram:!1,keepSpectra:!1,noContour:!1,nbContourLevels:7,noiseMultiplier:5,profiling:!1};function ue(e,t={}){e=(0,pe.ensureString)(e),t={...ve,...t},t.logger?.debug("Starting jcamp conversion"),t.wantXY=!t.withoutXY,t.start=Date.now();let r=[],a={profiling:t.profiling?[]:!1,logs:[],entries:[]},s={children:[]},l=s,p=[],i={};if(typeof e!="string")throw new TypeError("the JCAMP should be a string");L(a,"Before split to LDRS",t);let f=e.replace(/[\r\n]+##/g,`
5
+ `;let o=n.match(/^##(.*?)=(.+)/);if(o){let m=o[1].replace(/[ _-]/g,"").toUpperCase();m==="DATATYPE"&&(i.dataType=o[2].trim()),m==="DATACLASS"&&(i.dataClass=o[2].trim())}}n.substring(0,5)==="##END"&&f>0&&f--}return r?(s.forEach(c=>{c.children=void 0}),s):p}var oe=require("dynamic-typing"),pe=require("ensure-string");var $=["TIC",".RIC","SCANNUMBER"];function ae(e){let t=e.spectra,r=t.length,a={times:new Array(r),series:{ms:{dimension:2,data:new Array(r)}}},s=[];for(let l=0;l<$.length;l++){let p=U($[l]);t[0][p]&&(s.push(p),a.series[p]={dimension:1,data:new Array(r)})}for(let l=0;l<r;l++){let p=t[l];a.times[l]=p.pageValue;for(let i=0;i<s.length;i++)a.series[s[i]].data[l]=Number(p[s[i]]);p.data&&(a.series.ms.data[l]=[p.data.x,p.data.y])}e.chromatogram=a}function re(e){return $.indexOf(e)!==-1}function U(e){return e.toLowerCase().replace(/[^a-z0-9]/g,"")}function Y(e){let t=[];for(let r=0;r<e.length;r++)t.push(Number(e[r]));return t}function V(e,t){let r=e.yFactor,a=e.deltaX;e.isXYdata=!0;let s={x:[],y:[]};e.data=s;let l=e.firstX,p=e.firstY,i=!1,f,u=0;for(;u<t.length;u++)if(f=t.charCodeAt(u),f===13||f===10)i=!0;else if(i)break;let c=!0,b=!1,n=!1,o=0,m=!1,g=!1,h=0,T=0,A=!1,y=!1,N=!1,S=0;for(;u<=t.length;u++)if(u===t.length?f=13:f=t.charCodeAt(u),g)(f===13||f===10)&&(c=!0,g=!1);else if(f<=57&&f>=48)y=!0,S>0?h+=(f-48)/Math.pow(10,S++):(h*=10,h+=f-48);else if(f===44||f===46)y=!0,S++;else{if(y){if(c)c=!1,n&&(N=!0);else if(N)N=!1;else{b?(o=A?0-h:h,n=!0,b=!1):m||(T=A?0-h:h);let O=m?h-1:1;for(let F=0;F<O;F++)n?p+=o:p=T,s.x.push(l),s.y.push(p*r),l+=a}A=!1,h=0,S=0,y=!1,m=!1}if(f<74&&f>63)y=!0,n=!1,h=f-64;else if(f>96&&f<106)y=!0,n=!1,h=f-96,A=!0;else if(f===115)y=!0,m=!0,h=9;else if(f>82&&f<91)y=!0,m=!0,h=f-82;else if(f>73&&f<83)y=!0,b=!0,h=f-73;else if(f>105&&f<115)y=!0,b=!0,h=f-105,A=!0;else if(f===36&&t.charCodeAt(u+1)===36)y=!0,g=!0;else if(f===37)y=!0,b=!0,h=0,A=!1;else if(f===45){let O=t.charCodeAt(u+1);(O>=48&&O<=57||O===44||O===46)&&(y=!0,c||(n=!1),A=!0)}else(f===13||f===10)&&(c=!0,g=!1)}}var fe=/\$\$.*/,ne=/[,\t ]+/;function w(e,t,r){if(e.isPeaktable=!0,!e.variables||Object.keys(e.variables)===2?Xe(e,t,r):ye(e,t,r),e.variables)for(let a in e.variables)e.variables[a].data=e.data[a]}function Xe(e,t,r){let a={x:[],y:[]};e.data=a;let s=t.split(/,? *,?[;\r\n]+ */);for(let l=1;l<s.length;l++){let p=s[l].trim().replace(fe,"").split(ne);if(p.length%2===0)for(let i=0;i<p.length;i=i+2)a.x.push(Number(p[i])*e.xFactor),a.y.push(Number(p[i+1])*e.yFactor);else r.logs.push(`Format error: ${p}`)}}function ye(e,t,r){let a={},s=Object.keys(e.variables),l=s.length;s.forEach(i=>a[i]=[]),e.data=a;let p=t.split(/,? *,?[;\r\n]+ */);for(let i=1;i<p.length;i++){let f=p[i].trim().replace(fe,"").split(ne);if(f.length%l===0)for(let u=0;u<f.length;u++)a[s[u%l]].push(Number(f[u]));else r.logs.push(`Format error: ${f}`)}}function I(e,t){e.isXYAdata=!0;let r={};e.data=r;let a=t.split(/\r?\n/),s=a[0].replace(/^.*?([A-Z]+).*$/,"$1").split("").map(l=>l.toLowerCase());for(let l=1;l<a.length;l++){let p=a[l].replace(/^\((.*)\)$/,"$1").split(/ *, */);for(let i=0;i<s.length;i++){let f=p[i];switch(s[i]){case"x":case"y":case"w":f=Number.parseFloat(f);break;case"a":f=f.replace(/^<(.*)>$/,"$1");break;case"m":break;default:continue}r[s[i]]||(r[s[i]]=[]),r[s[i]].push(f)}}}var G=require("dynamic-typing");var E=Te(require("ml-array-median"));function k(e){let t=e[0].data.y[0],r=t,a=e.length,s=e[0].data.x.length,l=new Array(a);for(let n=0;n<a;n++){l[n]=e[n].data.y;for(let o=0;o<s;o++){let m=l[n][o];m<t&&(t=m),m>r&&(r=m)}}let p=e[0].data.x[0],i=e[0].data.x[e[0].data.x.length-1],f=e[0].pageValue,u=e[a-1].pageValue;if(p>i)for(let n of l)n.reverse();f>u&&l.reverse();let c=[];for(let n=0;n<l.length;n++){let o=Float64Array.from(l[n]);for(let m=0;m<o.length;m++)o[m]<0&&(o[m]=-o[m]);c.push((0,E.default)(o))}let b=(0,E.default)(c);return{z:l,minX:Math.min(p,i),maxX:Math.max(p,i),minY:Math.min(f,u),maxY:Math.max(f,u),minZ:t,maxZ:r,noise:b}}function Z(e,t){let r=e.noise,a=e.z,s,l,p,i,f,u,c,b,n=a.length,o=a[0].length,m,g,h,T,A=e.minX,N=(e.maxX-A)/(o-1),S=e.minY,F=(e.maxY-S)/(n-1),me=e.minZ,z=e.maxZ,W=t.nbContourLevels*2,J=new Array(W),x;for(let M=0;M<W;M++){let R={};J[M]=R;let ce=M%2,Q=(z-t.noiseMultiplier*r)*Math.exp((M>>1)-t.nbContourLevels);ce===0?x=Q+t.noiseMultiplier*r:x=0-Q-t.noiseMultiplier*r;let d=[];if(R.zValue=x,R.lines=d,!(x<=me||x>=z))for(let v=0;v<n-1;v++){let ee=a[v],te=a[v+1];for(let X=0;X<o-1;X++)s=ee[X],l=ee[X+1],p=te[X],i=te[X+1],f=s>x,u=l>x,c=p>x,b=i>x,f!==u&&f!==c&&(m=X+(x-s)/(l-s),g=v,h=X,T=v+(x-s)/(p-s),d.push(m*N+A),d.push(g*F+S),d.push(h*N+A),d.push(T*F+S)),b!==u&&b!==c&&(m=X+1,g=v+1-(x-i)/(l-i),h=X+1-(x-i)/(p-i),T=v+1,d.push(m*N+A),d.push(g*F+S),d.push(h*N+A),d.push(T*F+S)),u!==c&&(m=(X+1-(x-l)/(p-l))*N+A,g=(v+(x-l)/(p-l))*F+S,u!==f&&(h=X+1-(x-l)/(s-l),T=v,d.push(m),d.push(g),d.push(h*N+A),d.push(T*F+S)),c!==f&&(h=X,T=v+1-(x-p)/(s-p),d.push(m),d.push(g),d.push(h*N+A),d.push(T*F+S)),u!==b&&(h=X+1,T=v+(x-l)/(i-l),d.push(m),d.push(g),d.push(h*N+A),d.push(T*F+S)),c!==b&&(h=X+(x-p)/(i-p),T=v+1,d.push(m),d.push(g),d.push(h*N+A),d.push(T*F+S)))}}return{minX:e.minX,maxX:e.maxX,minY:e.minY,maxY:e.maxY,segments:J}}function j(e,t){let r=k(e.spectra);t.noContour||(e.contourLines=Z(r,t),delete r.z),e.minMax=r}var B=require("gyromagnetic-ratio");function q(e){for(let t of e){let r=0,a=0;for(let s of t.spectra){if(t.ntuples&&t.ntuples.symbol?(!r&&s.observeFrequency&&(r=s.observeFrequency),!a&&s.shiftOffsetVal&&(a=s.shiftOffsetVal)):(r=s.observeFrequency,a=s.shiftOffsetVal),r&&s.xUnits&&s.xUnits.toUpperCase().includes("HZ")){s.xUnits="PPM",s.xFactor=s.xFactor/r,s.firstX=s.firstX/r,s.lastX=s.lastX/r,s.deltaX=s.deltaX/r;for(let l=0;l<s.data.x.length;l++)s.data.x[l]/=r}if(a&&s.xUnits.toLowerCase().includes("ppm")){let l=s.firstX-a;s.firstX=s.firstX-l,s.lastX=s.lastX-l;for(let p=0;p<s.data.x.length;p++)s.data.x[p]-=l}if(t.ntuples&&t.ntuples.nucleus&&t.ntuples.symbol)for(let l=0;l<t.ntuples.nucleus.length;l++){let p=t.ntuples.symbol[l],i=t.ntuples.nucleus[l];if(p.match(/^[F|T]/)&&!i){if(p.match(/[F|T]1/))if(t.tmp.$NUC2)t.ntuples.nucleus[l]=t.tmp.$NUC2;else{let f=t.ntuples.symbol.indexOf(p.replace(/^([F|T]).*/,"$12"));f&&t.ntuples.nucleus[f]&&(t.ntuples.nucleus[l]=t.ntuples.nucleus[f])}p.match(/[F|T]2/)&&(t.ntuples.nucleus[l]=t.tmp.$NUC1)}p.match(/[F|T]2/)&&(t.yType=t.ntuples.nucleus[0])}if(r&&t.ntuples&&t.ntuples.symbol&&t.ntuples.nucleus){let l="",p=t.ntuples.symbol.indexOf(s.pageSymbol);if(t.ntuples.units&&t.ntuples.units[p]&&(l=t.ntuples.units[p]),l!=="PPM"){if(p!==0)throw Error("Not sure about this ntuples format");let i=B.gyromagneticRatio[t.ntuples.nucleus[0]],f=B.gyromagneticRatio[t.ntuples.nucleus[1]];if(!i||!f)throw Error("Problem with determination of gyromagnetic ratio");let u=i/f*r;s.pageValue/=u}}}}}function L(e,t,r={}){r.logger?.trace(t),e.profiling&&e.profiling.push({action:t,time:Date.now()-r.start})}function H(e){let t=e.spectra[0].data;e.chromatogram={times:t.x.slice(),series:{intensity:{dimension:1,data:t.y.slice()}}}}function K(e,t,r){q(e),Ne(e,r);for(let a of e){if(Object.keys(a.ntuples).length>0){let s=[],l=Object.keys(a.ntuples);for(let p=0;p<l.length;p++){let i=l[p],f=a.ntuples[i];for(let u=0;u<f.length;u++)s[u]||(s[u]={}),s[u][i]=f[u]}a.ntuples=s}a.twoD&&r.wantXY&&(j(a,r),L(t,"Finished countour plot calculation",r),r.keepSpectra||delete a.spectra),r.chromatogram&&(a.spectra.length>1?ae(a):H(a),L(t,"Finished chromatogram calculation",r)),delete a.tmp}}function Ne(e,t){for(let r of e)for(let a in r.meta){let s=r.meta[a];if(typeof s=="string"){if(s[0]==="{"){if(s[s.length-1]==="}"){let l=s.slice(1,-1).split(/[,; ]+/).filter(p=>p);for(let p=0;p<l.length;p++)r.meta[a+String(p)]=t.dynamicTyping?(0,G.parseString)(l[p]):l[p]}}else if(s[0]==="("){let l=s.split(/\r?\n/),p=/^\((?<from>\d+)\.\.(?<to>\d+)\)$/;if(p.test(l[0])){let[i,f]=l[0].match(p).slice(1).map(Number),u=l.slice(1).join(" ").split(/[,; ]+/).filter(c=>c);for(let c=i;c<=f;c++)r.meta[a+String(c)]=t.dynamicTyping?(0,G.parseString)(u[c-i]):u[c-i]}}}}}function _(e,t,r){let a=-1,s=-1,l="",p="";if(r.indexOf("++")>0)l=r.replace(/.*\(([a-zA-Z0-9]+)\+\+.*/,"$1"),p=r.replace(/.*\.\.([a-zA-Z0-9]+).*/,"$1");else{r=r.replace(/[^a-zA-Z]/g,""),l=r.charAt(0),p=r.charAt(1),t.variables={};for(let i of r){let f=i.toLowerCase(),u=e.ntuples.symbol.indexOf(i);if(u===-1)throw Error(`Symbol undefined: ${i}`);t.variables[f]={};for(let c in e.ntuples)e.ntuples[c][u]&&(t.variables[f][c.replace(/^var/,"")]=e.ntuples[c][u])}}a=e.ntuples.symbol.indexOf(l),s=e.ntuples.symbol.indexOf(p),a===-1&&(a=0),s===-1&&(s=0),e.ntuples.first&&(e.ntuples.first.length>a&&(t.firstX=e.ntuples.first[a]),e.ntuples.first.length>s&&(t.firstY=e.ntuples.first[s])),e.ntuples.last&&(e.ntuples.last.length>a&&(t.lastX=e.ntuples.last[a]),e.ntuples.last.length>s&&(t.lastY=e.ntuples.last[s])),e.ntuples.vardim&&e.ntuples.vardim.length>a&&(t.nbPoints=e.ntuples.vardim[a]),e.ntuples.factor&&(e.ntuples.factor.length>a&&(t.xFactor=e.ntuples.factor[a]),e.ntuples.factor.length>s&&(t.yFactor=e.ntuples.factor[s])),e.ntuples.units&&(e.ntuples.units.length>a&&(e.ntuples.varname&&e.ntuples.varname[a]?t.xUnits=`${e.ntuples.varname[a]} [${e.ntuples.units[a]}]`:t.xUnits=e.ntuples.units[a]),e.ntuples.units.length>s&&(e.ntuples.varname&&e.ntuples.varname[s]?t.yUnits=`${e.ntuples.varname[s]} [${e.ntuples.units[s]}]`:t.yUnits=e.ntuples.units[s]))}function D(e){e.xFactor||(e.xFactor=1),e.yFactor||(e.yFactor=1)}var C=/[ \t]*,[ \t]*/,ve={keepRecordsRegExp:/^$/,canonicDataLabels:!0,canonicMetadataLabels:!1,dynamicTyping:!0,withoutXY:!1,chromatogram:!1,keepSpectra:!1,noContour:!1,nbContourLevels:7,noiseMultiplier:5,profiling:!1};function ue(e,t={}){e=(0,pe.ensureString)(e),t={...ve,...t},t.logger?.debug("Starting jcamp conversion"),t.wantXY=!t.withoutXY,t.start=Date.now();let r=[],a={profiling:t.profiling?[]:!1,logs:[],entries:[]},s={children:[]},l=s,p=[],i={};if(typeof e!="string")throw new TypeError("the JCAMP should be a string");L(a,"Before split to LDRS",t);let f=e.replace(/[\r\n]+##/g,`
6
6
  ##`).split(`
7
- ##`);L(a,"Split to LDRS",t),f[0]&&(f[0]=f[0].replace(/^[\r\n ]*##/,""));for(let m of f){let c=m.indexOf("="),b=c>0?m.substring(0,c):m,n=c>0?m.substring(c+1).trim():"",o=b.replace(/[_ -]/g,"").toUpperCase();if(o==="DATATABLE"){let u=n.indexOf(`
8
- `);if(u===-1&&(u=n.indexOf("\r")),u>0){let d=n.substring(0,u).split(/[ ,;\t]+/);_(l,i,d[0]),i.datatable=d[0],d[1]&&d[1].indexOf("PEAKS")>-1?o="PEAKTABLE":d[1]&&(d[1].indexOf("XYDATA")||d[0].indexOf("++")>0)&&(o="XYDATA",i.nbPoints&&(i.deltaX=(i.lastX-i.firstX)/(i.nbPoints-1)))}}if(o==="XYDATA"){t.wantXY&&(D(i),n.match(/.*\+\+.*/)?(i.nbPoints&&(i.deltaX=(i.lastX-i.firstX)/(i.nbPoints-1)),V(i,n,a)):w(i,n,a),l.spectra.push(i),i={});continue}else if(o==="PEAKTABLE"){t.wantXY&&(D(i),w(i,n,a),l.spectra.push(i),i={});continue}if(o==="PEAKASSIGNMENTS"){t.wantXY&&(n.match(/.*([^A-Z]*).*/)&&I(i,n),l.spectra.push(i),i={});continue}if(o==="TITLE"){let u=l;u.children||(u.children=[]),l={spectra:[],ntuples:{},info:{},meta:{},tmp:{}},u.children.push(l),p.push(u),r.push(l),l.title=n}else o==="DATATYPE"?(l.dataType=n,n.match(/(^nd|\snd\s)/i)&&(l.twoD=!0)):o==="NTUPLES"?n.match(/(^nd|\snd\s)/i)&&(l.twoD=!0):o==="DATACLASS"?l.dataClass=n:o==="XUNITS"?i.xUnits=n:o==="YUNITS"?i.yUnits=n:o==="FIRSTX"?i.firstX=Number(n):o==="LASTX"?i.lastX=Number(n):o==="FIRSTY"?i.firstY=Number(n):o==="LASTY"?i.lastY=Number(n):o==="NPOINTS"?i.nbPoints=Number(n):o==="XFACTOR"?i.xFactor=Number(n):o==="YFACTOR"?i.yFactor=Number(n):o==="MAXX"?i.maxX=Number(n):o==="MINX"?i.minX=Number(n):o==="MAXY"?i.maxY=Number(n):o==="MINY"?i.minY=Number(n):o==="DELTAX"?i.deltaX=Number(n):o===".OBSERVEFREQUENCY"||o==="$SFO1"?i.observeFrequency||(i.observeFrequency=Number(n)):o===".OBSERVENUCLEUS"?i.xType||(l.xType=n.replace(/[^a-zA-Z0-9]/g,"")):o==="$OFFSET"?(l.shiftOffsetNum=0,i.shiftOffsetVal||(i.shiftOffsetVal=Number(n))):o==="$REFERENCEPOINT"||(o==="VARNAME"?l.ntuples.varname=n.split(C):o==="SYMBOL"?l.ntuples.symbol=n.split(C):o==="VARTYPE"?l.ntuples.vartype=n.split(C):o==="VARFORM"?l.ntuples.varform=n.split(C):o==="VARDIM"?l.ntuples.vardim=Y(n.split(C)):o==="UNITS"?l.ntuples.units=n.split(C):o==="FACTOR"?l.ntuples.factor=Y(n.split(C)):o==="FIRST"?l.ntuples.first=Y(n.split(C)):o==="LAST"?l.ntuples.last=Y(n.split(C)):o==="MIN"?l.ntuples.min=Y(n.split(C)):o==="MAX"?l.ntuples.max=Y(n.split(C)):o===".NUCLEUS"?l.ntuples&&(l.ntuples.nucleus=n.split(C).map(u=>u.replace(/[^a-zA-Z0-9]/g,""))):o==="PAGE"?(i.page=n.trim(),i.pageValue=Number(n.replace(/^.*=/,"")),i.pageSymbol=i.page.replace(/[=].*/,"")):o==="RETENTIONTIME"?i.pageValue=Number(n):re(o)?i[U(o)]=n:o==="SAMPLEDESCRIPTION"?i.sampleDescription=n:o.startsWith("$NUC")?!l.tmp[o]&&!n.includes("off")&&(l.tmp[o]=n.replace(/[<>]/g,"")):o==="END"&&(l=p.pop()));if(l&&l.info&&l.meta&&o.match(t.keepRecordsRegExp)){let u=n.trim(),d,h;b.startsWith("$")?(h=t.canonicMetadataLabels?o.substring(1):b.substring(1),d=l.meta):(h=t.canonicDataLabels?o:b,d=l.info),t.dynamicTyping&&(u=(0,oe.parseString)(u)),d[h]?(Array.isArray(d[h])||(d[h]=[d[h]]),d[h].push(u)):d[h]=u}}return L(a,"Finished parsing",t),K(r,a,t),L(a,"Total time",t),a.entries=s.children,a.flatten=r,a}
7
+ ##`);L(a,"Split to LDRS",t),f[0]&&(f[0]=f[0].replace(/^[\r\n ]*##/,""));for(let u of f){let c=u.indexOf("="),b=c>0?u.substring(0,c):u,n=c>0?u.substring(c+1).trim():"",o=b.replace(/[_ -]/g,"").toUpperCase();if(o==="DATATABLE"){let m=n.indexOf(`
8
+ `);if(m===-1&&(m=n.indexOf("\r")),m>0){let g=n.substring(0,m).split(/[ ,;\t]+/);_(l,i,g[0]),i.datatable=g[0],g[1]&&g[1].indexOf("PEAKS")>-1?o="PEAKTABLE":g[1]&&(g[1].indexOf("XYDATA")||g[0].indexOf("++")>0)&&(o="XYDATA",i.nbPoints&&(i.deltaX=(i.lastX-i.firstX)/(i.nbPoints-1)))}}if(o==="XYDATA"){t.wantXY&&(D(i),n.match(/.*\+\+.*/)?(i.nbPoints&&(i.deltaX=(i.lastX-i.firstX)/(i.nbPoints-1)),V(i,n,a)):w(i,n,a),l.spectra.push(i),i={});continue}else if(o==="PEAKTABLE"){t.wantXY&&(D(i),w(i,n,a),l.spectra.push(i),i={});continue}if(o==="PEAKASSIGNMENTS"){t.wantXY&&(n.match(/.*([^A-Z]*).*/)&&I(i,n),l.spectra.push(i),i={});continue}if(o==="TITLE"){let m=l;m.children||(m.children=[]),l={spectra:[],ntuples:{},info:{},meta:{},tmp:{}},m.children.push(l),p.push(m),r.push(l),l.title=n}else o==="DATATYPE"?(l.dataType=n,n.match(/(^nd|\snd\s)/i)&&(l.twoD=!0)):o==="NTUPLES"?n.match(/(^nd|\snd\s)/i)&&(l.twoD=!0):o==="DATACLASS"?l.dataClass=n:o==="XUNITS"?i.xUnits=n:o==="YUNITS"?i.yUnits=n:o==="FIRSTX"?i.firstX=Number(n):o==="LASTX"?i.lastX=Number(n):o==="FIRSTY"?i.firstY=Number(n):o==="LASTY"?i.lastY=Number(n):o==="NPOINTS"?i.nbPoints=Number(n):o==="XFACTOR"?i.xFactor=Number(n):o==="YFACTOR"?i.yFactor=Number(n):o==="MAXX"?i.maxX=Number(n):o==="MINX"?i.minX=Number(n):o==="MAXY"?i.maxY=Number(n):o==="MINY"?i.minY=Number(n):o==="DELTAX"?i.deltaX=Number(n):o===".OBSERVEFREQUENCY"||o==="$SFO1"?i.observeFrequency||(i.observeFrequency=Number(n)):o===".OBSERVENUCLEUS"?i.xType||(l.xType=n.replace(/[^a-zA-Z0-9]/g,"")):o==="$OFFSET"?(l.shiftOffsetNum=0,i.shiftOffsetVal||(i.shiftOffsetVal=Number(n))):o==="$REFERENCEPOINT"||(o==="VARNAME"?l.ntuples.varname=n.split(C):o==="SYMBOL"?l.ntuples.symbol=n.split(C):o==="VARTYPE"?l.ntuples.vartype=n.split(C):o==="VARFORM"?l.ntuples.varform=n.split(C):o==="VARDIM"?l.ntuples.vardim=Y(n.split(C)):o==="UNITS"?l.ntuples.units=n.split(C):o==="FACTOR"?l.ntuples.factor=Y(n.split(C)):o==="FIRST"?l.ntuples.first=Y(n.split(C)):o==="LAST"?l.ntuples.last=Y(n.split(C)):o==="MIN"?l.ntuples.min=Y(n.split(C)):o==="MAX"?l.ntuples.max=Y(n.split(C)):o===".NUCLEUS"?l.ntuples&&(l.ntuples.nucleus=n.split(C).map(m=>m.replace(/[^a-zA-Z0-9]/g,""))):o==="PAGE"?(i.page=n.trim(),i.pageValue=Number(n.replace(/^.*=/,"")),i.pageSymbol=i.page.replace(/[=].*/,"")):o==="RETENTIONTIME"?i.pageValue=Number(n):re(o)?i[U(o)]=n:o==="SAMPLEDESCRIPTION"?i.sampleDescription=n:o.startsWith("$NUC")?!l.tmp[o]&&!n.includes("off")&&(l.tmp[o]=n.replace(/[<>]/g,"")):o==="END"&&(l=p.pop()));if(l&&l.info&&l.meta&&o.match(t.keepRecordsRegExp)){let m=n.trim(),g,h;b.startsWith("$")?(h=t.canonicMetadataLabels?o.substring(1):b.substring(1),g=l.meta):(h=t.canonicDataLabels?o:b,g=l.info),t.dynamicTyping&&(m=(0,oe.parseString)(m)),g[h]?(Array.isArray(g[h])||(g[h]=[g[h]]),g[h].push(m)):g[h]=m}}if(L(a,"Finished parsing",t),K(r,a,t),L(a,"Total time",t),a.entries=s.children,a.flatten=r,t.logger){t.logger.debug("Finished jcamp conversion");for(let u of a.flatten)t.logger.debug(`${u.dataType} - ${u.title}`)}return a}
@@ -1,8 +1,8 @@
1
- import{ensureString as fe}from"ensure-string";function ne(e,i={}){e=fe(e);let{flatten:p=!1}=i;if(typeof e!="string")throw new TypeError("the JCAMP should be a string");let a=e.split(/[\r\n]+/),s=[],t=[],o=[],l,r=0,m=e.includes("## ");for(let c=0;c<a.length;c++){let b=a[c],f=m?b.replace(/ /g,""):b;if(f.substring(0,9)==="##NTUPLES"&&r++,f.substring(0,7)==="##TITLE"){let n=[f.substring(8).trim()];for(let u=c+1;u<a.length&&!a[u].startsWith("##");u++)n.push(a[u].trim());t.push({title:n.join(`
1
+ import{ensureString as fe}from"ensure-string";function ne(e,l={}){e=fe(e);let{flatten:p=!1}=l;if(typeof e!="string")throw new TypeError("the JCAMP should be a string");let a=e.split(/[\r\n]+/),s=[],t=[],o=[],i,r=0,u=e.includes("## ");for(let c=0;c<a.length;c++){let b=a[c],f=u?b.replace(/ /g,""):b;if(f.substring(0,9)==="##NTUPLES"&&r++,f.substring(0,7)==="##TITLE"){let n=[f.substring(8).trim()];for(let m=c+1;m<a.length&&!a[m].startsWith("##");m++)n.push(a[m].trim());t.push({title:n.join(`
2
2
  `),jcamp:`${b}
3
- `,children:[]}),l=t[t.length-1],s.push(l)}else if(f.substring(0,5)==="##END"&&r===0){l.jcamp+=`${b}
4
- `;let n=t.pop();t.length!==0?(l=t[t.length-1],l.children.push(n)):(l=void 0,o.push(n))}else if(l&&l.jcamp){l.jcamp+=`${b}
5
- `;let n=f.match(/^##(.*?)=(.+)/);if(n){let u=n[1].replace(/[ _-]/g,"").toUpperCase();u==="DATATYPE"&&(l.dataType=n[2].trim()),u==="DATACLASS"&&(l.dataClass=n[2].trim())}}f.substring(0,5)==="##END"&&r>0&&r--}return p?(s.forEach(c=>{c.children=void 0}),s):o}import{parseString as me}from"dynamic-typing";import{ensureString as ce}from"ensure-string";var R=["TIC",".RIC","SCANNUMBER"];function J(e){let i=e.spectra,p=i.length,a={times:new Array(p),series:{ms:{dimension:2,data:new Array(p)}}},s=[];for(let t=0;t<R.length;t++){let o=$(R[t]);i[0][o]&&(s.push(o),a.series[o]={dimension:1,data:new Array(p)})}for(let t=0;t<p;t++){let o=i[t];a.times[t]=o.pageValue;for(let l=0;l<s.length;l++)a.series[s[l]].data[t]=Number(o[s[l]]);o.data&&(a.series.ms.data[t]=[o.data.x,o.data.y])}e.chromatogram=a}function Q(e){return R.indexOf(e)!==-1}function $(e){return e.toLowerCase().replace(/[^a-z0-9]/g,"")}function Y(e){let i=[];for(let p=0;p<e.length;p++)i.push(Number(e[p]));return i}function U(e,i){let p=e.yFactor,a=e.deltaX;e.isXYdata=!0;let s={x:[],y:[]};e.data=s;let t=e.firstX,o=e.firstY,l=!1,r,m=0;for(;m<i.length;m++)if(r=i.charCodeAt(m),r===13||r===10)l=!0;else if(l)break;let c=!0,b=!1,f=!1,n=0,u=!1,d=!1,h=0,T=0,A=!1,N=!1,y=!1,S=0;for(;m<=i.length;m++)if(m===i.length?r=13:r=i.charCodeAt(m),d)(r===13||r===10)&&(c=!0,d=!1);else if(r<=57&&r>=48)N=!0,S>0?h+=(r-48)/Math.pow(10,S++):(h*=10,h+=r-48);else if(r===44||r===46)N=!0,S++;else{if(N){if(c)c=!1,f&&(y=!0);else if(y)y=!1;else{b?(n=A?0-h:h,f=!0,b=!1):u||(T=A?0-h:h);let O=u?h-1:1;for(let F=0;F<O;F++)f?o+=n:o=T,s.x.push(t),s.y.push(o*p),t+=a}A=!1,h=0,S=0,N=!1,u=!1}if(r<74&&r>63)N=!0,f=!1,h=r-64;else if(r>96&&r<106)N=!0,f=!1,h=r-96,A=!0;else if(r===115)N=!0,u=!0,h=9;else if(r>82&&r<91)N=!0,u=!0,h=r-82;else if(r>73&&r<83)N=!0,b=!0,h=r-73;else if(r>105&&r<115)N=!0,b=!0,h=r-105,A=!0;else if(r===36&&i.charCodeAt(m+1)===36)N=!0,d=!0;else if(r===37)N=!0,b=!0,h=0,A=!1;else if(r===45){let O=i.charCodeAt(m+1);(O>=48&&O<=57||O===44||O===46)&&(N=!0,c||(f=!1),A=!0)}else(r===13||r===10)&&(c=!0,d=!1)}}var ee=/\$\$.*/,te=/[,\t ]+/;function P(e,i,p){if(e.isPeaktable=!0,!e.variables||Object.keys(e.variables)===2?oe(e,i,p):pe(e,i,p),e.variables)for(let a in e.variables)e.variables[a].data=e.data[a]}function oe(e,i,p){let a={x:[],y:[]};e.data=a;let s=i.split(/,? *,?[;\r\n]+ */);for(let t=1;t<s.length;t++){let o=s[t].trim().replace(ee,"").split(te);if(o.length%2===0)for(let l=0;l<o.length;l=l+2)a.x.push(Number(o[l])*e.xFactor),a.y.push(Number(o[l+1])*e.yFactor);else p.logs.push(`Format error: ${o}`)}}function pe(e,i,p){let a={},s=Object.keys(e.variables),t=s.length;s.forEach(l=>a[l]=[]),e.data=a;let o=i.split(/,? *,?[;\r\n]+ */);for(let l=1;l<o.length;l++){let r=o[l].trim().replace(ee,"").split(te);if(r.length%t===0)for(let m=0;m<r.length;m++)a[s[m%t]].push(Number(r[m]));else p.logs.push(`Format error: ${r}`)}}function V(e,i){e.isXYAdata=!0;let p={};e.data=p;let a=i.split(/\r?\n/),s=a[0].replace(/^.*?([A-Z]+).*$/,"$1").split("").map(t=>t.toLowerCase());for(let t=1;t<a.length;t++){let o=a[t].replace(/^\((.*)\)$/,"$1").split(/ *, */);for(let l=0;l<s.length;l++){let r=o[l];switch(s[l]){case"x":case"y":case"w":r=Number.parseFloat(r);break;case"a":r=r.replace(/^<(.*)>$/,"$1");break;case"m":break;default:continue}p[s[l]]||(p[s[l]]=[]),p[s[l]].push(r)}}}import{parseString as se}from"dynamic-typing";import le from"ml-array-median";function I(e){let i=e[0].data.y[0],p=i,a=e.length,s=e[0].data.x.length,t=new Array(a);for(let f=0;f<a;f++){t[f]=e[f].data.y;for(let n=0;n<s;n++){let u=t[f][n];u<i&&(i=u),u>p&&(p=u)}}let o=e[0].data.x[0],l=e[0].data.x[e[0].data.x.length-1],r=e[0].pageValue,m=e[a-1].pageValue;if(o>l)for(let f of t)f.reverse();r>m&&t.reverse();let c=[];for(let f=0;f<t.length;f++){let n=Float64Array.from(t[f]);for(let u=0;u<n.length;u++)n[u]<0&&(n[u]=-n[u]);c.push(le(n))}let b=le(c);return{z:t,minX:Math.min(o,l),maxX:Math.max(o,l),minY:Math.min(r,m),maxY:Math.max(r,m),minZ:i,maxZ:p,noise:b}}function E(e,i){let p=e.noise,a=e.z,s,t,o,l,r,m,c,b,f=a.length,n=a[0].length,u,d,h,T,A=e.minX,y=(e.maxX-A)/(n-1),S=e.minY,F=(e.maxY-S)/(f-1),ae=e.minZ,H=e.maxZ,G=i.nbContourLevels*2,K=new Array(G),x;for(let M=0;M<G;M++){let D={};K[M]=D;let re=M%2,_=(H-i.noiseMultiplier*p)*Math.exp((M>>1)-i.nbContourLevels);re===0?x=_+i.noiseMultiplier*p:x=0-_-i.noiseMultiplier*p;let g=[];if(D.zValue=x,D.lines=g,!(x<=ae||x>=H))for(let v=0;v<f-1;v++){let z=a[v],W=a[v+1];for(let X=0;X<n-1;X++)s=z[X],t=z[X+1],o=W[X],l=W[X+1],r=s>x,m=t>x,c=o>x,b=l>x,r!==m&&r!==c&&(u=X+(x-s)/(t-s),d=v,h=X,T=v+(x-s)/(o-s),g.push(u*y+A),g.push(d*F+S),g.push(h*y+A),g.push(T*F+S)),b!==m&&b!==c&&(u=X+1,d=v+1-(x-l)/(t-l),h=X+1-(x-l)/(o-l),T=v+1,g.push(u*y+A),g.push(d*F+S),g.push(h*y+A),g.push(T*F+S)),m!==c&&(u=(X+1-(x-t)/(o-t))*y+A,d=(v+(x-t)/(o-t))*F+S,m!==r&&(h=X+1-(x-t)/(s-t),T=v,g.push(u),g.push(d),g.push(h*y+A),g.push(T*F+S)),c!==r&&(h=X,T=v+1-(x-o)/(s-o),g.push(u),g.push(d),g.push(h*y+A),g.push(T*F+S)),m!==b&&(h=X+1,T=v+(x-t)/(l-t),g.push(u),g.push(d),g.push(h*y+A),g.push(T*F+S)),c!==b&&(h=X+(x-o)/(l-o),T=v+1,g.push(u),g.push(d),g.push(h*y+A),g.push(T*F+S)))}}return{minX:e.minX,maxX:e.maxX,minY:e.minY,maxY:e.maxY,segments:K}}function k(e,i){let p=I(e.spectra);i.noContour||(e.contourLines=E(p,i),delete p.z),e.minMax=p}import{gyromagneticRatio as ie}from"gyromagnetic-ratio";function Z(e){for(let i of e){let p=0,a=0;for(let s of i.spectra){if(i.ntuples&&i.ntuples.symbol?(!p&&s.observeFrequency&&(p=s.observeFrequency),!a&&s.shiftOffsetVal&&(a=s.shiftOffsetVal)):(p=s.observeFrequency,a=s.shiftOffsetVal),p&&s.xUnits&&s.xUnits.toUpperCase().includes("HZ")){s.xUnits="PPM",s.xFactor=s.xFactor/p,s.firstX=s.firstX/p,s.lastX=s.lastX/p,s.deltaX=s.deltaX/p;for(let t=0;t<s.data.x.length;t++)s.data.x[t]/=p}if(a&&s.xUnits.toLowerCase().includes("ppm")){let t=s.firstX-a;s.firstX=s.firstX-t,s.lastX=s.lastX-t;for(let o=0;o<s.data.x.length;o++)s.data.x[o]-=t}if(i.ntuples&&i.ntuples.nucleus&&i.ntuples.symbol)for(let t=0;t<i.ntuples.nucleus.length;t++){let o=i.ntuples.symbol[t],l=i.ntuples.nucleus[t];if(o.match(/^[F|T]/)&&!l){if(o.match(/[F|T]1/))if(i.tmp.$NUC2)i.ntuples.nucleus[t]=i.tmp.$NUC2;else{let r=i.ntuples.symbol.indexOf(o.replace(/^([F|T]).*/,"$12"));r&&i.ntuples.nucleus[r]&&(i.ntuples.nucleus[t]=i.ntuples.nucleus[r])}o.match(/[F|T]2/)&&(i.ntuples.nucleus[t]=i.tmp.$NUC1)}o.match(/[F|T]2/)&&(i.yType=i.ntuples.nucleus[0])}if(p&&i.ntuples&&i.ntuples.symbol&&i.ntuples.nucleus){let t="",o=i.ntuples.symbol.indexOf(s.pageSymbol);if(i.ntuples.units&&i.ntuples.units[o]&&(t=i.ntuples.units[o]),t!=="PPM"){if(o!==0)throw Error("Not sure about this ntuples format");let l=ie[i.ntuples.nucleus[0]],r=ie[i.ntuples.nucleus[1]];if(!l||!r)throw Error("Problem with determination of gyromagnetic ratio");let m=l/r*p;s.pageValue/=m}}}}}function L(e,i,p={}){p.logger?.trace(i),e.profiling&&e.profiling.push({action:i,time:Date.now()-p.start})}function j(e){let i=e.spectra[0].data;e.chromatogram={times:i.x.slice(),series:{intensity:{dimension:1,data:i.y.slice()}}}}function B(e,i,p){Z(e),ue(e,p);for(let a of e){if(Object.keys(a.ntuples).length>0){let s=[],t=Object.keys(a.ntuples);for(let o=0;o<t.length;o++){let l=t[o],r=a.ntuples[l];for(let m=0;m<r.length;m++)s[m]||(s[m]={}),s[m][l]=r[m]}a.ntuples=s}a.twoD&&p.wantXY&&(k(a,p),L(i,"Finished countour plot calculation",p),p.keepSpectra||delete a.spectra),p.chromatogram&&(a.spectra.length>1?J(a):j(a),L(i,"Finished chromatogram calculation",p)),delete a.tmp}}function ue(e,i){for(let p of e)for(let a in p.meta){let s=p.meta[a];if(typeof s=="string"){if(s[0]==="{"){if(s[s.length-1]==="}"){let t=s.slice(1,-1).split(/[,; ]+/).filter(o=>o);for(let o=0;o<t.length;o++)p.meta[a+String(o)]=i.dynamicTyping?se(t[o]):t[o]}}else if(s[0]==="("){let t=s.split(/\r?\n/),o=/^\((?<from>\d+)\.\.(?<to>\d+)\)$/;if(o.test(t[0])){let[l,r]=t[0].match(o).slice(1).map(Number),m=t.slice(1).join(" ").split(/[,; ]+/).filter(c=>c);for(let c=l;c<=r;c++)p.meta[a+String(c)]=i.dynamicTyping?se(m[c-l]):m[c-l]}}}}}function q(e,i,p){let a=-1,s=-1,t="",o="";if(p.indexOf("++")>0)t=p.replace(/.*\(([a-zA-Z0-9]+)\+\+.*/,"$1"),o=p.replace(/.*\.\.([a-zA-Z0-9]+).*/,"$1");else{p=p.replace(/[^a-zA-Z]/g,""),t=p.charAt(0),o=p.charAt(1),i.variables={};for(let l of p){let r=l.toLowerCase(),m=e.ntuples.symbol.indexOf(l);if(m===-1)throw Error(`Symbol undefined: ${l}`);i.variables[r]={};for(let c in e.ntuples)e.ntuples[c][m]&&(i.variables[r][c.replace(/^var/,"")]=e.ntuples[c][m])}}a=e.ntuples.symbol.indexOf(t),s=e.ntuples.symbol.indexOf(o),a===-1&&(a=0),s===-1&&(s=0),e.ntuples.first&&(e.ntuples.first.length>a&&(i.firstX=e.ntuples.first[a]),e.ntuples.first.length>s&&(i.firstY=e.ntuples.first[s])),e.ntuples.last&&(e.ntuples.last.length>a&&(i.lastX=e.ntuples.last[a]),e.ntuples.last.length>s&&(i.lastY=e.ntuples.last[s])),e.ntuples.vardim&&e.ntuples.vardim.length>a&&(i.nbPoints=e.ntuples.vardim[a]),e.ntuples.factor&&(e.ntuples.factor.length>a&&(i.xFactor=e.ntuples.factor[a]),e.ntuples.factor.length>s&&(i.yFactor=e.ntuples.factor[s])),e.ntuples.units&&(e.ntuples.units.length>a&&(e.ntuples.varname&&e.ntuples.varname[a]?i.xUnits=`${e.ntuples.varname[a]} [${e.ntuples.units[a]}]`:i.xUnits=e.ntuples.units[a]),e.ntuples.units.length>s&&(e.ntuples.varname&&e.ntuples.varname[s]?i.yUnits=`${e.ntuples.varname[s]} [${e.ntuples.units[s]}]`:i.yUnits=e.ntuples.units[s]))}function w(e){e.xFactor||(e.xFactor=1),e.yFactor||(e.yFactor=1)}var C=/[ \t]*,[ \t]*/,he={keepRecordsRegExp:/^$/,canonicDataLabels:!0,canonicMetadataLabels:!1,dynamicTyping:!0,withoutXY:!1,chromatogram:!1,keepSpectra:!1,noContour:!1,nbContourLevels:7,noiseMultiplier:5,profiling:!1};function de(e,i={}){e=ce(e),i={...he,...i},i.logger?.debug("Starting jcamp conversion"),i.wantXY=!i.withoutXY,i.start=Date.now();let p=[],a={profiling:i.profiling?[]:!1,logs:[],entries:[]},s={children:[]},t=s,o=[],l={};if(typeof e!="string")throw new TypeError("the JCAMP should be a string");L(a,"Before split to LDRS",i);let r=e.replace(/[\r\n]+##/g,`
3
+ `,children:[]}),i=t[t.length-1],s.push(i)}else if(f.substring(0,5)==="##END"&&r===0){i.jcamp+=`${b}
4
+ `;let n=t.pop();t.length!==0?(i=t[t.length-1],i.children.push(n)):(i=void 0,o.push(n))}else if(i&&i.jcamp){i.jcamp+=`${b}
5
+ `;let n=f.match(/^##(.*?)=(.+)/);if(n){let m=n[1].replace(/[ _-]/g,"").toUpperCase();m==="DATATYPE"&&(i.dataType=n[2].trim()),m==="DATACLASS"&&(i.dataClass=n[2].trim())}}f.substring(0,5)==="##END"&&r>0&&r--}return p?(s.forEach(c=>{c.children=void 0}),s):o}import{parseString as me}from"dynamic-typing";import{ensureString as ce}from"ensure-string";var R=["TIC",".RIC","SCANNUMBER"];function J(e){let l=e.spectra,p=l.length,a={times:new Array(p),series:{ms:{dimension:2,data:new Array(p)}}},s=[];for(let t=0;t<R.length;t++){let o=$(R[t]);l[0][o]&&(s.push(o),a.series[o]={dimension:1,data:new Array(p)})}for(let t=0;t<p;t++){let o=l[t];a.times[t]=o.pageValue;for(let i=0;i<s.length;i++)a.series[s[i]].data[t]=Number(o[s[i]]);o.data&&(a.series.ms.data[t]=[o.data.x,o.data.y])}e.chromatogram=a}function Q(e){return R.indexOf(e)!==-1}function $(e){return e.toLowerCase().replace(/[^a-z0-9]/g,"")}function Y(e){let l=[];for(let p=0;p<e.length;p++)l.push(Number(e[p]));return l}function U(e,l){let p=e.yFactor,a=e.deltaX;e.isXYdata=!0;let s={x:[],y:[]};e.data=s;let t=e.firstX,o=e.firstY,i=!1,r,u=0;for(;u<l.length;u++)if(r=l.charCodeAt(u),r===13||r===10)i=!0;else if(i)break;let c=!0,b=!1,f=!1,n=0,m=!1,g=!1,h=0,T=0,A=!1,y=!1,N=!1,S=0;for(;u<=l.length;u++)if(u===l.length?r=13:r=l.charCodeAt(u),g)(r===13||r===10)&&(c=!0,g=!1);else if(r<=57&&r>=48)y=!0,S>0?h+=(r-48)/Math.pow(10,S++):(h*=10,h+=r-48);else if(r===44||r===46)y=!0,S++;else{if(y){if(c)c=!1,f&&(N=!0);else if(N)N=!1;else{b?(n=A?0-h:h,f=!0,b=!1):m||(T=A?0-h:h);let O=m?h-1:1;for(let F=0;F<O;F++)f?o+=n:o=T,s.x.push(t),s.y.push(o*p),t+=a}A=!1,h=0,S=0,y=!1,m=!1}if(r<74&&r>63)y=!0,f=!1,h=r-64;else if(r>96&&r<106)y=!0,f=!1,h=r-96,A=!0;else if(r===115)y=!0,m=!0,h=9;else if(r>82&&r<91)y=!0,m=!0,h=r-82;else if(r>73&&r<83)y=!0,b=!0,h=r-73;else if(r>105&&r<115)y=!0,b=!0,h=r-105,A=!0;else if(r===36&&l.charCodeAt(u+1)===36)y=!0,g=!0;else if(r===37)y=!0,b=!0,h=0,A=!1;else if(r===45){let O=l.charCodeAt(u+1);(O>=48&&O<=57||O===44||O===46)&&(y=!0,c||(f=!1),A=!0)}else(r===13||r===10)&&(c=!0,g=!1)}}var ee=/\$\$.*/,te=/[,\t ]+/;function P(e,l,p){if(e.isPeaktable=!0,!e.variables||Object.keys(e.variables)===2?oe(e,l,p):pe(e,l,p),e.variables)for(let a in e.variables)e.variables[a].data=e.data[a]}function oe(e,l,p){let a={x:[],y:[]};e.data=a;let s=l.split(/,? *,?[;\r\n]+ */);for(let t=1;t<s.length;t++){let o=s[t].trim().replace(ee,"").split(te);if(o.length%2===0)for(let i=0;i<o.length;i=i+2)a.x.push(Number(o[i])*e.xFactor),a.y.push(Number(o[i+1])*e.yFactor);else p.logs.push(`Format error: ${o}`)}}function pe(e,l,p){let a={},s=Object.keys(e.variables),t=s.length;s.forEach(i=>a[i]=[]),e.data=a;let o=l.split(/,? *,?[;\r\n]+ */);for(let i=1;i<o.length;i++){let r=o[i].trim().replace(ee,"").split(te);if(r.length%t===0)for(let u=0;u<r.length;u++)a[s[u%t]].push(Number(r[u]));else p.logs.push(`Format error: ${r}`)}}function V(e,l){e.isXYAdata=!0;let p={};e.data=p;let a=l.split(/\r?\n/),s=a[0].replace(/^.*?([A-Z]+).*$/,"$1").split("").map(t=>t.toLowerCase());for(let t=1;t<a.length;t++){let o=a[t].replace(/^\((.*)\)$/,"$1").split(/ *, */);for(let i=0;i<s.length;i++){let r=o[i];switch(s[i]){case"x":case"y":case"w":r=Number.parseFloat(r);break;case"a":r=r.replace(/^<(.*)>$/,"$1");break;case"m":break;default:continue}p[s[i]]||(p[s[i]]=[]),p[s[i]].push(r)}}}import{parseString as se}from"dynamic-typing";import le from"ml-array-median";function I(e){let l=e[0].data.y[0],p=l,a=e.length,s=e[0].data.x.length,t=new Array(a);for(let f=0;f<a;f++){t[f]=e[f].data.y;for(let n=0;n<s;n++){let m=t[f][n];m<l&&(l=m),m>p&&(p=m)}}let o=e[0].data.x[0],i=e[0].data.x[e[0].data.x.length-1],r=e[0].pageValue,u=e[a-1].pageValue;if(o>i)for(let f of t)f.reverse();r>u&&t.reverse();let c=[];for(let f=0;f<t.length;f++){let n=Float64Array.from(t[f]);for(let m=0;m<n.length;m++)n[m]<0&&(n[m]=-n[m]);c.push(le(n))}let b=le(c);return{z:t,minX:Math.min(o,i),maxX:Math.max(o,i),minY:Math.min(r,u),maxY:Math.max(r,u),minZ:l,maxZ:p,noise:b}}function E(e,l){let p=e.noise,a=e.z,s,t,o,i,r,u,c,b,f=a.length,n=a[0].length,m,g,h,T,A=e.minX,N=(e.maxX-A)/(n-1),S=e.minY,F=(e.maxY-S)/(f-1),ae=e.minZ,H=e.maxZ,G=l.nbContourLevels*2,K=new Array(G),x;for(let M=0;M<G;M++){let D={};K[M]=D;let re=M%2,_=(H-l.noiseMultiplier*p)*Math.exp((M>>1)-l.nbContourLevels);re===0?x=_+l.noiseMultiplier*p:x=0-_-l.noiseMultiplier*p;let d=[];if(D.zValue=x,D.lines=d,!(x<=ae||x>=H))for(let v=0;v<f-1;v++){let z=a[v],W=a[v+1];for(let X=0;X<n-1;X++)s=z[X],t=z[X+1],o=W[X],i=W[X+1],r=s>x,u=t>x,c=o>x,b=i>x,r!==u&&r!==c&&(m=X+(x-s)/(t-s),g=v,h=X,T=v+(x-s)/(o-s),d.push(m*N+A),d.push(g*F+S),d.push(h*N+A),d.push(T*F+S)),b!==u&&b!==c&&(m=X+1,g=v+1-(x-i)/(t-i),h=X+1-(x-i)/(o-i),T=v+1,d.push(m*N+A),d.push(g*F+S),d.push(h*N+A),d.push(T*F+S)),u!==c&&(m=(X+1-(x-t)/(o-t))*N+A,g=(v+(x-t)/(o-t))*F+S,u!==r&&(h=X+1-(x-t)/(s-t),T=v,d.push(m),d.push(g),d.push(h*N+A),d.push(T*F+S)),c!==r&&(h=X,T=v+1-(x-o)/(s-o),d.push(m),d.push(g),d.push(h*N+A),d.push(T*F+S)),u!==b&&(h=X+1,T=v+(x-t)/(i-t),d.push(m),d.push(g),d.push(h*N+A),d.push(T*F+S)),c!==b&&(h=X+(x-o)/(i-o),T=v+1,d.push(m),d.push(g),d.push(h*N+A),d.push(T*F+S)))}}return{minX:e.minX,maxX:e.maxX,minY:e.minY,maxY:e.maxY,segments:K}}function k(e,l){let p=I(e.spectra);l.noContour||(e.contourLines=E(p,l),delete p.z),e.minMax=p}import{gyromagneticRatio as ie}from"gyromagnetic-ratio";function Z(e){for(let l of e){let p=0,a=0;for(let s of l.spectra){if(l.ntuples&&l.ntuples.symbol?(!p&&s.observeFrequency&&(p=s.observeFrequency),!a&&s.shiftOffsetVal&&(a=s.shiftOffsetVal)):(p=s.observeFrequency,a=s.shiftOffsetVal),p&&s.xUnits&&s.xUnits.toUpperCase().includes("HZ")){s.xUnits="PPM",s.xFactor=s.xFactor/p,s.firstX=s.firstX/p,s.lastX=s.lastX/p,s.deltaX=s.deltaX/p;for(let t=0;t<s.data.x.length;t++)s.data.x[t]/=p}if(a&&s.xUnits.toLowerCase().includes("ppm")){let t=s.firstX-a;s.firstX=s.firstX-t,s.lastX=s.lastX-t;for(let o=0;o<s.data.x.length;o++)s.data.x[o]-=t}if(l.ntuples&&l.ntuples.nucleus&&l.ntuples.symbol)for(let t=0;t<l.ntuples.nucleus.length;t++){let o=l.ntuples.symbol[t],i=l.ntuples.nucleus[t];if(o.match(/^[F|T]/)&&!i){if(o.match(/[F|T]1/))if(l.tmp.$NUC2)l.ntuples.nucleus[t]=l.tmp.$NUC2;else{let r=l.ntuples.symbol.indexOf(o.replace(/^([F|T]).*/,"$12"));r&&l.ntuples.nucleus[r]&&(l.ntuples.nucleus[t]=l.ntuples.nucleus[r])}o.match(/[F|T]2/)&&(l.ntuples.nucleus[t]=l.tmp.$NUC1)}o.match(/[F|T]2/)&&(l.yType=l.ntuples.nucleus[0])}if(p&&l.ntuples&&l.ntuples.symbol&&l.ntuples.nucleus){let t="",o=l.ntuples.symbol.indexOf(s.pageSymbol);if(l.ntuples.units&&l.ntuples.units[o]&&(t=l.ntuples.units[o]),t!=="PPM"){if(o!==0)throw Error("Not sure about this ntuples format");let i=ie[l.ntuples.nucleus[0]],r=ie[l.ntuples.nucleus[1]];if(!i||!r)throw Error("Problem with determination of gyromagnetic ratio");let u=i/r*p;s.pageValue/=u}}}}}function L(e,l,p={}){p.logger?.trace(l),e.profiling&&e.profiling.push({action:l,time:Date.now()-p.start})}function j(e){let l=e.spectra[0].data;e.chromatogram={times:l.x.slice(),series:{intensity:{dimension:1,data:l.y.slice()}}}}function B(e,l,p){Z(e),ue(e,p);for(let a of e){if(Object.keys(a.ntuples).length>0){let s=[],t=Object.keys(a.ntuples);for(let o=0;o<t.length;o++){let i=t[o],r=a.ntuples[i];for(let u=0;u<r.length;u++)s[u]||(s[u]={}),s[u][i]=r[u]}a.ntuples=s}a.twoD&&p.wantXY&&(k(a,p),L(l,"Finished countour plot calculation",p),p.keepSpectra||delete a.spectra),p.chromatogram&&(a.spectra.length>1?J(a):j(a),L(l,"Finished chromatogram calculation",p)),delete a.tmp}}function ue(e,l){for(let p of e)for(let a in p.meta){let s=p.meta[a];if(typeof s=="string"){if(s[0]==="{"){if(s[s.length-1]==="}"){let t=s.slice(1,-1).split(/[,; ]+/).filter(o=>o);for(let o=0;o<t.length;o++)p.meta[a+String(o)]=l.dynamicTyping?se(t[o]):t[o]}}else if(s[0]==="("){let t=s.split(/\r?\n/),o=/^\((?<from>\d+)\.\.(?<to>\d+)\)$/;if(o.test(t[0])){let[i,r]=t[0].match(o).slice(1).map(Number),u=t.slice(1).join(" ").split(/[,; ]+/).filter(c=>c);for(let c=i;c<=r;c++)p.meta[a+String(c)]=l.dynamicTyping?se(u[c-i]):u[c-i]}}}}}function q(e,l,p){let a=-1,s=-1,t="",o="";if(p.indexOf("++")>0)t=p.replace(/.*\(([a-zA-Z0-9]+)\+\+.*/,"$1"),o=p.replace(/.*\.\.([a-zA-Z0-9]+).*/,"$1");else{p=p.replace(/[^a-zA-Z]/g,""),t=p.charAt(0),o=p.charAt(1),l.variables={};for(let i of p){let r=i.toLowerCase(),u=e.ntuples.symbol.indexOf(i);if(u===-1)throw Error(`Symbol undefined: ${i}`);l.variables[r]={};for(let c in e.ntuples)e.ntuples[c][u]&&(l.variables[r][c.replace(/^var/,"")]=e.ntuples[c][u])}}a=e.ntuples.symbol.indexOf(t),s=e.ntuples.symbol.indexOf(o),a===-1&&(a=0),s===-1&&(s=0),e.ntuples.first&&(e.ntuples.first.length>a&&(l.firstX=e.ntuples.first[a]),e.ntuples.first.length>s&&(l.firstY=e.ntuples.first[s])),e.ntuples.last&&(e.ntuples.last.length>a&&(l.lastX=e.ntuples.last[a]),e.ntuples.last.length>s&&(l.lastY=e.ntuples.last[s])),e.ntuples.vardim&&e.ntuples.vardim.length>a&&(l.nbPoints=e.ntuples.vardim[a]),e.ntuples.factor&&(e.ntuples.factor.length>a&&(l.xFactor=e.ntuples.factor[a]),e.ntuples.factor.length>s&&(l.yFactor=e.ntuples.factor[s])),e.ntuples.units&&(e.ntuples.units.length>a&&(e.ntuples.varname&&e.ntuples.varname[a]?l.xUnits=`${e.ntuples.varname[a]} [${e.ntuples.units[a]}]`:l.xUnits=e.ntuples.units[a]),e.ntuples.units.length>s&&(e.ntuples.varname&&e.ntuples.varname[s]?l.yUnits=`${e.ntuples.varname[s]} [${e.ntuples.units[s]}]`:l.yUnits=e.ntuples.units[s]))}function w(e){e.xFactor||(e.xFactor=1),e.yFactor||(e.yFactor=1)}var C=/[ \t]*,[ \t]*/,he={keepRecordsRegExp:/^$/,canonicDataLabels:!0,canonicMetadataLabels:!1,dynamicTyping:!0,withoutXY:!1,chromatogram:!1,keepSpectra:!1,noContour:!1,nbContourLevels:7,noiseMultiplier:5,profiling:!1};function ge(e,l={}){e=ce(e),l={...he,...l},l.logger?.debug("Starting jcamp conversion"),l.wantXY=!l.withoutXY,l.start=Date.now();let p=[],a={profiling:l.profiling?[]:!1,logs:[],entries:[]},s={children:[]},t=s,o=[],i={};if(typeof e!="string")throw new TypeError("the JCAMP should be a string");L(a,"Before split to LDRS",l);let r=e.replace(/[\r\n]+##/g,`
6
6
  ##`).split(`
7
- ##`);L(a,"Split to LDRS",i),r[0]&&(r[0]=r[0].replace(/^[\r\n ]*##/,""));for(let m of r){let c=m.indexOf("="),b=c>0?m.substring(0,c):m,f=c>0?m.substring(c+1).trim():"",n=b.replace(/[_ -]/g,"").toUpperCase();if(n==="DATATABLE"){let u=f.indexOf(`
8
- `);if(u===-1&&(u=f.indexOf("\r")),u>0){let d=f.substring(0,u).split(/[ ,;\t]+/);q(t,l,d[0]),l.datatable=d[0],d[1]&&d[1].indexOf("PEAKS")>-1?n="PEAKTABLE":d[1]&&(d[1].indexOf("XYDATA")||d[0].indexOf("++")>0)&&(n="XYDATA",l.nbPoints&&(l.deltaX=(l.lastX-l.firstX)/(l.nbPoints-1)))}}if(n==="XYDATA"){i.wantXY&&(w(l),f.match(/.*\+\+.*/)?(l.nbPoints&&(l.deltaX=(l.lastX-l.firstX)/(l.nbPoints-1)),U(l,f,a)):P(l,f,a),t.spectra.push(l),l={});continue}else if(n==="PEAKTABLE"){i.wantXY&&(w(l),P(l,f,a),t.spectra.push(l),l={});continue}if(n==="PEAKASSIGNMENTS"){i.wantXY&&(f.match(/.*([^A-Z]*).*/)&&V(l,f),t.spectra.push(l),l={});continue}if(n==="TITLE"){let u=t;u.children||(u.children=[]),t={spectra:[],ntuples:{},info:{},meta:{},tmp:{}},u.children.push(t),o.push(u),p.push(t),t.title=f}else n==="DATATYPE"?(t.dataType=f,f.match(/(^nd|\snd\s)/i)&&(t.twoD=!0)):n==="NTUPLES"?f.match(/(^nd|\snd\s)/i)&&(t.twoD=!0):n==="DATACLASS"?t.dataClass=f:n==="XUNITS"?l.xUnits=f:n==="YUNITS"?l.yUnits=f:n==="FIRSTX"?l.firstX=Number(f):n==="LASTX"?l.lastX=Number(f):n==="FIRSTY"?l.firstY=Number(f):n==="LASTY"?l.lastY=Number(f):n==="NPOINTS"?l.nbPoints=Number(f):n==="XFACTOR"?l.xFactor=Number(f):n==="YFACTOR"?l.yFactor=Number(f):n==="MAXX"?l.maxX=Number(f):n==="MINX"?l.minX=Number(f):n==="MAXY"?l.maxY=Number(f):n==="MINY"?l.minY=Number(f):n==="DELTAX"?l.deltaX=Number(f):n===".OBSERVEFREQUENCY"||n==="$SFO1"?l.observeFrequency||(l.observeFrequency=Number(f)):n===".OBSERVENUCLEUS"?l.xType||(t.xType=f.replace(/[^a-zA-Z0-9]/g,"")):n==="$OFFSET"?(t.shiftOffsetNum=0,l.shiftOffsetVal||(l.shiftOffsetVal=Number(f))):n==="$REFERENCEPOINT"||(n==="VARNAME"?t.ntuples.varname=f.split(C):n==="SYMBOL"?t.ntuples.symbol=f.split(C):n==="VARTYPE"?t.ntuples.vartype=f.split(C):n==="VARFORM"?t.ntuples.varform=f.split(C):n==="VARDIM"?t.ntuples.vardim=Y(f.split(C)):n==="UNITS"?t.ntuples.units=f.split(C):n==="FACTOR"?t.ntuples.factor=Y(f.split(C)):n==="FIRST"?t.ntuples.first=Y(f.split(C)):n==="LAST"?t.ntuples.last=Y(f.split(C)):n==="MIN"?t.ntuples.min=Y(f.split(C)):n==="MAX"?t.ntuples.max=Y(f.split(C)):n===".NUCLEUS"?t.ntuples&&(t.ntuples.nucleus=f.split(C).map(u=>u.replace(/[^a-zA-Z0-9]/g,""))):n==="PAGE"?(l.page=f.trim(),l.pageValue=Number(f.replace(/^.*=/,"")),l.pageSymbol=l.page.replace(/[=].*/,"")):n==="RETENTIONTIME"?l.pageValue=Number(f):Q(n)?l[$(n)]=f:n==="SAMPLEDESCRIPTION"?l.sampleDescription=f:n.startsWith("$NUC")?!t.tmp[n]&&!f.includes("off")&&(t.tmp[n]=f.replace(/[<>]/g,"")):n==="END"&&(t=o.pop()));if(t&&t.info&&t.meta&&n.match(i.keepRecordsRegExp)){let u=f.trim(),d,h;b.startsWith("$")?(h=i.canonicMetadataLabels?n.substring(1):b.substring(1),d=t.meta):(h=i.canonicDataLabels?n:b,d=t.info),i.dynamicTyping&&(u=me(u)),d[h]?(Array.isArray(d[h])||(d[h]=[d[h]]),d[h].push(u)):d[h]=u}}return L(a,"Finished parsing",i),B(p,a,i),L(a,"Total time",i),a.entries=s.children,a.flatten=p,a}export{de as convert,ne as createTree};
7
+ ##`);L(a,"Split to LDRS",l),r[0]&&(r[0]=r[0].replace(/^[\r\n ]*##/,""));for(let u of r){let c=u.indexOf("="),b=c>0?u.substring(0,c):u,f=c>0?u.substring(c+1).trim():"",n=b.replace(/[_ -]/g,"").toUpperCase();if(n==="DATATABLE"){let m=f.indexOf(`
8
+ `);if(m===-1&&(m=f.indexOf("\r")),m>0){let g=f.substring(0,m).split(/[ ,;\t]+/);q(t,i,g[0]),i.datatable=g[0],g[1]&&g[1].indexOf("PEAKS")>-1?n="PEAKTABLE":g[1]&&(g[1].indexOf("XYDATA")||g[0].indexOf("++")>0)&&(n="XYDATA",i.nbPoints&&(i.deltaX=(i.lastX-i.firstX)/(i.nbPoints-1)))}}if(n==="XYDATA"){l.wantXY&&(w(i),f.match(/.*\+\+.*/)?(i.nbPoints&&(i.deltaX=(i.lastX-i.firstX)/(i.nbPoints-1)),U(i,f,a)):P(i,f,a),t.spectra.push(i),i={});continue}else if(n==="PEAKTABLE"){l.wantXY&&(w(i),P(i,f,a),t.spectra.push(i),i={});continue}if(n==="PEAKASSIGNMENTS"){l.wantXY&&(f.match(/.*([^A-Z]*).*/)&&V(i,f),t.spectra.push(i),i={});continue}if(n==="TITLE"){let m=t;m.children||(m.children=[]),t={spectra:[],ntuples:{},info:{},meta:{},tmp:{}},m.children.push(t),o.push(m),p.push(t),t.title=f}else n==="DATATYPE"?(t.dataType=f,f.match(/(^nd|\snd\s)/i)&&(t.twoD=!0)):n==="NTUPLES"?f.match(/(^nd|\snd\s)/i)&&(t.twoD=!0):n==="DATACLASS"?t.dataClass=f:n==="XUNITS"?i.xUnits=f:n==="YUNITS"?i.yUnits=f:n==="FIRSTX"?i.firstX=Number(f):n==="LASTX"?i.lastX=Number(f):n==="FIRSTY"?i.firstY=Number(f):n==="LASTY"?i.lastY=Number(f):n==="NPOINTS"?i.nbPoints=Number(f):n==="XFACTOR"?i.xFactor=Number(f):n==="YFACTOR"?i.yFactor=Number(f):n==="MAXX"?i.maxX=Number(f):n==="MINX"?i.minX=Number(f):n==="MAXY"?i.maxY=Number(f):n==="MINY"?i.minY=Number(f):n==="DELTAX"?i.deltaX=Number(f):n===".OBSERVEFREQUENCY"||n==="$SFO1"?i.observeFrequency||(i.observeFrequency=Number(f)):n===".OBSERVENUCLEUS"?i.xType||(t.xType=f.replace(/[^a-zA-Z0-9]/g,"")):n==="$OFFSET"?(t.shiftOffsetNum=0,i.shiftOffsetVal||(i.shiftOffsetVal=Number(f))):n==="$REFERENCEPOINT"||(n==="VARNAME"?t.ntuples.varname=f.split(C):n==="SYMBOL"?t.ntuples.symbol=f.split(C):n==="VARTYPE"?t.ntuples.vartype=f.split(C):n==="VARFORM"?t.ntuples.varform=f.split(C):n==="VARDIM"?t.ntuples.vardim=Y(f.split(C)):n==="UNITS"?t.ntuples.units=f.split(C):n==="FACTOR"?t.ntuples.factor=Y(f.split(C)):n==="FIRST"?t.ntuples.first=Y(f.split(C)):n==="LAST"?t.ntuples.last=Y(f.split(C)):n==="MIN"?t.ntuples.min=Y(f.split(C)):n==="MAX"?t.ntuples.max=Y(f.split(C)):n===".NUCLEUS"?t.ntuples&&(t.ntuples.nucleus=f.split(C).map(m=>m.replace(/[^a-zA-Z0-9]/g,""))):n==="PAGE"?(i.page=f.trim(),i.pageValue=Number(f.replace(/^.*=/,"")),i.pageSymbol=i.page.replace(/[=].*/,"")):n==="RETENTIONTIME"?i.pageValue=Number(f):Q(n)?i[$(n)]=f:n==="SAMPLEDESCRIPTION"?i.sampleDescription=f:n.startsWith("$NUC")?!t.tmp[n]&&!f.includes("off")&&(t.tmp[n]=f.replace(/[<>]/g,"")):n==="END"&&(t=o.pop()));if(t&&t.info&&t.meta&&n.match(l.keepRecordsRegExp)){let m=f.trim(),g,h;b.startsWith("$")?(h=l.canonicMetadataLabels?n.substring(1):b.substring(1),g=t.meta):(h=l.canonicDataLabels?n:b,g=t.info),l.dynamicTyping&&(m=me(m)),g[h]?(Array.isArray(g[h])||(g[h]=[g[h]]),g[h].push(m)):g[h]=m}}if(L(a,"Finished parsing",l),B(p,a,l),L(a,"Total time",l),a.entries=s.children,a.flatten=p,l.logger){l.logger.debug("Finished jcamp conversion");for(let u of a.flatten)l.logger.debug(`${u.dataType} - ${u.title}`)}return a}export{ge as convert,ne as createTree};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jcampconverter",
3
- "version": "9.2.2",
3
+ "version": "9.3.0",
4
4
  "description": "Parse and convert JCAMP data",
5
5
  "types": "jcampconverter.d.ts",
6
6
  "main": "jcampconverter.cjs",
@@ -50,25 +50,25 @@
50
50
  "url": "https://github.com/cheminfo/nmrium/issues/new?title=%5Bjcampconverter%5D"
51
51
  },
52
52
  "devDependencies": {
53
- "@microsoft/api-extractor": "^7.37.2",
54
- "@types/jest": "^29.5.5",
53
+ "@microsoft/api-extractor": "^7.38.3",
54
+ "@types/jest": "^29.5.8",
55
55
  "@types/pino": "^7.0.5",
56
- "@vitest/coverage-v8": "^0.34.5",
56
+ "@vitest/coverage-v8": "^0.34.6",
57
57
  "benchmark": "^2.1.4",
58
58
  "c8": "^8.0.1",
59
59
  "cheminfo-build": "^1.2.0",
60
- "esbuild": "^0.19.3",
61
- "eslint": "^8.49.0",
60
+ "esbuild": "^0.19.5",
61
+ "eslint": "^8.53.0",
62
62
  "eslint-config-cheminfo-typescript": "^12.0.4",
63
63
  "esm": "^3.2.25",
64
64
  "fifo-logger": "^0.6.1",
65
65
  "jest-matcher-deep-close-to": "^3.0.2",
66
66
  "jscpd": "^3.5.10",
67
- "ml-spectra-processing": "^12.5.1",
68
- "prettier": "^3.0.3",
69
- "rimraf": "^5.0.1",
67
+ "ml-spectra-processing": "^12.6.0",
68
+ "prettier": "^3.1.0",
69
+ "rimraf": "^5.0.5",
70
70
  "typescript": "^5.2.2",
71
- "vitest": "^0.34.5"
71
+ "vitest": "^0.34.6"
72
72
  },
73
73
  "dependencies": {
74
74
  "cheminfo-types": "^1.7.2",