diff --git a/lib/cff/formatters/formatter.rb b/lib/cff/formatters/formatter.rb index 02731d4..b557d06 100644 --- a/lib/cff/formatters/formatter.rb +++ b/lib/cff/formatters/formatter.rb @@ -40,8 +40,14 @@ def self.select_and_check_model(model, preferred_citation) model.authors.empty? || model.title.empty? ? nil : model end + def self.initial_from_name_part(part) + return part[0].capitalize unless part.include?('-') + + part.split('-').filter_map { |sub| sub[0]&.capitalize }.join('.-') + end + def self.initials(name) - name.split.map { |part| part[0].capitalize }.join('. ') + name.split.map { |part| initial_from_name_part(part) }.join('. ') end def self.note_from_model(model) diff --git a/test/files/formatted/hyphenated-names.apa b/test/files/formatted/hyphenated-names.apa new file mode 100644 index 0000000..4a589ed --- /dev/null +++ b/test/files/formatted/hyphenated-names.apa @@ -0,0 +1 @@ +Kling, M.-U., Picard, J.-L., & Smith, J. (2021). Hyphenated names test [Computer software] diff --git a/test/files/formatted/hyphenated-names.bibtex b/test/files/formatted/hyphenated-names.bibtex new file mode 100644 index 0000000..12a767a --- /dev/null +++ b/test/files/formatted/hyphenated-names.bibtex @@ -0,0 +1,6 @@ +@software{Kling_Hyphenated_names_test_2021, +author = {Kling, Marc-Uwe and Picard, Jean-Luc and Smith, John}, +month = sep, +title = {{Hyphenated names test}}, +year = {2021} +} diff --git a/test/files/formatter/hyphenated-names.cff b/test/files/formatter/hyphenated-names.cff new file mode 100644 index 0000000..5be3d82 --- /dev/null +++ b/test/files/formatter/hyphenated-names.cff @@ -0,0 +1,11 @@ +cff-version: 1.2.0 +message: If you use this software, please cite it as below. +authors: + - family-names: Kling + given-names: Marc-Uwe + - family-names: Picard + given-names: Jean-Luc + - family-names: Smith + given-names: John +title: Hyphenated names test +date-released: 2021-09-21 diff --git a/test/formatters/formatter_test.rb b/test/formatters/formatter_test.rb index 63c6ba0..44aed9b 100644 --- a/test/formatters/formatter_test.rb +++ b/test/formatters/formatter_test.rb @@ -20,6 +20,21 @@ require 'cff/index' class CFFFormatterTest < Minitest::Test + def test_initial_from_name_part + assert_equal 'A', CFF::Formatters::Formatter.initial_from_name_part('Arfon') + assert_equal 'M', CFF::Formatters::Formatter.initial_from_name_part('M.') + assert_equal 'M.-U', CFF::Formatters::Formatter.initial_from_name_part('Marc-Uwe') + assert_equal 'J.-L', CFF::Formatters::Formatter.initial_from_name_part('Jean-Luc') + assert_equal 'A.-B.-C', CFF::Formatters::Formatter.initial_from_name_part('A-B-C') + end + + def test_initials + assert_equal 'A. M', CFF::Formatters::Formatter.initials('Arfon M.') + assert_equal 'M.-U', CFF::Formatters::Formatter.initials('Marc-Uwe') + assert_equal 'J.-L. P', CFF::Formatters::Formatter.initials('Jean-Luc Picard') + assert_equal 'A. B. C', CFF::Formatters::Formatter.initials('Arfon B. C.') + end + def test_month_and_year_from_date [nil, '', ' ', 'nil'].each do |date| assert_equal(['', ''], CFF::Formatters::Formatter.month_and_year_from_date(date))