1.1.1◀Back | Minified | Changelog
Object.merge, is for merging deep objects together (often seen as "extend" in other libraries). Previously this method worked as with most other libraries, allowing an arbitrary number of arguments to be merged together. In v1.1.1, this method will now only allow 2 objects to be merged at a time. In the place of this functionality is a third parameter for intelligent property conflict resolution:
In these examples, the attempt to merge 2 object literals together results in a conflict. Standard
merge behavior is to override all properties, resulting in the target object always receiving the source object's properties (this is typical of "extend" in other libraries as well). As you can see this is still the default behavior, with the 3rd property "override" as
true by default. However, passing
false will now allow the target object's properties to be preserved. This can be of great help in a number of situations, most notably the ability to set default properties on an object. In fact with this change the behavior of both
_.extend in Underscore can now be handled with one intuitive method. But it gets better:
By passing a function as the third parameter, you can resolve conflicts dynamically by performing any operations necessary, and returning the result. This gives you a great degree of control over dynamic data. As the conflicting key is passed as the first parameter, you can perform different merge strategies accordingly depending on what field is conflicting.
Not being able to merge more than 2 objects together may be a bother, however by using extended objects, you can chain methods and get around this. Plus each can have their own merge strategy!
This way conflicts no longer have to be either/or. Strings can be concatenated, numbers averaged... anything you choose!
Also added this round is
Object.tap, which allows "tapping" into the middle of a method chain by running a callback and returning the object it was called on. This method is a bit hard to explain but easy to demonstrate. As part of the
Object class, the most standard form exists as a class method, where it doesn't make too much sense. However using extended objects shows how this can be effective:
Tap will always return
obj here regardless of what the function passed to it returns, making it chainable. If you are using
Object.sugar() which maps
Object class methods as instance methods onto
The final addition to
Object is the method
Object.isNaN which will return true only if the object pass is
NaN, as opposed to the global method
isNaN which returns true for every data type that is considered "not a number", such as strings, etc.
Array#zipwas added by popular demand, and will now zip up any number of arrays into one.
Array#flattenwill now accept a number that will limit the "level" to which arrays will be flattened (default is all levels). Not sure of the demand for this, but the gain was made nearly for free due to a cleanup of interal code.
Consolidated the parameters being passed to mapping functions to match browser native arguments passed to
Array#map. Generally this means adding the array index, and the array itself to the arguments passed to mapping functions passed to
max. This helps Sugar conform more closely to ES5 behaviors.