@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.
142 lines (141 loc) • 6.28 kB
JavaScript
/*!
*
* 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 var CitationStyle;
(function (CitationStyle) {
CitationStyle["APA"] = "APA";
CitationStyle["CHICAGO"] = "Chicago";
CitationStyle["IEEE"] = "IEEE";
CitationStyle["HARVARD"] = "Harvard";
CitationStyle["ANGLIA_RUSKIN"] = "Anglia Ruskin";
})(CitationStyle || (CitationStyle = {}));
export function formatCitationPreview(title, creators, year, style = CitationStyle.APA) {
if (!title || creators.length === 0) {
return { citation: title || 'Untitled', tooltip: title || 'Untitled' };
}
const firstAuthor = creators[0];
const authorCount = creators.length;
const truncateCitation = false;
switch (style) {
case CitationStyle.APA:
return {
citation: formatAPA(firstAuthor, authorCount, title, year, truncateCitation),
tooltip: formatAPA(firstAuthor, authorCount, title, year, false),
};
case CitationStyle.CHICAGO:
return {
citation: formatChicago(firstAuthor, authorCount, title, year, truncateCitation),
tooltip: formatChicago(firstAuthor, authorCount, title, year, false),
};
case CitationStyle.IEEE:
return {
citation: formatIEEE(firstAuthor, authorCount, title, year, truncateCitation),
tooltip: formatIEEE(firstAuthor, authorCount, title, year, false),
};
case CitationStyle.HARVARD:
return {
citation: formatHarvard(firstAuthor, authorCount, title, year, truncateCitation),
tooltip: formatHarvard(firstAuthor, authorCount, title, year, false),
};
case CitationStyle.ANGLIA_RUSKIN:
return {
citation: formatAngliaRuskin(firstAuthor, authorCount, title, year, truncateCitation),
tooltip: formatAngliaRuskin(firstAuthor, authorCount, title, year, false),
};
default:
return {
citation: formatAPA(firstAuthor, authorCount, title, year, truncateCitation),
tooltip: formatAPA(firstAuthor, authorCount, title, year, false),
};
}
}
function formatAPA(author, count, title, year, truncate) {
const authorName = author.familyName || author.name.split(' ').pop() || author.name;
const etAl = count > 1 ? ' et al.' : '';
const yearPart = year ? ` (${year.split('-')[0]})` : '';
const titleTrunc = truncateTitle(title, 60);
return `${authorName}${etAl}${yearPart}. ${truncate ? titleTrunc : title}`;
}
function formatChicago(author, count, title, year, truncate) {
const authorName = author.familyName || author.name.split(' ').pop() || author.name;
const firstName = author.givenName || author.name.split(' ')[0] || '';
const etAl = count > 1 ? ' et al.' : '';
const yearPart = year ? ` ${year.split('-')[0]}.` : '';
const titleTrunc = truncateTitle(title, 60);
return `${authorName}, ${firstName}${etAl}.${yearPart} "${truncate ? titleTrunc : title}"`;
}
function formatIEEE(author, count, title, year, truncate) {
const initial = author.givenName ? author.givenName.charAt(0) + '.' : '';
const authorName = author.familyName || author.name.split(' ').pop() || author.name;
const etAl = count > 1 ? ' et al.' : '';
const titleTrunc = truncateTitle(title, 60);
const yearPart = year ? `, ${year.split('-')[0]}` : '';
return `${initial} ${authorName}${etAl}, "${truncate ? titleTrunc : title}"${yearPart}`;
}
function formatHarvard(author, count, title, year, truncate) {
const authorName = author.familyName || author.name.split(' ').pop() || author.name;
const initials = author.givenName
? author.givenName.split(' ').map(n => n.charAt(0) + '.').join('')
: '';
const etAl = count > 1 ? ' et al.' : '';
const yearPart = year ? `, ${year.split('-')[0]}` : '';
const titleTrunc = truncateTitle(title, 60);
return `${authorName}, ${initials}${etAl}${yearPart}. ${truncate ? titleTrunc : title}`;
}
function formatAngliaRuskin(author, count, title, year, truncate) {
const authorName = (author.familyName || author.name.split(' ').pop() || author.name).toUpperCase();
const etAl = count > 1 ? ' ET AL.' : '';
const yearPart = year ? ` ${year.split('-')[0]}.` : '';
const titleTrunc = truncateTitle(title, 60);
return `${authorName}${etAl}${yearPart} ${truncate ? titleTrunc : title}`;
}
function truncateTitle(title, maxLength) {
if (title.length <= maxLength)
return title;
const truncated = title.substring(0, maxLength);
const lastSpace = truncated.lastIndexOf(' ');
if (lastSpace > maxLength * 0.7) {
return truncated.substring(0, lastSpace) + '...';
}
return truncated + '...';
}
export function getDefaultCitationStyle() {
return CitationStyle.APA;
}
export function getCitationStyleFromSettings(settings) {
if (!settings)
return getDefaultCitationStyle();
const styleSetting = settings.find(s => s.name === 'citationStyle');
if (!styleSetting)
return getDefaultCitationStyle();
const styleValue = String(styleSetting.value).toUpperCase();
switch (styleValue) {
case 'APA':
return CitationStyle.APA;
case 'CHICAGO':
return CitationStyle.CHICAGO;
case 'IEEE':
return CitationStyle.IEEE;
case 'HARVARD':
return CitationStyle.HARVARD;
case 'ANGLIA_RUSKIN':
return CitationStyle.ANGLIA_RUSKIN;
default:
return getDefaultCitationStyle();
}
}
//# sourceMappingURL=CitationStyles.js.map