We decided to tackle the problem head on: by writing just such a library!
The second question we had to address, turned out to be more complex; “How can we implement a function like
How to (badly) implement equals
It seems like there are a lot of implementations of these kinds of functions; lodash has one for instance. For
hash() there is object-hash, which I contributed to myself a while ago! In principle, implementing a function like equals is relatively easy:
All these implementations have a drawback though: They can’t support any types they don’t know about; like this one for instance. They have to add an if clause for every single type they want to support; this gets tedious very quickly.
Using ferrum.js to get it right
So, in order to implement
equals() correctly, we need to support all the types your users might want to create; the function needs to be extensible!
Ferrum already provides implementations of eq() for all standard types (Array, Map, Number, Date, etc.), so you just have to implement an equality function for your new type.
The Iterator Protocol uses Symbols in this way to implement ES6 iterators. You can even wrap existing protocols; for instance, the Sequence Trait is just a wrapper around the iterator protocol, created to make use of the advanced edge case handling of ferrum. One example: The Sequence Trait can support plain Objects, while the Iterator Protocol cannot.
Ferrum is also designed to be null/undefined safe; many functions explicitly handle null/undefined as a special edge case. Traits can even be implemented for null and/or undefined; our
equals() implementation above on the other hand would simply crash. Ferrum even provides the typesafe module to safely deal with null/undefined values.
Ferrum is currently under active development. One upcoming big feature (again, borrowed from rust) is documentation testing. Ever found that the examples in your documentation were full of bugs? This allows you test your documentation!
Other features expected to be released this year as a part of ferrum:
Hashtrait, and Hash tables supporting arbitrary keys.
Ordtrait and ordered maps supporting arbitrary keys.
- Support for rxjs Observables and Asynchronous Iterators; all using the familiar Ferrum Sequence api!