Last active
April 12, 2020 11:17
-
-
Save tonytonyjan/361282d5b6771660b88c to your computer and use it in GitHub Desktop.
中華民國身分證字號驗證器
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
require 'id_check' | |
require 'taiwanese_id_builder' | |
require 'TaiwanUserID' | |
n = 100000 | |
Benchmark.bmbm do |x| | |
x.report('wayne5540 '){ n.times{ TaiwaneseIdBuilder.valid?('A123456789') } } | |
dummy = Object.new.extend(TaiwanUserID) | |
x.report('kaochenlong'){ n.times{ dummy.is_valid?('A123456789') } } | |
x.report('tonytonyjan'){ n.times{ id_check('A123456789') } } | |
end | |
# wayne5540 1.080000 0.230000 1.310000 ( 1.319509) | |
# kaochenlong 1.860000 0.010000 1.870000 ( 1.879476) | |
# tonytonyjan 0.550000 0.000000 0.550000 ( 0.557314) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MAP = { | |
'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' => [3, 4], | |
'J' => [1, 8], 'K' => [1, 9], 'L' => [2, 0], 'M' => [2, 1], 'N' => [2, 2], 'O' => [3, 5], 'P' => [2, 3], 'Q' => [2, 4], 'R' => [2, 5], | |
'S' => [2, 6], 'T' => [2, 7], 'U' => [2, 8], 'V' => [2, 9], 'W' => [3, 2], 'X' => [3, 0], 'Y' => [3, 1], 'Z' => [3, 3] | |
} | |
MULTIPLIER = [1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1] | |
def id_check id | |
return false unless id =~ /\A[A-Z]\d{9}\z/ | |
chars = id.chars | |
numbers = MAP[chars.shift] + chars.map!(&:to_i) | |
sum, i = 0, 0 | |
while i < 11 | |
sum += numbers[i] * MULTIPLIER[i] | |
i += 1 | |
end | |
sum % 10 == 0 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
应该写个gem啊,不然不好找