lib/vindetta/calculator.rb in vindetta-0.19.0 vs lib/vindetta/calculator.rb in vindetta-0.20.0
- old
+ new
@@ -1,19 +1,21 @@
module Vindetta
class Calculator
- CHECK_DIGITS = "0123456789X".chars
- WEIGHTS = [8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2]
-
def self.check_digit(vin)
- CHECK_DIGITS[sum(vin) % CHECK_DIGITS.length]
- end
+ transliterator = -> (c) do
+ "0123456789.ABCDEFGH..JKLMN.P.R..STUVWXYZ".chars.index(c) % 10
+ end
- private
+ summer = -> (sum, (a, b)) do
+ sum + (a * b)
+ end
- def self.sum(vin)
- Transliterator
- .vin(vin)
- .zip(WEIGHTS)
- .reduce(0) { |sum, (a, b)| sum + (a * b) }
+ 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