diff --git a/CHANGELOG.md b/CHANGELOG.md index 9989005..98555d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## [Unreleased] +Fixes: +- Fix Denmark to ignore a `-` 🇩🇰 + ## [1.3.1] - 2026-03-24 Fixes: diff --git a/lib/tin_valid/denmark_tin.rb b/lib/tin_valid/denmark_tin.rb index da60c90..2eced28 100644 --- a/lib/tin_valid/denmark_tin.rb +++ b/lib/tin_valid/denmark_tin.rb @@ -15,7 +15,7 @@ def initialize(tin:, birth_date: nil) def valid? match = MATCHER.match(tin) return false unless match - return false if tin == "0000000000" + return false if normalized_tin == "0000000000" if birth_date year = "#{birth_century}#{match[:year]}" @@ -35,6 +35,7 @@ def valid? (?[0-3][0-9]) (?[0-1][0-9]) (?[0-9]{2}) + -? (? [0-9]{3} (?[0-9]) @@ -43,6 +44,8 @@ def valid? }x private_constant :MATCHER + def normalized_tin = @normalized_tin ||= tin.sub("-", "") + def birth_century = birth_date.strftime("%Y")[..1] def birth_year = birth_date.year @@ -51,7 +54,7 @@ def checksum weights = [4, 3, 2, 7, 6, 5, 4, 3, 2] .each_with_index - .map { |char, index| char.to_i * tin[index].to_i } + .map { |char, index| char.to_i * normalized_tin[index].to_i } # 2. Add up the results of the above multiplications; sum = weights.sum diff --git a/spec/tin_valid/denmark_tin_spec.rb b/spec/tin_valid/denmark_tin_spec.rb index c7b5441..6d92bac 100644 --- a/spec/tin_valid/denmark_tin_spec.rb +++ b/spec/tin_valid/denmark_tin_spec.rb @@ -4,6 +4,7 @@ describe "#valid?" do valid_values = [ ["0101111113", nil], + ["010111-1113", nil], ["0101111113", Date.new(1911, 1, 1)], ["2808784028", nil], ["2808784028", Date.new(1978, 8, 28)],