diff --git a/locales/en.yml b/locales/en.yml index 3c613875..f20cb918 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -3686,6 +3686,7 @@ tools: placeholder-e-g-https-example-com: e.g. https://example.com label-citation-style: Citation Style title-generated-citation: Generated Citation + label-vancouver: Vancouver cookies-parser: title: Cookies Parser description: Parse Cookie HTTP Header diff --git a/src/tools/citation-generator/citation-generator.service.test.ts b/src/tools/citation-generator/citation-generator.service.test.ts new file mode 100644 index 00000000..5d4095ab --- /dev/null +++ b/src/tools/citation-generator/citation-generator.service.test.ts @@ -0,0 +1,86 @@ +import { describe, expect, it } from 'vitest'; + +import { formatVancouverCitation } from './citation-generator.service'; // adjust path as needed + +describe('formatVancouverCitation', () => { + it('formats a single author with initials', () => { + const entry = { + authors: [{ first: 'John', last: 'Doe' }], + year: '2021', + title: 'Sample Title', + publisher: 'Publisher', + url: 'https://example.com', + }; + expect(formatVancouverCitation(entry)) + .toBe('Doe J. Sample Title. Publisher; 2021. Available from: https://example.com'); + }); + + it('formats multiple authors correctly', () => { + const entry = { + authors: [ + { first: 'John', last: 'Doe' }, + { first: 'Jane', last: 'Smith' }, + ], + year: '2020', + title: 'Multi Author Work', + publisher: 'TechPress', + }; + expect(formatVancouverCitation(entry)) + .toBe('Doe J, Smith J. Multi Author Work. TechPress; 2020.'); + }); + + it('uses et al. when more than 6 authors', () => { + const entry = { + authors: [ + { first: 'A', last: 'One' }, + { first: 'B', last: 'Two' }, + { first: 'C', last: 'Three' }, + { first: 'D', last: 'Four' }, + { first: 'E', last: 'Five' }, + { first: 'F', last: 'Six' }, + { first: 'G', last: 'Seven' }, + ], + year: '2019', + title: 'Big Team Paper', + publisher: 'SciencePub', + }; + expect(formatVancouverCitation(entry)) + .toBe('One A, Two B, Three C, Four D, Five E, Six F, et al. Big Team Paper. SciencePub; 2019.'); + }); + + it('handles multiple initials in first name', () => { + const entry = { + authors: [{ first: 'John Michael', last: 'Doe' }], + title: 'Initials Test', + }; + expect(formatVancouverCitation(entry)) + .toBe('Doe JM. Initials Test.'); + }); + + it('falls back to Anonymous if no authors', () => { + const entry = { + title: 'No Author Work', + year: '2022', + }; + expect(formatVancouverCitation(entry)) + .toBe('Anonymous. No Author Work. 2022.'); + }); + + it('handles missing title gracefully', () => { + const entry = { + authors: [{ first: 'Jane', last: 'Smith' }], + year: '2023', + }; + expect(formatVancouverCitation(entry)) + .toBe('Smith J. [No title]. 2023.'); + }); + + it('handles missing publisher and year', () => { + const entry = { + authors: [{ first: 'Jane', last: 'Smith' }], + title: 'Untimed Work', + }; + expect(formatVancouverCitation(entry)) + .toBe('Smith J. Untimed Work.'); + }); +}); diff --git a/src/tools/citation-generator/citation-generator.service.ts b/src/tools/citation-generator/citation-generator.service.ts new file mode 100644 index 00000000..fbeecfa2 --- /dev/null +++ b/src/tools/citation-generator/citation-generator.service.ts @@ -0,0 +1,47 @@ +interface Author { first?: string; last?: string } +interface CitationInput { + authors?: Author[] + year?: string + title?: string + publisher?: string + url?: string +} + +export function formatVancouverCitation(entry: CitationInput): string { + const formatAuthor = (author: Author): string => { + if (!author.last) { + return ''; + } + const initials = (author.first || '') + .split(/\s+/) + .map(name => name.charAt(0).toUpperCase()) + .join(''); + return `${author.last} ${initials}`; + }; + + const authors = Array.isArray(entry.authors) ? entry.authors.map(formatAuthor).filter(Boolean) : []; + let authorStr = 'Anonymous'; + if (authors.length > 0) { + authorStr = authors.length > 6 + ? `${authors.slice(0, 6).join(', ')}, et al` + : authors.join(', '); + } + + const title = entry.title?.trim() || '[No title]'; + const publisher = entry.publisher?.trim(); + const year = entry.year?.trim(); + const url = entry.url?.trim(); + + let citation = `${authorStr}. ${title}.`; + if (publisher) { + citation += ` ${publisher};`; + } + if (year) { + citation += ` ${year}.`; + } + if (url) { + citation += ` Available from: ${url}`; + } + + return citation; +} diff --git a/src/tools/citation-generator/citation-generator.vue b/src/tools/citation-generator/citation-generator.vue index d842770d..7365c099 100644 --- a/src/tools/citation-generator/citation-generator.vue +++ b/src/tools/citation-generator/citation-generator.vue @@ -2,6 +2,7 @@ import { useI18n } from 'vue-i18n'; import { ref } from 'vue'; import { APA, MLA } from 'citation-formatter'; +import { formatVancouverCitation } from './citation-generator.service'; const { t } = useI18n(); @@ -25,14 +26,21 @@ const formattedCitation = computed(() => { || !citationData.value.year || !authors.length ) { return '### Please fill Title, Publisher, Year and authors'; } + + const citationInData = { + authors, + title: citationData.value.title, + publisher: citationData.value.publisher, + year: citationData.value.year.toString(), + url: citationData.value.url, + }; + + if (style.value === 'vancouver') { + return formatVancouverCitation(citationInData); + } return (style.value === 'apa' ? APA : MLA)([ - { - authors, - title: citationData.value.title, - publisher: citationData.value.publisher, - year: citationData.value.year, - url: citationData.value.url, - }]); + citationInData, + ]); } catch (e: any) { return e.toString(); @@ -72,6 +80,7 @@ const formattedCitation = computed(() => { :options="[ { label: t('tools.citation-generator.texts.label-apa'), value: 'apa' }, { label: t('tools.citation-generator.texts.label-mla'), value: 'mla' }, + { label: t('tools.citation-generator.texts.label-vancouver'), value: 'vancouver' }, ]" />