Implement Chars#swapcase.
This commit is contained in:
parent
fc956425d4
commit
cac15643cf
@ -118,6 +118,14 @@ def downcase
|
|||||||
chars Unicode.downcase(@wrapped_string)
|
chars Unicode.downcase(@wrapped_string)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Converts characters in the string to the opposite case.
|
||||||
|
#
|
||||||
|
# Example:
|
||||||
|
# 'El Cañón".mb_chars.swapcase.to_s # => "eL cAÑÓN"
|
||||||
|
def swapcase
|
||||||
|
chars Unicode.swapcase(@wrapped_string)
|
||||||
|
end
|
||||||
|
|
||||||
# Converts the first character to uppercase and the remainder to lowercase.
|
# Converts the first character to uppercase and the remainder to lowercase.
|
||||||
#
|
#
|
||||||
# Example:
|
# Example:
|
||||||
|
@ -293,9 +293,17 @@ def upcase(string)
|
|||||||
apply_mapping string, :uppercase_mapping
|
apply_mapping string, :uppercase_mapping
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def swapcase(string)
|
||||||
|
apply_mapping string, :swapcase_mapping
|
||||||
|
end
|
||||||
|
|
||||||
# Holds data about a codepoint in the Unicode database
|
# Holds data about a codepoint in the Unicode database
|
||||||
class Codepoint
|
class Codepoint
|
||||||
attr_accessor :code, :combining_class, :decomp_type, :decomp_mapping, :uppercase_mapping, :lowercase_mapping
|
attr_accessor :code, :combining_class, :decomp_type, :decomp_mapping, :uppercase_mapping, :lowercase_mapping
|
||||||
|
|
||||||
|
def swapcase_mapping
|
||||||
|
uppercase_mapping > 0 ? uppercase_mapping : lowercase_mapping
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Holds static data from the Unicode database
|
# Holds static data from the Unicode database
|
||||||
|
@ -106,7 +106,7 @@ def test_split_should_return_an_array_of_chars_instances
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
%w{capitalize downcase lstrip reverse rstrip upcase}.each do |method|
|
%w{capitalize downcase lstrip reverse rstrip swapcase upcase}.each do |method|
|
||||||
class_eval(<<-EOTESTS)
|
class_eval(<<-EOTESTS)
|
||||||
def test_#{method}_bang_should_return_self_when_modifying_wrapped_string
|
def test_#{method}_bang_should_return_self_when_modifying_wrapped_string
|
||||||
chars = ' él piDió Un bUen café '
|
chars = ' él piDió Un bUen café '
|
||||||
@ -161,6 +161,7 @@ def test_string_methods_are_chainable
|
|||||||
assert chars('').decompose.kind_of?(ActiveSupport::Multibyte.proxy_class)
|
assert chars('').decompose.kind_of?(ActiveSupport::Multibyte.proxy_class)
|
||||||
assert chars('').compose.kind_of?(ActiveSupport::Multibyte.proxy_class)
|
assert chars('').compose.kind_of?(ActiveSupport::Multibyte.proxy_class)
|
||||||
assert chars('').tidy_bytes.kind_of?(ActiveSupport::Multibyte.proxy_class)
|
assert chars('').tidy_bytes.kind_of?(ActiveSupport::Multibyte.proxy_class)
|
||||||
|
assert chars('').swapcase.kind_of?(ActiveSupport::Multibyte.proxy_class)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_should_be_equal_to_the_wrapped_string
|
def test_should_be_equal_to_the_wrapped_string
|
||||||
@ -432,6 +433,11 @@ def test_downcase_should_downcase_ascii_characters
|
|||||||
assert_equal 'abc', 'aBc'.mb_chars.downcase
|
assert_equal 'abc', 'aBc'.mb_chars.downcase
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_swapcase_should_swap_ascii_characters
|
||||||
|
assert_equal '', ''.mb_chars.swapcase
|
||||||
|
assert_equal 'AbC', 'aBc'.mb_chars.swapcase
|
||||||
|
end
|
||||||
|
|
||||||
def test_capitalize_should_work_on_ascii_characters
|
def test_capitalize_should_work_on_ascii_characters
|
||||||
assert_equal '', ''.mb_chars.capitalize
|
assert_equal '', ''.mb_chars.capitalize
|
||||||
assert_equal 'Abc', 'abc'.mb_chars.capitalize
|
assert_equal 'Abc', 'abc'.mb_chars.capitalize
|
||||||
@ -466,10 +472,15 @@ def test_upcase_should_be_unicode_aware
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_downcase_should_be_unicode_aware
|
def test_downcase_should_be_unicode_aware
|
||||||
assert_equal "абвгд\0f", chars("аБвгд\0f").downcase
|
assert_equal "абвгд\0f", chars("аБвгд\0F").downcase
|
||||||
assert_equal 'こにちわ', chars('こにちわ').downcase
|
assert_equal 'こにちわ', chars('こにちわ').downcase
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_swapcase_should_be_unicode_aware
|
||||||
|
assert_equal "аaéÜ\0f", chars("АAÉü\0F").swapcase
|
||||||
|
assert_equal 'こにちわ', chars('こにちわ').swapcase
|
||||||
|
end
|
||||||
|
|
||||||
def test_capitalize_should_be_unicode_aware
|
def test_capitalize_should_be_unicode_aware
|
||||||
{ 'аБвг аБвг' => 'Абвг абвг',
|
{ 'аБвг аБвг' => 'Абвг абвг',
|
||||||
'аБвг АБВГ' => 'Абвг абвг',
|
'аБвг АБВГ' => 'Абвг абвг',
|
||||||
|
Loading…
Reference in New Issue
Block a user