From 9b3d127ef0d251597b260877358a2bc546f6b6f3 Mon Sep 17 00:00:00 2001 From: Team 1280 Programming Date: Mon, 19 Feb 2024 12:32:47 -0800 Subject: [PATCH] feat: add sorting of affiliations based on research output This commit adds a sorting functionality to the `getAffiliations` function. The function now calculates the number of documents associated with each affiliation and sorts the affiliations in descending order based on the number of documents. This allows for a more meaningful representation of affiliations based on their research output. --- src/app/affiliation/getAffiliations.ts | 65 +++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/src/app/affiliation/getAffiliations.ts b/src/app/affiliation/getAffiliations.ts index 32fdd68..3f3a16d 100644 --- a/src/app/affiliation/getAffiliations.ts +++ b/src/app/affiliation/getAffiliations.ts @@ -1,6 +1,57 @@ -import { authors } from '@/app/db/data' +import { Document, authors, documents } from '@/app/db/data' + +// Interface for the return output +interface DocumentWithSlug { + slug: string + doc: Document +} + +const findDocumentsByAuthor = (authorId: string): DocumentWithSlug[] => { + // Filter documents by author + return Object.entries(documents) + .filter(([_, doc]) => doc.manifest.authors.includes(authorId)) + .map(([slug, doc]) => ({ slug, doc })) +} + +const checkAffiliation = ( + affiliationStr: string[], + affiliationShort: string +) => { + for (const affiliation of affiliationStr) { + if (affiliation.split('@')[1] === affiliationShort) { + return true + } + } + return false +} + +const getNumberOfDocumentsByAffiliation = ( + affiliationShort: string +) => { + // Find authors with the given affiliation + const results: DocumentWithSlug[] = [] + for (const [key, value] of Object.entries(authors)) { + if ( + checkAffiliation(value.affiliation, affiliationShort) || + (value.formerAffiliations && + checkAffiliation(value.formerAffiliations, affiliationShort)) + ) { + // Check if document is already in results + const additions = findDocumentsByAuthor(key) + for (const addition of additions) { + const existing = results.find((doc) => doc.slug === addition.slug) + if (!existing) { + results.push(addition) + } + } + } + } + + return results.length +} export default function getAffiliations(): string[] { + // Get affiliations const affiliations: string[] = [] for (const [_key, value] of Object.entries(authors)) { for (const affiliation of value.affiliation) { @@ -10,5 +61,17 @@ export default function getAffiliations(): string[] { } } } + + // Sort affiliations by research output + const affiliationDocs: { [key: string]: number } = {} + for (let i = 0; i < affiliations.length; i++) { + affiliationDocs[affiliations[i]] = getNumberOfDocumentsByAffiliation( + affiliations[i] + ) + } + affiliations.sort( + (a, b) => affiliationDocs[b] - affiliationDocs[a] + ) + return affiliations }