Skip to content

Commit

Permalink
Add Faker::Company.czech_organisation_number (faker-ruby#1101)
Browse files Browse the repository at this point in the history
* Add czech_organisation_number support

* Add czech_organisation_number support

* Add czech_organisation_number support (docs)

* Update changelog.md
  • Loading branch information
jindrichskupa authored and vbrazo committed Jun 2, 2018
1 parent 36cb30a commit 93baf1a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Change Log

## HEAD Unreleased
### Latest update: 2018-06-01
### Latest update: 2018-06-02

### Feature Request
- [PR #1101](https://github.com/stympy/faker/pull/1101) Add Faker::Company.czech_organisation_number [@jindrichskupa](https://github.com/jindrichskupa)
- [PR #1265](https://github.com/stympy/faker/pull/1265) Add Faker::WorldCup [@snayrouz](https://github.com/snayrouz)
- [PR #1141](https://github.com/stympy/faker/pull/1141) Add Faker::Coffee.intensifier [@oyeanuj](https://github.com/oyeanuj)
- [PR #1260](https://github.com/stympy/faker/pull/1260) Add Faker::Auto features to Faker::Vehicle [@mrstebo](https://github.com/mrstebo)
Expand Down
2 changes: 2 additions & 0 deletions doc/company.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Faker::Company.profession #=> "firefighter"

Faker::Company.swedish_organisation_number #=> "7962578022"

Faker::Company.czech_organisation_number #=> "77778171"

Faker::Company.french_siren_number #=> "819489626"

Faker::Company.french_siret_number #=> "81948962600013"
Expand Down
11 changes: 11 additions & 0 deletions lib/faker/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ def swedish_organisation_number
base + luhn_algorithm(base).to_s
end

def czech_organisation_number
sum = 0
base = []
[8, 7, 6, 5, 4, 3, 2].each do |weight|
base << sample((0..9).to_a)
sum += (weight * base.last)
end
base << (11 - (sum % 11)) % 10
base.join
end

# Get a random French SIREN number. See more here https://fr.wikipedia.org/wiki/Syst%C3%A8me_d%27identification_du_r%C3%A9pertoire_des_entreprises
def french_siren_number
base = (1..8).map { rand(10) }.join
Expand Down
17 changes: 17 additions & 0 deletions test/test_faker_company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ def test_swedish_organisation_number
assert org_no[9] == @tester.send(:luhn_algorithm, org_no[0..8]).to_s
end

def test_czech_organisation_number
org_no = @tester.czech_organisation_number
assert org_no.match(/\d{8}/)
assert [0, 1, 2, 3, 5, 6, 7, 8, 9].include?(org_no[0].to_i)
assert czech_o_n_checksum(org_no) == org_no[-1].to_i
end

def test_french_siren_number
siren = @tester.french_siren_number
assert siren.match(/\A\d{9}\z/)
Expand Down Expand Up @@ -109,6 +116,16 @@ def test_mod11

private

def czech_o_n_checksum(org_no)
weights = [8, 7, 6, 5, 4, 3, 2]
sum = 0
digits = org_no.split('').map(&:to_i)
weights.each_with_index.map do |w, i|
sum += (w * digits[i])
end
(11 - (sum % 11)) % 10
end

def abn_checksum(abn)
abn_weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

Expand Down

0 comments on commit 93baf1a

Please sign in to comment.