{"version":3,"file":"rxn-parser.esm.js","sources":["../node_modules/ensure-string/lib/index.js","../src/index.js"],"sourcesContent":["/**\n * Ensure that the data is string. If it is an ArrayBuffer it will be converted to string using TextDecoder.\n * @param blob\n * @param options\n * @returns\n */\nexport function ensureString(blob, options = {}) {\n    if (typeof blob === 'string') {\n        return blob;\n    }\n    if (ArrayBuffer.isView(blob) || blob instanceof ArrayBuffer) {\n        if (options.encoding) {\n            return new TextDecoder(options.encoding).decode(blob);\n        }\n        else {\n            return decodeText(blob);\n        }\n    }\n    throw new TypeError(`blob must be a string, ArrayBuffer or ArrayBufferView`);\n}\nfunction decodeText(blob) {\n    const uint8 = ArrayBuffer.isView(blob)\n        ? new Uint8Array(blob.buffer, blob.byteOffset, blob.byteLength)\n        : new Uint8Array(blob);\n    if (uint8.length >= 2) {\n        if (uint8[0] === 0xfe && uint8[1] === 0xff) {\n            return new TextDecoder('utf-16be').decode(uint8);\n        }\n        if (uint8[0] === 0xff && uint8[1] === 0xfe) {\n            return new TextDecoder('utf-16le').decode(uint8);\n        }\n    }\n    try {\n        return new TextDecoder('utf-8', { fatal: true }).decode(uint8);\n    }\n    catch {\n        return new TextDecoder('latin1').decode(uint8);\n    }\n}\n//# sourceMappingURL=index.js.map","import { ensureString } from 'ensure-string';\n\n/**\n * Parse a rxn file and return an object with reagents and products.\n * @param {import('cheminfo-types').TextData} rxn - RXN file content.\n * @returns {{ reagents: string[], products: string[] }} Parsed reagents and products.\n */\nexport function parse(rxn) {\n  rxn = ensureString(rxn);\n  // we will find the delimiter in order to be much faster and not use regular expression\n  const header = rxn.slice(0, 1000);\n  let crlf = '\\n';\n  if (header.includes('\\r\\n')) {\n    crlf = '\\r\\n';\n  } else if (header.includes('\\r')) {\n    crlf = '\\r';\n  }\n\n  const rxnParts = rxn.split(`${crlf}$MOL${crlf}`);\n\n  const reagents = [];\n  const products = [];\n\n  // the first part is expected to contain the number of reagents and products\n\n  // First part should start with $RXN\n  // and the fifth line should contain the number of reagents and products\n  if (rxnParts.length === 0) throw new Error('file looks empty');\n\n  const headerPart = rxnParts[0];\n  if (headerPart.indexOf('$RXN') !== 0) {\n    throw new Error('file does not start with $RXN');\n  }\n\n  const lines = headerPart.split(crlf);\n  if (lines.length < 5) throw new Error('incorrect number of lines in header');\n\n  let numberReagents = lines[4].slice(0, 3) >> 0;\n  const numberProducts = lines[4].slice(3, 6) >> 0;\n\n  // hack for JSME\n  const thirdNumber = lines[4].slice(6, 9) >> 0; // for jsme\n\n  if (thirdNumber && rxnParts[1]) {\n    const jsmeLines = rxnParts[1].split(crlf);\n    if (jsmeLines[0]) {\n      numberReagents = jsmeLines[0]\n        .trim()\n        .replace(/>[^>]*$/, '')\n        .split(/[.>]/).length;\n    }\n  }\n\n  if (numberReagents + numberProducts !== rxnParts.length - 1) {\n    throw new Error('not the correct number of molecules');\n  }\n\n  for (let i = 1; i < rxnParts.length; i++) {\n    if (i <= numberReagents) {\n      reagents.push(rxnParts[i]);\n    } else {\n      products.push(rxnParts[i]);\n    }\n  }\n  return { reagents, products };\n}\n"],"names":["ensureString","blob","options","ArrayBuffer","isView","encoding","TextDecoder","decode","decodeText","TypeError","uint8","Uint8Array","buffer","byteOffset","byteLength","length","fatal","parse","rxn","header","slice","crlf","includes","rxnParts","split","reagents","products","Error","headerPart","indexOf","lines","numberReagents","numberProducts","thirdNumber","jsmeLines","trim","replace","i","push"],"mappings":";;;;;;;AAYA;;;;;;AAMM,SAAUA,YAAYA,CAC1BC,IAAc,EACdC,OAAA,GAA+B,EAAE,EAAA;AAEjC,EAAA,IAAI,OAAOD,IAAI,KAAK,QAAQ,EAAE;AAC5B,IAAA,OAAOA,IAAI;AACb,EAAA;EACA,IAAIE,WAAW,CAACC,MAAM,CAACH,IAAI,CAAC,IAAIA,IAAI,YAAYE,WAAW,EAAE;IAC3D,IAAID,OAAO,CAACG,QAAQ,EAAE;MACpB,OAAO,IAAIC,WAAW,CAACJ,OAAO,CAACG,QAAQ,CAAC,CAACE,MAAM,CAACN,IA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