taiwan_id_validator 1.0.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.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +43 -0
  4. data/lib/taiwan_id_validator.rb +128 -0
  5. metadata +60 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e27b20cfcf0bd1b75f3d5093a51b06610c63251338a7202e920ccdf88235d541
4
+ data.tar.gz: 61edb228f56683a8d18049ec6f139b37ca4c826b4defaa752d39bcc6e5357278
5
+ SHA512:
6
+ metadata.gz: 9538506497569a6ff52de63cb55665cb7324915c0d5e49eefdf9dc16fac8733deb588ab84c046468c2d6b201b0c366c074a3354303b8dd33a2681e275a7e58dd
7
+ data.tar.gz: dc4e7fec03230cc0cbb9953f03e3434cca562b970a686c5bb4260097c05362e94f0407cd7f3be81d777216dfa8cbd1e3316555243d5947efa799ed2d4cf20b9a
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Chen Guan-Ting
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Taiwan ID Validator (for Ruby)
2
+
3
+ [![Ruby CI](https://github.com/guanting112/taiwan-id-validator-ruby/actions/workflows/ci.yml/badge.svg)](https://github.com/guanting112/taiwan-id-validator-ruby/actions)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ `taiwan_id_validator` is a lightweight and zero-dependency Ruby gem for validating Taiwan National IDs, Alien Resident Certificates (ARC), and Unified Business Numbers (UBN).
7
+
8
+ It supports:
9
+ - **Taiwan National ID** (中華民國身分證字號)
10
+ - **New Alien Resident Certificate** (新式外來人口統一證號) (Since 2021)
11
+ - **Old Alien Resident Certificate** (舊式外來人口統一證號)
12
+ - **Unified Business Number (UBN)**: Validates company tax IDs, fully supporting the latest logic (including the 7th-digit rule). (新舊格式的統一編號)
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ gem install taiwan_id_validator
18
+ # or
19
+ bundle add taiwan_id_validator
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ require 'taiwan_id_validator'
26
+
27
+ TaiwanIdValidator.valid?('A123456789') # true
28
+ TaiwanIdValidator.valid?('A824285055') # true
29
+ TaiwanIdValidator.valid?('G951582036') # true
30
+ TaiwanIdValidator.valid?('AC01234567') # true
31
+ TaiwanIdValidator.valid?('AC01234566') # false
32
+ TaiwanIdValidator.valid?('A123456788') # false
33
+
34
+ TaiwanIdValidator.valid_national_id?('A123456789') # true
35
+ TaiwanIdValidator.valid_national_id?('A123456788') # false
36
+
37
+ TaiwanIdValidator.valid_arc_id?('AC01234567') # true
38
+ TaiwanIdValidator.valid_arc_id?('AC01234565') # false
39
+
40
+ TaiwanIdValidator.valid_ubn?('00000000') # false
41
+ TaiwanIdValidator.valid_ubn?('22099131') # true
42
+ TaiwanIdValidator.valid_ubn?('84149961') # true
43
+ ```
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TaiwanIdValidator
4
+ UBN_WEIGHTS = [1, 2, 1, 2, 1, 2, 4, 1].freeze
5
+ LETTER_WEIGHTS = {
6
+ 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15, 'G' => 16,
7
+ 'H' => 17, 'J' => 18, 'K' => 19, 'L' => 20, 'M' => 21, 'N' => 22, 'P' => 23,
8
+ 'Q' => 24, 'R' => 25, 'S' => 26, 'T' => 27, 'U' => 28, 'V' => 29, 'X' => 30,
9
+ 'Y' => 31, 'W' => 32, 'Z' => 33, 'I' => 34, 'O' => 35
10
+ }.freeze
11
+
12
+ NATIONAL_ID_REGEX = /^[A-Z][12]\d{8}$/.freeze
13
+ NEW_ARC_ID_REGEX = /^[A-Z][89]\d{8}$/.freeze
14
+ OLD_ARC_ID_REGEX = /^[A-Z][A-Z]\d{8}$/.freeze
15
+ UBN_REGEX = /^\d{8}$/.freeze
16
+
17
+ module_function
18
+
19
+ # valid? checks if the given ID is a valid Taiwan National ID or Alien Resident Certificate number.
20
+ def valid?(id)
21
+ id = id.to_s.upcase
22
+ if NATIONAL_ID_REGEX.match?(id) || NEW_ARC_ID_REGEX.match?(id)
23
+ validate_modern_id_card_format(id)
24
+ elsif OLD_ARC_ID_REGEX.match?(id)
25
+ validate_old_arc_id_format(id)
26
+ else
27
+ false
28
+ end
29
+ end
30
+
31
+ # ValidateNationId checks if the given ID is a valid Taiwan National ID.
32
+ def valid_national_id?(id)
33
+ id = id.to_s.upcase
34
+ return false unless NATIONAL_ID_REGEX.match?(id)
35
+
36
+ validate_modern_id_card_format(id)
37
+ end
38
+
39
+ # valid_arc_id? checks if the given ID is a valid Taiwan Alien Resident Certificate number.
40
+ def valid_arc_id?(id)
41
+ id = id.to_s.upcase
42
+ if NEW_ARC_ID_REGEX.match?(id)
43
+ validate_modern_id_card_format(id)
44
+ elsif OLD_ARC_ID_REGEX.match?(id)
45
+ validate_old_arc_id_format(id)
46
+ else
47
+ false
48
+ end
49
+ end
50
+
51
+ # valid_ubn? checks if the given UBN is a valid Taiwan Company Tax ID.
52
+ def valid_ubn?(ubn)
53
+ ubn = ubn.to_s
54
+ return false unless UBN_REGEX.match?(ubn)
55
+ return false if ubn == "00000000"
56
+
57
+ sum1 = 0
58
+ sum2 = 0
59
+
60
+ UBN_WEIGHTS.each_with_index do |weight, i|
61
+ digit = ubn[i].to_i
62
+ if i == 6 && digit == 7
63
+ sum1 += 1
64
+ sum2 += 0
65
+ else
66
+ product = digit * weight
67
+ sum1 += (product / 10) + (product % 10)
68
+ sum2 += (product / 10) + (product % 10)
69
+ end
70
+ end
71
+
72
+ (sum1 % 5).zero? || (sum2 % 5).zero?
73
+ end
74
+
75
+ class << self
76
+ private
77
+
78
+ def validate_modern_id_card_format(id)
79
+ sum = 0
80
+
81
+ area_char = id[0]
82
+ area_weight = LETTER_WEIGHTS[area_char]
83
+ return false unless area_weight
84
+
85
+ sum += (area_weight / 10) * 1 + (area_weight % 10) * 9
86
+
87
+ weights = [8, 7, 6, 5, 4, 3, 2, 1]
88
+
89
+ id[1..8].chars.each_with_index do |char, i|
90
+ num = char.to_i
91
+ sum += num * weights[i]
92
+ end
93
+
94
+ check_digit = id[9].to_i
95
+ sum += check_digit
96
+
97
+ (sum % 10).zero?
98
+ end
99
+
100
+ def validate_old_arc_id_format(id)
101
+ sum = 0
102
+
103
+ area_char = id[0]
104
+ area_weight = LETTER_WEIGHTS[area_char]
105
+ return false unless area_weight
106
+
107
+ sum += (area_weight / 10) * 1 + (area_weight % 10) * 9
108
+
109
+ gender_char = id[1]
110
+ gender_weight = LETTER_WEIGHTS[gender_char]
111
+ return false unless gender_weight
112
+
113
+ sum += (gender_weight % 10) * 8
114
+
115
+ weights = [7, 6, 5, 4, 3, 2, 1]
116
+
117
+ id[2..8].chars.each_with_index do |char, i|
118
+ num = char.to_i
119
+ sum += num * weights[i]
120
+ end
121
+
122
+ check_digit = id[9].to_i
123
+ sum += check_digit
124
+
125
+ (sum % 10).zero?
126
+ end
127
+ end
128
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: taiwan_id_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Guanting112
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-12-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ description: 快速驗證台灣身分證、居留證、統一編號 / Validates Taiwan National ID (Citizen), Alien Resident
28
+ Certificate (ARC, both old and new formats), and Unified Business Number (UBN).
29
+ email:
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - lib/taiwan_id_validator.rb
37
+ homepage: https://github.com/guanting112/taiwan-id-validator
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.3.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.5.22
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: A validator for Taiwan National ID, ARC, and UBN (Company Tax ID)
60
+ test_files: []