Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 47a3d474d749
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 71bc1d565e44
Choose a head ref
  • 4 commits
  • 1 file changed
  • 1 contributor

Commits on Nov 13, 2013

  1. Compliancy fixes for String#ljust

    meh committed Nov 13, 2013
    Copy the full SHA
    ac2846f View commit details
  2. Compliancy fixes for String#rjust

    meh committed Nov 13, 2013
    Copy the full SHA
    b6b22c6 View commit details
  3. Copy the full SHA
    7e446f6 View commit details

Commits on Nov 14, 2013

  1. Complancy fixes for String#<=>

    meh committed Nov 14, 2013
    Copy the full SHA
    71bc1d5 View commit details
Showing with 36 additions and 10 deletions.
  1. +36 −10 opal/core/string.rb
46 changes: 36 additions & 10 deletions opal/core/string.rb
Original file line number Diff line number Diff line change
@@ -50,16 +50,21 @@ def +(other)
end

def <=>(other)
%x{
if (other._isString) {
return self > other ? 1 : (self < other ? -1 : 0);
}
}

if other.respond_to? :to_str
other = other.to_str.to_s

`self > other ? 1 : (self < other ? -1 : 0)`
else
%x{
var cmp = #{other <=> self};
if (cmp === nil) {
return nil;
}
else {
return cmp > 0 ? -1 : (cmp < 0 ? 1 : 0);
}
}
end
end

@@ -144,13 +149,20 @@ def casecmp(other)
end

def center(width, padstr = ' ')
return self if `width === self.length`
width = Opal.coerce_to(width, Integer, :to_int)
padstr = Opal.coerce_to(padstr, String, :to_str).to_s

if padstr.empty?
raise ArgumentError, 'zero width padding'
end

return self if `width <= self.length`

%x{
var ljustified = #{ljust ((width + @length) / 2).floor, padstr},
rjustified = #{rjust ((width + @length) / 2).ceil, padstr};
var ljustified = #{ljust ((width + @length) / 2).ceil, padstr},
rjustified = #{rjust ((width + @length) / 2).floor, padstr};
return ljustified + rjustified.slice(self.length);
return rjustified + ljustified.slice(self.length);
}
end

@@ -399,6 +411,13 @@ def length
end

def ljust(width, padstr = ' ')
width = Opal.coerce_to(width, Integer, :to_int)
padstr = Opal.coerce_to(padstr, String, :to_str).to_s

if padstr.empty?
raise ArgumentError, 'zero width padding'
end

return self if `width <= self.length`

%x{
@@ -507,6 +526,13 @@ def rindex(search, offset = undefined)
end

def rjust(width, padstr = ' ')
width = Opal.coerce_to(width, Integer, :to_int)
padstr = Opal.coerce_to(padstr, String, :to_str).to_s

if padstr.empty?
raise ArgumentError, 'zero width padding'
end

return self if `width <= self.length`

%x{