From f0415a8b25b8eb8a203abd392b80ed6bc8afe32b Mon Sep 17 00:00:00 2001 From: drewthorp Date: Fri, 9 Feb 2024 09:58:27 +0000 Subject: [PATCH] Added new 'log10' cleaning method (Resolves #NDRS2-711) - Tidied the `clean` method code into a concern and split the large case statment into methods. - Added new 'log10' cleaning method - Extended cleaning tests to cover the new method --- CHANGELOG.md | 5 ++++- lib/ndr_support/string/clean_methodable.rb | 10 +++++++++- test/string/cleaning_test.rb | 4 ++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f834204..b2d00fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## [Unreleased] -* No unreleased changes +## Fixed +* Refactored the cleaning code to fix Rubcocop issues +## Added +* Added a new 'log10' cleaning method ## 5.10.1 / 2024-01-04 ## Fixed diff --git a/lib/ndr_support/string/clean_methodable.rb b/lib/ndr_support/string/clean_methodable.rb index aff66ef..0ba7e5c 100644 --- a/lib/ndr_support/string/clean_methodable.rb +++ b/lib/ndr_support/string/clean_methodable.rb @@ -16,7 +16,8 @@ module CleanMethodable xmlsafe: :clean_xmlsafe, make_xml_safe: :clean_xmlsafe, roman5: :clean_roman5, tnmcategory: :clean_tnmcategory, - strip: :strip, upcase: :upcase, itself: :itself + strip: :strip, upcase: :upcase, itself: :itself, + log10: :clean_log10 }.freeze def clean(what) @@ -137,4 +138,11 @@ def clean_code_opcs db_code end.compact.join(' ') end + + def clean_log10 + f_value = Float(self, exception: false) + return self if f_value.nil? || f_value.negative? + + f_value.zero? ? '0.0' : Math.log10(f_value).to_s + end end diff --git a/test/string/cleaning_test.rb b/test/string/cleaning_test.rb index 8984bdb..bbd559e 100644 --- a/test/string/cleaning_test.rb +++ b/test/string/cleaning_test.rb @@ -30,6 +30,10 @@ class CleaningTest < Minitest::Test assert_equal 'IP222', 'IP222'.postcodeize(:db) assert_equal 'IP222E', 'IP222E'.postcodeize(:db) assert_equal 'HANTS', 'HANTS'.postcodeize(:db) + # Log10 + assert '0.0', '0'.clean(:log10) + assert '-10.1', '-10.1'.clean(:log10) + assert '1.1', '0.04139268515822508'.clean(:log10) end test 'xml_unsafe?' do