module RubyRunJs::JsNumberMethods

Constants

RADIX_SYMBOLS

Public Class Methods

constructor(builtin, this, *args) click to toggle source
# File lib/ruby_run_js/object_methods/js_number.rb, line 59
def constructor(builtin, this, *args)
  if args.length == 0
    return 0.0
  end
  to_number(args[0])
end
constructor_new(builtin, this, *args) click to toggle source
# File lib/ruby_run_js/object_methods/js_number.rb, line 66
def constructor_new(builtin, this, *args)
  builtin.new_number(args.length == 0 ? 0.0 : args[0])
end
property_values() click to toggle source
# File lib/ruby_run_js/object_methods/js_number.rb, line 49
def property_values
  {
    'MAX_VALUE' => Float::MAX,
    'MIN_VALUE' => Float::MIN,
    'NAN' => Float::NAN,
    'NEGATIVE_INFINITY' => -Float::INFINITY,
    'POSITIVE_INFINITY' => Float::INFINITY
  }
end
prototype_toExponential(builtin, this, fraction_digits) click to toggle source
# File lib/ruby_run_js/object_methods/js_number.rb, line 131
def prototype_toExponential(builtin, this, fraction_digits)
  if this.js_class != 'Number'
    raise make_error('TypeError', 'Number.prototype.toExponential is not generic')
  end

  f = to_integer(fraction_digits)
  if f < 0 || f > 20
    raise make_error('RangeError', 'toExponential() digits argument must be between 0 and 20')
  end

  if this.js_type == :Object
    this = this.value
  end

  if this.infinite?
    return this > 0 ? 'Infinity' : '-Infinity'
  end

  if this.nan?
    return 'NaN'
  end

  "%0.#{f}e" % this
end
prototype_toFixed(builtin, this, fraction_digits) click to toggle source
# File lib/ruby_run_js/object_methods/js_number.rb, line 106
def prototype_toFixed(builtin, this, fraction_digits)
  if this.js_class != 'Number'
    raise make_error('TypeError', 'Number.prototype.toFixed is not generic')
  end

  f = to_integer(fraction_digits)
  if f < 0 || f > 20
    raise make_error('RangeError', 'toFixed() digits argument must be between 0 and 20')
  end

  if this.js_type == :Object
    this = this.value
  end

  if this.infinite?
    return this > 0 ? 'Infinity' : '-Infinity'
  end

  if this.nan?
    return 'NaN'
  end

  "%0.#{f}f" % this
end
prototype_toLocaleString(builtin, this, radix)
Alias for: prototype_toString
prototype_toPrecision(builtin, this, precision) click to toggle source
# File lib/ruby_run_js/object_methods/js_number.rb, line 156
def prototype_toPrecision(builtin, this, precision)
  if this.js_class != 'Number'
    raise make_error('TypeError', 'Number.prototype.toPrecision is not generic')
  end

  if this.js_type == :Object
    this = this.value
  end

  return to_string(this) if precision == undefined

  f = to_integer(precision)

  if this.infinite?
    return this > 0 ? 'Infinity' : '-Infinity'
  end

  if this.nan?
    return 'NaN'
  end

  if f < 1 || f > 20
    raise make_error('RangeError', 'toPrecision() digits argument must be between 1 and 20')
  end

  digs = f - this.to_i.to_s.length

  digs >= 0 ? ("%0.#{digs}f" % this) : ("%0.#{f - 1}f" % this)
end
prototype_toString(builtin, this, radix) click to toggle source
# File lib/ruby_run_js/object_methods/js_number.rb, line 70
def prototype_toString(builtin, this, radix)
  if this.js_class != 'Number'
    raise make_error('TypeError', 'Number.prototype.toString is not generic')
  end

  radix = radix == undefined ? 10 : to_integer(radix)
  if radix < 2 || radix > 36
    raise make_error('RangeError', 'Number.prototype.toString() radix argument must be an integer between 2 and 36')
  end

  num = to_integer(this)

  sign = ''
  if num < 0
    sign = '-'
    num = -num
  end
  result = ''
  while num > 0
    s = RADIX_SYMBOLS[num % radix]
    num = num / radix
    result = s + result
  end
  sign + (result == '' ? '0' : result)
end
Also aliased as: prototype_toLocaleString
prototype_valueOf(builtin, this) click to toggle source
# File lib/ruby_run_js/object_methods/js_number.rb, line 96
def prototype_valueOf(builtin, this)
  if this.js_class != 'Number'
    raise make_error('TypeError', 'Number.prototype.valueOf is not generic')
  end
  if this.js_type == :Object
    this = this.value
  end
  this
end