Skip to content

Commit

Permalink
Added new 'log10' cleaning method (Resolves #NDRS2-711)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
drewthorp committed Feb 9, 2024
1 parent 81dcc4e commit f0415a8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 9 additions & 1 deletion lib/ndr_support/string/clean_methodable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions test/string/cleaning_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f0415a8

Please sign in to comment.