Sha256: e01e3ba57c121bf18095aa5e09935fbba928d36a6c1b717366a129cbde843834

Contents?: true

Size: 636 Bytes

Versions: 3

Compression:

Stored size: 636 Bytes

Contents

# frozen_string_literal: true

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

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

        raise InvalidCharacterError if index.nil?

        index % 10
      end

      summer = lambda do |sum, (a, b)|
        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.28.0 lib/vindetta/calculator.rb
vindetta-0.27.0 lib/vindetta/calculator.rb
vindetta-0.26.0 lib/vindetta/calculator.rb