Skip to content

Commit

Permalink
Implement String#squeeze
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Dec 3, 2013
1 parent dc98d0c commit e6c944e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 0 deletions.
22 changes: 22 additions & 0 deletions opal/corelib/string.rb
Expand Up @@ -661,6 +661,28 @@ def split(pattern = $; || ' ', limit = undefined)
}
end

def squeeze(*sets)
%x{
if (sets.length === 0) {
return self.replace(/(.)\\1+/g, '$1');
}
}

%x{
var set = #{Opal.coerce_to(`sets[0]`, String, :to_str).chars};
for (var i = 1, length = sets.length; i < length; i++) {
set = #{`set` & Opal.coerce_to(`sets[i]`, String, :to_str).chars};
}
if (set.length === 0) {
return self;
}
return self.replace(new RegExp("([" + #{Regexp.escape(`set`.join)} + "])\\\\1+", "g"), "$1");
}
end

def start_with?(*prefixes)
%x{
for (var i = 0, length = prefixes.length; i < length; i++) {
Expand Down
4 changes: 4 additions & 0 deletions spec/opal/filters/bugs/string.rb
Expand Up @@ -182,6 +182,10 @@
fails "String#split with Regexp returns subclass instances based on self"
fails "String#split with Regexp does not call constructor on created subclass instances"

fails "String#squeeze negates sets starting with ^"
fails "String#squeeze squeezes all chars in a sequence"
fails "String#squeeze raises an ArgumentError when the parameter is out of sequence"

fails "String#start_with? ignores arguments not convertible to string"
fails "String#start_with? converts its argument using :to_str"

Expand Down
5 changes: 5 additions & 0 deletions spec/opal/filters/unsupported/immutable_strings.rb
Expand Up @@ -136,6 +136,11 @@
fails "String#slice! with String returns a subclass instance when given a subclass instance"
fails "String#slice! with String raises a RuntimeError if self is frozen"

fails "String#squeeze! modifies self in place and returns self"
fails "String#squeeze! returns nil if no modifications were made"
fails "String#squeeze! raises an ArgumentError when the parameter is out of sequence"
fails "String#squeeze! raises a RuntimeError when self is frozen"

fails "String#strip! modifies self in place and returns self"
fails "String#strip! returns nil if no modifications where made"
fails "String#strip! modifies self removing trailing NULL bytes and whitespace"
Expand Down
1 change: 1 addition & 0 deletions spec/opal/filters/unsupported/string_subclasses.rb
Expand Up @@ -17,6 +17,7 @@
fails "String#ljust with length, padding returns subclass instances when called on subclasses"
fails "String#next returns subclass instances when called on a subclass"
fails "String#rjust with length, padding returns subclass instances when called on subclasses"
fails "String#squeeze returns subclass instances when called on a subclass"
fails "String#sub with pattern, replacement returns subclass instances when called on a subclass"
fails "String#succ returns subclass instances when called on a subclass"
fails "String#tr returns subclass instances when called on a subclass"
Expand Down
2 changes: 2 additions & 0 deletions spec/opal/filters/unsupported/tainted.rb
Expand Up @@ -119,6 +119,8 @@
fails "String#split with Regexp taints an empty string if self is tainted"
fails "String#split with Regexp doesn't taints the resulting strings if the Regexp is tainted"

fails "String#squeeze taints the result when self is tainted"

fails "String#strip taints the result when self is tainted"

fails "String#swapcase taints resulting string when self is tainted"
Expand Down
1 change: 1 addition & 0 deletions spec/opal/rubyspecs
Expand Up @@ -298,6 +298,7 @@ core/string/rstrip_spec
core/string/size_spec
#core/string/slice_spec - fails to find a shared thing
core/string/split_spec
core/string/squeeze_spec
core/string/start_with_spec
core/string/strip_spec
core/string/sub_spec
Expand Down

0 comments on commit e6c944e

Please sign in to comment.