tw_id_number 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/tw/id_number.rb +63 -0
- metadata +43 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 22ed29d503c145e6537309fdb2ff0eb65686629678eed85b957cb6608809665a
|
|
4
|
+
data.tar.gz: 2af9f42aba73945a9d14ec873a79d51cd72c5e12946947f2310678beb91c0a71
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a66e25e9a7d85c891acde295c313253f02e79dca04cd5fa426fa0553bed55b2bbe96665df63de2661e00c3b2acff7091328544e3e6e3b8a5599941ee448269e8
|
|
7
|
+
data.tar.gz: 60c896d1777e7ef29142e998a05a326b8110cd65994a94dc86cac55ac35e818fa876ae022468fc8a6942698338a0bf7dfd810c6be446ac3ba52705cacb3e45b5
|
data/lib/tw/id_number.rb
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
CONVERSION_TABLE = {
|
|
4
|
+
'A': '10',
|
|
5
|
+
'B': '11',
|
|
6
|
+
'C': '12',
|
|
7
|
+
'D': '13',
|
|
8
|
+
'E': '14',
|
|
9
|
+
'F': '15',
|
|
10
|
+
'G': '16',
|
|
11
|
+
'H': '17',
|
|
12
|
+
'J': '18',
|
|
13
|
+
'K': '19',
|
|
14
|
+
'L': '20',
|
|
15
|
+
'M': '21',
|
|
16
|
+
'N': '22',
|
|
17
|
+
'P': '23',
|
|
18
|
+
'Q': '24',
|
|
19
|
+
'R': '25',
|
|
20
|
+
'S': '26',
|
|
21
|
+
'T': '27',
|
|
22
|
+
'U': '28',
|
|
23
|
+
'V': '29',
|
|
24
|
+
'X': '30',
|
|
25
|
+
'Y': '31',
|
|
26
|
+
'W': '32',
|
|
27
|
+
'Z': '33',
|
|
28
|
+
'I': '34',
|
|
29
|
+
'O': '35'
|
|
30
|
+
}.freeze
|
|
31
|
+
|
|
32
|
+
MULTIPLIER = [1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1].freeze
|
|
33
|
+
|
|
34
|
+
module TW
|
|
35
|
+
module IDNumber
|
|
36
|
+
def self.generate
|
|
37
|
+
id_number = String.new("#{[*'A'..'Z'].sample}#{%w[1 2].sample}#{8.times.map { rand(1..9) }.join}")
|
|
38
|
+
return id_number if valid?(id_number)
|
|
39
|
+
|
|
40
|
+
generate
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def self.valid?(string)
|
|
44
|
+
digitizing_number = CONVERSION_TABLE[string.chr.to_sym] + string.delete(string.chr)
|
|
45
|
+
|
|
46
|
+
digits = digitizing_number.chars.map(&:to_i)
|
|
47
|
+
|
|
48
|
+
special_sum = calculate_special_sum(digits)
|
|
49
|
+
|
|
50
|
+
return true if (special_sum % 10).zero?
|
|
51
|
+
|
|
52
|
+
false
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def self.calculate_special_sum(digits)
|
|
56
|
+
digits.map.with_index do |_digit, i|
|
|
57
|
+
digits[i] * MULTIPLIER[i]
|
|
58
|
+
end.sum
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
private_class_method :calculate_special_sum
|
|
62
|
+
end
|
|
63
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: tw_id_number
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- RobertChang
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2022-07-14 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Generate TW personal ID number randomly and validation
|
|
14
|
+
email: fishppm0212@gmail.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/tw/id_number.rb
|
|
20
|
+
homepage: https://github.com/5xTraining/tw_id_number
|
|
21
|
+
licenses:
|
|
22
|
+
- MIT
|
|
23
|
+
metadata: {}
|
|
24
|
+
post_install_message:
|
|
25
|
+
rdoc_options: []
|
|
26
|
+
require_paths:
|
|
27
|
+
- lib
|
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: 2.6.0
|
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
34
|
+
requirements:
|
|
35
|
+
- - ">="
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '0'
|
|
38
|
+
requirements: []
|
|
39
|
+
rubygems_version: 3.3.7
|
|
40
|
+
signing_key:
|
|
41
|
+
specification_version: 4
|
|
42
|
+
summary: Generate TW personal ID number and validate correctly
|
|
43
|
+
test_files: []
|