├── .github └── workflows │ └── test.yaml ├── .gitignore ├── .rspec ├── CODE_OF_CONDUCT.md ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── bin ├── console └── setup ├── lib ├── wavedash.rb └── wavedash │ └── version.rb ├── spec ├── encoding │ ├── cp932_spec.rb │ ├── euc_jp_spec.rb │ ├── eucjp_ms_spec.rb │ └── shift_jis_spec.rb ├── spec_helper.rb └── wavedash_spec.rb └── wavedash.gemspec /.github/workflows/test.yaml: -------------------------------------------------------------------------------- 1 | name: Test 2 | on: 3 | push: 4 | branches: [master] 5 | pull_request: 6 | branches: [master] 7 | jobs: 8 | test: 9 | runs-on: ubuntu-latest 10 | strategy: 11 | fail-fast: false 12 | matrix: 13 | ruby-version: ["3.0", "3.1", "3.2", "3.3"] 14 | steps: 15 | - uses: actions/checkout@v2 16 | - name: Set up Ruby ${{ matrix.ruby-version }} 17 | uses: ruby/setup-ruby@v1 18 | with: 19 | ruby-version: ${{ matrix.ruby-version }} 20 | bundler-cache: true 21 | - name: Install dependencies 22 | run: bundle install 23 | - name: Run tests 24 | run: bundle exec rake 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. 4 | 5 | We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion. 6 | 7 | Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct. 8 | 9 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team. 10 | 11 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. 12 | 13 | This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/) 14 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in wavedash.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Takatoshi Ono 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 13 | all 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 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Wavedash ![test](https://github.com/takatoshiono/wavedash/actions/workflows/test.yaml/badge.svg) 2 | 3 | Normalize unencodable characters that raise `Encoding::UndefinedConversionError` exception in `String#encode`. 4 | 5 | ### Support encoding 6 | 7 | - eucjp-ms 8 | - euc-jp 9 | - cp932 10 | - shift_jis 11 | 12 | ## Installation 13 | 14 | Add this line to your application's Gemfile: 15 | 16 | ```ruby 17 | gem 'wavedash' 18 | ``` 19 | 20 | And then execute: 21 | 22 | $ bundle 23 | 24 | Or install it yourself as: 25 | 26 | $ gem install wavedash 27 | 28 | ## Usage 29 | 30 | ### destination_encoding 31 | 32 | First af all, configure destination encoding that your application needs. 33 | 34 | ```ruby 35 | require 'wavedash' 36 | 37 | Wavedash.destination_encoding = 'eucjp-ms' 38 | ``` 39 | 40 | ### #normalize 41 | 42 | Normalize characters like "WAVE DASH (U+301C)" 43 | 44 | ```ruby 45 | str = "こんにちは\u{301C}" # => "こんにちは〜" 46 | str.encode('eucjp-ms') # => Encoding::UndefinedConversionError: U+301C from UTF-8 to eucJP-ms 47 | 48 | normalized = Wavedash.normalize(str) # => "こんにちは~" 49 | normalized.encode('eucjp-ms') # => "\x{A4B3}\x{A4F3}\x{A4CB}\x{A4C1}\x{A4CF}\x{A1C1}" ("こんにちは~") 50 | ``` 51 | 52 | ### #invalid? 53 | 54 | Detect unencodable characters 55 | 56 | ```ruby 57 | str = "こんにちは\u{301C}" # => "こんにちは〜" 58 | Wavedash.invalid?(str) # => true 59 | ``` 60 | 61 | ### Thought of Wavedash 62 | 63 | Character code conversion is required when interact between softwares that treet different character code. For example, it is a situation such as the following. 64 | 65 | - A Web application written in UTF-8 using a database saved in EUC-JP 66 | - Exchanging data files(csv,tsv,..) between different systems 67 | 68 | In Ruby, You can convert a character code using `String#encode`, but some characters cannot. `Encoding::UndefinedConversionError` raises when a character is undefined in the destination encoding. But `String#encode` has options. You can specify `:undef => :replace` then replace the undefined characters with the replacement character. 69 | 70 | `Wavedash` is similar to `String#encode` with `:undef => :replace`, but it does more aggressive character conversion. 71 | 72 | Despite some characters have resembling shape, character code point is different each other. For example, when you convert characters to EUCJP-MS from UTF-8, can convert "~" (FULLWIDTH TILDE U+FF5E) but cannot convert "〜" (WAVE DASH U+301C). The opposite will occur when you convert to EUC-JP. 73 | 74 | | UNICODE | EUC-JP | EUCJP-MS | 75 | | --------------------------- | ---------------------------------- | ---------------------------------- | 76 | | "〜" U+301C WAVE-DASH | 0xA1C1 | Encoding::UndefinedConversionError | 77 | | "~" U+FF5E FULLWIDTH TILDE | Encoding::UndefinedConversionError | 0xA1C1 | 78 | 79 | In Web applications, it depends on the client environment that the input character as "〜" is U+301C or U+FF5E. This cannot select by the application. What you can do with the application is only to determine the handling of the unencodable characters. 80 | 81 | `Wavedash` offers the option that to convert unencodable characters to resembling characters. 82 | 83 | ## Development 84 | 85 | After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. 86 | 87 | To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). 88 | 89 | ## Contributing 90 | 91 | 1. Fork it 92 | 2. Create your feature branch (git checkout -b my-new-feature) 93 | 3. Commit your changes (git commit -am 'Add some feature') 94 | 4. Push to the branch (git push origin my-new-feature) 95 | 5. Create new Pull Request 96 | 97 | ## License 98 | 99 | The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). 100 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require "rspec/core/rake_task" 3 | 4 | RSpec::Core::RakeTask.new(:spec) 5 | 6 | task :default => :spec 7 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "bundler/setup" 4 | require "wavedash" 5 | 6 | # You can add fixtures and/or initialization code here to make experimenting 7 | # with your gem easier. You can also use a different console, if you like. 8 | 9 | # (If you use this, don't forget to add pry to your Gemfile!) 10 | # require "pry" 11 | # Pry.start 12 | 13 | require "irb" 14 | IRB.start 15 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | 5 | bundle install 6 | 7 | # Do any other automated setup that you need to do here 8 | -------------------------------------------------------------------------------- /lib/wavedash.rb: -------------------------------------------------------------------------------- 1 | require "wavedash/version" 2 | 3 | module Wavedash 4 | @@destination_encoding = nil 5 | 6 | CHARACTER_CODE_MAPPING = { 7 | 'eucjp-ms' => { 8 | "\u{301C}" => "\u{FF5E}", # 'WAVE DASH' => 'FULLWIDTH TILDE' 9 | "\u{2212}" => "\u{FF0D}", # 'MINUS SIGN' => 'FULLWIDTH HYPHEN-MINUS' 10 | "\u{2016}" => "\u{2225}", # 'DOUBLE VERTICAL LINE' => 'PARALLEL TO' 11 | "\u{2014}" => "\u{2015}", # 'EM DASH' => 'HORIZONTAL BAR' 12 | "\u{00A2}" => "\u{FFE0}", # 'CENT SIGN' => 'FULLWIDTH CENT SIGN' 13 | "\u{00A3}" => "\u{FFE1}", # 'POUND SIGN' => 'FULLWIDTH POUND SIGN' 14 | "\u{00AC}" => "\u{FFE2}", # 'NOT SIGN' => 'FULLWIDTH NOT SIGN' 15 | }, 16 | 'euc-jp' => { 17 | "\u{FF5E}" => "\u{301C}", # 'FULLWIDTH TILDE' => 'WAVE DASH' 18 | "\u{FF0D}" => "\u{2212}", # 'FULLWIDTH HYPHEN-MINUS' => 'MINUS SIGN' 19 | "\u{2225}" => "\u{2016}", # 'PARALLEL TO' => 'DOUBLE VERTICAL LINE' 20 | "\u{FFE0}" => "\u{00A2}", # 'FULLWIDTH CENT SIGN' => 'CENT SIGN' 21 | "\u{FFE1}" => "\u{00A3}", # 'FULLWIDTH POUND SIGN' => 'POUND SIGN' 22 | "\u{FFE2}" => "\u{00AC}", # 'FULLWIDTH NOT SIGN' => 'NOT SIGN' 23 | }, 24 | 'cp932' => { 25 | "\u{301C}" => "\u{FF5E}", # 'WAVE DASH' => 'FULLWIDTH TILDE' 26 | "\u{2212}" => "\u{FF0D}", # 'MINUS SIGN' => 'FULLWIDTH HYPHEN-MINUS' 27 | "\u{2016}" => "\u{2225}", # 'DOUBLE VERTICAL LINE' => 'PARALLEL TO' 28 | "\u{2014}" => "\u{2015}", # 'EM DASH' => 'HORIZONTAL BAR' 29 | "\u{00A2}" => "\u{FFE0}", # 'CENT SIGN' => 'FULLWIDTH CENT SIGN' 30 | "\u{00A3}" => "\u{FFE1}", # 'POUND SIGN' => 'FULLWIDTH POUND SIGN' 31 | "\u{00AC}" => "\u{FFE2}", # 'NOT SIGN' => 'FULLWIDTH NOT SIGN' 32 | }, 33 | 'shift_jis' => { 34 | "\u{FF5E}" => "\u{301C}", # 'FULLWIDTH TILDE' => 'WAVE DASH' 35 | "\u{FF0D}" => "\u{2212}", # 'FULLWIDTH HYPHEN-MINUS' => 'MINUS SIGN' 36 | "\u{2225}" => "\u{2016}", # 'PARALLEL TO' => 'DOUBLE VERTICAL LINE' 37 | "\u{2013}" => "\u{2014}", # 'EN DASH' => 'EM DASH' 38 | "\u{FFE0}" => "\u{00A2}", # 'FULLWIDTH CENT SIGN' => 'CENT SIGN' 39 | "\u{FFE1}" => "\u{00A3}", # 'FULLWIDTH POUND SIGN' => 'POUND SIGN' 40 | "\u{FFE2}" => "\u{00AC}", # 'FULLWIDTH NOT SIGN' => 'NOT SIGN' 41 | }, 42 | } 43 | 44 | def self.destination_encoding=(encoding) 45 | @@destination_encoding = encoding 46 | end 47 | 48 | def self.normalize(str) 49 | mapping = CHARACTER_CODE_MAPPING[@@destination_encoding] 50 | return str unless mapping 51 | str.tr(mapping.keys.join, mapping.values.join) 52 | end 53 | 54 | def self.invalid?(str) 55 | return false unless str.is_a?(String) 56 | mapping = CHARACTER_CODE_MAPPING[@@destination_encoding] 57 | return false unless mapping 58 | str.each_char.any? { |c| mapping[c] } 59 | end 60 | end 61 | -------------------------------------------------------------------------------- /lib/wavedash/version.rb: -------------------------------------------------------------------------------- 1 | module Wavedash 2 | VERSION = "0.1.2" 3 | end 4 | -------------------------------------------------------------------------------- /spec/encoding/cp932_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe 'destination encoding is cp932' do 4 | let(:encoding) { 'cp932' } 5 | 6 | before do 7 | Wavedash.destination_encoding = encoding 8 | end 9 | 10 | describe '#normalize' do 11 | context 'includes some invalid characters' do 12 | let(:str) { "こんにちは〜。コンニチハ−" } 13 | let(:normalized_str) { "こんにちは~。コンニチハ-" } 14 | 15 | it_behaves_like 'a unencodable string before-after' 16 | it_behaves_like 'a expected normalization' 17 | end 18 | 19 | context 'includes WAVE DASH(U+301C)' do 20 | let(:str) { "こんにちは〜" } 21 | let(:normalized_str) { "こんにちは~" } 22 | 23 | it_behaves_like 'a unencodable string before-after' 24 | it_behaves_like 'a expected normalization' 25 | end 26 | 27 | context 'includes MINUS SIGN(U+2212)' do 28 | let(:str) { "−" } 29 | let(:normalized_str) { "-" } 30 | 31 | it_behaves_like 'a unencodable string before-after' 32 | it_behaves_like 'a expected normalization' 33 | end 34 | 35 | context 'includes DOUBLE VERTICAL LINE(U+2016)' do 36 | let(:str) { "‖" } 37 | let(:normalized_str) { "∥" } 38 | 39 | it_behaves_like 'a unencodable string before-after' 40 | it_behaves_like 'a expected normalization' 41 | end 42 | 43 | context 'includes EM DASH(U+2014)' do 44 | let(:str) { "—" } 45 | let(:normalized_str) { "―" } 46 | 47 | it_behaves_like 'a unencodable string before-after' 48 | it_behaves_like 'a expected normalization' 49 | end 50 | 51 | context 'includes CENT SIGN(U+00A2)' do 52 | let(:str) { "¢" } 53 | let(:normalized_str) { "¢" } 54 | 55 | it_behaves_like 'a unencodable string before-after' 56 | it_behaves_like 'a expected normalization' 57 | end 58 | 59 | context 'includes POUND SIGN(U+00A3)' do 60 | let(:str) { "£" } 61 | let(:normalized_str) { "£" } 62 | 63 | it_behaves_like 'a unencodable string before-after' 64 | it_behaves_like 'a expected normalization' 65 | end 66 | 67 | context 'includes NOT SIGN(U+00AC)' do 68 | let(:str) { "¬" } 69 | let(:normalized_str) { "¬" } 70 | 71 | it_behaves_like 'a unencodable string before-after' 72 | it_behaves_like 'a expected normalization' 73 | end 74 | end 75 | 76 | describe '#invalid?' do 77 | it 'returns false when not invalid characters' do 78 | expect(Wavedash.invalid?('こんにちは')).to be_falsey 79 | end 80 | 81 | it 'returns true when include WAVE DASH(U+301C)' do 82 | expect(Wavedash.invalid?("こんにちは〜")).to be_truthy 83 | end 84 | 85 | it 'returns true when include MINUS SIGN(U+2212)' do 86 | expect(Wavedash.invalid?("−")).to be_truthy 87 | end 88 | 89 | it 'returns true when include DOUBLE VERTICAL LINE(U+2016)' do 90 | expect(Wavedash.invalid?("‖")).to be_truthy 91 | end 92 | 93 | it 'returns true when include EM DASH(U+2014)' do 94 | expect(Wavedash.invalid?("—")).to be_truthy 95 | end 96 | 97 | it 'returns true when include CENT SIGN(U+00A2)' do 98 | expect(Wavedash.invalid?("¢")).to be_truthy 99 | end 100 | 101 | it 'returns true when include POUND SIGN(U+00A3)' do 102 | expect(Wavedash.invalid?("£")).to be_truthy 103 | end 104 | 105 | it 'returns true when include NOT SIGN(U+00AC)' do 106 | expect(Wavedash.invalid?("¬")).to be_truthy 107 | end 108 | end 109 | end 110 | -------------------------------------------------------------------------------- /spec/encoding/euc_jp_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe 'destination encoding is euc-jp' do 4 | let(:encoding) { 'euc-jp' } 5 | 6 | before do 7 | Wavedash.destination_encoding = encoding 8 | end 9 | 10 | describe '#normalize' do 11 | context 'includes FULLWIDTH TILDE(U+FF5E)' do 12 | let(:str) { "こんにちは~" } 13 | let(:normalized_str) { "こんにちは〜" } 14 | 15 | it_behaves_like 'a unencodable string before-after' 16 | it_behaves_like 'a expected normalization' 17 | end 18 | 19 | context 'includes FULLWIDTH HYPHEN-MINUS(U+FF0D)' do 20 | let(:str) { "-" } 21 | let(:normalized_str) { "−" } 22 | 23 | it_behaves_like 'a unencodable string before-after' 24 | it_behaves_like 'a expected normalization' 25 | end 26 | 27 | context 'includes PARALLEL TO(U+2225)' do 28 | let(:str) { "∥" } 29 | let(:normalized_str) { "‖" } 30 | 31 | it_behaves_like 'a unencodable string before-after' 32 | it_behaves_like 'a expected normalization' 33 | end 34 | 35 | context 'includes FULLWIDTH CENT SIGN(U+FFE0)' do 36 | let(:str) { "¢" } 37 | let(:normalized_str) { "¢" } 38 | 39 | it_behaves_like 'a unencodable string before-after' 40 | it_behaves_like 'a expected normalization' 41 | end 42 | 43 | context 'includes FULLWIDTH POUND SIGN(U+FFE1)' do 44 | let(:str) { "£" } 45 | let(:normalized_str) { "£" } 46 | 47 | it_behaves_like 'a unencodable string before-after' 48 | it_behaves_like 'a expected normalization' 49 | end 50 | 51 | context 'includes FULLWIDTH NOT SIGN(U+FFE2)' do 52 | let(:str) { "¬" } 53 | let(:normalized_str) { "¬" } 54 | 55 | it_behaves_like 'a unencodable string before-after' 56 | it_behaves_like 'a expected normalization' 57 | end 58 | end 59 | 60 | describe '#invalid?' do 61 | it 'returns false when not invalid characters' do 62 | expect(Wavedash.invalid?('こんにちは')).to be_falsey 63 | end 64 | 65 | it 'returns true when include FULLWIDTH TILDE(U+FF5E)' do 66 | expect(Wavedash.invalid?("こんにちは~")).to be_truthy 67 | end 68 | 69 | it 'returns true when include FULLWIDTH HYPHEN-MINUS(U+FF0D)' do 70 | expect(Wavedash.invalid?("-")).to be_truthy 71 | end 72 | 73 | it 'returns true when include PARALLEL TO(U+2225)' do 74 | expect(Wavedash.invalid?("∥")).to be_truthy 75 | end 76 | 77 | it 'returns true when include FULLWIDTH CENT SIGN(U+FFE0)' do 78 | expect(Wavedash.invalid?("¢")).to be_truthy 79 | end 80 | 81 | it 'returns true when include FULLWIDTH POUND SIGN(0+FFE1)' do 82 | expect(Wavedash.invalid?("£")).to be_truthy 83 | end 84 | 85 | it 'returns true when include FULLWIDTH NOT SIGN(0+FFE2)' do 86 | expect(Wavedash.invalid?("¬")).to be_truthy 87 | end 88 | end 89 | end 90 | -------------------------------------------------------------------------------- /spec/encoding/eucjp_ms_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe 'destination encoding is eucjp-ms' do 4 | let(:encoding) { 'eucjp-ms' } 5 | 6 | before do 7 | Wavedash.destination_encoding = encoding 8 | end 9 | 10 | describe '#normalize' do 11 | context 'includes some invalid characters' do 12 | let(:str) { "こんにちは〜。コンニチハ−" } 13 | let(:normalized_str) { "こんにちは~。コンニチハ-" } 14 | 15 | it_behaves_like 'a unencodable string before-after' 16 | it_behaves_like 'a expected normalization' 17 | end 18 | 19 | context 'includes WAVE DASH(U+301C)' do 20 | let(:str) { "こんにちは〜" } 21 | let(:normalized_str) { "こんにちは~" } 22 | 23 | it_behaves_like 'a unencodable string before-after' 24 | it_behaves_like 'a expected normalization' 25 | end 26 | 27 | context 'includes MINUS SIGN(U+2212)' do 28 | let(:str) { "−" } 29 | let(:normalized_str) { "-" } 30 | 31 | it_behaves_like 'a unencodable string before-after' 32 | it_behaves_like 'a expected normalization' 33 | end 34 | 35 | context 'includes DOUBLE VERTICAL LINE(U+2016)' do 36 | let(:str) { "‖" } 37 | let(:normalized_str) { "∥" } 38 | 39 | it_behaves_like 'a unencodable string before-after' 40 | it_behaves_like 'a expected normalization' 41 | end 42 | 43 | context 'includes EM DASH(U+2014)' do 44 | let(:str) { "—" } 45 | let(:normalized_str) { "―" } 46 | 47 | it_behaves_like 'a unencodable string before-after' 48 | it_behaves_like 'a expected normalization' 49 | end 50 | 51 | context 'includes CENT SIGN(U+00A2)' do 52 | let(:str) { "¢" } 53 | let(:normalized_str) { "¢" } 54 | 55 | it_behaves_like 'a unencodable string before-after' 56 | it_behaves_like 'a expected normalization' 57 | end 58 | 59 | context 'includes POUND SIGN(U+00A3)' do 60 | let(:str) { "£" } 61 | let(:normalized_str) { "£" } 62 | 63 | it_behaves_like 'a unencodable string before-after' 64 | it_behaves_like 'a expected normalization' 65 | end 66 | 67 | context 'includes NOT SIGN(U+00AC)' do 68 | let(:str) { "¬" } 69 | let(:normalized_str) { "¬" } 70 | 71 | it_behaves_like 'a unencodable string before-after' 72 | it_behaves_like 'a expected normalization' 73 | end 74 | end 75 | 76 | describe '#invalid?' do 77 | it 'returns false when not invalid characters' do 78 | expect(Wavedash.invalid?('こんにちは')).to be_falsey 79 | end 80 | 81 | it 'returns true when include WAVE DASH(U+301C)' do 82 | expect(Wavedash.invalid?("こんにちは〜")).to be_truthy 83 | end 84 | 85 | it 'returns true when include MINUS SIGN(U+2212)' do 86 | expect(Wavedash.invalid?("−")).to be_truthy 87 | end 88 | 89 | it 'returns true when include DOUBLE VERTICAL LINE(U+2016)' do 90 | expect(Wavedash.invalid?("‖")).to be_truthy 91 | end 92 | 93 | it 'returns true when include EM DASH(U+2014)' do 94 | expect(Wavedash.invalid?("—")).to be_truthy 95 | end 96 | 97 | it 'returns true when include CENT SIGN(U+00A2)' do 98 | expect(Wavedash.invalid?("¢")).to be_truthy 99 | end 100 | 101 | it 'returns true when include POUND SIGN(U+00A3)' do 102 | expect(Wavedash.invalid?("£")).to be_truthy 103 | end 104 | 105 | it 'returns true when include NOT SIGN(U+00AC)' do 106 | expect(Wavedash.invalid?("¬")).to be_truthy 107 | end 108 | end 109 | end 110 | -------------------------------------------------------------------------------- /spec/encoding/shift_jis_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe 'destination encoding is shift_jis' do 4 | let(:encoding) { 'shift_jis' } 5 | 6 | before do 7 | Wavedash.destination_encoding = encoding 8 | end 9 | 10 | describe '#normalize' do 11 | context 'includes FULLWIDTH TILDE(U+FF5E)' do 12 | let(:str) { "こんにちは~" } 13 | let(:normalized_str) { "こんにちは〜" } 14 | 15 | it_behaves_like 'a unencodable string before-after' 16 | it_behaves_like 'a expected normalization' 17 | end 18 | 19 | context 'includes FULLWIDTH HYPHEN-MINUS(U+FF0D)' do 20 | let(:str) { "-" } 21 | let(:normalized_str) { "−" } 22 | 23 | it_behaves_like 'a unencodable string before-after' 24 | it_behaves_like 'a expected normalization' 25 | end 26 | 27 | context 'includes PARALLEL TO(U+2225)' do 28 | let(:str) { "∥" } 29 | let(:normalized_str) { "‖" } 30 | 31 | it_behaves_like 'a unencodable string before-after' 32 | it_behaves_like 'a expected normalization' 33 | end 34 | 35 | context 'includes FULLWIDTH CENT SIGN(U+FFE0)' do 36 | let(:str) { "¢" } 37 | let(:normalized_str) { "¢" } 38 | 39 | it_behaves_like 'a unencodable string before-after' 40 | it_behaves_like 'a expected normalization' 41 | end 42 | 43 | context 'includes FULLWIDTH POUND SIGN(U+FFE1)' do 44 | let(:str) { "£" } 45 | let(:normalized_str) { "£" } 46 | 47 | it_behaves_like 'a unencodable string before-after' 48 | it_behaves_like 'a expected normalization' 49 | end 50 | 51 | context 'includes FULLWIDTH NOT SIGN(U+FFE2)' do 52 | let(:str) { "¬" } 53 | let(:normalized_str) { "¬" } 54 | 55 | it_behaves_like 'a unencodable string before-after' 56 | it_behaves_like 'a expected normalization' 57 | end 58 | 59 | context 'includes EN DASH(U+2013)' do 60 | let(:str) { "–" } 61 | let(:normalized_str) { "—" } 62 | 63 | it_behaves_like 'a unencodable string before-after' 64 | it_behaves_like 'a expected normalization' 65 | end 66 | end 67 | 68 | describe '#invalid?' do 69 | it 'returns false when not invalid characters' do 70 | expect(Wavedash.invalid?('こんにちは')).to be_falsey 71 | end 72 | 73 | it 'returns true when include FULLWIDTH TILDE(U+FF5E)' do 74 | expect(Wavedash.invalid?("こんにちは~")).to be_truthy 75 | end 76 | 77 | it 'returns true when include FULLWIDTH HYPHEN-MINUS(U+FF0D)' do 78 | expect(Wavedash.invalid?("-")).to be_truthy 79 | end 80 | 81 | it 'returns true when include PARALLEL TO(U+2225)' do 82 | expect(Wavedash.invalid?("∥")).to be_truthy 83 | end 84 | 85 | it 'returns true when include FULLWIDTH CENT SIGN(U+FFE0)' do 86 | expect(Wavedash.invalid?("¢")).to be_truthy 87 | end 88 | 89 | it 'returns true when include FULLWIDTH POUND SIGN(0+FFE1)' do 90 | expect(Wavedash.invalid?("£")).to be_truthy 91 | end 92 | 93 | it 'returns true when include FULLWIDTH NOT SIGN(0+FFE2)' do 94 | expect(Wavedash.invalid?("¬")).to be_truthy 95 | end 96 | 97 | it 'returns true when include EN DASH(0+2013)' do 98 | expect(Wavedash.invalid?("–")).to be_truthy 99 | end 100 | end 101 | end 102 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) 2 | require 'wavedash' 3 | 4 | RSpec.configure do |config| 5 | config.filter_run_including :focus => true 6 | config.run_all_when_everything_filtered = true 7 | end 8 | 9 | RSpec.shared_examples 'a unencodable string before-after' do 10 | it 'raises exception when encode before normalize' do 11 | expect { 12 | str.encode(encoding) 13 | }.to raise_error Encoding::UndefinedConversionError 14 | end 15 | 16 | it 'not raise when encode after normalize' do 17 | expect { 18 | Wavedash.normalize(str).encode(encoding) 19 | }.not_to raise_error 20 | end 21 | end 22 | 23 | RSpec.shared_examples 'a expected normalization' do 24 | it "is converted to a encodable string" do 25 | expect(Wavedash.normalize(str)).to eq normalized_str 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /spec/wavedash_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Wavedash do 4 | it 'has a version number' do 5 | expect(Wavedash::VERSION).not_to be nil 6 | end 7 | 8 | describe '#normalize' do 9 | before do 10 | Wavedash.destination_encoding = encoding 11 | end 12 | 13 | context 'destination encoding is not set' do 14 | let(:encoding) { nil } 15 | let(:str) { 'こんにちは〜' } 16 | 17 | it 'not raise error' do 18 | expect { Wavedash.normalize(str) }.not_to raise_error 19 | end 20 | 21 | it 'returns the untouched argument' do 22 | expect(Wavedash.normalize(str)).to eq str 23 | end 24 | end 25 | end 26 | 27 | describe '#invalid?' do 28 | before do 29 | Wavedash.destination_encoding = encoding 30 | end 31 | 32 | context 'destination encoding is not set' do 33 | let(:encoding) { nil } 34 | let(:str) { 'こんにちは〜' } 35 | 36 | it 'not raise error' do 37 | expect { Wavedash.invalid?(str) }.not_to raise_error 38 | end 39 | 40 | it 'returns false' do 41 | expect(Wavedash.invalid?(str)).to be_falsey 42 | end 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /wavedash.gemspec: -------------------------------------------------------------------------------- 1 | lib = File.expand_path('../lib', __FILE__) 2 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 3 | require 'wavedash/version' 4 | 5 | Gem::Specification.new do |spec| 6 | spec.name = "wavedash" 7 | spec.version = Wavedash::VERSION 8 | spec.authors = ["Takatoshi Ono"] 9 | spec.email = ["takatoshi.ono@gmail.com"] 10 | 11 | spec.summary = %q{Normalize unencodable characters} 12 | spec.description = %q{Normalize unencodable characters} 13 | spec.homepage = "https://github.com/takatoshiono/wavedash" 14 | spec.license = "MIT" 15 | 16 | spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } 17 | spec.bindir = "exe" 18 | spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } 19 | spec.require_paths = ["lib"] 20 | 21 | spec.add_development_dependency "bundler" 22 | spec.add_development_dependency "rake" 23 | spec.add_development_dependency "rspec" 24 | end 25 | --------------------------------------------------------------------------------