Polyfill

The Polyfill filter adds JavaScript prototype polyfills for Ruby methods that don’t have direct JavaScript equivalents. Unlike the Functions filter which transforms method calls inline (e.g., .first becomes [0]), the Polyfill filter preserves the Ruby method names and adds runtime polyfill definitions at the top of your output.

This is useful when you want to:

  • Keep Ruby-style method names in your JavaScript for readability
  • Use methods like first and last as property accessors (without parentheses)
  • Maintain Ruby’s mutating behavior for methods like compact

Supported Methods

Array Methods

Ruby JavaScript Polyfill
.first Property getter returning this[0]
.last Property getter returning this.at(-1)
.compact Property getter that removes null/undefined in place
.rindex { } Method that finds last index matching block
.insert(i, items) Method using splice to insert items
.delete_at(i) Method using splice to remove item at index

String Methods

Ruby JavaScript Polyfill
.chomp(suffix) Removes suffix (or \r?\n if no arg) from end
.count(chars) Counts occurrences of any character in chars

Object Methods

Ruby JavaScript Polyfill
.to_a Property getter returning Object.entries(this)

Examples

# Input
arr.first
arr.last
arr.compact
str.chomp("\n")
hash.to_a
// Output (polyfills prepended, then code)
Object.defineProperty(Array.prototype, "first", {
  get() { return this[0] },
  configurable: true
});

Object.defineProperty(Array.prototype, "last", {
  get() { return this.at(-1) },
  configurable: true
});

Object.defineProperty(Array.prototype, "compact", {
  get() {
    let i = this.length - 1;
    while (i >= 0) {
      if (this[i] === null || this[i] === undefined) this.splice(i, 1);
      i--
    }
    return this
  },
  configurable: true
});

if (!String.prototype.chomp) {
  String.prototype.chomp = function(suffix) {
    if (suffix === undefined) return this.replace(/\r?\n$/m, "");
    if (this.endsWith(suffix)) return this.slice(0, this.length - suffix.length);
    return String(this)
  }
}

Object.defineProperty(Object.prototype, "to_a", {
  get() { return Object.entries(this) },
  configurable: true
});

arr.first;
arr.last;
arr.compact;
str.chomp("\n");
hash.to_a

Polyfill vs Functions Filter

The key difference between Polyfill and Functions:

Method Polyfill Filter Functions Filter
arr.first arr.first (property) arr[0]
arr.last arr.last (property) arr[arr.length - 1]
arr.compact arr.compact (mutating) arr.filter(x => x != null)
str.chomp str.chomp() (method) str.replace(/\r?\n$/, "")

Choose Polyfill when you want Ruby-style method names preserved in the output. Choose Functions when you want zero-runtime-overhead inline transformations.

Next: Return