1. Aug 25th, 2011

    Why you absolutely should be using Sugar.js

    Writing JavaScript used to be painful. You should expect that much from a language that has such an anemic standard library. Coming from Ruby I sure missed ActiveSupport.

    There’s Underscore.js which makes some of that pain go away, especially when dealing with collections.

    Then there’s Sugar.js.

    I got the chance to use Sugar.js on a side project and it’s been heck of an improvement over anything else I used. I no longer miss ActiveSupport.

    You get JavaScript 1.6 support in lesser browsers (indexOf, forEach, reduce, etc) along with useful Array methods like groupBy, average, first(n), etc.

    You can camelize, capitalize, base64 encode, pad and split a String into words.

    You can count from the Number 0.upto(10), find out that 10.isEven() and format it into a readable string.

    You can create a lazy Function, delay, defer and cancel it.

    You can easily check if an Object is empty, a string or date. And just as easily clone or merge it.

    And then … then, there’s Date. If ever you want to punish someone on the team, make them use JavaScript’s stock Date library.

    Sugar.js will parse “Tuesday May 25th, 2010″ and just as easily “one day before yesterday”. And you can format that date anyway you like, including relative time (“2 hours ago” and such).

    You can advance a date by a minute, rewind by an hour, set to beginningOfWeek(). You can check if isBefore(another), or isYesterday() or just isThisWeek(). You can do time arithmetic by adding 5.minutes() + 2.hours().

    It will work in your browser, yes even that which we don’t call by name. You can also npm install sugar.

    If that’s not kick-ass, I don’t know what.

    1. Aug 25th, 2011

      Dextro

      Ok this is indeed very good news for me. For the most part I was thinking that I could make do without most of that stuff, underscore.js (and underscore.string) already give me most of what I need but then I was sold when you mentioned the Date library. Oh how I loathe the js date library!

    2. Aug 25th, 2011

      Jack Dempsey

      Nice Assaf, hadn’t heard of it. Are you moving away from Underscore and just using this instead now?

    3. Aug 25th, 2011

      Assaf

      @Jack, yes, using sugar.js on new projects, even thinking of upgrading old ones.

    4. Aug 25th, 2011

      Brendan Loudermilk

      I’m surprised to see you use a library that pollutes native prototypes. What are your thoughts on this?

    5. Aug 25th, 2011

      Assaf

      Brendan, whether it’s sugar.js or ActiveSupport, I don’t have a problem using one/two core libraries specifically deployed for that purpose.

      It’s intended and contained.

      It’s only a problem when a library pulls that trick out of sheer laziness and stupidity, be it database driver, graphics package or Facebook API client. These libraries have no business polluting the global namespace.

    6. Aug 26th, 2011

      Brendan Loudermilk

      Asaaf, Thanks for the reply. Your logic makes sense. I suppose the whole “Don’t pollute the native prototypes” thing is just really engrained in me. I read more on sugar.js and they seem to be taking the safe road in regards to adding new functions to native prototypes.

      Thanks for the insight!

    7. Aug 26th, 2011

      Travis Tilley

      10.isEven() is completely invalid JavaScript. You need to do 10..isEven() or (10).isEven() due to how numbers are parsed.

      I really am OCD enough to make this comment. ;)

    8. Aug 26th, 2011

      Assaf

      Travis, it is invalid JavaScript, but it’s valid CoffeeScript.

    9. Aug 26th, 2011

      Travis Tilley

      One of at least a billion reasons why CoffeeScript is awesome, and one I wasn’t aware of. Apparently it “transpiles” to the double-dot form. Nice.

    Your comment, here ⇓