Back | Minified | Changelog

Another fairly big release for Sugar, v0.9.5 adds improvements to the Function class as well as some other niceties.


Here's a quick overview: lazy creates throttled, non-blocking functions for heavy operations. The API documentation page of the Sugar site itself uses this to create all the HTML. delay will execute a function after a given delay. Without any arguments, it defaults to 0ms which means it will simply execute after the current stack is clear (often known as defer in other frameworks). debounce creates a function that will execute only once in a given time period, and is used to prevent a single function from getting "hammered" or called too frequently. after creates a function that executes the original function after a certain number of calls to itself. It is useful to run a final callback after several simultaneous, asynchronous events. Finally, once will create a function that will only be run once, and caches the result.

Most of these functions were mainly, and somewhat flagrantly, inspired by Underscore.js, They are quite powerful, despite being a bit unintuitive at first. I plan on writing a longer post on their benefits and some examples of how they can be used. I really don't know what I was thinking making Function.lazy into a class method. I hope this change won't negatively affect too many people but it most definitely should have been an instance method!

Query String Objects

The method toObject on the String class is now called Object.fromQueryString. There were a few factors in changing this method, but the biggest was that the use case of creating an object from a string came with certain presumptions about the format, andtoObject was too generalized to be useful. Now, in addition to creating "flat" objects, Object.fromQueryString also can create nested objects using syntax like the following:


A second argument will prevent nesting, as this format is still not universally supported.

String Methods

String now adds two more methods, the most interesting being String#truncate, which will truncate strings without breaking words apart. Also new is String#escapeHTML, which performs simple HTML escaping.

Sprinkling the Sugar

Sugar never overwrites methods that already exist, so it is safe to include alongside other frameworks such as Prototype. However, this also meant that it was impossible to allow Sugar to override these methods when collisions occur. Now with the sugar method, which exists on each class, you can opt-in Sugar functionality on a per-method or per-module level:

Array.sugar('include'); String.sugar();

The first example will re-assign just the include method, and the second will re-assign all methods in that class back to Sugar. Class vs. instance method does not have to be specified here as there are no collisions with the method names (the Object module does not in fact add to Object.prototype, so there are technically no instance methods). This should allow more granular control when collisions do occur, both for existing frameworks and future ones.

Other Changes

That's all for now! I have a couple of big changes coming up for v1.0 that are now in the works... more later.