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.
This commit is contained in:
Team 1280 Programming 2024-02-19 12:32:47 -08:00 committed by Team 1280 Programming Laptop
parent d651fc58e0
commit 9b3d127ef0

View file

@ -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[] { export default function getAffiliations(): string[] {
// Get affiliations
const affiliations: string[] = [] const affiliations: string[] = []
for (const [_key, value] of Object.entries(authors)) { for (const [_key, value] of Object.entries(authors)) {
for (const affiliation of value.affiliation) { 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 return affiliations
} }