Menu
Alfredo Motta
  • Home
  • Newsletter
  • Articles
  • About me
  • Events
  • Books
  • Research
  • Learn to code in London
Alfredo Motta

Bringing Ruby fetch to the Javascript world

Posted on February 23, 2015May 18, 2022

If you are a Rubyist you are probably comfortable using the fetch method on a day-to-day basis but when you are developing in Javascript this sweetness is not immediately available. This is why I wrote underscorejs-fetch.

The gist of the fetch method in Ruby is the following:

Ruby #fetch method
Ruby
1
2
3
4
5
6
7
8
9
10
11
12
item = { name: 'pen', color: 'blue' }
item.fetch(:name)                                     #=> 'pen'
 
item.fetch(:price)                                    #=> key not found error
item.fetch(:price, '2
 
I wished I could find exactly the same in <a href="http://underscorejs.org/">UnderscoreJS</a> but <a href="http://underscorejs.org/#has">_.has()</a>, <a href="http://underscorejs.org/#findKey">_.findKey()</a> or <a href="http://underscorejs.org/#property">_.property()</a> are not quite what I am looking for.
 
So I went ahead and I <a href="https://github.com/mottalrd/underscore-fetch">implemented it myself</a>. Here is how it works. If you have  an object and you want to <strong>fetch</strong> an existing property, you simply get it:
 
<pre class="lang:js decode:true">var item = { name: 'pen', color: 'blue' };
_.fetch(item, 'name'); //=> pen

On the other hand if the property is not available, you get an Attribute not found error in return:

JavaScript
1
2
var item = { name: 'pen', color: 'blue' };
_.fetch(item, 'price'); //=> Error('Attribute not found');

Now the sweetest part of fetch. When the property you are asking can’t be found, you can simply pass a default:

1
2
3
4
5
6
7
8
9
10
var item = { name: 'pen', color: 'blue' };
_.fetch(item, 'price', '2
 
Finally -- for sophisticated people -- you also have the option to pass a callback in case the key you are looking for is not found:
 
<pre class="lang:default decode:true ">var item = { name: 'pen', color: 'blue' };
_.fetch(item, 'price', function(key) {
  return "The " + key + " cannot be found";
});
//=> 'The price cannot be found'

These example are also available as Jasmine specs in the Github repository. Running the specs is super-easy, just checkout the repository and run:

Shell
1
2
3
sudo npm install -g grunt-cli
npm install
grunt jasmine

There is no reason to publish a 33 lines method function apart from sharing it and having the opportunity to discuss it with the community.

You love fetch? Then go fetch the repo on Github and tell me what you think!

) #=> ‘2I wished I could find exactly the same in UnderscoreJS but _.has(), _.findKey() or _.property() are not quite what I am looking for.

So I went ahead and I implemented it myself. Here is how it works. If you have  an object and you want to fetch an existing property, you simply get it:

1
 

On the other hand if the property is not available, you get an Attribute not found error in return:

1
 

Now the sweetest part of fetch. When the property you are asking can’t be found, you can simply pass a default:

1
 

Finally — for sophisticated people — you also have the option to pass a callback in case the key you are looking for is not found:

1
 

These example are also available as Jasmine specs in the Github repository. Running the specs is super-easy, just checkout the repository and run:

1
 

There is no reason to publish a 33 lines method function apart from sharing it and having the opportunity to discuss it with the community.

You love fetch? Then go fetch the repo on Github and tell me what you think!

item.fetch(:price) { |key| “the #{key.to_s} is 2$”} #=> “the price is 2$”

I wished I could find exactly the same in UnderscoreJS but _.has(), _.findKey() or _.property() are not quite what I am looking for.

So I went ahead and I implemented it myself. Here is how it works. If you have  an object and you want to fetch an existing property, you simply get it:

1
 

On the other hand if the property is not available, you get an Attribute not found error in return:

1
 

Now the sweetest part of fetch. When the property you are asking can’t be found, you can simply pass a default:

1
 

Finally — for sophisticated people — you also have the option to pass a callback in case the key you are looking for is not found:

1
 

These example are also available as Jasmine specs in the Github repository. Running the specs is super-easy, just checkout the repository and run:

1
 

There is no reason to publish a 33 lines method function apart from sharing it and having the opportunity to discuss it with the community.

You love fetch? Then go fetch the repo on Github and tell me what you think!
); //=> ‘2

Finally — for sophisticated people — you also have the option to pass a callback in case the key you are looking for is not found:

1
 

These example are also available as Jasmine specs in the Github repository. Running the specs is super-easy, just checkout the repository and run:

1
 

There is no reason to publish a 33 lines method function apart from sharing it and having the opportunity to discuss it with the community.

You love fetch? Then go fetch the repo on Github and tell me what you think!
) #=> ‘2

I wished I could find exactly the same in UnderscoreJS but _.has(), _.findKey() or _.property() are not quite what I am looking for.

So I went ahead and I implemented it myself. Here is how it works. If you have  an object and you want to fetch an existing property, you simply get it:

1
 

On the other hand if the property is not available, you get an Attribute not found error in return:

1
 

Now the sweetest part of fetch. When the property you are asking can’t be found, you can simply pass a default:

1
 

Finally — for sophisticated people — you also have the option to pass a callback in case the key you are looking for is not found:

1
 

These example are also available as Jasmine specs in the Github repository. Running the specs is super-easy, just checkout the repository and run:

1
 

There is no reason to publish a 33 lines method function apart from sharing it and having the opportunity to discuss it with the community.

You love fetch? Then go fetch the repo on Github and tell me what you think!

item.fetch(:price) { |key| “the #{key.to_s} is 2$”} #=> “the price is 2$”

I wished I could find exactly the same in UnderscoreJS but _.has(), _.findKey() or _.property() are not quite what I am looking for.

So I went ahead and I implemented it myself. Here is how it works. If you have  an object and you want to fetch an existing property, you simply get it:

1
 

On the other hand if the property is not available, you get an Attribute not found error in return:

1
 

Now the sweetest part of fetch. When the property you are asking can’t be found, you can simply pass a default:

1
 

Finally — for sophisticated people — you also have the option to pass a callback in case the key you are looking for is not found:

1
 

These example are also available as Jasmine specs in the Github repository. Running the specs is super-easy, just checkout the repository and run:

1
 

There is no reason to publish a 33 lines method function apart from sharing it and having the opportunity to discuss it with the community.

You love fetch? Then go fetch the repo on Github and tell me what you think!

Finally — for sophisticated people — you also have the option to pass a callback in case the key you are looking for is not found:

1
 

These example are also available as Jasmine specs in the Github repository. Running the specs is super-easy, just checkout the repository and run:

1
 

There is no reason to publish a 33 lines method function apart from sharing it and having the opportunity to discuss it with the community.

You love fetch? Then go fetch the repo on Github and tell me what you think!
) #=> ‘2

I wished I could find exactly the same in UnderscoreJS but _.has(), _.findKey() or _.property() are not quite what I am looking for.

So I went ahead and I implemented it myself. Here is how it works. If you have  an object and you want to fetch an existing property, you simply get it:

1
 

On the other hand if the property is not available, you get an Attribute not found error in return:

1
 

Now the sweetest part of fetch. When the property you are asking can’t be found, you can simply pass a default:

1
 

Finally — for sophisticated people — you also have the option to pass a callback in case the key you are looking for is not found:

1
 

These example are also available as Jasmine specs in the Github repository. Running the specs is super-easy, just checkout the repository and run:

1
 

There is no reason to publish a 33 lines method function apart from sharing it and having the opportunity to discuss it with the community.

You love fetch? Then go fetch the repo on Github and tell me what you think!

item.fetch(:price) { |key| “the #{key.to_s} is 2$”} #=> “the price is 2$”I wished I could find exactly the same in UnderscoreJS but _.has(), _.findKey() or _.property() are not quite what I am looking for.

So I went ahead and I implemented it myself. Here is how it works. If you have  an object and you want to fetch an existing property, you simply get it:

1
 

On the other hand if the property is not available, you get an Attribute not found error in return:

1
 

Now the sweetest part of fetch. When the property you are asking can’t be found, you can simply pass a default:

1
 

Finally — for sophisticated people — you also have the option to pass a callback in case the key you are looking for is not found:

1
 

These example are also available as Jasmine specs in the Github repository. Running the specs is super-easy, just checkout the repository and run:

1
 

There is no reason to publish a 33 lines method function apart from sharing it and having the opportunity to discuss it with the community.

You love fetch? Then go fetch the repo on Github and tell me what you think!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Answer the question * Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Tweets by mottalrd

Recent Comments

  • mottalrd on Not So Random Software #21 – Technical debt
  • mottalrd on Not So Random Software #24 – Climate Change Tech
  • mottalrd on A/B Testing, from scratch
  • mottalrd on A/B Testing, from scratch
  • Karim on A/B Testing, from scratch
©2023 Alfredo Motta | Powered by SuperbThemes & WordPress