|
| 1 | +import Papa from "papaparse"; |
| 2 | +import { z } from "zod"; |
| 3 | + |
| 4 | +const fileSchema = z.array( |
| 5 | + z.object({ |
| 6 | + Authors: z.string(), |
| 7 | + "Author Keywords": z.string(), |
| 8 | + "Index Keywords": z.string(), |
| 9 | + References: z.string(), |
| 10 | + }), |
| 11 | +); |
| 12 | + |
| 13 | +type CsvFileType = z.infer<typeof fileSchema>; |
| 14 | + |
| 15 | +const readCsvText = (text: string): CsvFileType => { |
| 16 | + const { data } = Papa.parse(text, { header: true, skipEmptyLines: true }); |
| 17 | + return fileSchema.parse(data); |
| 18 | +}; |
| 19 | + |
| 20 | +const looksLikeScopusCsv = (text: string): boolean => { |
| 21 | + try { |
| 22 | + const data = readCsvText(text); |
| 23 | + return data.length > 0; |
| 24 | + } catch { |
| 25 | + return false; |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +const keywords = (text: string): string[] => { |
| 30 | + const data = readCsvText(text); |
| 31 | + const keywords = data.flatMap((item) => [ |
| 32 | + ...item["Author Keywords"].split(";"), |
| 33 | + ...item["Index Keywords"].split(";"), |
| 34 | + ]); |
| 35 | + return Array.from(new Set(keywords)); |
| 36 | +}; |
| 37 | + |
| 38 | +const countArticles = (text: string): number => { |
| 39 | + const data = readCsvText(text); |
| 40 | + return data.length; |
| 41 | +}; |
| 42 | + |
| 43 | +const countReferences = (text: string): number => { |
| 44 | + const data = readCsvText(text); |
| 45 | + const references = data.flatMap((item) => item["References"].split("; ")); |
| 46 | + return references.length; |
| 47 | +}; |
| 48 | + |
| 49 | +export { looksLikeScopusCsv, keywords, countArticles, countReferences }; |
0 commit comments