Skip to content

Commit adba5e6

Browse files
committed
feat(download): Expand CSV download functionality
Updated to include additional fields such as TM Number, origin details, and repository information. Improved the data extraction logic for inscriptions, ensuring proper handling of optional fields and enhancing the overall structure of the downloaded CSV file.
1 parent 6de0584 commit adba5e6

File tree

1 file changed

+42
-25
lines changed

1 file changed

+42
-25
lines changed

frontend/src/lib/utils/download.js

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,79 @@
1-
import { base } from '$app/paths';
21
import JSZip from 'jszip';
2+
import { base } from '$app/paths';
33

44
/**
55
* Creates and downloads a CSV file from inscription data
66
*
77
* @param {string} summary - The summary of the search
8-
* @param {Array<{ file: string; title: string; notBefore: number; notAfter: number; places: Array<{ offset: string; _: string; type: string; }>; status: { _: string; }; type: { _: string; }; objectType: { _: string; }; language: { _: string; }; settlement: { _: string; }; }>} inscriptions - Array of inscription objects
8+
* @param {Array<{ file: string; tmNumber: string; title: string; notBefore: number; notAfter: number; places: Array<{ offset: string; _: string; type: string; }>; status: { _: string; }; type: { _: string; }; objectType: { _: string; }; language: { _: string; }; settlement: { _: string; }; }>} inscriptions - Array of inscription objects
99
*/
1010
export async function downloadInscriptionsCSV(summary, inscriptions) {
11-
const headers =
12-
'ID,Title,"Not before","No earlier than","No later than",Place,Status,Type,Object type,Language,Settlement\n';
11+
const headers = [
12+
'ID',
13+
'TM Number',
14+
'Date notBefore',
15+
'Date notAfter',
16+
'Origin (ancient)',
17+
'Origin (modern)',
18+
'Origin latitude',
19+
'Origin longitude',
20+
'Provenance latitude',
21+
'Provenance longitude',
22+
'Material',
23+
'Object type',
24+
'Type',
25+
'Execution type 1',
26+
'Execution type 2',
27+
'Language',
28+
'Repository name',
29+
'Inventory number'
30+
].join(',');
1331

1432
const rows = inscriptions
1533
.map((inscription) => {
1634
return [
1735
inscription.file,
18-
`"${inscription.title}"`,
19-
inscription.notBefore,
20-
inscription.notAfter,
21-
`"${getInscriptionPlace(inscription)}"`,
22-
inscription.status._,
23-
`"${getInscriptionType(inscription)}"`,
36+
`"${inscription?.tmNumber || ''}"`,
37+
inscription?.notBefore || '',
38+
inscription?.notAfter || '',
39+
`"${getInscriptionPlace(inscription, 'ancient')}"`,
40+
`"${getInscriptionPlace(inscription, 'modern')}"`,
41+
inscription.geo?.[0]?.[0] || '',
42+
inscription.geo?.[0]?.[1] || '',
43+
inscription.provenanceGeo?.[0] || '',
44+
inscription.provenanceGeo?.[1] || '',
45+
`${inscription.material?.at(-1).replaceAll(':::', '.') || ''}`,
2446
`"${getInscriptionObjectType(inscription)}"`,
47+
`"${getInscriptionType(inscription)}"`,
48+
`"${inscription.technique?.at(-1)?.replaceAll(':::', '.') || ''}"`,
49+
`"${inscription.pigment?.at(-1)?.replaceAll(':::', '.') || ''}"`,
2550
`"${getInscriptionLanguage(inscription)}"`,
26-
`"${getInscriptionSettlement(inscription)}"`
51+
`"${inscription.repository.at(-1)?.split(':::').at(-1) || ''}"`,
52+
`"${inscription.idno?._ || ''}"`
2753
].join(',');
2854
})
2955
.join('\n');
3056

3157
const a = document.createElement('a');
32-
a.href = `data:text/csv;charset=utf-8,${encodeURIComponent(`"${summary}"\n${headers}${rows}`)}`;
58+
a.href = `data:text/csv;charset=utf-8,${encodeURIComponent(`"${summary}"\n${headers}\n${rows}`)}`;
3359
a.download = 'inscriptions.csv';
3460
a.click();
3561
a.remove();
3662
}
3763

3864
/**
3965
* @param {any} inscription
66+
* @param {string} type
4067
*/
41-
function getInscriptionPlace(inscription) {
42-
return inscription.places
43-
.map((/** @type {{ offset: string; _: string; type: string; }} */ place) => {
44-
let placeString = '';
45-
if (place.offset) {
46-
placeString += `${place.offset} `;
47-
}
48-
placeString += `${place._}`;
49-
placeString += `(${place.type})`;
50-
return placeString;
51-
})
52-
.join(', ');
68+
function getInscriptionPlace(inscription, type) {
69+
return inscription?.places?.find((place) => place.type === type)?._ || '';
5370
}
5471

5572
/**
5673
* @param {{ type: { _: string; }; }} inscription
5774
*/
5875
function getInscriptionType(inscription) {
59-
return inscription.type?._ || 'N/A';
76+
return inscription.type?._?.trim() || 'N/A';
6077
}
6178

6279
/**

0 commit comments

Comments
 (0)