diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 138c57bb6..f5f859032 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,6 +1,8 @@ module ApplicationHelper APP_HOST = ENV['APP_HOST'] APP_HOST_LEGACY = ENV['APP_HOST_LEGACY'] + REGEXP_REPLACE_TRAILING_EXTENSION = /(\.\w+)+$/.freeze + REGEXP_REPLACE_WORD_SEPARATOR = /[\s_-]+/.freeze def app_host_legacy?(request) return false if APP_HOST_LEGACY.blank? @@ -145,4 +147,11 @@ module ApplicationHelper tag.span(class: class_names(classes, 'fr-icon--sm': sm, 'fr-mr-1v': mr), "aria-hidden" => true) end + + def acronymize(str) + str.gsub(REGEXP_REPLACE_TRAILING_EXTENSION, '') + .split(REGEXP_REPLACE_WORD_SEPARATOR) + .map { |word| word[0].upcase } + .join + end end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index dc3998c9b..524fc6faf 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -112,4 +112,29 @@ describe ApplicationHelper do it { is_expected.to eq("") } end end + describe '#acronymize' do + it 'returns the acronym of a given string' do + expect(helper.acronymize('Application Name')).to eq('AN') + expect(helper.acronymize('Hello World')).to eq('HW') + expect(helper.acronymize('Demarches Simplifiees')).to eq('DS') + end + + it 'handles single word input' do + expect(helper.acronymize('Word')).to eq('W') + end + + it 'returns an empty string for empty input' do + expect(helper.acronymize('')).to eq('') + end + + it 'handles strings with extensions' do + expect(helper.acronymize('file_name.txt')).to eq('FN') + expect(helper.acronymize('example.pdf')).to eq('E') + end + + it 'handles strings with various word separators' do + expect(helper.acronymize('multi-word_string')).to eq('MWS') + expect(helper.acronymize('another_example-test')).to eq('AET') + end + end end