If you want to jump straight to the action (GitHub repo), click here.
I've worked and played with many languages and frameworks throughout my career and learned a lot with each one of them.
With some research I found that there are also some implementations of this using Ruby, one of my favorite languages. Ruby's syntax is very fluid, so higher-order messaging looks very natural to it. Using Nat Pryce's implementation, here's how the same code would look like in Ruby.
Since I couldn't find a library that allowed me to accomplish that same behavior, I wanted to see if that was even possible. I started hacking a version in my spare time and turns out it could be done with one of ES6’s features, Proxy! \o/
Pretty similar to the example I showed you above, huh?!? Let's see a full code sample now, step by step.
Step 0. Add Jewell to your project
npm install jewell --save
Step 1. Import
jewellPrototype and apply it to the
Array class, so we can use higher-order messaging with native array methods.
Step 2. Declare a class. In our case it's Diamond
Step 3. Create an array of Diamonds
Step 4. Have fun
Notice that in our
Get all blue diamonds example we call
#isBlue, that's because
#isBlue is a function. If that's the case, why do we not call it here?
The answer is: you can, but it's not necessary. Since
isBlue has no arguments, Jewell assumes it should be invoked to allow the chaining.
Besides jewelling the
Array class itself, you can also use Jewell with your own library that has shortcuts to manipulating arrays or individual instances.
⚠ Warning: Keep in mind that Jewell replaces the original method with a Proxy, so before shipping it to production, make sure you’re not creating some crazy behaviors in your app or slowing everything down.
You can check out more examples in the repo. If you have any questions please leave a comment bellow or open an issue on the repo. You're also very welcome to contribute with Pull Requests and suggestions.
With no further ado, here's the GitHub repo:
Create your free account to unlock your custom reading experience.