Skip to content

Commit

Permalink
Fix Luhn algorithm used in french companie's SIRET and sweden SSN
Browse files Browse the repository at this point in the history
  • Loading branch information
Christophe Chailloleau-Leclerc committed Mar 28, 2024
1 parent 5f5ace8 commit bd59f86
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1028,8 +1028,8 @@
| Method | Example |
| ------ | ------- |
| `name` | Pascal, Vasseur and Mendes, Rossi et fils, Guillou-Collet |
| `siren` | 739836110, 690890420, 300789380 |
| `siret` | 06746846000000, 70493562000500, 45666261000350 |
| `siren` | 739836112, 690890421, 300789385 |
| `siret` | 06746846200006, 70493562600504 , 45666261800358 |
| `suffix` | SA, Groupe, SARL |

## FFaker::CompanyIT
Expand Down
21 changes: 6 additions & 15 deletions lib/ffaker/utils/module_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,12 @@ def unique(max_retries = 10_000)

# http://en.wikipedia.org/wiki/Luhn_algorithm
def luhn_check(number)
multiplications = []

number.chars.each_with_index do |digit, i|
multiplications << i.even? ? digit.to_i * 2 : digit.to_i
end

sum = 0
multiplications.each do |num|
num.to_s.each_byte do |character|
sum += character.chr.to_i
end
end

control_digit = (sum % 10).zero? ? 0 : (((sum / 10) + 1) * 10) - sum
control_digit.to_s
sum = number.chars
.map(&:to_i)
.reverse
.each_with_index
.sum { |digit, index| index.even? ? (2 * digit).digits.sum : digit }
((10 - sum%10)%10).to_s

Check failure on line 46 in lib/ffaker/utils/module_utils.rb

View workflow job for this annotation

GitHub Actions / RuboCop lint

Wrap expressions with varying precedence with parentheses to avoid ambiguity.

Check failure on line 46 in lib/ffaker/utils/module_utils.rb

View workflow job for this annotation

GitHub Actions / RuboCop lint

Surrounding space missing for operator %.

Check failure on line 46 in lib/ffaker/utils/module_utils.rb

View workflow job for this annotation

GitHub Actions / RuboCop lint

Surrounding space missing for operator %.
end
end
end
9 changes: 9 additions & 0 deletions test/test_module_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,13 @@ def generator.test
FFaker::UniqueUtils.clear
generator.unique.test
end

def test_luhn_check
obj = Object.new
obj.extend FFaker::ModuleUtils
assert obj.luhn_check("97248708") == "6"
assert obj.luhn_check("1789372997") == "4"
assert obj.luhn_check("8899982700037") == "1"
assert obj.luhn_check("1234567820001") == "0"
end
end

0 comments on commit bd59f86

Please sign in to comment.