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.

212 lines (211 loc) 8.69 kB
/*! * * Copyright 2024 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 * * http://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 { cachedFetch } from "../../utils/DataCache"; export class ORCIDInfo { constructor(orcid, ORCiDJSON, familyName, givenNames, employments, preferredLocale, biography, emails, keywords, researcherUrls, country) { this._orcid = orcid; this._familyName = familyName; this._givenNames = givenNames; this._employments = employments; this._preferredLocale = preferredLocale; this._biography = biography; this._emails = emails; this._keywords = keywords; this._researcherUrls = researcherUrls; this._country = country; this._ORCiDJSON = ORCiDJSON; } get orcid() { return this._orcid; } get familyName() { return this._familyName; } get givenNames() { return this._givenNames; } get ORCiDJSON() { return this._ORCiDJSON; } get employments() { return this._employments; } get preferredLocale() { return this._preferredLocale; } get biography() { return this._biography; } get emails() { return this._emails; } get keywords() { return this._keywords; } get researcherUrls() { return this._researcherUrls; } get country() { return this._country; } static isORCiD(text) { const regex = new RegExp('^(https://orcid.org/)?[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{3}[0-9X]$'); return regex.test(text); } static async getORCiDInfo(orcid) { if (!ORCIDInfo.isORCiD(orcid)) throw new Error('Invalid input'); if (orcid.match('^(https://orcid.org/)?[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{3}[0-9X]$') !== null) orcid = orcid.replace('https://orcid.org/', ''); const rawOrcidJSON = await cachedFetch(`https://pub.orcid.org/v3.0/${orcid}`, { headers: { Accept: 'application/json', }, }); let familyName = ''; let givenNames = []; try { familyName = rawOrcidJSON['person']['name']['family-name']['value']; } catch (e) { console.debug(e); } try { givenNames = rawOrcidJSON['person']['name']['given-names']['value']; } catch (e) { console.debug(e); } const affiliations = rawOrcidJSON['activities-summary']['employments']['affiliation-group']; const employments = []; try { for (let i = 0; i < affiliations.length; i++) { const employmentSummary = affiliations[i]['summaries'][0]['employment-summary']; const employment = new Employment(new Date(), null, '', ''); if (employmentSummary['start-date'] !== null) employment.startDate = new Date(employmentSummary['start-date']['year']['value'], employmentSummary['start-date']['month']['value'], employmentSummary['start-date']['day']['value']); if (employmentSummary['end-date'] !== null) employment.endDate = new Date(employmentSummary['end-date']['year']['value'], employmentSummary['end-date']['month']['value'], employmentSummary['end-date']['day']['value']); employment.organization = employmentSummary['organization']['name']; employment.department = employmentSummary['department-name']; employments.push(employment); } } catch (e) { console.debug(e); } const preferredLocale = rawOrcidJSON['preferences']['locale'] !== null ? rawOrcidJSON['preferences']['locale'] : undefined; const biography = rawOrcidJSON['person']['biography'] !== null ? rawOrcidJSON['person']['biography']['content'] : undefined; const emails = []; if (rawOrcidJSON['person']['emails']['email'] !== null) { for (const email of rawOrcidJSON['person']['emails']['email']) { emails.push({ email: email['email'], primary: email['primary'], verified: email['verified'], }); } } const keywords = []; if (rawOrcidJSON['person']['keywords']['keyword'] !== null) { for (const keyword of rawOrcidJSON['person']['keywords']['keyword']) { keywords.push({ content: keyword['content'], index: keyword['display-index'], }); } keywords.sort((a, b) => a.index - b.index); } const researcherUrls = []; if (rawOrcidJSON['person']['researcher-urls']['researcher-url'] !== null) { for (const researcherUrl of rawOrcidJSON['person']['researcher-urls']['researcher-url']) { researcherUrls.push({ url: researcherUrl['url']['value'], name: researcherUrl['url-name'], index: researcherUrl['display-index'], }); } researcherUrls.sort((a, b) => a.index - b.index); } const country = rawOrcidJSON['person']['addresses']['address'].length > 0 ? rawOrcidJSON['person']['addresses']['address'][0]['country']['value'] : undefined; return new ORCIDInfo(orcid, rawOrcidJSON, familyName, givenNames, employments, preferredLocale, biography, emails, keywords, researcherUrls, country); } static fromJSON(serialized) { const data = JSON.parse(serialized); const employments = data.employments.map(employment => Employment.fromJSON(employment)); return new ORCIDInfo(data.orcid, data.ORCiDJSON, data.familyName, data.givenNames, employments, data.preferredLocale, data.biography, data.emails, data.keywords, data.researcherUrls, data.country); } getAffiliationsAt(date) { const affiliations = []; for (const employment of this._employments) { if (employment.startDate <= date && employment.endDate === null) affiliations.push(employment); if (employment.startDate <= date && employment.endDate !== null && employment.endDate >= date) affiliations.push(employment); } return affiliations; } getAffiliationAsString(affiliation, showDepartment = true) { if (affiliation === undefined || affiliation.organization === null) return undefined; else { if (showDepartment && affiliation.department !== null) return `${affiliation.organization} [${affiliation.department}]`; else return affiliation.organization; } } toObject() { return { orcid: this._orcid, ORCiDJSON: this._ORCiDJSON, familyName: this._familyName, givenNames: this._givenNames, employments: this._employments.map(employment => JSON.stringify(employment.toObject())), preferredLocale: this._preferredLocale, biography: this._biography, emails: this._emails, keywords: this._keywords, researcherUrls: this._researcherUrls, country: this._country, }; } } class Employment { constructor(startDate, endDate, organization, department) { this.startDate = startDate; this.endDate = endDate; this.organization = organization; this.department = department; } static fromJSON(serialized) { const data = JSON.parse(serialized); const startDate = new Date(data.startDate); const endDate = data.endDate === null ? null : new Date(data.endDate); return new Employment(startDate, endDate, data.organization, data.department); } toObject() { return { startDate: this.startDate, endDate: this.endDate, organization: this.organization, department: this.department, }; } } //# sourceMappingURL=ORCIDInfo.js.map