phone-format-check 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d3e683fc9275de62e49c4069823104a10bd8bba980bc96d8ed2c8d4de676b5c8
4
+ data.tar.gz: 7a88abb3dcc21fc95a244433cc31d257ca5d96ee34bbad564030b13091aa050e
5
+ SHA512:
6
+ metadata.gz: eaf3a030864319616eabe9ac50b0c5fb06fcc0af36f769395b3f9ec622efa52011434ae8b506f84bdd79ad892363ca205d40c1fd0869261496f4cd6f4fc28bd9
7
+ data.tar.gz: b48c12403164158a01ae12c546213baefed485f73404d705ea0c93bde8e581b62cb1fdeac5f3de4094389d065f0d935f191a5d5c14dd5f9f4ac75709d44a5356
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 OneGTMLab
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,50 @@
1
+ # phone-format-check
2
+
3
+ Ruby command line tool and library. Give it a CSV of contact ids and phone numbers. It checks the shape of each number, counts the rows per class with their share, counts the rows that carry an extension, shows the normalised form of the valid ones and lists the numbers that appear on two or more rows. It checks shape only: a valid shape does not mean the number exists, who owns it, which country it is in, or whether it is a mobile line. It does not look anything up and does not call any service.
4
+
5
+ Part of the [OneGTMLab](https://www.onegtmlab.dev) free GTM tools. Background on the idea: [Lusha](https://www.onegtmlab.dev/tool-directory/sourcing/lusha).
6
+
7
+ ## Install
8
+
9
+ ```
10
+ gem install phone-format-check
11
+ ```
12
+
13
+ ## Use
14
+
15
+ CSV columns: contact_id, phone. Each phone goes through these steps, in this order: (1) trim; (2) cut a trailing extension, meaning text that starts with x or ext (upper or lower case, an optional dot after ext) followed by digits, which is remembered and reported but is not part of the normalised value; (3) remove spaces, dots, hyphens and round brackets; (4) the rest must be a plus sign followed by digits only. Classes: valid shape (a plus sign and from --min-digits to 15 digits; 15 is the maximum length of the international E.164 numbering plan; --min-digits is a whole number from 1 to 15, default 8, set by you), no plus sign (digits only, so the country code is unknown), too short (a plus sign and fewer digits than --min-digits, a lone plus counts here), too long (more than 15 digits after the plus), invalid characters (anything else, for example letters, a plus sign in the middle, or an en dash), empty (nothing is left before the extension). Normalised means the plus sign and the digits, for the valid shapes only; the output shows the first 5 of them. Duplicates are valid shapes with the same normalised value on two or more rows; the first 10 groups are shown, largest group first, then by value. Shares are percentages of all rows, rounded half up to 1 decimal. Contact ids are trimmed and compared exactly. Option --out FILE writes contact_id, phone, normalised and class for every row to FILE and says where. A missing file, a missing column, an empty or duplicate contact_id (the row is named), a bad --min-digits, an unknown option or a CSV with no rows stops the run with an error and a non-zero exit code.
16
+
17
+ ```
18
+ $ ruby bin/phone-format-check sample.csv
19
+ rows: 12
20
+ minimum digits: 8 (set by you), maximum 15 (E.164)
21
+
22
+ class rows share
23
+ valid shape 7 58.3%
24
+ no plus sign 1 8.3%
25
+ too short 1 8.3%
26
+ too long 1 8.3%
27
+ invalid characters 1 8.3%
28
+ empty 1 8.3%
29
+
30
+ rows with an extension: 2
31
+
32
+ normalised (first 5 valid shapes)
33
+ c01 +1 (555) 010-0123 > +15550100123
34
+ c02 +15550100123 > +15550100123
35
+ c03 +44 20 7946 0000 x45 > +442079460000
36
+ c09 +33.1.23.45.67.89 ext. 12 > +33123456789
37
+ c10 +442079460000 > +442079460000
38
+
39
+ duplicates (same normalised value on 2 or more rows): 2 group(s)
40
+ +15550100123 c01, c02, c11
41
+ +442079460000 c03, c10
42
+ ```
43
+
44
+ ## Test
45
+
46
+ ```
47
+ ruby test/run.rb
48
+ ```
49
+
50
+ Offline, no network calls, no dependencies beyond the language runtime. MIT licensed.
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/phone_format_check'
5
+
6
+ if ARGV.empty? || ARGV.include?('-h') || ARGV.include?('--help')
7
+ warn "usage: phone-format-check contacts.csv [--min-digits N] [--out FILE]\n" \
8
+ "CSV columns: contact_id, phone. More: https://www.onegtmlab.dev/tool-directory/sourcing/lusha"
9
+ exit 2
10
+ end
11
+
12
+ begin
13
+ path, min_digits, out = PhoneFormatCheck.parse_args(ARGV)
14
+ results = PhoneFormatCheck.from_csv(File.read(path), min_digits: min_digits)
15
+ print PhoneFormatCheck.render(PhoneFormatCheck.analyze(results, min_digits: min_digits))
16
+ if out
17
+ File.write(out, PhoneFormatCheck.to_csv(results))
18
+ puts "\nwrote #{results.size} rows to #{out}"
19
+ end
20
+ rescue ArgumentError, SystemCallError, CSV::MalformedCSVError => e
21
+ abort "error: #{e.message}"
22
+ end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'csv'
4
+
5
+ # Check the SHAPE of phone numbers in a CSV. It never looks anything up: a valid shape does not mean the number exists.
6
+ module PhoneFormatCheck
7
+ CLASSES = ['valid shape', 'no plus sign', 'too short', 'too long', 'invalid characters', 'empty'].freeze
8
+ MAX_DIGITS = 15 # the maximum length of the international E.164 numbering plan
9
+ DEFAULT_MIN_DIGITS = 8
10
+ EXTENSION = /(?:x|ext)\.?\s*(\d+)\z/i.freeze
11
+
12
+ Result = Struct.new(:id, :phone, :normalised, :klass, :extension, keyword_init: true)
13
+
14
+ # Percentage of n in d, rounded half up to 1 decimal. 0.0 when d is 0.
15
+ def self.pct(n, d)
16
+ return 0.0 if d.zero?
17
+
18
+ Rational(n * 100, d).round(1).to_f
19
+ end
20
+
21
+ # Steps, in this order: trim; cut a trailing extension; remove spaces, dots, hyphens and round brackets; shape test.
22
+ def self.classify(phone, min_digits: DEFAULT_MIN_DIGITS)
23
+ text = phone.to_s.strip
24
+ extension = nil
25
+ if (m = text.match(EXTENSION))
26
+ extension = m[1]
27
+ text = text[0, m.begin(0)]
28
+ end
29
+ rest = text.gsub(/[\s.\-()]/, '')
30
+ return ['empty', nil, extension] if rest.empty?
31
+ return classify_digits(rest, rest.size, min_digits, extension) if rest.match?(/\A\+\d*\z/)
32
+ return ['no plus sign', nil, extension] if rest.match?(/\A\d+\z/)
33
+
34
+ ['invalid characters', nil, extension]
35
+ end
36
+
37
+ def self.classify_digits(rest, digits, min_digits, extension)
38
+ digits -= 1 # the plus sign
39
+ return ['too short', nil, extension] if digits < min_digits
40
+ return ['too long', nil, extension] if digits > MAX_DIGITS
41
+
42
+ ['valid shape', rest, extension]
43
+ end
44
+
45
+ def self.parse_min_digits(raw)
46
+ s = raw.to_s.strip
47
+ unless s.match?(/\A\d+\z/) && s.to_i.between?(1, MAX_DIGITS)
48
+ raise ArgumentError, "bad --min-digits #{raw.inspect}, expected a whole number from 1 to #{MAX_DIGITS}"
49
+ end
50
+
51
+ s.to_i
52
+ end
53
+
54
+ # Returns an array of Result in file order. Raises ArgumentError on bad input.
55
+ def self.from_csv(text, min_digits: DEFAULT_MIN_DIGITS)
56
+ table = CSV.parse(text.sub(/\A/, ''), headers: true, header_converters: ->(h) { h.to_s.strip.downcase })
57
+ missing = %w[contact_id phone] - table.headers.compact
58
+ raise ArgumentError, "missing column(s): #{missing.join(', ')}" unless missing.empty?
59
+ raise ArgumentError, 'no rows in the CSV' if table.empty?
60
+
61
+ seen = {}
62
+ table.each_with_index.map do |r, i|
63
+ line = i + 2
64
+ id = r['contact_id'].to_s.strip
65
+ raise ArgumentError, "row #{line}: empty contact_id" if id.empty?
66
+ raise ArgumentError, "duplicate contact_id #{id.inspect} (row #{line})" if seen[id]
67
+
68
+ seen[id] = true
69
+ klass, normalised, extension = classify(r['phone'], min_digits: min_digits)
70
+ Result.new(id: id, phone: r['phone'].to_s, normalised: normalised, klass: klass, extension: extension)
71
+ end
72
+ end
73
+
74
+ def self.analyze(results, min_digits: DEFAULT_MIN_DIGITS)
75
+ total = results.size
76
+ classes = CLASSES.map do |c|
77
+ n = results.count { |r| r.klass == c }
78
+ { name: c, count: n, share: pct(n, total) }
79
+ end
80
+ groups = results.reject { |r| r.normalised.nil? }.group_by(&:normalised)
81
+ .select { |_v, rs| rs.size > 1 }
82
+ .map { |v, rs| { value: v, ids: rs.map(&:id) } }
83
+ .sort_by { |g| [-g[:ids].size, g[:value]] }
84
+ { rows: total, min_digits: min_digits, classes: classes, with_extension: results.count(&:extension),
85
+ duplicate_groups: groups, examples: results.reject { |r| r.normalised.nil? }.first(5) }
86
+ end
87
+
88
+ def self.render(a)
89
+ out = +"rows: #{a[:rows]}\n"
90
+ out << "minimum digits: #{a[:min_digits]} (set by you), maximum #{MAX_DIGITS} (E.164)\n\n"
91
+ out << format("%-20s %6s %7s\n", 'class', 'rows', 'share')
92
+ a[:classes].each { |c| out << format("%-20s %6d %6.1f%%\n", c[:name], c[:count], c[:share]) }
93
+ out << "\nrows with an extension: #{a[:with_extension]}\n"
94
+ out << "\nnormalised (first #{a[:examples].size} valid shapes)\n"
95
+ a[:examples].each { |r| out << format(" %-6s %-26s > %s\n", r.id, r.phone.strip, r.normalised) }
96
+ groups = a[:duplicate_groups]
97
+ out << "\nduplicates (same normalised value on 2 or more rows): #{groups.size} group(s)\n"
98
+ groups.first(10).each { |g| out << format(" %-16s %s\n", g[:value], g[:ids].join(', ')) }
99
+ out << " and #{groups.size - 10} more group(s) not shown\n" if groups.size > 10
100
+ out
101
+ end
102
+
103
+ def self.to_csv(results)
104
+ CSV.generate do |csv|
105
+ csv << %w[contact_id phone normalised class]
106
+ results.each { |r| csv << [r.id, r.phone, r.normalised.to_s, r.klass] }
107
+ end
108
+ end
109
+
110
+ # Returns [path, min_digits, out_path]; raises ArgumentError on bad input.
111
+ def self.parse_args(argv)
112
+ args = argv.dup
113
+ path = nil
114
+ min_digits = DEFAULT_MIN_DIGITS
115
+ out = nil
116
+ until args.empty?
117
+ arg = args.shift
118
+ case arg
119
+ when '--min-digits'
120
+ raise ArgumentError, '--min-digits needs a number' if args.empty?
121
+
122
+ min_digits = parse_min_digits(args.shift)
123
+ when '--out'
124
+ raise ArgumentError, '--out needs a file name' if args.empty?
125
+
126
+ out = args.shift.to_s
127
+ raise ArgumentError, '--out needs a non-empty file name' if out.strip.empty?
128
+ when /\A-/ then raise ArgumentError, "unknown option #{arg}"
129
+ else
130
+ raise ArgumentError, "unexpected extra argument #{arg.inspect}" if path
131
+
132
+ path = arg
133
+ end
134
+ end
135
+ raise ArgumentError, 'no CSV file given' unless path
136
+
137
+ [path, min_digits, out]
138
+ end
139
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phone-format-check
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - OneGTMLab
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: csv
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ -
17
+ - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: "0"
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ -
25
+ - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: "0"
28
+ description: "Ruby CLI and library: from a CSV of contact ids and phone numbers, classify each number by shape (valid shape, no plus sign, too short, too long, invalid characters, empty), report extensions, normalise valid shapes to a plus and digits, and list duplicates. It checks shape only and never looks a number up. Offline."
29
+ executables:
30
+ - phone-format-check
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - bin/phone-format-check
37
+ - lib/phone_format_check.rb
38
+ homepage: "https://www.onegtmlab.dev/tool-directory/sourcing/lusha"
39
+ licenses:
40
+ - MIT
41
+ metadata:
42
+ source_code_uri: "https://github.com/jaizendra-netizen/phone-format-check"
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ -
49
+ - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "2.7"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ -
55
+ - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ requirements: []
59
+ rubygems_version: 4.1.0
60
+ specification_version: 4
61
+ summary: "Check the shape of phone numbers in a CSV: counts per class, extensions, normalised values and duplicates"
62
+ test_files: []