Skip to content

Commit

Permalink
Fix Luhn algorithm used in french companie's SIRET and sweden SSN (#548)
Browse files Browse the repository at this point in the history
* Fix Luhn algorithm used in french companie's SIRET and sweden SSN

* Fix linting

---------

Co-authored-by: Christophe Chailloleau-Leclerc <christophe@squadracer.com>
  • Loading branch information
Krap and Christophe Chailloleau-Leclerc committed Apr 8, 2024
1 parent 5f5ace8 commit 3676ce1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
4 changes: 2 additions & 2 deletions REFERENCE.md
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
4 changes: 2 additions & 2 deletions lib/ffaker/name_pl.rb
Expand Up @@ -90,9 +90,9 @@ def academic_degree_prefix

def name_for_gender(name_type, gender) # :nodoc:
raise(ArgumentError, "Gender must be one of: #{GENDERS}") unless GENDERS.include?(gender)
return send("#{gender}_#{name_type}") unless gender == :random
return send(:"#{gender}_#{name_type}") unless gender == :random

fetch_sample([send("female_#{name_type}"), send("male_#{name_type}")])
fetch_sample([send(:"female_#{name_type}"), send(:"male_#{name_type}")])
end
end
end
23 changes: 7 additions & 16 deletions lib/ffaker/utils/module_utils.rb
Expand Up @@ -14,7 +14,7 @@ def k(arg)

def const_missing(const_name)
if const_name.match?(/[a-z]/) # Not a constant, probably a class/module name.
super const_name
super(const_name)
else
mod_name = ancestors.first.to_s.split('::').last
data_path = "#{FFaker::BASE_LIB_PATH}/ffaker/data/#{underscore(mod_name)}/#{underscore(const_name.to_s)}"
Expand All @@ -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
end
end
end
8 changes: 4 additions & 4 deletions test/helper.rb
Expand Up @@ -35,7 +35,7 @@ def assert_deterministic(options = {}, &block)
end
operator_name += '_or_equal_to' if operator[1] == '='

define_method "assert_#{operator_name}" do |got, expected|
define_method :"assert_#{operator_name}" do |got, expected|
assert(
got.public_send(operator, expected),
"Expected #{operator} \"#{expected}\", but got #{got}"
Expand All @@ -56,8 +56,8 @@ def assert_random(original_block, *args)
end

%w[less_than_or_equal_to between].each do |method_name|
define_method "assert_random_#{method_name}" do |*args, &block|
assert_random(block) { send "assert_#{method_name}", block.call, *args }
define_method :"assert_random_#{method_name}" do |*args, &block|
assert_random(block) { send :"assert_#{method_name}", block.call, *args }
end
end

Expand All @@ -76,7 +76,7 @@ module ClassMethods
# }
def assert_methods_are_deterministic(klass, *methods)
Array(methods).each do |meth|
define_method "test_#{meth}_is_deterministic" do
define_method :"test_#{meth}_is_deterministic" do
assert_deterministic(message: "Results from `#{klass}.#{meth}` are not repeatable") do
klass.send(meth)
end
Expand Down
9 changes: 9 additions & 0 deletions test/test_module_utils.rb
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 3676ce1

Please sign in to comment.