lib/sec_id/base.rb in sec_id-1.0.0 vs lib/sec_id/base.rb in sec_id-1.1.0

- old
+ new

@@ -1,11 +1,31 @@ # frozen_string_literal: true module SecId class Base - LETTERS = Set.new('A'..'Z').freeze + CHAR_TO_DIGITS = { + '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, + '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, + 'A' => [1, 0], 'B' => [1, 1], 'C' => [1, 2], 'D' => [1, 3], 'E' => [1, 4], + 'F' => [1, 5], 'G' => [1, 6], 'H' => [1, 7], 'I' => [1, 8], 'J' => [1, 9], + 'K' => [2, 0], 'L' => [2, 1], 'M' => [2, 2], 'N' => [2, 3], 'O' => [2, 4], + 'P' => [2, 5], 'Q' => [2, 6], 'R' => [2, 7], 'S' => [2, 8], 'T' => [2, 9], + 'U' => [3, 0], 'V' => [3, 1], 'W' => [3, 2], 'X' => [3, 3], 'Y' => [3, 4], 'Z' => [3, 5], + '*' => [3, 6], '@' => [3, 7], '#' => [3, 8] + }.freeze + CHAR_TO_DIGIT = { + '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, + '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, + 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, + 'F' => 15, 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, + 'K' => 20, 'L' => 21, 'M' => 22, 'N' => 23, 'O' => 24, + 'P' => 25, 'Q' => 26, 'R' => 27, 'S' => 28, 'T' => 29, + 'U' => 30, 'V' => 31, 'W' => 32, 'X' => 33, 'Y' => 34, 'Z' => 35, + '*' => 36, '@' => 37, '#' => 38 + }.freeze + attr_reader :identifier, :check_digit def self.valid?(id) new(id).valid? end @@ -48,17 +68,18 @@ alias to_str to_s private def digitized_identifier - @digitized_identifier ||= identifier.each_char.flat_map { |char| char_to_digits char } + raise NotImplementedError end def char_to_digits(char) - return char.to_i unless LETTERS.include? char + CHAR_TO_DIGITS.fetch(char) + end - number = char.to_i(36) - [number / 10, number % 10] + def char_to_digit(char) + CHAR_TO_DIGIT.fetch(char) end def mod_10(sum) (10 - (sum % 10)) % 10 end