Permalink
Browse files

Add Normalization.normalize method that supports :using option.

  • Loading branch information...
1 parent c9db75d commit 009536a42c3c82f9b5e846a248ad376a49047b21 @KL-7 KL-7 committed Aug 14, 2012
Showing with 81 additions and 22 deletions.
  1. +1 −9 lib/twitter_cldr/core_ext/string.rb
  2. +23 −0 lib/twitter_cldr/normalization.rb
  3. +15 −13 spec/core_ext/string_spec.rb
  4. +42 −0 spec/normalization_spec.rb
@@ -11,7 +11,6 @@ def localize(locale = TwitterCldr.get_locale)
module TwitterCldr
class LocalizedString < LocalizedObject
- VALID_NORMALIZERS = [:NFD, :NFKD, :NFC, :NFKC]
# Uses wrapped string object as a format specification and returns the result of applying it to +args+ (see
# +TwitterCldr::Utils.interpolate+ method for interpolation syntax).
@@ -29,14 +28,7 @@ def formatter_const
end
def normalize(options = {})
- options[:using] ||= :NFD
-
- if VALID_NORMALIZERS.include?(options[:using])
- normalizer_const = TwitterCldr::Normalization.const_get(options[:using])
- LocalizedString.new(normalizer_const.normalize(@base_obj), @locale)
- else
- raise ArgumentError.new("Invalid normalization form specified with :using option. Choices are [#{VALID_NORMALIZERS.map(&:to_s).join(", ")}]")
- end
+ TwitterCldr::Normalization.normalize(@base_obj, options).localize(@locale)
end
def code_points
@@ -11,5 +11,28 @@ module Normalization
autoload :NFD, 'twitter_cldr/normalization/nfd'
autoload :NFKC, 'twitter_cldr/normalization/nfkc'
autoload :NFKD, 'twitter_cldr/normalization/nfkd'
+
+ VALID_NORMALIZERS = [:NFD, :NFKD, :NFC, :NFKC]
+ DEFAULT_NORMALIZER = :NFD
+
+ class << self
+
+ def normalize(string, options = {})
+ normalizer(options[:using] || DEFAULT_NORMALIZER).normalize(string)
+ end
+
+ private
+
+ def normalizer(normalizer_name)
+ const_name = normalizer_name.to_s.upcase.to_sym
+
+ if VALID_NORMALIZERS.include?(const_name)
+ const_get(const_name)
+ else
+ raise ArgumentError.new("#{normalizer_name.inspect} is not a valid normalizer (valid normalizers are #{VALID_NORMALIZERS.join(', ')})")
+ end
+ end
+
+ end
end
end
@@ -110,24 +110,26 @@
end
describe "#normalize" do
- it "returns a normalized instance of LocalizedString, defaults to NFD" do
- mock.proxy(TwitterCldr::Normalization::NFD).normalize("español")
- "español".bytes.to_a.should == [101, 115, 112, 97, 195, 177, 111, 108]
- result = "español".localize.normalize
- result.should be_a(LocalizedString)
- result.to_s.bytes.to_a.should == [101, 115, 112, 97, 110, 204, 131, 111, 108]
+ let(:string) { 'string' }
+ let(:normalized_string) { 'normalized' }
+ let(:localized_string) { string.localize }
+
+ it 'returns a LocalizedString' do
+ localized_string.normalize.should be_an_instance_of(LocalizedString)
+ end
+
+ it 'it uses NFD by default' do
+ mock(TwitterCldr::Normalization::NFD).normalize(string) { normalized_string }
+ localized_string.normalize.base_obj.should == normalized_string
end
- it "returns a normalized instance of LocalizedString using the specified algorithm" do
- mock.proxy(TwitterCldr::Normalization::NFKD).normalize("español")
- "español".bytes.to_a.should == [101, 115, 112, 97, 195, 177, 111, 108]
- result = "español".localize.normalize(:using => :NFKD)
- result.should be_a(LocalizedString)
- result.to_s.bytes.to_a.should == [101, 115, 112, 97, 110, 204, 131, 111, 108]
+ it "uses specified algorithm if there is any" do
+ mock(TwitterCldr::Normalization::NFKD).normalize(string) { normalized_string }
+ localized_string.normalize(:using => :NFKD).base_obj.should == normalized_string
end
it "raises an ArgumentError if passed an unsupported normalization form" do
- lambda { "español".localize.normalize(:using => :blarg) }.should raise_error(ArgumentError)
+ lambda { localized_string.normalize(:using => :blarg) }.should raise_error(ArgumentError)
end
end
View
@@ -0,0 +1,42 @@
+# encoding: UTF-8
+
+# Copyright 2012 Twitter, Inc
+# http://www.apache.org/licenses/LICENSE-2.0
+
+require 'spec_helper'
+
+include TwitterCldr
+
+describe TwitterCldr::Normalization do
+
+ describe "#normalize" do
+ let(:string) { 'string' }
+ let(:normalized_string) { 'normalized' }
+
+ it 'it uses NFD by default' do
+ mock(TwitterCldr::Normalization::NFD).normalize(string) { normalized_string }
+ Normalization.normalize(string).should == normalized_string
+ end
+
+ it "uses specified algorithm if there is any" do
+ mock(TwitterCldr::Normalization::NFKD).normalize(string) { normalized_string }
+ Normalization.normalize(string, :using => :NFKD).should == normalized_string
+ end
+
+ it "raises an ArgumentError if passed an unsupported normalizer name" do
+ lambda { Normalization.normalize(string, :using => :blarg) }.should raise_error(ArgumentError)
+ end
+
+ it 'accepts normalizer name in a lower case' do
+ mock(TwitterCldr::Normalization::NFKD).normalize(string) { normalized_string }
+ Normalization.normalize(string, :using => :nfkd).should == normalized_string
+ end
+
+ it 'accepts a string' do
+ mock(TwitterCldr::Normalization::NFKD).normalize(string) { normalized_string }
+ Normalization.normalize(string, :using => 'NFKD').should == normalized_string
+ end
+
+ end
+
+end

0 comments on commit 009536a

Please sign in to comment.