br-utils 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/.gitignore +120 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +348 -0
- data/Rakefile +6 -0
- data/examples/boleto_usage_example.rb +79 -0
- data/examples/cep_usage_example.rb +148 -0
- data/examples/cnh_usage_example.rb +120 -0
- data/examples/cnpj_usage_example.rb +227 -0
- data/examples/cpf_usage_example.rb +237 -0
- data/examples/currency_usage_example.rb +266 -0
- data/examples/date_usage_example.rb +259 -0
- data/examples/email_usage_example.rb +321 -0
- data/examples/legal_nature_usage_example.rb +437 -0
- data/examples/legal_process_usage_example.rb +444 -0
- data/examples/license_plate_usage_example.rb +440 -0
- data/examples/phone_usage_example.rb +595 -0
- data/examples/pis_usage_example.rb +588 -0
- data/examples/renavam_usage_example.rb +499 -0
- data/examples/voter_id_usage_example.rb +573 -0
- data/lib/brazilian-utils/boleto-utils.rb +176 -0
- data/lib/brazilian-utils/cep-utils.rb +330 -0
- data/lib/brazilian-utils/cnh-utils.rb +88 -0
- data/lib/brazilian-utils/cnpj-utils.rb +202 -0
- data/lib/brazilian-utils/cpf-utils.rb +192 -0
- data/lib/brazilian-utils/currency-utils.rb +226 -0
- data/lib/brazilian-utils/data/legal_process_ids.json +38 -0
- data/lib/brazilian-utils/date-utils.rb +244 -0
- data/lib/brazilian-utils/email-utils.rb +54 -0
- data/lib/brazilian-utils/legal-nature-utils.rb +235 -0
- data/lib/brazilian-utils/legal-process-utils.rb +240 -0
- data/lib/brazilian-utils/license-plate-utils.rb +279 -0
- data/lib/brazilian-utils/phone-utils.rb +272 -0
- data/lib/brazilian-utils/pis-utils.rb +151 -0
- data/lib/brazilian-utils/renavam-utils.rb +113 -0
- data/lib/brazilian-utils/voter-id-utils.rb +165 -0
- metadata +123 -0
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
require 'brazilian-utils/phone-utils'
|
|
2
|
+
|
|
3
|
+
include BrazilianUtils::PhoneUtils
|
|
4
|
+
|
|
5
|
+
puts "=" * 80
|
|
6
|
+
puts "Brazilian Phone Utils - Usage Examples"
|
|
7
|
+
puts "=" * 80
|
|
8
|
+
|
|
9
|
+
# ============================================================================
|
|
10
|
+
# Section 1: Formatting Mobile Numbers
|
|
11
|
+
# ============================================================================
|
|
12
|
+
puts "\n1. Formatting Mobile Numbers"
|
|
13
|
+
puts "-" * 80
|
|
14
|
+
|
|
15
|
+
mobile_numbers = [
|
|
16
|
+
'11994029275',
|
|
17
|
+
'21987654321',
|
|
18
|
+
'85912345678',
|
|
19
|
+
'47999887766'
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
mobile_numbers.each do |phone|
|
|
23
|
+
formatted = format_phone(phone)
|
|
24
|
+
puts " #{phone.ljust(15)} → #{formatted}"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# ============================================================================
|
|
28
|
+
# Section 2: Formatting Landline Numbers
|
|
29
|
+
# ============================================================================
|
|
30
|
+
puts "\n2. Formatting Landline Numbers"
|
|
31
|
+
puts "-" * 80
|
|
32
|
+
|
|
33
|
+
landline_numbers = [
|
|
34
|
+
'1635014415',
|
|
35
|
+
'1122334455',
|
|
36
|
+
'2133445566',
|
|
37
|
+
'8544556677',
|
|
38
|
+
'4755667788'
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
landline_numbers.each do |phone|
|
|
42
|
+
formatted = format_phone(phone)
|
|
43
|
+
puts " #{phone.ljust(15)} → #{formatted}"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# ============================================================================
|
|
47
|
+
# Section 3: Using the format Alias
|
|
48
|
+
# ============================================================================
|
|
49
|
+
puts "\n3. Using the format Alias"
|
|
50
|
+
puts "-" * 80
|
|
51
|
+
|
|
52
|
+
test_phones = ['11994029275', '1635014415', '21987654321']
|
|
53
|
+
|
|
54
|
+
test_phones.each do |phone|
|
|
55
|
+
formatted = format(phone)
|
|
56
|
+
puts " #{phone.ljust(15)} → #{formatted}"
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# ============================================================================
|
|
60
|
+
# Section 4: Validating Any Type (Default)
|
|
61
|
+
# ============================================================================
|
|
62
|
+
puts "\n4. Validating Any Type (Default)"
|
|
63
|
+
puts "-" * 80
|
|
64
|
+
|
|
65
|
+
all_types = [
|
|
66
|
+
'11994029275', # Valid mobile
|
|
67
|
+
'1635014415', # Valid landline
|
|
68
|
+
'21987654321', # Valid mobile
|
|
69
|
+
'1122334455', # Valid landline
|
|
70
|
+
'123456', # Invalid - too short
|
|
71
|
+
'01987654321', # Invalid - DDD starts with 0
|
|
72
|
+
'1166778899', # Invalid - landline with 6 after DDD
|
|
73
|
+
'' # Invalid - empty
|
|
74
|
+
]
|
|
75
|
+
|
|
76
|
+
all_types.each do |phone|
|
|
77
|
+
valid = is_valid(phone)
|
|
78
|
+
status = valid ? '✓ VALID' : '✗ INVALID'
|
|
79
|
+
puts " #{phone.ljust(20)} → #{status}"
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# ============================================================================
|
|
83
|
+
# Section 5: Validating Mobile Numbers Only
|
|
84
|
+
# ============================================================================
|
|
85
|
+
puts "\n5. Validating Mobile Numbers Only"
|
|
86
|
+
puts "-" * 80
|
|
87
|
+
|
|
88
|
+
mobile_tests = [
|
|
89
|
+
'11994029275', # Valid mobile
|
|
90
|
+
'21987654321', # Valid mobile
|
|
91
|
+
'85912345678', # Valid mobile
|
|
92
|
+
'1635014415', # Invalid - landline
|
|
93
|
+
'11887654321', # Invalid - no 9 after DDD
|
|
94
|
+
'123456' # Invalid - too short
|
|
95
|
+
]
|
|
96
|
+
|
|
97
|
+
mobile_tests.each do |phone|
|
|
98
|
+
valid = is_valid(phone, :mobile)
|
|
99
|
+
status = valid ? '✓ VALID MOBILE' : '✗ NOT MOBILE'
|
|
100
|
+
puts " #{phone.ljust(20)} → #{status}"
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# ============================================================================
|
|
104
|
+
# Section 6: Validating Landline Numbers Only
|
|
105
|
+
# ============================================================================
|
|
106
|
+
puts "\n6. Validating Landline Numbers Only"
|
|
107
|
+
puts "-" * 80
|
|
108
|
+
|
|
109
|
+
landline_tests = [
|
|
110
|
+
'1635014415', # Valid landline
|
|
111
|
+
'1122334455', # Valid landline (starts with 2)
|
|
112
|
+
'2133445566', # Valid landline (starts with 3)
|
|
113
|
+
'8544556677', # Valid landline (starts with 4)
|
|
114
|
+
'4755667788', # Valid landline (starts with 5)
|
|
115
|
+
'11994029275', # Invalid - mobile
|
|
116
|
+
'1166778899', # Invalid - starts with 6
|
|
117
|
+
'123456' # Invalid - too short
|
|
118
|
+
]
|
|
119
|
+
|
|
120
|
+
landline_tests.each do |phone|
|
|
121
|
+
valid = is_valid(phone, :landline)
|
|
122
|
+
status = valid ? '✓ VALID LANDLINE' : '✗ NOT LANDLINE'
|
|
123
|
+
puts " #{phone.ljust(20)} → #{status}"
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# ============================================================================
|
|
127
|
+
# Section 7: Using String Type Parameter
|
|
128
|
+
# ============================================================================
|
|
129
|
+
puts "\n7. Using String Type Parameter"
|
|
130
|
+
puts "-" * 80
|
|
131
|
+
|
|
132
|
+
puts " Testing with 'mobile' string:"
|
|
133
|
+
puts " 11994029275 → #{is_valid('11994029275', 'mobile')}"
|
|
134
|
+
puts " 1635014415 → #{is_valid('1635014415', 'mobile')}"
|
|
135
|
+
|
|
136
|
+
puts "\n Testing with 'landline' string:"
|
|
137
|
+
puts " 1635014415 → #{is_valid('1635014415', 'landline')}"
|
|
138
|
+
puts " 11994029275 → #{is_valid('11994029275', 'landline')}"
|
|
139
|
+
|
|
140
|
+
# ============================================================================
|
|
141
|
+
# Section 8: Using the valid? Alias
|
|
142
|
+
# ============================================================================
|
|
143
|
+
puts "\n8. Using the valid? Alias"
|
|
144
|
+
puts "-" * 80
|
|
145
|
+
|
|
146
|
+
test_numbers = ['11994029275', '1635014415', 'INVALID123']
|
|
147
|
+
|
|
148
|
+
test_numbers.each do |phone|
|
|
149
|
+
if valid?(phone)
|
|
150
|
+
puts " ✓ #{phone} is valid"
|
|
151
|
+
else
|
|
152
|
+
puts " ✗ #{phone} is invalid"
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# ============================================================================
|
|
157
|
+
# Section 9: Removing Symbols
|
|
158
|
+
# ============================================================================
|
|
159
|
+
puts "\n9. Removing Symbols"
|
|
160
|
+
puts "-" * 80
|
|
161
|
+
|
|
162
|
+
phones_with_symbols = [
|
|
163
|
+
'(11)994029275',
|
|
164
|
+
'11-99402-9275',
|
|
165
|
+
'+5511994029275',
|
|
166
|
+
'11 99402 9275',
|
|
167
|
+
'+55 (11) 99402-9275',
|
|
168
|
+
'(16) 3501-4415'
|
|
169
|
+
]
|
|
170
|
+
|
|
171
|
+
phones_with_symbols.each do |phone|
|
|
172
|
+
clean = remove_symbols_phone(phone)
|
|
173
|
+
puts " #{phone.ljust(25)} → #{clean}"
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# ============================================================================
|
|
177
|
+
# Section 10: Using remove_symbols Alias
|
|
178
|
+
# ============================================================================
|
|
179
|
+
puts "\n10. Using remove_symbols Alias"
|
|
180
|
+
puts "-" * 80
|
|
181
|
+
|
|
182
|
+
puts " remove_symbols:"
|
|
183
|
+
puts " (11)99402-9275 → #{remove_symbols('(11)99402-9275')}"
|
|
184
|
+
|
|
185
|
+
puts "\n sieve:"
|
|
186
|
+
puts " +55 11 99402-9275 → #{sieve('+55 11 99402-9275')}"
|
|
187
|
+
|
|
188
|
+
# ============================================================================
|
|
189
|
+
# Section 11: Removing International Dialing Code
|
|
190
|
+
# ============================================================================
|
|
191
|
+
puts "\n11. Removing International Dialing Code"
|
|
192
|
+
puts "-" * 80
|
|
193
|
+
|
|
194
|
+
international_numbers = [
|
|
195
|
+
'5511994029275',
|
|
196
|
+
'+5511994029275',
|
|
197
|
+
'551635014415',
|
|
198
|
+
'+551635014415',
|
|
199
|
+
'11994029275',
|
|
200
|
+
'1635014415',
|
|
201
|
+
'555511994029275'
|
|
202
|
+
]
|
|
203
|
+
|
|
204
|
+
international_numbers.each do |phone|
|
|
205
|
+
without_code = remove_international_dialing_code(phone)
|
|
206
|
+
puts " #{phone.ljust(20)} → #{without_code}"
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# ============================================================================
|
|
210
|
+
# Section 12: Generating Random Type (Default)
|
|
211
|
+
# ============================================================================
|
|
212
|
+
puts "\n12. Generating Random Type (Default)"
|
|
213
|
+
puts "-" * 80
|
|
214
|
+
|
|
215
|
+
puts " Generating 5 random phone numbers (mobile or landline):"
|
|
216
|
+
5.times do
|
|
217
|
+
phone = generate
|
|
218
|
+
type = is_valid(phone, :mobile) ? 'Mobile' : 'Landline'
|
|
219
|
+
formatted = format_phone(phone)
|
|
220
|
+
puts " #{formatted.ljust(20)} [#{type}] (#{phone})"
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# ============================================================================
|
|
224
|
+
# Section 13: Generating Mobile Numbers
|
|
225
|
+
# ============================================================================
|
|
226
|
+
puts "\n13. Generating Mobile Numbers"
|
|
227
|
+
puts "-" * 80
|
|
228
|
+
|
|
229
|
+
puts " Generating 5 mobile numbers:"
|
|
230
|
+
5.times do
|
|
231
|
+
phone = generate(:mobile)
|
|
232
|
+
formatted = format_phone(phone)
|
|
233
|
+
puts " #{formatted.ljust(20)} (#{phone}) - 3rd digit: #{phone[2]}"
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# ============================================================================
|
|
237
|
+
# Section 14: Generating Landline Numbers
|
|
238
|
+
# ============================================================================
|
|
239
|
+
puts "\n14. Generating Landline Numbers"
|
|
240
|
+
puts "-" * 80
|
|
241
|
+
|
|
242
|
+
puts " Generating 5 landline numbers:"
|
|
243
|
+
5.times do
|
|
244
|
+
phone = generate(:landline)
|
|
245
|
+
formatted = format_phone(phone)
|
|
246
|
+
puts " #{formatted.ljust(20)} (#{phone}) - 3rd digit: #{phone[2]}"
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
# ============================================================================
|
|
250
|
+
# Section 15: Using String Type for Generation
|
|
251
|
+
# ============================================================================
|
|
252
|
+
puts "\n15. Using String Type for Generation"
|
|
253
|
+
puts "-" * 80
|
|
254
|
+
|
|
255
|
+
puts " Generating with 'mobile' string:"
|
|
256
|
+
3.times do
|
|
257
|
+
phone = generate('mobile')
|
|
258
|
+
puts " #{format(phone)}"
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
puts "\n Generating with 'landline' string:"
|
|
262
|
+
3.times do
|
|
263
|
+
phone = generate('landline')
|
|
264
|
+
puts " #{format(phone)}"
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
# ============================================================================
|
|
268
|
+
# Section 16: Complete Workflow - User Input with International Code
|
|
269
|
+
# ============================================================================
|
|
270
|
+
puts "\n16. Complete Workflow - User Input with International Code"
|
|
271
|
+
puts "-" * 80
|
|
272
|
+
|
|
273
|
+
user_input = '+55 (11) 99402-9275'
|
|
274
|
+
|
|
275
|
+
puts " User input: #{user_input}"
|
|
276
|
+
puts
|
|
277
|
+
|
|
278
|
+
# Step 1: Remove symbols
|
|
279
|
+
clean = remove_symbols(user_input)
|
|
280
|
+
puts " 1. After remove_symbols: #{clean}"
|
|
281
|
+
|
|
282
|
+
# Step 2: Remove international code
|
|
283
|
+
without_code = remove_international_dialing_code(clean)
|
|
284
|
+
puts " 2. After remove_international_dialing_code: #{without_code}"
|
|
285
|
+
|
|
286
|
+
# Step 3: Validate
|
|
287
|
+
if is_valid(without_code)
|
|
288
|
+
puts " 3. Validation: ✓ VALID"
|
|
289
|
+
|
|
290
|
+
# Step 4: Check type
|
|
291
|
+
if is_valid(without_code, :mobile)
|
|
292
|
+
puts " 4. Type: Mobile"
|
|
293
|
+
elsif is_valid(without_code, :landline)
|
|
294
|
+
puts " 4. Type: Landline"
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
# Step 5: Format for display
|
|
298
|
+
formatted = format_phone(without_code)
|
|
299
|
+
puts " 5. Formatted: #{formatted}"
|
|
300
|
+
else
|
|
301
|
+
puts " 3. Validation: ✗ INVALID"
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
# ============================================================================
|
|
305
|
+
# Section 17: Complete Workflow - Landline
|
|
306
|
+
# ============================================================================
|
|
307
|
+
puts "\n17. Complete Workflow - Landline"
|
|
308
|
+
puts "-" * 80
|
|
309
|
+
|
|
310
|
+
landline_input = '(16) 3501-4415'
|
|
311
|
+
|
|
312
|
+
puts " User input: #{landline_input}"
|
|
313
|
+
puts
|
|
314
|
+
|
|
315
|
+
# Clean
|
|
316
|
+
clean = remove_symbols(landline_input)
|
|
317
|
+
puts " 1. After remove_symbols: #{clean}"
|
|
318
|
+
|
|
319
|
+
# Validate
|
|
320
|
+
if valid?(clean)
|
|
321
|
+
puts " 2. Validation: ✓ VALID"
|
|
322
|
+
|
|
323
|
+
# Check type
|
|
324
|
+
if valid?(clean, :mobile)
|
|
325
|
+
puts " 3. Type: Mobile"
|
|
326
|
+
elsif valid?(clean, :landline)
|
|
327
|
+
puts " 3. Type: Landline"
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
# Format
|
|
331
|
+
formatted = format(clean)
|
|
332
|
+
puts " 4. Formatted: #{formatted}"
|
|
333
|
+
else
|
|
334
|
+
puts " 2. Validation: ✗ INVALID"
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
# ============================================================================
|
|
338
|
+
# Section 18: Validating Phone List
|
|
339
|
+
# ============================================================================
|
|
340
|
+
puts "\n18. Validating Phone List"
|
|
341
|
+
puts "-" * 80
|
|
342
|
+
|
|
343
|
+
phone_list = [
|
|
344
|
+
'11994029275',
|
|
345
|
+
'1635014415',
|
|
346
|
+
'(21)98765-4321',
|
|
347
|
+
'+5585912345678',
|
|
348
|
+
'123456',
|
|
349
|
+
'01987654321',
|
|
350
|
+
'1166778899'
|
|
351
|
+
]
|
|
352
|
+
|
|
353
|
+
puts " Validating #{phone_list.length} phone numbers:"
|
|
354
|
+
puts
|
|
355
|
+
|
|
356
|
+
valid_count = 0
|
|
357
|
+
invalid_count = 0
|
|
358
|
+
|
|
359
|
+
phone_list.each do |phone|
|
|
360
|
+
# Clean if needed
|
|
361
|
+
clean = remove_symbols(phone)
|
|
362
|
+
clean = remove_international_dialing_code(clean)
|
|
363
|
+
|
|
364
|
+
if valid?(clean)
|
|
365
|
+
valid_count += 1
|
|
366
|
+
type = valid?(clean, :mobile) ? 'Mobile' : 'Landline'
|
|
367
|
+
formatted = format(clean)
|
|
368
|
+
puts " ✓ #{formatted.ljust(20)} [#{type}]"
|
|
369
|
+
else
|
|
370
|
+
invalid_count += 1
|
|
371
|
+
puts " ✗ #{phone.ljust(20)} [INVALID]"
|
|
372
|
+
end
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
puts
|
|
376
|
+
puts " Summary:"
|
|
377
|
+
puts " Valid: #{valid_count}"
|
|
378
|
+
puts " Invalid: #{invalid_count}"
|
|
379
|
+
puts " Total: #{phone_list.length}"
|
|
380
|
+
|
|
381
|
+
# ============================================================================
|
|
382
|
+
# Section 19: Generating Test Data
|
|
383
|
+
# ============================================================================
|
|
384
|
+
puts "\n19. Generating Test Data"
|
|
385
|
+
puts "-" * 80
|
|
386
|
+
|
|
387
|
+
puts " Generating test dataset with 10 phones (5 mobile, 5 landline):"
|
|
388
|
+
puts
|
|
389
|
+
|
|
390
|
+
test_data = []
|
|
391
|
+
|
|
392
|
+
puts " Mobile numbers:"
|
|
393
|
+
5.times do
|
|
394
|
+
phone = generate(:mobile)
|
|
395
|
+
test_data << { number: phone, type: 'mobile' }
|
|
396
|
+
puts " #{format(phone)}"
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
puts "\n Landline numbers:"
|
|
400
|
+
5.times do
|
|
401
|
+
phone = generate(:landline)
|
|
402
|
+
test_data << { number: phone, type: 'landline' }
|
|
403
|
+
puts " #{format(phone)}"
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
puts "\n Verifying all generated numbers are valid:"
|
|
407
|
+
test_data.each do |data|
|
|
408
|
+
valid = is_valid(data[:number], data[:type].to_sym)
|
|
409
|
+
status = valid ? '✓' : '✗'
|
|
410
|
+
puts " #{status} #{format(data[:number])} [#{data[:type]}]"
|
|
411
|
+
end
|
|
412
|
+
|
|
413
|
+
# ============================================================================
|
|
414
|
+
# Section 20: Phone Type Detection
|
|
415
|
+
# ============================================================================
|
|
416
|
+
puts "\n20. Phone Type Detection"
|
|
417
|
+
puts "-" * 80
|
|
418
|
+
|
|
419
|
+
mixed_phones = [
|
|
420
|
+
'11994029275',
|
|
421
|
+
'1635014415',
|
|
422
|
+
'21987654321',
|
|
423
|
+
'1122334455',
|
|
424
|
+
'85912345678',
|
|
425
|
+
'4755667788'
|
|
426
|
+
]
|
|
427
|
+
|
|
428
|
+
puts " Detecting phone types:"
|
|
429
|
+
mixed_phones.each do |phone|
|
|
430
|
+
if is_valid(phone, :mobile)
|
|
431
|
+
type = 'Mobile'
|
|
432
|
+
pattern = 'DDD + 9 + 8 digits'
|
|
433
|
+
elsif is_valid(phone, :landline)
|
|
434
|
+
type = 'Landline'
|
|
435
|
+
digit = phone[2]
|
|
436
|
+
pattern = "DDD + #{digit} + 7 digits"
|
|
437
|
+
else
|
|
438
|
+
type = 'Invalid'
|
|
439
|
+
pattern = 'N/A'
|
|
440
|
+
end
|
|
441
|
+
|
|
442
|
+
formatted = format(phone) || phone
|
|
443
|
+
puts " #{formatted.ljust(20)} → #{type.ljust(10)} (#{pattern})"
|
|
444
|
+
end
|
|
445
|
+
|
|
446
|
+
# ============================================================================
|
|
447
|
+
# Section 21: Error Handling
|
|
448
|
+
# ============================================================================
|
|
449
|
+
puts "\n21. Error Handling"
|
|
450
|
+
puts "-" * 80
|
|
451
|
+
|
|
452
|
+
invalid_inputs = [
|
|
453
|
+
nil,
|
|
454
|
+
'',
|
|
455
|
+
123456,
|
|
456
|
+
'ABC123DEF456',
|
|
457
|
+
'(11)99402-9275', # Has symbols
|
|
458
|
+
'00987654321', # DDD starts with 0
|
|
459
|
+
'1199001122' # 10 digits starting with 9 (neither mobile nor landline)
|
|
460
|
+
]
|
|
461
|
+
|
|
462
|
+
invalid_inputs.each do |input|
|
|
463
|
+
puts " Input: #{input.inspect.ljust(25)}"
|
|
464
|
+
puts " is_valid: #{is_valid(input)}"
|
|
465
|
+
puts " format_phone: #{format_phone(input).inspect}"
|
|
466
|
+
|
|
467
|
+
if input.is_a?(String) && !input.empty?
|
|
468
|
+
clean = remove_symbols(input)
|
|
469
|
+
puts " remove_symbols: #{clean.inspect}"
|
|
470
|
+
end
|
|
471
|
+
|
|
472
|
+
puts
|
|
473
|
+
end
|
|
474
|
+
|
|
475
|
+
# ============================================================================
|
|
476
|
+
# Section 22: DDD (Area Code) Examples
|
|
477
|
+
# ============================================================================
|
|
478
|
+
puts "\n22. DDD (Area Code) Examples"
|
|
479
|
+
puts "-" * 80
|
|
480
|
+
|
|
481
|
+
ddd_examples = [
|
|
482
|
+
{ ddd: '11', city: 'São Paulo (SP)', mobile: '11987654321', landline: '1133221100' },
|
|
483
|
+
{ ddd: '21', city: 'Rio de Janeiro (RJ)', mobile: '21987654321', landline: '2133221100' },
|
|
484
|
+
{ ddd: '85', city: 'Fortaleza (CE)', mobile: '85987654321', landline: '8533221100' },
|
|
485
|
+
{ ddd: '47', city: 'Joinville/Blumenau (SC)', mobile: '47987654321', landline: '4733221100' },
|
|
486
|
+
{ ddd: '51', city: 'Porto Alegre (RS)', mobile: '51987654321', landline: '5133221100' }
|
|
487
|
+
]
|
|
488
|
+
|
|
489
|
+
ddd_examples.each do |example|
|
|
490
|
+
puts " DDD #{example[:ddd]} - #{example[:city]}"
|
|
491
|
+
puts " Mobile: #{format(example[:mobile])}"
|
|
492
|
+
puts " Landline: #{format(example[:landline])}"
|
|
493
|
+
puts
|
|
494
|
+
end
|
|
495
|
+
|
|
496
|
+
# ============================================================================
|
|
497
|
+
# Section 23: Reverse Operation (Format → Clean → Format)
|
|
498
|
+
# ============================================================================
|
|
499
|
+
puts "\n23. Reverse Operation (Format → Clean → Format)"
|
|
500
|
+
puts "-" * 80
|
|
501
|
+
|
|
502
|
+
original_numbers = ['11994029275', '1635014415']
|
|
503
|
+
|
|
504
|
+
original_numbers.each do |phone|
|
|
505
|
+
# Format
|
|
506
|
+
formatted = format(phone)
|
|
507
|
+
puts " Original: #{phone}"
|
|
508
|
+
puts " 1. Formatted: #{formatted}"
|
|
509
|
+
|
|
510
|
+
# Remove symbols (clean)
|
|
511
|
+
cleaned = remove_symbols(formatted)
|
|
512
|
+
puts " 2. Cleaned: #{cleaned}"
|
|
513
|
+
|
|
514
|
+
# Format again
|
|
515
|
+
reformatted = format(cleaned)
|
|
516
|
+
puts " 3. Reformatted: #{reformatted}"
|
|
517
|
+
|
|
518
|
+
# Verify they match
|
|
519
|
+
match = (formatted == reformatted)
|
|
520
|
+
puts " 4. Match: #{match ? '✓ YES' : '✗ NO'}"
|
|
521
|
+
puts
|
|
522
|
+
end
|
|
523
|
+
|
|
524
|
+
# ============================================================================
|
|
525
|
+
# Section 24: Batch Generation with Statistics
|
|
526
|
+
# ============================================================================
|
|
527
|
+
puts "\n24. Batch Generation with Statistics"
|
|
528
|
+
puts "-" * 80
|
|
529
|
+
|
|
530
|
+
puts " Generating 20 random phones and analyzing distribution:"
|
|
531
|
+
puts
|
|
532
|
+
|
|
533
|
+
generated = 20.times.map { generate }
|
|
534
|
+
|
|
535
|
+
mobile_count = generated.count { |p| is_valid(p, :mobile) }
|
|
536
|
+
landline_count = generated.count { |p| is_valid(p, :landline) }
|
|
537
|
+
|
|
538
|
+
puts " Statistics:"
|
|
539
|
+
puts " Total generated: #{generated.length}"
|
|
540
|
+
puts " Mobile: #{mobile_count} (#{(mobile_count * 100.0 / generated.length).round(1)}%)"
|
|
541
|
+
puts " Landline: #{landline_count} (#{(landline_count * 100.0 / generated.length).round(1)}%)"
|
|
542
|
+
puts
|
|
543
|
+
puts " Sample (first 5):"
|
|
544
|
+
generated.first(5).each do |phone|
|
|
545
|
+
type = is_valid(phone, :mobile) ? 'Mobile' : 'Landline'
|
|
546
|
+
puts " #{format(phone).ljust(20)} [#{type}]"
|
|
547
|
+
end
|
|
548
|
+
|
|
549
|
+
# ============================================================================
|
|
550
|
+
# Section 25: Format Comparison Table
|
|
551
|
+
# ============================================================================
|
|
552
|
+
puts "\n25. Format Comparison Table"
|
|
553
|
+
puts "-" * 80
|
|
554
|
+
|
|
555
|
+
puts " Mobile vs Landline Comparison:"
|
|
556
|
+
puts
|
|
557
|
+
puts " Aspect | Mobile | Landline"
|
|
558
|
+
puts " ----------------|---------------------|--------------------"
|
|
559
|
+
puts " Total Digits | 11 | 10"
|
|
560
|
+
puts " Pattern | (DD)9NNNN-NNNN | (DD)NNNN-NNNN"
|
|
561
|
+
puts " DDD | 2 digits (11-99) | 2 digits (11-99)"
|
|
562
|
+
puts " 3rd Digit | Always 9 | 2, 3, 4, or 5"
|
|
563
|
+
puts " Example | (11)99402-9275 | (16)3501-4415"
|
|
564
|
+
puts " Raw Format | 11994029275 | 1635014415"
|
|
565
|
+
puts
|
|
566
|
+
|
|
567
|
+
# ============================================================================
|
|
568
|
+
# Section 26: International Code Scenarios
|
|
569
|
+
# ============================================================================
|
|
570
|
+
puts "\n26. International Code Scenarios"
|
|
571
|
+
puts "-" * 80
|
|
572
|
+
|
|
573
|
+
intl_scenarios = [
|
|
574
|
+
{ input: '5511994029275', expected: '11994029275', description: 'Mobile with 55' },
|
|
575
|
+
{ input: '+5511994029275', expected: '+11994029275', description: 'Mobile with +55' },
|
|
576
|
+
{ input: '551635014415', expected: '1635014415', description: 'Landline with 55' },
|
|
577
|
+
{ input: '11994029275', expected: '11994029275', description: 'Mobile without code' },
|
|
578
|
+
{ input: '555511994029275', expected: '5511994029275', description: 'Double 55 (removes first)' }
|
|
579
|
+
]
|
|
580
|
+
|
|
581
|
+
intl_scenarios.each do |scenario|
|
|
582
|
+
result = remove_international_dialing_code(scenario[:input])
|
|
583
|
+
match = (result == scenario[:expected])
|
|
584
|
+
status = match ? '✓' : '✗'
|
|
585
|
+
|
|
586
|
+
puts " #{status} #{scenario[:description]}"
|
|
587
|
+
puts " Input: #{scenario[:input]}"
|
|
588
|
+
puts " Expected: #{scenario[:expected]}"
|
|
589
|
+
puts " Result: #{result}"
|
|
590
|
+
puts
|
|
591
|
+
end
|
|
592
|
+
|
|
593
|
+
puts "=" * 80
|
|
594
|
+
puts "Phone Utils Examples Complete!"
|
|
595
|
+
puts "=" * 80
|