0.9.5◀Back | Minified | Changelog
Another fairly big release for Sugar, v0.9.5 adds improvements to the
Function class as well as some other niceties.
- Lazy functions can now be canceled with
deferis now simply
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
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, and
toObject 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 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:
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.
Object.cloneis now shallow by default for Prototype compatibility, option allows deep cloning
Object.mergenow ignores non-objects
Array#removenow accepts unlimited parameters
Array#excludenow accepts unlimited parameters
Array#unionnow accepts unlimited parameters
Array#subtractnow accepts unlimited parameters
Array#intersectnow accepts unlimited parameters
Array#compactnow accepts a flag to compact all falsy values
Number#upto/downtonow accept a third parameter to step in multiples of > 1
Number#hexcan now pad the resulting hexadecimal string
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.