|
| 1 | +/** |
| 2 | + * Export Utilities |
| 3 | + * |
| 4 | + * Provides functionality to export search results to CSV and BibTeX formats. |
| 5 | + */ |
| 6 | + |
| 7 | +import type { AutocompleteResult } from '@bibgraph/types'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Convert search results to CSV format |
| 11 | + */ |
| 12 | +export const exportToCSV = (results: AutocompleteResult[], filename?: string): void => { |
| 13 | + if (results.length === 0) return; |
| 14 | + |
| 15 | + // CSV headers |
| 16 | + const headers = [ |
| 17 | + 'ID', |
| 18 | + 'Display Name', |
| 19 | + 'Entity Type', |
| 20 | + 'Citation Count', |
| 21 | + 'Works Count', |
| 22 | + 'Hint', |
| 23 | + 'URL', |
| 24 | + ]; |
| 25 | + |
| 26 | + // Convert results to CSV rows |
| 27 | + const rows = results.map((result) => [ |
| 28 | + result.id, |
| 29 | + `"${result.display_name.replace(/"/g, '""')}"`, // Escape quotes |
| 30 | + result.entity_type, |
| 31 | + result.cited_by_count ?? 0, |
| 32 | + result.works_count ?? 0, |
| 33 | + result.hint ?? '', |
| 34 | + result.id, |
| 35 | + ]); |
| 36 | + |
| 37 | + // Combine headers and rows |
| 38 | + const csvContent = [headers.join(','), ...rows.map((row) => row.join(','))].join('\n'); |
| 39 | + |
| 40 | + // Create blob and download |
| 41 | + const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' }); |
| 42 | + const link = document.createElement('a'); |
| 43 | + const url = URL.createObjectURL(blob); |
| 44 | + |
| 45 | + link.setAttribute('href', url); |
| 46 | + link.setAttribute('download', filename || `search-results-${Date.now()}.csv`); |
| 47 | + link.style.visibility = 'hidden'; |
| 48 | + document.body.appendChild(link); |
| 49 | + link.click(); |
| 50 | + document.body.removeChild(link); |
| 51 | +}; |
| 52 | + |
| 53 | +/** |
| 54 | + * Convert a work result to BibTeX format |
| 55 | + */ |
| 56 | +const workToBibTeX = (result: AutocompleteResult): string => { |
| 57 | + const id = result.id.replace('https://openalex.org/', '').toUpperCase(); |
| 58 | + const bibKey = `work${id}`.replace(/[^a-zA-Z0-9]/g, ''); |
| 59 | + |
| 60 | + let bibtex = `@misc{${bibKey},\n`; |
| 61 | + bibtex += ` title = {${result.display_name}},\n`; |
| 62 | + |
| 63 | + if (result.hint) { |
| 64 | + bibtex += ` howpublished = {${result.hint}},\n`; |
| 65 | + } |
| 66 | + |
| 67 | + if (result.cited_by_count) { |
| 68 | + bibtex += ` citations = {${result.cited_by_count}},\n`; |
| 69 | + } |
| 70 | + |
| 71 | + bibtex += ` url = {${result.id}},\n`; |
| 72 | + bibtex += `}\n`; |
| 73 | + |
| 74 | + return bibtex; |
| 75 | +}; |
| 76 | + |
| 77 | +/** |
| 78 | + * Convert author result to BibTeX format |
| 79 | + */ |
| 80 | +const authorToBibTeX = (result: AutocompleteResult): string => { |
| 81 | + const id = result.id.replace('https://openalex.org/', '').toUpperCase(); |
| 82 | + const bibKey = `${result.display_name.split(' ').pop() || 'author'}${id}`.replace(/[^a-zA-Z0-9]/g, ''); |
| 83 | + |
| 84 | + let bibtex = `@misc{${bibKey},\n`; |
| 85 | + bibtex += ` author = {${result.display_name}},\n`; |
| 86 | + |
| 87 | + if (result.cited_by_count) { |
| 88 | + bibtex += ` citations = {${result.cited_by_count}},\n`; |
| 89 | + } |
| 90 | + |
| 91 | + if (result.works_count) { |
| 92 | + bibtex += ` works = {${result.works_count}},\n`; |
| 93 | + } |
| 94 | + |
| 95 | + bibtex += ` url = {${result.id},\n`; |
| 96 | + bibtex += `}\n`; |
| 97 | + |
| 98 | + return bibtex; |
| 99 | +}; |
| 100 | + |
| 101 | +/** |
| 102 | + * Convert search results to BibTeX format |
| 103 | + */ |
| 104 | +export const exportToBibTeX = (results: AutocompleteResult[], filename?: string): void => { |
| 105 | + if (results.length === 0) return; |
| 106 | + |
| 107 | + // Convert results to BibTeX entries |
| 108 | + const bibtexEntries = results.map((result) => { |
| 109 | + if (result.entity_type === 'work') { |
| 110 | + return workToBibTeX(result); |
| 111 | + } |
| 112 | + return authorToBibTeX(result); |
| 113 | + }); |
| 114 | + |
| 115 | + const bibtexContent = bibtexEntries.join('\n'); |
| 116 | + |
| 117 | + // Create blob and download |
| 118 | + const blob = new Blob([bibtexContent], { type: 'text/plain;charset=utf-8;' }); |
| 119 | + const link = document.createElement('a'); |
| 120 | + const url = URL.createObjectURL(blob); |
| 121 | + |
| 122 | + link.setAttribute('href', url); |
| 123 | + link.setAttribute('download', filename || `search-results-${Date.now()}.bib`); |
| 124 | + link.style.visibility = 'hidden'; |
| 125 | + document.body.appendChild(link); |
| 126 | + link.click(); |
| 127 | + document.body.removeChild(link); |
| 128 | +}; |
| 129 | + |
| 130 | +/** |
| 131 | + * Get export filename based on current search query |
| 132 | + */ |
| 133 | +export const getExportFilename = (query: string, format: 'csv' | 'bib'): string => { |
| 134 | + const sanitizedQuery = query.trim().replace(/[^a-zA-Z0-9]/g, '-').toLowerCase(); |
| 135 | + const timestamp = new Date().toISOString().slice(0, 10); |
| 136 | + return sanitizedQuery |
| 137 | + ? `${sanitizedQuery}-results-${timestamp}.${format}` |
| 138 | + : `search-results-${timestamp}.${format}`; |
| 139 | +}; |
0 commit comments