Sha256: bdabcb9e3f71d5d2d5b0c73b2bb38b41ed77c79f1370708359cd50073cbb7ef0

Contents?: true

Size: 966 Bytes

Versions: 9

Compression:

Stored size: 966 Bytes

Contents

# frozen_string_literal: true

module FFakerTW
  module Code
    extend ModuleUtils
    extend self

    def npi(legacy: false)
      max = legacy ? 299_999_999 : 999_999_999
      base_npi = rand(100_000_000..max).to_s

      summed_digits = base_npi
                      .chars
                      .each_with_index
                      .map { |n, i| (i.even? ? n.to_i * 2 : n).to_s }
                      .join
                      .chars
                      .inject(0) { |sum, digit| sum + digit.to_i }

      npi_checksum = (10 - (24 + summed_digits) % 10).to_s.chars.last

      base_npi + npi_checksum
    end

    def ean
      ean = rand(100_000_000_000..999_999_999_999).to_s

      sum = 0
      ean.chars.each_with_index { |c, i| sum += c.to_i * (i.odd? ? 3 : 1) }

      nearest_multiple_of_ten = sum.round(-1)
      nearest_multiple_of_ten += 10 if nearest_multiple_of_ten < sum

      "#{ean}#{nearest_multiple_of_ten - sum}"
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ffakerTW-1.8.0 lib/ffakerTW/code.rb
ffakerTW-1.7.0 lib/ffakerTW/code.rb
ffakerTW-1.6.0 lib/ffakerTW/code.rb
ffakerTW-1.5.0 lib/ffakerTW/code.rb
ffakerTW-1.4.0 lib/ffakerTW/code.rb
ffakerTW-1.3.0 lib/ffakerTW/code.rb
ffakerTW-1.2.0 lib/ffakerTW/code.rb
ffakerTW-1.1.0 lib/ffakerTW/code.rb
ffakerTW-0.1.0 lib/ffakerTW/code.rb