PostsSifting for Javascript, Ruby Style
Posted 11/14/2014

Header Photo credit to Thomas Fischer Rare Book Library

Panning for Rubies

It is difficult to wrangle long hashes, and big objects when debugging. Often times, you know the name of the attribute or method, and need to visually scan through a list. Ruby has a built in solution to make this process less painful: The object.methods.grep and hash.keys.grep patterns. Here is each one in action:

Sifting Arrays

Filtering through arrays in Ruby is as easy as using the grep command.

> hash.methods.length
 => 147
> hash.methods.grep /to_/
=> [:to_hash, :to_h, :to_a, :to_s, :to_enum]

Instead of reading through a list of 147 entries, the programmer can filter it to what is important.

Feeling the Pain

A common painpoint faced in javascript is sifting through json keys. Sometimes you know the key is something like reference_number, but object.reference_number is undefined. In this case, you may need to pop open the object and read through the list. If the key was order_reference_number it may be hard to find. uspecially in a long list.

A Solution

Here are two small tools inspired by Ruby's grep to mitigate this pain:

Array.prototype.grep = function (regex) {
  this.filter( function (elem) {
    return elem.match(regex)})}

Object.grepKeys = function (object, regex) {
  Object.keys(object)
    .grep(regex)
    .reduce(function(memo, val){
      memo[val] = object[val];
      return memo}, {})}

With these methods at hand, wrangling arrays, and objects becomes much simpler:

> array = ['foo', 'bar', 'baz']
> array.grep(/b/)
['bar', 'baz']
> array.grep(/ar/)
['bar']

> obj = [foo: 1, bar: 1, baz: 1]
> Object.grepKeys(obj, /oo/)
{foo: 1}
> Object.grepKeys(obj, /[^b]/)
{bar: 1, baz: 1}

I dont actually use these functions in my application code, but include them to make debugging less painful.

For The Faint Hearted

Some are weary of overwriting builtin prototypes. This currying solution provided by Ampersand55 on reddit may suit them better than the above solution.

function grep(regex) {
  return function(el) {
    return regex.test(el)
  }
}

Enjoy, and happy coding!!!