UNPKG

@kit-data-manager/pid-component

Version:

The PID-Component is a web component that can be used to evaluate and display FAIR Digital Objects, PIDs, ORCiDs, and possibly other identifiers in a user-friendly way. It is easily extensible to support other identifier types.

152 lines (151 loc) 5.74 kB
/*! * * Copyright 2024-2026 Karlsruhe Institute of Technology. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ import { renderers } from "./utils"; function getEffectiveRenderers(orderedRendererKeys) { if (!orderedRendererKeys || orderedRendererKeys.length === 0) { return renderers; } const result = []; for (const key of orderedRendererKeys) { const found = renderers.find(r => r.key === key); if (found) { result.push(found); } else { console.warn(`Parser: Unknown renderer key "${key}" in ordered renderer list, skipping.`); } } return result; } export class Parser { static async getEstimatedPriority(value) { for (let i = 0; i < renderers.length; i++) { const obj = new renderers[i].constructor(value); const quickResult = obj.quickCheck(); if (quickResult === true) { return i; } if (quickResult === undefined) { const hasMeaningful = await obj.hasMeaningfulInformation(); if (hasMeaningful) { return i; } } } return 0; } static getBestFitQuick(value, orderedRendererKeys) { const effective = getEffectiveRenderers(orderedRendererKeys); if (orderedRendererKeys && orderedRendererKeys.length > 0) { for (const entry of effective) { const obj = new entry.constructor(value); if (obj.quickCheck()) { return entry.key; } } return null; } let bestKey = null; for (let i = effective.length - 1; i >= 0; i--) { const entry = effective[i]; const obj = new entry.constructor(value); if (obj.quickCheck() && entry.key !== 'FallbackType') { bestKey = entry.key; } } return bestKey; } static async getBestFit(value, settings, orderedRendererKeys, fallbackToAll = true) { const effective = getEffectiveRenderers(orderedRendererKeys); const hasOrderedList = orderedRendererKeys && orderedRendererKeys.length > 0; if (hasOrderedList) { for (const entry of effective) { const obj = new entry.constructor(value); const quickResult = obj.quickCheck(); if (quickResult === true) { Parser.applySettings(obj, settings); await obj.init(); return obj; } if (quickResult === undefined || !quickResult) { if (await obj.hasMeaningfulInformation()) { Parser.applySettings(obj, settings); await obj.init(); return obj; } } } if (fallbackToAll) { return Parser.getBestFit(value, settings, undefined, false); } return null; } const candidates = []; for (let i = 0; i < effective.length; i++) { const obj = new effective[i].constructor(value); const quickResult = obj.quickCheck(); if (quickResult === true || quickResult === undefined) { candidates.push({ index: i, obj, uncertain: quickResult === undefined }); } } if (candidates.length === 0) { return null; } const results = await Promise.all(candidates.map(async ({ index, obj }) => { const meaningful = await obj.hasMeaningfulInformation(); const relevance = this.calculateRelevance(obj, index); return { obj, meaningful, relevance, index }; })); const validResults = results.filter(r => r.meaningful); if (validResults.length === 0) { return null; } validResults.sort((a, b) => { if (b.relevance !== a.relevance) { return b.relevance - a.relevance; } return a.index - b.index; }); const best = validResults[0].obj; Parser.applySettings(best, settings); await best.init(); return best; } static calculateRelevance(obj, index) { const priorityWeight = 1000; const itemWeight = 1; const actionWeight = 1; const priorityScore = (renderers.length - index) * priorityWeight; const itemScore = obj.items.length * itemWeight; const actionScore = obj.actions.length * actionWeight; return priorityScore + itemScore + actionScore; } static applySettings(obj, settings) { var _a; try { const settingsKey = obj.getSettingsKey(); const settingsValues = (_a = settings.find(v => v.type === settingsKey)) === null || _a === void 0 ? void 0 : _a.values; if (settingsValues) obj.settings = settingsValues; } catch (e) { console.warn('Error while adding settings to object:', e); } } } //# sourceMappingURL=Parser.js.map