Back | Minified | Changelog

Function changes... lazy now not AS lazy

First, lazy functions, which are throttled to execute only once after a certain delay, will now fire instantly and lock themselves to be fired again after a certain timeout. Previously they would wait before their initial execution. This can have a big effect to increase the perceived responsiveness of an application. Much appreciation goes to the Underscore.js team, from who I blatantly lifted this idea.

Also added was Function#fill, which is an equivalent of partial in Python. This method allows arguments to be filled in to another method, also known as argument "currying". It differs from standard argument currying however, as undefined will be filled in later, allowing awkwardly placed arguments to work as well. The example is worth a thousand words here:

var delayOneSecond = setTimeout.fill(undefined, 1000); delayOneSecond(function() { // Will execute one second later });

This method has a lot of interesting potential, as it can be used to compose new functions based on a more specific set of arguments, without constantly passing them in:

Number.prototype.euro = Number.prototype.format.fill(undefined, '.', ','); (2345.45).euro() > "2.345,45"


The method Object.equals was renamed to Object.equal in it's class method form only. Extended objects still use the syntax a.equals(b). Additionally, the functionality was refactored to be much more robust in testing equalities on Javascript primitive types, regexes, etc. as well as handling cyclic references. Underscore recently refactored their _.isEqual method that was again the inspiration here. I also blatantly pilfered an entire chunk of their unit tests ;)

New Date formatting tokens

After having a look at other libs like XDate and moment.js, I've added a few tokens for formatting dates and removed others that were kind of a hodgepodge before. Specifically the tokens {f}, {ff}, {fff} were added as a shortcut for formatting milliseconds with different padding levels. Additionally, {h}/{hh} are now for hours in 12 hour format and {H}/{HH} are 24hr equivalents. Additionally cleaned up unused "word" tokens so there's only one per unit, ie. {seconds}, {minutes}, {date}, etc.

Number formatting to a decimal point

TheNumber#format method now accepts a number as the first parameter that will determine the decimal point:

(5298).format(2) > "5,298.00"

If the argument is undefined the decimal will be the inherent precision of the number:

(5.245).format() > "5.245" (5).format() > "5"

This change pushes back the two original arguments (the thousands and decimal separator strings), so if you were using it before, please note this.

Unique arrays on a mapping function

The Array#unique method will now accept a mapping function or a string shortcut to that function which will indicate the field to unique on. This is useful when uniquing JSON objects that have a unique key. Passing the unique field will ensure that the method does not perform a deep match of all the object's properties:

json.unique() > (will perform a deep match of all objects) json.unique(function(a){ return a.id; }) > (the function indicates that this field is the unique key) json.unique('id') > (same as above)

Other changes

I have also refactored Sugar to work under the highest level of compression in the Google Closure Compiler. This means that despite adding new methods/functionality the final release script has shed about 3kb.

Even better, Sugar will soon (still have one kink to work out) be available in Ender, which is a very cool client JS package management system that can compile all your Javascript into a single production ready release script. Have a look here for more.