Permalink
Browse files

+ Add String#ascii_only & #ascii_only!

  • Loading branch information...
1 parent d1ec550 commit a4f35e88dcb9404f36b7790e06aa03f2da899930 @PikachuEXE PikachuEXE committed Sep 16, 2013
Showing with 89 additions and 0 deletions.
  1. +3 −0 CHANGELOG.md
  2. +1 −0 lib/powerpack/string.rb
  3. +42 −0 lib/powerpack/string/ascii_only.rb
  4. +43 −0 spec/powerpack/string/ascii_only_spec.rb
View
@@ -2,6 +2,9 @@
## master (unreleased)
+* Added `String#ascii_only`
+* Added `String#ascii_only!`
+
## 0.0.8 (13/09/2013)
### New features
View
@@ -1,3 +1,4 @@
+require_relative 'string/ascii_only'
require_relative 'string/blank'
require_relative 'string/format'
require_relative 'string/strip_indent'
@@ -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
@@ -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

0 comments on commit a4f35e8

Please sign in to comment.