UNPKG

@vidal-community/vidal-web-components

Version:

Vidal Web Components

160 lines 9.81 kB
import { Drug, DrugType } from '../model/drug'; import { Classifications } from '../model/classifications'; import { Classification } from '../model/classification'; import { max, parse } from 'date-fns'; export function parseSearchResult(searchResult) { const domParser = new DOMParser(); const dom = domParser.parseFromString(searchResult, 'text/xml'); const entry = dom.querySelector('feed > entry'); const category = entry.getAttribute('vidal:categories'); const vmpElement = entry.getElementsByTagName('vidal:vmp')[0]; const vmpName = vmpElement === null || vmpElement === void 0 ? void 0 : vmpElement.textContent; const vmpId = vmpElement === null || vmpElement === void 0 ? void 0 : vmpElement.getAttribute('vidalId'); const id = entry.getElementsByTagName('vidal:id')[0].textContent; const marketStatus = entry .getElementsByTagName('vidal:marketStatus')[0] .getAttribute('name'); const offMarketDate = parseDateElement(entry, 'vidal:offMarketDate'); if (category === 'PACKAGE') { const drugName = entry.getElementsByTagName('vidal:shortLabel')[0].textContent; const cip13 = entry.getElementsByTagName('vidal:cip13')[0].textContent; const cip = entry.getElementsByTagName('vidal:cip')[0].textContent; const productId = entry.getElementsByTagName('vidal:productId')[0].textContent; return new Drug(Number(id), Number(cip13), Number(cip), drugName.toString(), undefined, Number(undefined), DrugType.PACK, vmpName !== null && vmpName !== void 0 ? vmpName : '', marketStatus.toString(), vmpId ? Number(vmpId) : undefined, offMarketDate, Number(productId)); } else if (category === 'UCD') { const drugName = entry.getElementsByTagName('vidal:name')[0].textContent; const ucd = entry.getElementsByTagName('vidal:ucd')[0].textContent; const ucd13 = entry.getElementsByTagName('vidal:ucd13')[0].textContent; const links = entry.getElementsByTagName('link'); const productIdLink = [...links].filter((link) => link.getAttribute('rel') === 'related' && link.getAttribute('title') === 'PRODUCT'); const productId = productIdLink[0].getAttribute('href').split('/').pop(); return new Drug(Number(id), Number(undefined), Number(undefined), drugName.toString(), ucd !== null && ucd !== void 0 ? ucd : undefined, Number(ucd13), DrugType.UCD, vmpName !== null && vmpName !== void 0 ? vmpName : '', marketStatus.toString(), vmpId ? Number(vmpId) : undefined, offMarketDate, Number(productId)); } return null; } export function parseAtcClassification(atcClassification) { const domParser = new DOMParser(); const dom = domParser.parseFromString(atcClassification, 'text/xml'); const entries = dom.querySelectorAll('feed > entry'); const atcLeafs = [...entries].filter((entry) => entry.getAttribute('vidal:categories') === 'ATC_CLASSIFICATION' && !entry.querySelector("link[title='CHILDREN']")); const leafId = getEntryValue(atcLeafs[0], 'vidal:id'); const leafName = getEntryValue(atcLeafs[0], 'vidal:name'); const leafCode = getEntryValue(atcLeafs[0], 'vidal:code'); const arborescences = []; atcLeafs.forEach((atcLeaf) => { var _a, _b, _c, _d; const arborescence = []; let leafParentId = (_b = (_a = atcLeaf .querySelector('link[rel=inline]')) === null || _a === void 0 ? void 0 : _a.getAttributeNode('href')) === null || _b === void 0 ? void 0 : _b.value; while (leafParentId) { const parent = [...entries].filter((entry) => { var _a, _b; return entry.getAttribute('vidal:categories') === 'ATC_CLASSIFICATION' && ((_a = entry.getElementsByTagName('id')[0]) === null || _a === void 0 ? void 0 : _a.textContent) === leafParentId && ((_b = entry.getElementsByTagName('vidal:id')[0]) === null || _b === void 0 ? void 0 : _b.textContent) !== '0'; })[0]; const leafParentName = parent === null || parent === void 0 ? void 0 : parent.getElementsByTagName('vidal:name')[0].textContent; if (leafParentName != null) { arborescence.push(leafParentName); } leafParentId = (_d = (_c = parent === null || parent === void 0 ? void 0 : parent.querySelector('link[rel=inline]')) === null || _c === void 0 ? void 0 : _c.getAttributeNode('href')) === null || _d === void 0 ? void 0 : _d.value; } arborescences.push(arborescence); }); const atcRoots = [...entries].filter((entry) => { var _a, _b; return entry.getAttribute('vidal:categories') === 'ATC_CLASSIFICATION' && entry.querySelector("link[title='CHILDREN']") && ((_b = (_a = entry.getElementsByTagName('vidal:code')[0]) === null || _a === void 0 ? void 0 : _a.textContent) === null || _b === void 0 ? void 0 : _b.length) === 1; }); const rootId = getEntryValue(atcRoots[0], 'vidal:id'); const rootName = getEntryValue(atcRoots[0], 'vidal:name'); const rootCode = getEntryValue(atcRoots[0], 'vidal:code'); const atcLeavesHaveSameParent = atcLeafs.filter((leaf) => { const code = getEntryValue(leaf, 'vidal:code'); const firstCodeLetterOfLeaves = atcLeafs.map((x) => getEntryValue(x, 'vidal:code')[0]); return (firstCodeLetterOfLeaves.filter((firstCodeLetter) => firstCodeLetter !== code[0]).length > 0); }).length === 0; const hasMultipleClassifications = atcLeafs.length > 1 && !atcLeavesHaveSameParent; return new Classifications(new Classification(Number(rootId), rootName.toString(), rootCode.toString(), ''), new Classification(Number(leafId), leafName.toString(), leafCode.toString(), arborescences .map((arb) => `${arb.reverse().join(' / ')} / ${leafName}`) .join('\n\n')), hasMultipleClassifications); } export function parseVidalClassification(vidalClassification) { const domParser = new DOMParser(); const dom = domParser.parseFromString(vidalClassification, 'text/xml'); const entries = dom.querySelectorAll('feed > entry'); const roots = [...entries].filter((entry) => { var _a; return entry.getAttribute('vidal:categories') === 'VIDAL_CLASSIFICATION' && ((_a = entry.querySelector('link[rel=inline]')) === null || _a === void 0 ? void 0 : _a.getAttribute('href')) === 'vidal://vidal_classification/0'; }); const { id, name } = roots .map((root) => { return { id: getEntryValue(root, 'vidal:id'), name: getEntryValue(root, 'vidal:name'), }; }) .sort()[0]; const arborescences = []; [...entries] .filter((entry) => entry.getAttribute('vidal:categories') === 'VIDAL_CLASSIFICATION' && !entry.querySelector("link[title='CHILDREN']")) .forEach((atcLeaf) => { var _a, _b, _c, _d; const arborescence = []; let leafParentId = (_b = (_a = atcLeaf .querySelector('link[rel=inline]')) === null || _a === void 0 ? void 0 : _a.getAttributeNode('href')) === null || _b === void 0 ? void 0 : _b.value; while (leafParentId) { const parent = [...entries].filter((entry) => { var _a, _b; return entry.getAttribute('vidal:categories') === 'VIDAL_CLASSIFICATION' && ((_a = entry.getElementsByTagName('id')[0]) === null || _a === void 0 ? void 0 : _a.textContent) === leafParentId && ((_b = entry.getElementsByTagName('vidal:id')[0]) === null || _b === void 0 ? void 0 : _b.textContent) !== '0'; })[0]; const leafParentName = parent === null || parent === void 0 ? void 0 : parent.getElementsByTagName('vidal:name')[0].textContent; if (leafParentName != null) { arborescence.push(leafParentName); } leafParentId = (_d = (_c = parent === null || parent === void 0 ? void 0 : parent.querySelector('link[rel=inline]')) === null || _c === void 0 ? void 0 : _c.getAttributeNode('href')) === null || _d === void 0 ? void 0 : _d.value; } arborescences.push(arborescence); }); const hasMultipleClassifications = roots.length > 1; return new Classifications(new Classification(Number(id), name.toString(), name.toString(), arborescences.map((arb) => `${arb.reverse().join(' / ')}`).join('\n\n')), Classification.emptyClass, hasMultipleClassifications); } export function parsePrescribableName(prescribable) { var _a; const domParser = new DOMParser(); const dom = domParser.parseFromString(prescribable, 'text/xml'); const entry = dom.querySelector('feed > entry'); const category = entry.getAttribute('vidal:categories'); if (category === 'PRESCRIBABLE') { return (_a = entry.getElementsByTagName('summary')[0]) === null || _a === void 0 ? void 0 : _a.textContent; } return null; } function getEntryValue(entry, value) { return entry.getElementsByTagName(value)[0].textContent; } export function findMostRecentOffMarketDate(xml) { const domParser = new DOMParser(); const dom = domParser.parseFromString(xml, 'text/xml'); const entries = dom.querySelectorAll('feed > entry'); const dates = Array.from(entries).map((entry) => parseDateElement(entry, 'vidal:offMarketDate')); return max(dates); } function parseDateElement(entry, tagName) { const element = entry.getElementsByTagName(tagName)[0]; if (element === null || element === void 0 ? void 0 : element.textContent) { const dateFormat = element.getAttribute('format'); return parse(element.textContent, dateFormat, new Date()); } return undefined; } //# sourceMappingURL=karadoc.js.map