the singularity of being and nothingness
Sugar: A Bit of JavaScript Sweetness
Twitter never lets me down 🙂
Today, I stumbled upon Sugar, a really nice–and small–JavaScript library. Unlike monster libraries such as jQuery and ExtJS (which are awesome in their own ways…), Sugar is narrowly focused. It’s niche is simply extending native JS objects with intuitive methods.
So want to capitalize the first word in a string? Easy: string.capitalize(). Want to replace dashes/underscores with spaces? Nothing to it: string.spacify().
I’ve worked up a quick couple of demos of a few of the string methods, just to show how dead simple Sugar is to use. Of course, you should check out Sugar’s API for the full enchilada. Mmmm, enchildas…
Impressions
While I am a die-hard ExtJS user, there is something about the simplicity of Sugar that I really like. While it certainly won’t give you the tools to build a full-fledged web app with components, data grids, and what-not, it’s perfect for the small or one-off websites/pages where JS’ native objects could use some extra lovin’.
The one drawback (and it’s a slight one) is that there does not appear to be a way to do a custom build of Sugar to include *only* those methods which you need. According to their blog, this is because of some internals of Sugar that are needed throughout the library for IE support. What, IE ruining yet another otherwise great project? Surely not!
Although the custom-build-ness is lacking, the full library is only 17k gzipped, so there’s really not a huge hit including the whole thing anyway. That’s what caching is for anyway, right?
Wrapping Up
Honestly, I haven’t looked closely at Sugar’s internals to make a judgement about how “well done” their extension of native JS objects is. However, my initial impression is that I really like the approach they’ve taken. The ridiculously simple implementation of their API makes Sugar a library worth keeping in mind when you’ve got some light to moderate JS-in’ to do.
Print article | This entry was posted by existdissolve on January 5, 2012 at 3:11 am, and is filed under JavaScript. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 13 years ago
I’m also die hard ExtJS and it has plenty of string methods as well.
Would you use this lib alongside ExtJS4?
about 13 years ago
You could include it alongside, for sure. I personally would not, because–as you rightly point out–ExtJS has its own helpful String/Array/etc. extensions, so including Sugar would be a bit redundant.
For me, I would consider using Sugar on a very small site/project/etc. where the fullness of ExtJS/jQuery isn’t necessary. I imagine an argument could be made that perhaps no such project exists, and it may be worth the extra bandwidth to go ahead and use ExtJS/jQuery ANYWAY, since the site will mostly likely grow in complexity and JS need, not shrink.
On the other hand, given the tiny size of Sugar, there may be 3 or 4 methods that Sugar has that ExtJS/jQuery do not. If you’re lazy and don’t feel like extending either framework to include your need, Sugar might be a really nice fit.
about 13 years ago
Here’s string prototype methods, I assume, including “ToUpperCase”:
http://docs.sencha.com/ext-js/4-0/#!/api/String
…and here are non-prototype:
http://docs.sencha.com/ext-js/4-0/#!/api/Ext.String