Sha256: 46d4c1bf6ff452ecb4e86e87d261f45fb824b35d8ba7a4b20f8fe9c366387616

Contents?: true

Size: 1.27 KB

Versions: 14

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module Faker
  class NationalHealthService < Base
    class << self
      def british_number
        base_number = rand(400_000_001...499_999_999)
        # If the check digit is equivalent to 10, the number is invalid.
        # See https://en.wikipedia.org/wiki/NHS_number
        base_number -= 1 if check_digit(number: base_number) == 10
        "#{base_number}#{check_digit(number: base_number)}".to_s
                                                           .chars
                                                           .insert(3, ' ')
                                                           .insert(7, ' ')
                                                           .join('')
      end

      def check_digit(legacy_number = NOT_GIVEN, number: 0)
        warn_for_deprecated_arguments do |keywords|
          keywords << :number if legacy_number != NOT_GIVEN
        end

        sum = 0
        number.to_s.chars.each_with_index do |digit, idx|
          position = idx + 1
          sum += (digit.to_i * (11 - position))
        end
        result = 11 - (sum % 11)

        # A value of 11 is considered the same as 0
        # See https://en.wikipedia.org/wiki/NHS_number
        return 0 if result == 11

        result
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
faker-2.12.0 lib/faker/default/nhs.rb
faker-2.11.0 lib/faker/default/nhs.rb
faker-2.10.2 lib/faker/default/nhs.rb
faker-2.10.1 lib/faker/default/nhs.rb
faker-2.10.0 lib/faker/default/nhs.rb
faker-2.9.0 lib/faker/default/nhs.rb
faker-2.8.1 lib/faker/default/nhs.rb
faker-2.8.0 lib/faker/default/nhs.rb
faker-2.7.0 lib/faker/default/nhs.rb
faker-2.6.0 lib/faker/default/nhs.rb
faker-2.5.0 lib/faker/default/nhs.rb
faker-2.4.0 lib/faker/default/nhs.rb
faker-2.3.0 lib/faker/default/nhs.rb
faker-2.2.2 lib/faker/default/nhs.rb