Sha256: 6bb3b811f43fac7e3a0d944a339760b6888746c6857eddea11c04b9320639369

Contents?: true

Size: 582 Bytes

Versions: 3

Compression:

Stored size: 582 Bytes

Contents

module Vindetta
  class Calculator
    InvalidCharacterError = Class.new(StandardError)

    def self.check_digit(vin)
      transliterator = -> (c) do
        index = "0123456789.ABCDEFGH..JKLMN.P.R..STUVWXYZ".chars.index(c)

        raise InvalidCharacterError if index.nil?

         index % 10
      end

      summer = -> (sum, (a, b)) do
        sum + (a * b)
      end

      sum = vin
        .chars
        .map(&transliterator)
        .zip([8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2])
        .reduce(0, &summer)

      "0123456789X"[sum % 11]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vindetta-0.25.0 lib/vindetta/calculator.rb
vindetta-0.24.0 lib/vindetta/calculator.rb
vindetta-0.23.0 lib/vindetta/calculator.rb