Project

charwidth

0.02
No release in over 3 years
Low commit activity in last 3 years
Normalize Japanese / Korean fullwidth (zenkaku) and halfwidth (hankaku) characters
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

charwidth

Normalize Unicode fullwidth / halfwidth (zenkaku / hankaku) characters.

Installation

Add this line to your application's Gemfile:

gem 'charwidth'

And then execute:

$ bundle

Or install it yourself as:

$ gem install charwidth

Usage

require "charwidth"
Charwidth.normalize("Hello, World!") # => "Hello, World!"
Charwidth.normalize!("Hello, World!") # destructive
Charwidth.normalize("「ハローワールド」") # => "「ハローワールド」"

Or extend String class.

require "charwidth/string"
"Hello, World!".normalize_charwidth # => "Hello, World!"
"Hello, World!".normalize_charwidth! # => destructive
"「ハローワールド」".normalize_charwidth # => "「ハローワールド」"

Rails integration

Add lines below to app/models/application_record.rb.

require "charwidth/active_model"
class ApplicationRecord < ActiveRecord::Base
  include Charwidth::ActiveModel
end

And call normalize_charwidth class method in model class.

class Article < ActiveRecord::Base
  normalize_charwidth :title, :body
end

a = Article.new(title: "Hello, World!")
a.title # => "Hello, World!"

CLI

# Call charwidth command with arguments
$ charwidth "Hello, World!"
Hello, World!
# Or as STDIN
$ echo "Hello, World!" | charwidth
Hello, World!

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request