UNPKG

pubchem

Version:

pubchem simplifies the data retrieval from the PubChem API.

119 lines 4.75 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getGHS = getGHS; exports.getGHSSummary = getGHSSummary; const jsonpath_1 = __importDefault(require("jsonpath")); const getReferences_js_1 = require("../getReferences.js"); const ghsPictograms_js_1 = require("./ghsPictograms.js"); const hazardStatements_js_1 = require("./hazardStatements.js"); const precautionaryStatements_js_1 = require("./precautionaryStatements.js"); /** * Extracts GHS information (H/P-Statements and pictograms) * @param data The data to extract from * @return The extracted data */ function getGHS(data) { const references = (0, getReferences_js_1.getReferences)(data); let pictograms; try { pictograms = jsonpath_1.default .query(data, '$.Section[?(@.TOCHeading==="Safety and Hazards")]' + '.Section[?(@.TOCHeading==="Hazards Identification")]' + '.Section[?(@.TOCHeading==="GHS Classification")]' + '.Information[?(@.Name==="Pictogram(s)")]') .map((entry) => ({ reference: references[entry.ReferenceNumber], data: jsonpath_1.default .query(entry, '$.Value.StringWithMarkup[*].Markup[*]') .map((entry) => { const code = entry.URL.match(/GHS\d+/)[0]; return { code, description: ghsPictograms_js_1.ghsPictogramText[code], }; }), })); } catch { pictograms = []; } let hStatements; try { hStatements = jsonpath_1.default .query(data, '$.Section[?(@.TOCHeading==="Safety and Hazards")]' + '.Section[?(@.TOCHeading==="Hazards Identification")]' + '.Section[?(@.TOCHeading==="GHS Classification")]' + '.Information[?(@.Name==="GHS Hazard Statements")]') .map((entry) => ({ reference: references[entry.ReferenceNumber], data: jsonpath_1.default .query(entry, '$.Value.StringWithMarkup[*]') .map((entry) => entry.String.match(/H\d+/)[0]) .map((hCode) => ({ code: hCode, description: hazardStatements_js_1.hazardStatements[hCode], })), })); } catch { hStatements = []; } //ToDo(kjappelbaum): investigate in more detail why they do not have the full P statements //For P statements the full sentence (with conditions) is more important than just the number let pStatements; try { pStatements = jsonpath_1.default .query(data, '$.Section[?(@.TOCHeading==="Safety and Hazards")]' + '.Section[?(@.TOCHeading==="Hazards Identification")]' + '.Section[?(@.TOCHeading==="GHS Classification")]' + '.Information[?(@.Name==="Precautionary Statement Codes")]') .map((entry) => ({ reference: references[entry.ReferenceNumber], data: jsonpath_1.default .query(entry, '$.Value.StringWithMarkup[*]') .map((entry) => entry.String.match(/(?<oneP>(?<!\+)P\d\d\d(?!\+))|(?<twoP>P\d\d\d\+P\d\d\d(?!\+))|(?<threeP>P\d\d\d\+P\d\d\d\+P\d\d\d(?!\+))/gm))[0] .map((pCode) => ({ code: pCode, description: precautionaryStatements_js_1.precautionaryStatements[pCode], })), })); } catch { pStatements = []; } return { pictograms, hStatements, pStatements, }; } /** * We combine all the results and keep */ function getGHSSummary(data, options = {}) { const { sourceName } = options; const result = getGHS(data); if (sourceName) { result.pictograms = result.pictograms.filter((entry) => entry.reference.sourceName.match(sourceName)); result.hStatements = result.hStatements.filter((entry) => entry.reference.sourceName.match(sourceName)); result.pStatements = result.pStatements.filter((entry) => entry.reference.sourceName.match(sourceName)); } return { pictograms: getUnique(result.pictograms), hStatements: getUnique(result.hStatements), pStatements: getUnique(result.pStatements), }; } function getUnique(entries) { const uniqueMap = {}; entries.map((entry) => entry.data.forEach((line) => { uniqueMap[line.code] = line; })); return Object.keys(uniqueMap) .map((code) => uniqueMap[code]) .sort((a, b) => (a.code < b.code ? -1 : 1)); } //# sourceMappingURL=getGHS.js.map