1.2.0◀Back | Minified | Changelog
v1.2 is now released! This release adds some cool new features that will take Sugar in a bit of a new direction.
Sugar is now extendable.
New in 1.2 is the
defineProperty if available to prevent the method from appearing in
for..in loops as an enumerable property. Additionally, when extending native objects you can have better control in the case of collisions. By default, Sugar will always defer to methods that already exist, as it does internally. However, passing
true as the second property to
extend will override any existing methods:
Where it starts getting really interesting, however, is by passing a function as the override property. This will allow you to override methods but defer to them conditionally:
In addition to the methods themselves, the module also adds a new object
String.Inflector to help augment the inflector itself:
These replacements will be run when calling the appropriate method.
irregular apply to both
pluralize. Note also
acronym, which is a new part of ActiveSupport that hasn't found its way to Rails yet. Also a point of personal pride is
titleize which will properly not capitalize prepositions, articles, and the like.
String#namespace was added, which will find a global namespace or return
undefined if none is found:
If any of the namespaces are not defined along the way, for example
Ext.Base, the method will return undefined instead of throwing an error.
Array fuzzy object finding
Sugar previously supported passing an object to array methods like
any. This would recursively match every property of that object against elements in the array, allowing for identical objects whose references are different to match. Compare this to built-in
indexOf, which does not match object properties:
Although this would properly match, objects previously had to be identical. This behavior has changed and now all array methods are "fuzzy" by default when passing an object:
Where this gets even more interesting is that now any property of the search object passed can also be either a regex, which will match a string against the pattern, or a function which will match against a callback. Both of these were available previously to Sugar when directly passed, however now that they themselves are subject to recursive matching inside objects, some very cool patterns become possible:
This opens up a whole range of possibilties such as building up search filters that can be cached, and much, much more. This behavior is now the default for all Sugar array methods (when passing an object), which affects the following methods:
One of the more cool things is that this change is across the board, as those methods in the ES5 spec (
filter) do not natively support passing an object as an argument, so the default behavior is still preserved. Of course if you want to match identical objects, this is still perfectly possible:
Or even simpler:
If you can assure that your data all has the same number of properties, then you don't even have to bother with this:
Another new feature in v1.2 is abbreviation methods on the Number class:
Number#abbr will abbreviate the number with an upper limit of
t for "trillion", beyond which it will pretty format the numeral using
Number#format. It can be passed a single number that will determine it's rounding precision. From here we get a bit crazy:
Number#metric will render the number into the metric system, using standard unit suffixes. Like
Number#abbr, the first argument is the rounding precision. The second parameter is the "limit" which caps the highest unit to be used. The default is
1, which is
k ("kilo-"), however, this can be changed by passing a different value, or by passing
false, in which case the only imposed limit is the highest unit, currently
Number#metric can also convert extremely small numbers as well, down to the "nano" level.
Number#bytes hooks into the same system to produce "bytes" abbreviations on a base 2 system. Arguments are identical, but the default limit is
T ("tera-"). However, this default may eventually be subject to Moore's law :)
The "sugar" method is now split into two:
- Restoring methods is now
- Object.prototype extension functionality is now on
- For more info see objects.
Number#isIntegerwhich returns true if the number has no decimal.
- Object.keys now passes values as part of its callback, when passed.
- Object.merge now merges undefined properties as well.
Array#cloneis now no longer based off
Array#concat, which will fail on sparse arrays in IE7.
- Fixed issue with
Number#ordinalizewhere 113 would be "113rd".
String#eachwill now pass the match into the callback like array methods.
String#toDatewill now check for Date.create before hooking into it.
String#underscorewill now check for acronyms if Inflectors module is present.
String#camelizewill now check for acronyms if Inflectors module is present.
RegExp.escapewill now perform a
[toString]operation on non-strings (ie. numbers, etc).
Function#fillnow uses internal Array#splice to fill in arguments.
- Added support for JSON date format
- Fixed issues with
- Fixed issues with traversing months before January.