Skip to content

Commit 5dd0a83

Browse files
Copilothainesr
andcommitted
Fix APA style output for hyphenated first names.
Co-authored-by: hainesr <143558+hainesr@users.noreply.github.com>
1 parent 499f9bd commit 5dd0a83

5 files changed

Lines changed: 42 additions & 3 deletions

File tree

lib/cff/formatters/formatter.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
# Copyright (c) 2018-2023 The Ruby Citation File Format Developers.
3+
# Copyright (c) 2018-2026 The Ruby Citation File Format Developers.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -40,8 +40,14 @@ def self.select_and_check_model(model, preferred_citation)
4040
model.authors.empty? || model.title.empty? ? nil : model
4141
end
4242

43+
def self.initial_from_name_part(part)
44+
return part[0].capitalize unless part.include?('-')
45+
46+
part.split('-').filter_map { |sub| sub[0]&.capitalize }.join('.-')
47+
end
48+
4349
def self.initials(name)
44-
name.split.map { |part| part[0].capitalize }.join('. ')
50+
name.split.map { |part| initial_from_name_part(part) }.join('. ')
4551
end
4652

4753
def self.note_from_model(model)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Kling, M.-U., Picard, J.-L., & Smith, J. (2021). Hyphenated names test [Computer software]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@software{Kling_Hyphenated_names_test_2021,
2+
author = {Kling, Marc-Uwe and Picard, Jean-Luc and Smith, John},
3+
month = sep,
4+
title = {{Hyphenated names test}},
5+
year = {2021}
6+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cff-version: 1.2.0
2+
message: If you use this software, please cite it as below.
3+
authors:
4+
- family-names: Kling
5+
given-names: Marc-Uwe
6+
- family-names: Picard
7+
given-names: Jean-Luc
8+
- family-names: Smith
9+
given-names: John
10+
title: Hyphenated names test
11+
date-released: 2021-09-21

test/formatters/formatter_test.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
# Copyright (c) 2018-2023 The Ruby Citation File Format Developers.
3+
# Copyright (c) 2018-2026 The Ruby Citation File Format Developers.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -20,6 +20,21 @@
2020
require 'cff/index'
2121

2222
class CFFFormatterTest < Minitest::Test
23+
def test_initial_from_name_part
24+
assert_equal('A', CFF::Formatters::Formatter.initial_from_name_part('Arfon'))
25+
assert_equal('M', CFF::Formatters::Formatter.initial_from_name_part('M.'))
26+
assert_equal('M.-U', CFF::Formatters::Formatter.initial_from_name_part('Marc-Uwe'))
27+
assert_equal('J.-L', CFF::Formatters::Formatter.initial_from_name_part('Jean-Luc'))
28+
assert_equal('A.-B.-C', CFF::Formatters::Formatter.initial_from_name_part('A-B-C'))
29+
end
30+
31+
def test_initials
32+
assert_equal('A. S', CFF::Formatters::Formatter.initials('Arfon Smith'))
33+
assert_equal('M.-U', CFF::Formatters::Formatter.initials('Marc-Uwe'))
34+
assert_equal('J.-L. P', CFF::Formatters::Formatter.initials('Jean-Luc Picard'))
35+
assert_equal('A. B. C', CFF::Formatters::Formatter.initials('Arfon B. C.'))
36+
end
37+
2338
def test_month_and_year_from_date
2439
[nil, '', ' ', 'nil'].each do |date|
2540
assert_equal(['', ''], CFF::Formatters::Formatter.month_and_year_from_date(date))

0 commit comments

Comments
 (0)