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: 64caa9f68662
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: b5235f4d8b5b
Choose a head ref
  • 2 commits
  • 5 files changed
  • 2 contributors

Commits on Apr 12, 2015

  1. Copy the full SHA
    8ed9fc6 View commit details
  2. Merge pull request #794 from vais/integer

    Kernel#Integer fully compliant with rubyspec
    adambeynon committed Apr 12, 2015
    Copy the full SHA
    b5235f4 View commit details
104 changes: 75 additions & 29 deletions opal/corelib/kernel.rb
Original file line number Diff line number Diff line change
@@ -441,42 +441,88 @@ def instance_variables
}
end

def Integer(value, base = nil)
if String === value
if value.empty?
raise ArgumentError, "invalid value for Integer: (empty string)"
end
def Integer(value, base = undefined)
%x{
var i, str;
return `parseInt(#{value}, #{base || `undefined`})`
end
if (!value.$$is_string) {
if (base !== undefined) {
#{raise ArgumentError, "base specified for non string value"}
}
if (value === nil) {
#{raise TypeError, "can't convert nil into Integer"}
}
if (value.$$is_number) {
if (value === Infinity || value === -Infinity || isNaN(value)) {
#{raise FloatDomainError, value}
}
return Math.floor(value);
}
if (#{value.respond_to?(:to_int)}) {
i = #{value.to_int};
if (i !== nil) {
return i;
}
}
return #{Opal.coerce_to!(value, Integer, :to_i)};
}
if base
raise ArgumentError "base is only valid for String values"
end
if (base === undefined) {
base = 0;
} else {
base = #{Opal.coerce_to(`base`, Integer, :to_int)};
if (base === 1 || base < 0 || base > 36) {
#{raise ArgumentError, "invalid radix #{base}"}
}
}
str = value.toLowerCase();
case value
when Integer
value
if (/^\s*_|__|_\s*$/.test(str)) {
#{raise ArgumentError, "invalid value for Integer(): \"#{value}\""}
}
str = str.replace(/_/g, '');
if (!/^\s*[+-]?[0-9a-z]+\s*$/.test(str)) {
#{raise ArgumentError, "invalid value for Integer(): \"#{value}\""}
}
when Float
if value.nan? or value.infinite?
raise FloatDomainError, "unable to coerce #{value} to Integer"
end
str = str.replace(/^(\s*[+-]?)(0[bodx]?)/, function (_, head, flag) {
switch (flag) {
case '0b':
if (base === 0 || base === 2) {
base = 2;
return head;
}
case '0':
case '0o':
if (base === 0 || base === 8) {
base = 8;
return head;
}
case '0d':
if (base === 0 || base === 10) {
base = 10;
return head;
}
case '0x':
if (base === 0 || base === 16) {
base = 16;
return head;
}
}
#{raise ArgumentError, "invalid value for Integer(): \"#{value}\""}
});
value.to_int
i = parseInt(str, base);
when NilClass
raise TypeError, "can't convert nil into Integer"
if (isNaN(i)) {
#{raise ArgumentError, "invalid value for Integer(): \"#{value}\""}
}
else
if value.respond_to? :to_int
value.to_int
elsif value.respond_to? :to_i
value.to_i
else
raise TypeError, "can't convert #{value.class} into Integer"
end
end
return i;
}
end

def Float(value)
Loading