Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Fixes:
- Fix Belgium to ignore `-` and `.` 🇧🇪
- Fix Czechia to accept `CZ` prefix 🇨🇿
- Fix Sweden TINs versions with 4-digit years 🇸🇪

## [1.3.0] - 2025-12-12

Expand Down
8 changes: 6 additions & 2 deletions lib/tin_valid/sweden_tin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ def valid?
day = match[:day]
next false unless date_valid?(year, month, day)

id = match[:id]
match[:check].to_i == checksum("#{year[..2]}#{month}#{day}#{id}")
check_match?(year:, month:, day:, id: match[:id], check: match[:check])
end
end

Expand Down Expand Up @@ -70,6 +69,11 @@ def valid?
].freeze
private_constant :VERSIONS

def check_match?(year:, month:, day:, id:, check:)
checkyear = year.size == 4 ? year[2..] : year
check.to_i == checksum("#{checkyear}#{month}#{day}#{id}")
end

def checksum(numbers)
# 1. Multiply the values of each position by the corresponding weight:
# - C1: 2
Expand Down
2 changes: 2 additions & 0 deletions spec/tin_valid/sweden_tin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
describe "#valid?" do
valid_values = [
["640883-3231", nil],
["199109141086", nil],
["040731-1372", nil],
["640823-3234", nil],
["640823-3234", Date.new(1964, 8, 23)],
]
Expand Down
Loading