Sha256: b5749e69041e591aa27e4ad374aff8740fa271d75a54ee3f17a674cbc9014259

Contents?: true

Size: 458 Bytes

Versions: 3

Compression:

Stored size: 458 Bytes

Contents

# encoding: utf-8
# frozen_string_literal: true

module FFaker
  module Code
    extend ModuleUtils
    extend self

    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

3 entries across 3 versions & 1 rubygems

Version Path
ffaker-2.11.0 lib/ffaker/code.rb
ffaker-2.10.0 lib/ffaker/code.rb
ffaker-2.9.0 lib/ffaker/code.rb