├── .rspec ├── data ├── special_codes_11.txt ├── cellphone_codes.txt ├── special_codes_10.txt └── area_codes.txt ├── lib ├── tel_formatter │ └── version.rb └── tel_formatter.rb ├── spec ├── spec_helper.rb └── tel_formatter_spec.rb ├── .travis.yml ├── Gemfile ├── .gitignore ├── Rakefile ├── tel_formatter.gemspec ├── LICENSE.txt └── README.md /.rspec: -------------------------------------------------------------------------------- 1 | -c 2 | -------------------------------------------------------------------------------- /data/special_codes_11.txt: -------------------------------------------------------------------------------- 1 | 0800 2 | -------------------------------------------------------------------------------- /data/cellphone_codes.txt: -------------------------------------------------------------------------------- 1 | 020 2 | 050 3 | 060 4 | 070 5 | 080 6 | 090 7 | -------------------------------------------------------------------------------- /data/special_codes_10.txt: -------------------------------------------------------------------------------- 1 | 0120 2 | 0990 3 | 0180 4 | 0570 5 | 0170 6 | -------------------------------------------------------------------------------- /lib/tel_formatter/version.rb: -------------------------------------------------------------------------------- 1 | module TelFormatter 2 | VERSION = "1.0.0" 3 | end 4 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'coveralls' 2 | Coveralls.wear! 3 | 4 | require 'rspec' 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | script: bundle exec rspec 3 | rvm: 4 | - 2.6.2 5 | - 2.5.5 6 | - 2.4.6 7 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | # Specify your gem"s dependencies in tel_formatter.gemspec 4 | gemspec 5 | 6 | group :test do 7 | gem "rspec" 8 | gem "coveralls", require: false 9 | end 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | 3 | begin 4 | require "rspec/core/rake_task" 5 | RSpec::Core::RakeTask.new(:spec) do |spec| 6 | spec.pattern = "spec/**/*_spec.rb" 7 | spec.rspec_opts = ["-cfs"] 8 | end 9 | rescue LoadError => e 10 | end 11 | -------------------------------------------------------------------------------- /tel_formatter.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path("../lib", __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require "tel_formatter/version" 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "tel_formatter" 8 | spec.version = TelFormatter::VERSION 9 | spec.authors = ["iTakeshi"] 10 | spec.email = ["takeshi.ito.doraemon@gmail.com"] 11 | spec.summary = %q{Format your telephone numbers} 12 | spec.homepage = "https://github.com/iTakeshi/tel_formatter" 13 | spec.license = "MIT" 14 | 15 | spec.files = `git ls-files`.split($/) 16 | spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) 17 | spec.require_paths = ["lib"] 18 | 19 | spec.add_development_dependency "bundler" 20 | spec.add_development_dependency "rake" 21 | end 22 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 iTakeshi 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TelFormatter 2 | 3 | [![Build Status](https://travis-ci.org/iTakeshi/tel_formatter.png?branch=master)](https://travis-ci.org/iTakeshi/tel_formatter) 4 | [![Coverage Status](https://coveralls.io/repos/iTakeshi/tel_formatter/badge.png)](https://coveralls.io/r/iTakeshi/tel_formatter) 5 | 6 | Format your telephone numbers. 7 | 8 | ## Notice 9 | 10 | This gem is only applicable for Japanese phone number formatting. 11 | 12 | ## Installation 13 | 14 | Add this line to your application's Gemfile: 15 | 16 | gem 'tel_formatter' 17 | 18 | And then execute: 19 | 20 | $ bundle 21 | 22 | Or install it yourself as: 23 | 24 | $ gem install tel_formatter 25 | 26 | ## Usage 27 | 28 | ```ruby 29 | require 'tel_formatter' 30 | 31 | TelFormatter.format("0312345678") #=> "03-1234-5678" 32 | TelFormatter.format("03.1235.5678") #=> "03-1234-5678" 33 | ``` 34 | 35 | ## Contributing 36 | 37 | 1. Fork it ( http://github.com/iTakeshi/tel_formatter/fork ) 38 | 2. Create your feature branch (`git checkout -b my-new-feature`) 39 | 3. Commit your changes (`git commit -am 'Add some feature'`) 40 | 4. Write spec and run tests (`bundle exec rspec`) 41 | 5. Push to the branch (`git push origin my-new-feature`) 42 | 6. Create new Pull Request 43 | -------------------------------------------------------------------------------- /lib/tel_formatter.rb: -------------------------------------------------------------------------------- 1 | require "nkf" 2 | require "tel_formatter/version" 3 | 4 | module TelFormatter 5 | AREA_CODES = File.read(File.expand_path("../../data/area_codes.txt", __FILE__)).split("\n").reverse 6 | SPECIAL_CODES_10 = File.read(File.expand_path("../../data/special_codes_10.txt", __FILE__)).split("\n").reverse 7 | SPECIAL_CODES_11 = File.read(File.expand_path("../../data/special_codes_11.txt", __FILE__)).split("\n").reverse 8 | CELLPHONE_CODES = File.read(File.expand_path("../../data/cellphone_codes.txt", __FILE__)).split("\n").reverse 9 | AREA_CODE_REGEXP = /\A(#{AREA_CODES.join("|")})(\d{1,4})(\d{4})\Z/ 10 | SPECIAL_CODE_10_REGEXP = /\A(#{SPECIAL_CODES_10.join("|")})(\d{6})\Z/ 11 | SPECIAL_CODE_11_REGEXP = /\A(#{SPECIAL_CODES_11.join("|")})(\d{7})\Z/ 12 | CELLPHONE_CODE_10_REGEXP = /\A(#{CELLPHONE_CODES.join("|")})(\d{3})(\d{4})\Z/ 13 | CELLPHONE_CODE_REGEXP = /\A(#{CELLPHONE_CODES.join("|")})([1-9]\d{3})(\d{4})\Z/ 14 | 15 | def self.format(tel) 16 | self.split(tel).join("-") 17 | end 18 | 19 | def self.split(tel) 20 | tel = self.preprocess(tel) 21 | case tel.length 22 | when 10 23 | if AREA_CODE_REGEXP =~ tel 24 | return [$1, $2, $3] 25 | elsif SPECIAL_CODE_10_REGEXP =~ tel 26 | return [$1, $2] 27 | elsif CELLPHONE_CODE_10_REGEXP =~ tel 28 | return [$1, $2, $3] 29 | else 30 | raise ArgumentError, "Invalid telephone number" 31 | end 32 | when 11 33 | if CELLPHONE_CODE_REGEXP =~ tel 34 | return [$1, $2, $3] 35 | elsif SPECIAL_CODE_11_REGEXP =~ tel 36 | return [$1, $2] 37 | else 38 | raise ArgumentError, "Invalid telephone number" 39 | end 40 | else 41 | raise ArgumentError, "Invalid telephone number" 42 | end 43 | end 44 | 45 | def self.preprocess(tel) 46 | NKF.nkf("-m0Z0 -w", tel).split("").select { |c| /\d/ =~ c }.join 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /spec/tel_formatter_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | require "tel_formatter" 3 | 4 | describe TelFormatter do 5 | describe ".format" do 6 | it "returns hyphenated telephone number" do 7 | tel = "0300000000" 8 | expect(TelFormatter.format(tel)).to eq("03-0000-0000") 9 | end 10 | 11 | it "returns hyphenated telephone number based on split format 3 digit (eg Koganei, Tokyo) geo code" do 12 | tel = "0429991111" 13 | expect(TelFormatter.format(tel)).to eq("042-999-1111") 14 | end 15 | 16 | it "returns hyphenated telephone number based on split format 4 digit (eg Nishitama, Tokyo) geo code" do 17 | tel = "0428991111" 18 | expect(TelFormatter.format(tel)).to eq("0428-99-1111") 19 | end 20 | 21 | it "returns hyphenated telephone number based on split format 4 digit (eg Chofu, Tokyo) geo code" do 22 | tel = "0422991111" 23 | expect(TelFormatter.format(tel)).to eq("0422-99-1111") 24 | # This should NOT be "042-299-1111" 25 | end 26 | end 27 | 28 | describe ".split" do 29 | it "splits telephone number into 2 or 3 blocks" do 30 | tel = "0300000000" 31 | expect(TelFormatter.split(tel)).to eq(["03", "0000", "0000"]) 32 | tel = "0110000000" 33 | expect(TelFormatter.split(tel)).to eq(["011", "000", "0000"]) 34 | tel = "0123000000" 35 | expect(TelFormatter.split(tel)).to eq(["0123", "00", "0000"]) 36 | tel = "0126700000" 37 | expect(TelFormatter.split(tel)).to eq(["01267", "0", "0000"]) 38 | tel = "0120000000" 39 | expect(TelFormatter.split(tel)).to eq(["0120", "000000"]) 40 | tel = "08099999999" 41 | expect(TelFormatter.split(tel)).to eq(["080", "9999", "9999"]) 42 | tel = "08000000000" 43 | expect(TelFormatter.split(tel)).to eq(["0800", "0000000"]) 44 | 45 | tel = "0429991111" 46 | expect(TelFormatter.split(tel)).to eq(["042", "999", "1111"]) 47 | tel = "0428991111" 48 | expect(TelFormatter.split(tel)).to eq(["0428", "99", "1111"]) 49 | tel = "0422991111" 50 | expect(TelFormatter.split(tel)).to eq(["0422", "99", "1111"]) 51 | tel = "0709999999" 52 | expect(TelFormatter.split(tel)).to eq(["070", "999", "9999"]) 53 | end 54 | 55 | it "raises ArgumentError when Invalid number is given" do 56 | tel = "0100000000" 57 | expect { TelFormatter.split(tel) }.to raise_error(ArgumentError) 58 | tel = "04000000000" 59 | expect { TelFormatter.split(tel) }.to raise_error(ArgumentError) 60 | tel = "050000000" 61 | expect { TelFormatter.split(tel) }.to raise_error(ArgumentError) 62 | end 63 | 64 | it "can handle Zenkaku chars and ignore non-numerical chars" do 65 | tel = "03−0000−0000" 66 | expect(TelFormatter.split(tel)).to eq(["03", "0000", "0000"]) 67 | end 68 | end 69 | 70 | describe ".preprocess" do 71 | it "converts Zenkaku chars to Hankaku, and excludes any non-numerical chars" do 72 | tel = "03−0000−0000" 73 | expect(TelFormatter.preprocess(tel)).to eq("0300000000") 74 | end 75 | end 76 | end 77 | -------------------------------------------------------------------------------- /data/area_codes.txt: -------------------------------------------------------------------------------- 1 | 011 2 | 0123 3 | 0124 4 | 0125 5 | 0126 6 | 01267 7 | 0133 8 | 0134 9 | 0135 10 | 0136 11 | 0137 12 | 01372 13 | 01374 14 | 01377 15 | 0138 16 | 0139 17 | 01392 18 | 01397 19 | 01398 20 | 0142 21 | 0143 22 | 0144 23 | 0145 24 | 01456 25 | 01457 26 | 0146 27 | 01466 28 | 015 29 | 0152 30 | 0153 31 | 0154 32 | 01547 33 | 0155 34 | 01558 35 | 0156 36 | 01564 37 | 0157 38 | 0158 39 | 01586 40 | 01587 41 | 0162 42 | 0163 43 | 01632 44 | 01634 45 | 01635 46 | 0164 47 | 01648 48 | 0165 49 | 01654 50 | 01655 51 | 01656 52 | 01658 53 | 0166 54 | 0167 55 | 017 56 | 0172 57 | 0173 58 | 0174 59 | 0175 60 | 0176 61 | 0178 62 | 0179 63 | 018 64 | 0182 65 | 0183 66 | 0184 67 | 0185 68 | 0186 69 | 0187 70 | 019 71 | 0191 72 | 0192 73 | 0193 74 | 0194 75 | 0195 76 | 0197 77 | 0198 78 | 022 79 | 0220 80 | 0223 81 | 0224 82 | 0225 83 | 0226 84 | 0228 85 | 0229 86 | 023 87 | 0233 88 | 0234 89 | 0235 90 | 0237 91 | 0238 92 | 024 93 | 0240 94 | 0241 95 | 0242 96 | 0243 97 | 0244 98 | 0246 99 | 0247 100 | 0248 101 | 025 102 | 0250 103 | 0254 104 | 0255 105 | 0256 106 | 0257 107 | 0258 108 | 0259 109 | 026 110 | 0260 111 | 0261 112 | 0263 113 | 0264 114 | 0265 115 | 0266 116 | 0267 117 | 0268 118 | 0269 119 | 027 120 | 0270 121 | 0274 122 | 0276 123 | 0277 124 | 0278 125 | 0279 126 | 028 127 | 0280 128 | 0282 129 | 0283 130 | 0284 131 | 0285 132 | 0287 133 | 0288 134 | 0289 135 | 029 136 | 0291 137 | 0293 138 | 0294 139 | 0295 140 | 0296 141 | 0297 142 | 0299 143 | 03 144 | 04 145 | 042 146 | 0422 147 | 0428 148 | 043 149 | 0436 150 | 0438 151 | 0439 152 | 044 153 | 045 154 | 046 155 | 0460 156 | 0463 157 | 0465 158 | 0466 159 | 0467 160 | 047 161 | 0470 162 | 0475 163 | 0476 164 | 0478 165 | 0479 166 | 048 167 | 0480 168 | 049 169 | 0493 170 | 0494 171 | 0495 172 | 04992 173 | 04994 174 | 04996 175 | 04998 176 | 052 177 | 053 178 | 0531 179 | 0532 180 | 0533 181 | 0536 182 | 0537 183 | 0538 184 | 0539 185 | 054 186 | 0544 187 | 0545 188 | 0547 189 | 0548 190 | 055 191 | 0550 192 | 0551 193 | 0553 194 | 0554 195 | 0555 196 | 0556 197 | 0557 198 | 0558 199 | 0561 200 | 0562 201 | 0563 202 | 0564 203 | 0565 204 | 0566 205 | 0567 206 | 0568 207 | 0569 208 | 0572 209 | 0573 210 | 0574 211 | 0575 212 | 0576 213 | 05769 214 | 0577 215 | 0578 216 | 058 217 | 0581 218 | 0584 219 | 0585 220 | 0586 221 | 0587 222 | 059 223 | 0594 224 | 0595 225 | 0596 226 | 0597 227 | 05979 228 | 0598 229 | 0599 230 | 06 231 | 072 232 | 0721 233 | 0725 234 | 073 235 | 0735 236 | 0736 237 | 0737 238 | 0738 239 | 0739 240 | 0740 241 | 0742 242 | 0743 243 | 0744 244 | 0745 245 | 0746 246 | 07468 247 | 0747 248 | 0748 249 | 0749 250 | 075 251 | 076 252 | 0761 253 | 0763 254 | 0765 255 | 0766 256 | 0767 257 | 0768 258 | 077 259 | 0770 260 | 0771 261 | 0772 262 | 0773 263 | 0774 264 | 0776 265 | 0778 266 | 0779 267 | 078 268 | 079 269 | 0790 270 | 0791 271 | 0794 272 | 0795 273 | 0796 274 | 0797 275 | 0798 276 | 0799 277 | 082 278 | 0820 279 | 0823 280 | 0824 281 | 0826 282 | 0827 283 | 0829 284 | 083 285 | 0833 286 | 0834 287 | 0835 288 | 0836 289 | 0837 290 | 0838 291 | 08387 292 | 08388 293 | 08396 294 | 084 295 | 0845 296 | 0846 297 | 0847 298 | 08477 299 | 0848 300 | 08512 301 | 08514 302 | 0852 303 | 0853 304 | 0854 305 | 0855 306 | 0856 307 | 0857 308 | 0858 309 | 0859 310 | 086 311 | 0863 312 | 0865 313 | 0866 314 | 0867 315 | 0868 316 | 0869 317 | 087 318 | 0875 319 | 0877 320 | 0879 321 | 088 322 | 0880 323 | 0883 324 | 0884 325 | 0885 326 | 0887 327 | 0889 328 | 089 329 | 0892 330 | 0893 331 | 0894 332 | 0895 333 | 0896 334 | 0897 335 | 0898 336 | 092 337 | 0920 338 | 093 339 | 0930 340 | 0940 341 | 0942 342 | 0943 343 | 0944 344 | 0946 345 | 0947 346 | 0948 347 | 0949 348 | 09496 349 | 095 350 | 0950 351 | 0952 352 | 0954 353 | 0955 354 | 0956 355 | 0957 356 | 0959 357 | 096 358 | 0964 359 | 0965 360 | 0966 361 | 0967 362 | 0968 363 | 0969 364 | 097 365 | 0972 366 | 0973 367 | 0974 368 | 0977 369 | 0978 370 | 0979 371 | 098 372 | 0980 373 | 09802 374 | 0982 375 | 0983 376 | 0984 377 | 0985 378 | 0986 379 | 0987 380 | 099 381 | 09912 382 | 09913 383 | 0993 384 | 0994 385 | 0995 386 | 0996 387 | 09969 388 | 0997 389 | 390 | --------------------------------------------------------------------------------