Sha256: 45aa75e5f900a56dd972f056876edf29040cd18eb9269214708e93be894630f5

Contents?: true

Size: 902 Bytes

Versions: 5

Compression:

Stored size: 902 Bytes

Contents

# frozen_string_literal: true

class Valvat
  module Checksum
    class FR < Base
      # the valid characters for the first two digits (O and I are missing)
      ALPHABET = '0123456789ABCDEFGHJKLMNPQRSTUVWXYZ'
      NUMERIC = /^\d+$/.freeze

      def validate
        return super if str_wo_country[0..1] =~ NUMERIC

        check = alt_check_digit

        (str_wo_country[2..].to_i + 1 + (check / 11)) % 11 == check % 11
      end

      private

      def check_digit
        siren = str_wo_country[2..].to_i
        (12 + ((3 * siren) % 97)) % 97
      end

      def given_check_digit
        str_wo_country[0..1].to_i
      end

      def alt_check_digit
        first_is_numeric = str_wo_country[0] =~ NUMERIC

        (ALPHABET.index(str_wo_country[0]) * (first_is_numeric ? 24 : 34)) +
          ALPHABET.index(str_wo_country[1]) - (first_is_numeric ? 10 : 100)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
valvat-1.4.4 lib/valvat/checksum/fr.rb
valvat-1.4.3 lib/valvat/checksum/fr.rb
valvat-1.4.2 lib/valvat/checksum/fr.rb
valvat-1.4.1 lib/valvat/checksum/fr.rb
valvat-1.4.0 lib/valvat/checksum/fr.rb