From a4f35e88dcb9404f36b7790e06aa03f2da899930 Mon Sep 17 00:00:00 2001 From: PikachuEXE Date: Mon, 16 Sep 2013 18:00:14 +0800 Subject: [PATCH] + Add String#ascii_only & #ascii_only! --- CHANGELOG.md | 3 ++ lib/powerpack/string.rb | 1 + lib/powerpack/string/ascii_only.rb | 42 +++++++++++++++++++++++ spec/powerpack/string/ascii_only_spec.rb | 43 ++++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 lib/powerpack/string/ascii_only.rb create mode 100644 spec/powerpack/string/ascii_only_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index d1478cd..fda4f8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## master (unreleased) +* Added `String#ascii_only` +* Added `String#ascii_only!` + ## 0.0.8 (13/09/2013) ### New features diff --git a/lib/powerpack/string.rb b/lib/powerpack/string.rb index 93a748d..75d5d89 100644 --- a/lib/powerpack/string.rb +++ b/lib/powerpack/string.rb @@ -1,3 +1,4 @@ +require_relative 'string/ascii_only' require_relative 'string/blank' require_relative 'string/format' require_relative 'string/strip_indent' diff --git a/lib/powerpack/string/ascii_only.rb b/lib/powerpack/string/ascii_only.rb new file mode 100644 index 0000000..1b49f3c --- /dev/null +++ b/lib/powerpack/string/ascii_only.rb @@ -0,0 +1,42 @@ +# encoding: utf-8 + +class String + # Return a copy of string with ASCII characters only + # Source: http://stackoverflow.com/questions/1268289/how-to-get-rid-of-non-ascii-characters-in-ruby + # + # @return [String] a copy of string with ASCII characters only + # + # @example + # 'abc'.ascii_only #=> 'abc' + # + # @example + # '中文123'.ascii_only #=> '123' + unless String.method_defined? :ascii_only + def ascii_only + dup.ascii_only! + end + end + + # Modify self and keep ASCII characters only + # Returns the string even if no changes were made. + # Source: http://stackoverflow.com/questions/1268289/how-to-get-rid-of-non-ascii-characters-in-ruby + # + # @return [String] The result string + # + # @example + # 'abc'.ascii_only! #=> 'abc' + # + # @example + # '中文123'.ascii_only! #=> '123' + unless String.method_defined? :ascii_only! + def ascii_only! + encoding_options = { + :invalid => :replace, # Replace invalid byte sequences + :undef => :replace, # Replace anything not defined in ASCII + :replace => '', # Use a blank for those replacements + :UNIVERSAL_NEWLINE_DECORATOR => true # Always break lines with \n + } + self.encode! Encoding.find('ASCII'), encoding_options + end + end +end diff --git a/spec/powerpack/string/ascii_only_spec.rb b/spec/powerpack/string/ascii_only_spec.rb new file mode 100644 index 0000000..abd7ebe --- /dev/null +++ b/spec/powerpack/string/ascii_only_spec.rb @@ -0,0 +1,43 @@ +# encoding: utf-8 + +require 'spec_helper' + +describe 'String#ascii_only' do + it 'returns same value for string with ASCII chars only' do + expect('abc'.ascii_only).to eq 'abc' + end + + it 'returns string without non-ASCII chars' do + expect("abc\u{6666}".force_encoding("UTF-8").ascii_only).to eq 'abc' + end + + it 'returns string without non-ASCII chars and with ASCII chars' do + expect("\u{6666}".force_encoding("UTF-8").ascii_only).to eq '' + end + + it 'does not change the original string' do + string = "abc\u{6666}".force_encoding("UTF-8") + string.ascii_only + expect(string).to eq "abc\u{6666}".force_encoding("UTF-8") + end +end + +describe 'String#ascii_only!' do + it 'returns same value for string with ASCII chars only' do + expect('abc'.ascii_only!).to eq 'abc' + end + + it 'returns string without non-ASCII chars' do + expect("abc\u{6666}".force_encoding("UTF-8").ascii_only!).to eq 'abc' + end + + it 'returns string without non-ASCII chars and with ASCII chars' do + expect("\u{6666}".force_encoding("UTF-8").ascii_only!).to eq '' + end + + it 'changes the original string' do + string = "abc\u{6666}".force_encoding("UTF-8") + string.ascii_only! + expect(string).to eq 'abc' + end +end