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.

79 lines (78 loc) 3.02 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/utils"; function buildDetectionRegistry() { const priorities = new Map(); return renderers .filter(r => r.key !== 'FallbackType') .map(renderer => { var _a; priorities.set(renderer.key, (_a = renderer.priority) !== null && _a !== void 0 ? _a : 99); return { key: renderer.key, autoDiscoverableByDefault: renderer.autoDiscoverableByDefault, check: (value) => { const instance = new renderer.constructor(value); return instance.quickCheck(); }, }; }) .sort((a, b) => { var _a, _b; return ((_a = priorities.get(a.key)) !== null && _a !== void 0 ? _a : 99) - ((_b = priorities.get(b.key)) !== null && _b !== void 0 ? _b : 99); }); } let _detectionRegistry; function getDetectionRegistry() { if (!_detectionRegistry) { _detectionRegistry = buildDetectionRegistry(); } return _detectionRegistry; } export const detectionRegistry = []; getDetectionRegistry().forEach(entry => detectionRegistry.push(entry)); const LEADING_STRIP = /^[\s.,;:!?\-–—"'`´«»()[\]{}<>*/\\#@^~|]+/; const TRAILING_STRIP = /[\s.,;:!?\-–—"'`´«»()[\]{}<>*/\\#@^~|]+$/; export function sanitizeToken(token) { const leadingMatch = LEADING_STRIP.exec(token); const leadingStripped = leadingMatch ? leadingMatch[0].length : 0; let sanitized = token.substring(leadingStripped); const trailingMatch = TRAILING_STRIP.exec(sanitized); if (trailingMatch) { sanitized = sanitized.substring(0, sanitized.length - trailingMatch[0].length); } return { sanitized, leadingStripped }; } export function detectBestFit(value, orderedRendererKeys, fallbackToAll = true) { const registry = getDetectionRegistry(); if (orderedRendererKeys && orderedRendererKeys.length > 0) { for (const key of orderedRendererKeys) { const entry = registry.find(e => e.key === key); if (entry && entry.check(value)) { return entry.key; } } if (!fallbackToAll) { return null; } } for (const entry of registry) { if (entry.autoDiscoverableByDefault && entry.check(value)) { return entry.key; } } return null; } //# sourceMappingURL=detection-registry.js.map