Functions

The Functions filter provides a large number of convenience methods Rubyists are familar with. Statements such as "252.3".to_i transform into parseInt("252.3"), or [1,3,5].yield_self { |arr| arr[1] } into (arr => arr[1])([1, 3, 5]). Generally you will want to include this filter in your configuration unless you have specific reason not to.

List of Transformations

  • .abs Math.abs()
  • .all? .every
  • .any? .some
  • .ceil Math.ceil()
  • .chars Array.from()
  • .chr fromCharCode
  • .clear .length = 0
  • .define_method klass.prototype.meth = function ...
  • .delete delete target[arg]
  • .downcase .toLowerCase
  • .each .forEach
  • .each_key for (i in ...) {}
  • .each_pair for (var key in item) {var value = item[key]; ...}
  • .each_value .forEach
  • .each_with_index .forEach
  • .end_with? .slice(-arg.length) == arg
  • .empty? .length == 0
  • .find_index findIndex
  • .first [0]
  • .first(n) .slice(0, n)
  • .floor Math.floor()
  • .gsub replace(//g)
  • .include? .indexOf() != -1
  • .index indexOf (when using arg) or findIndex (when using block)
  • .inspect JSON.stringify()
  • .keys() Object.keys()
  • .last [*.length-1]
  • .last(n) .slice(*.length-1, *.length)
  • .lstrip .replace(/^\s+/, "")
  • .max Math.max.apply(Math)
  • .merge Object.assign({}, ...)
  • .merge! Object.assign()
  • .method_defined? klass.prototype.hasOwnProperty(meth) or meth in klass.prototype
  • .min Math.min.apply(Math)
  • .new(size,default) == .new(size).fill(default)
  • .nil? == null
  • .ord charCodeAt(0)
  • puts console.log
  • rand Math.random
  • .replace .length = 0; ...push.apply(*)
  • .respond_to? right in left
  • .rindex .lastIndexOf
  • .round Math.round()
  • .rstrip .replace(/s+$/, "")
  • .scan .match(//g)
  • .sum .reduce(function(a, b) {a + b}, 0)
  • .start_with? .substring(0, arg.length) == arg or .startsWith(arg) for ES2015+
  • .upto(lim) for (var i=num; i<=lim; i+=1)
  • .downto(lim) for (var i=num; i>=lim; i-=1)
  • .step(lim, n).each for (var i=num; i<=lim; i+=n)
  • .step(lim, -n).each for (var i=num; i>=lim; i-=n)
  • (0..a).to_a Array.apply(null, {length: a}).map(Function.call, Number)
  • (b..a).to_a Array.apply(null, {length: (a-b+1)}).map(Function.call, Number).map(function (idx) { return idx+b })
  • (b...a).to_a Array.apply(null, {length: (a-b)}).map(Function.call, Number).map(function (idx) { return idx+b })
  • .strip .trim
  • .sub .replace
  • .tap {|n| n} (function(n) {n; return n})(...)
  • .to_f parseFloat
  • .to_i parseInt
  • .to_s .toString
  • .to_json JSON.stringify(obj)
  • .upcase .toUpperCase
  • .yield_self {|n| n} (function(n) {return n})(...)
  • [-n] [*.length-n] for literal values of n
  • [n...m] .slice(n,m)
  • [n..m] .slice(n,m+1)
  • [/r/, n] .match(/r/)[n]
  • [/r/, n]= .replace(/r/, ...)
  • (1..2).each {|i| ...} for (var i=1 i<=2; i+=1)
  • "string" * length new Array(length + 1).join("string") or "string".repeat(length) for ES2015+
  • @foo.call(args) this._foo(args)
  • @@foo.call(args) this.constructor._foo(args)
  • Array(x) Array.prototype.slice.call(x)
  • delete x delete x (note lack of parenthesis)

Additional Features

  • .sub! and .gsub! become equivalent x = x.replace statements
  • .map!, .reverse!, and .select! become equivalent .splice(0, .length, *.method()) statements
  • setInterval and setTimeout allow block to be treated as the first parameter on the call
  • for the following methods, if the block consists entirely of a simple expression (or ends with one), a return is added prior to the expression: sub, gsub, any?, all?, map, find, find_index.
  • New classes subclassed off of Exception will become subclassed off of Error instead; and default constructors will be provided
  • loop do...end will be replaced with while (true) {...}
  • raise Exception.new(...) will be replaced with throw new Error(...)
  • block_given? will check for the presence of optional argument _implicitBlockYield which is a function made accessible through the use of yield in a method body.
  • alias_method works both inside of a class definition as well as called directly on a class name (e.g. MyClass.alias_method)

Additionally, there is two mappings that will only be done if explicitly included (pass include: [:class, :call] as a convert option to enable):

  • .class .constructor
  • a.call a()