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.

122 lines (121 loc) 4.28 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. * */ export function sanitizeData(data) { if (Array.isArray(data)) { return data.map(sanitizeData); } if (data !== null && typeof data === 'object') { const cleaned = {}; Object.entries(data).forEach(([key, value]) => { if (!key.startsWith('$')) { cleaned[key] = sanitizeData(value); } }); return cleaned; } return data; } export function expandNodeRecursive(data, path, expandedNodes) { if (data !== null && typeof data === 'object') { expandedNodes.add(path); Object.entries(data).forEach(([key, value]) => { const newPath = path ? `${path}.${key}` : key; expandNodeRecursive(value, newPath, expandedNodes); }); return new Set(expandedNodes); } return expandedNodes; } export function getNodeType(value) { if (value !== null && typeof value === 'object') { return Array.isArray(value) ? 'array' : 'object'; } return 'primitive'; } export function getNodeInfo(data, key, path) { const isExpandable = typeof data === 'object' && data !== null; const isArray = Array.isArray(data); let itemCount; if (isExpandable) { const entries = Object.entries(data); itemCount = entries.length; } return { key, value: data, path, isExpandable, isArray, itemCount, }; } export function formatCodeLine(line, isDarkMode) { const stringClass = isDarkMode ? 'text-green-400' : 'text-green-600'; const booleanClass = isDarkMode ? 'text-purple-400' : 'text-purple-600'; const nullClass = isDarkMode ? 'text-gray-400' : 'text-gray-500'; const numberClass = isDarkMode ? 'text-blue-400' : 'text-blue-600'; return line .replace(/(".+?")(: )?/g, `<span class="${stringClass}">$1</span>$2`) .replace(/: (true|false)([,}\]\s])/g, `: <span class="${booleanClass}">$1</span>$2`) .replace(/: (null)([,}\]\s])/g, `: <span class="${nullClass}">$1</span>$2`) .replace(/: ([0-9]+(\.[0-9]+)?)([,}\]\s])/g, `: <span class="${numberClass}">$1</span>$3`); } export function getItemCountText(count) { return `${count} ${count === 1 ? 'item' : 'items'}`; } export function getValueDisplay(value) { if (value === null) { return { display: 'null', type: 'null' }; } const type = typeof value; if (type === 'string') { return { display: JSON.stringify(value), type: 'string' }; } if (type === 'number') { return { display: JSON.stringify(value), type: 'number' }; } if (type === 'boolean') { return { display: JSON.stringify(value), type: 'boolean' }; } if (type === 'object' || type === 'symbol' || typeof value === 'function') { return { display: JSON.stringify(value), type: 'object' }; } return { display: String(value), type }; } export function parseJsonSafe(input) { if (input === null || input === undefined) { return { data: null, error: 'Invalid data format' }; } if (typeof input === 'object') { return { data: input, error: null }; } if (typeof input !== 'string') { return { data: null, error: 'Invalid data format' }; } try { const parsed = JSON.parse(input); return { data: parsed, error: null }; } catch (err) { return { data: null, error: err instanceof Error ? err.message : 'Invalid JSON' }; } } export const DEFAULT_MAX_HEIGHT = 500; export const DEFAULT_LINE_HEIGHT = 24; export const DEFAULT_VIEW_MODE = 'tree'; //# sourceMappingURL=jsonViewerUtils.js.map