Underscore

The Underscore filter maps relevant Ruby methods to their Underscore.js library equivalents.

List of Transformations

  • .clone() _.clone()
  • .compact() _.compact()
  • .count_by {} _.countBy {}
  • .find {} _.find {}
  • .find_by() _.findWhere()
  • .flatten() _.flatten()
  • .group_by {} _.groupBy {}
  • .has_key?() _.has()
  • .index_by {} _.indexBy {}
  • .invert() _.invert()
  • .invoke(&:n) _.invoke(, :n)
  • .map(&:n) _.pluck(, :n)
  • .merge!() _.extend()
  • .merge() _.extend({}, )
  • .reduce {} _.reduce {}
  • .reduce() _.reduce()
  • .reject {} _.reject {}
  • .sample() _.sample()
  • .select {} _.select {}
  • .shuffle() _.shuffle()
  • .size() _.size()
  • .sort() _.sort_by(, _.identity)
  • .sort_by {} _.sortBy {}
  • .times {} _.times {}
  • .values() _.values()
  • .where() _.where()
  • .zip() _.zip()
  • (n...m) _.range(n, m)
  • (n..m) _.range(n, m+1)
  • n.take(m) _.take(n, m)
  • n.drop(m) _.drop(n, m)
  • .min _.min()
  • .max _.max()
  • n.each_slice(m) _.chunk(n, m)

Additional Features

  • .compact!, .flatten!, shuffle!, reject!, sort_by!, and .uniq become equivalent .splice(0, .length, *.method()) statements
  • 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: reduce, sort_by, group_by, index_by, count_by, find, select, reject.
  • is_a? and kind_of? map to Object.prototype.toString.call() === "[object #{type}]" for the following types: Arguments, Boolean, Date, Error, Function, Number, Object, RegExp, String; and maps Ruby names to JavaScript equivalents for Exception, Float, Hash, Proc, and Regexp. Additionally, is_a? and kind_of? map to Array.isArray() for Array`.

Next: Vue