Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ GEM

PLATFORMS
arm64-darwin-22
arm64-darwin-24
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added automatically, and I try to refrain from manually removing lines from the lock file.

Question for the Faker maintainers: Should this change be removed or let it?

x86_64-linux

DEPENDENCIES
Expand Down
3 changes: 3 additions & 0 deletions doc/default/id_number.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ Faker::IdNumber.danish_id_number(formatted: true) #=> "050390-9980"
Faker::IdNumber.danish_id_number(birthday: Date.new(1990, 3, 5)) #=> "050390-9980"
Faker::IdNumber.danish_id_number(gender: :female) #=> "050390-9980"

# Generate a Dutch ID number (BSN)
Faker::IdNumber.dutch_bsn # => "365371960"

# Generate a valid French Social Security number (INSEE number)
Faker::IdNumber.french_insee_number #=> "22510589696868"
```
31 changes: 31 additions & 0 deletions lib/faker/default/id_number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,23 @@ def danish_id_number(formatted: false, birthday: Faker::Date.birthday, gender: n
].join
end

##
# Produces a random Dutch social security number (Burger Service Nummer).
#
# @return [String]
#
# @example
# Faker::IdNumber.dutch_bsn #=> "697116694"
#
# @faker.version next
def dutch_bsn
bsn = ''

bsn = Faker::Number.numerify('#########') until valid_dutch_bsn?(bsn)

bsn
end

##
# Produces a random French social security number (INSEE number).
#
Expand Down Expand Up @@ -416,6 +433,20 @@ def danish_control_digits(birthday)
end
end

def valid_dutch_bsn?(bsn)
length = bsn.length

# If it's not numeric, or not 8 or 9 digits long, it can never be a valid Dutch BSN.
return false unless /^\d{8,9}$/.match?(bsn)

sum = 0
bsn.chars.each_with_index do |c, i|
sum += Integer(c) * (i == length - 1 ? -1 : length - i)
end

(sum % 11).zero?
end

def _translate(key)
parse("id_number.#{key}")
end
Expand Down
12 changes: 12 additions & 0 deletions test/faker/default/test_faker_id_number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ def test_danish_id_number_invalid_gender
end
end

def test_dutch_bsn
sample = @tester.dutch_bsn

assert_dutch_bsn sample
end

private

def south_african_id_number_to_date_of_birth_string(sample)
Expand All @@ -287,4 +293,10 @@ def assert_valid_south_african_id_number(sample)
assert_equal Faker::IdNumber::ZA_RACE_DIGIT, sample[11]
assert Date.parse(south_african_id_number_to_date_of_birth_string(sample))
end

def assert_dutch_bsn(sample)
assert_equal 9, sample.length
assert_kind_of String, sample
refute_nil Integer(sample)
end
end