News for hackers. Stuff that matters.

JS Humanize (Give data a human touch)

https://github.com/milanvrekic/JS-humanize

If you are familiar with Django you are familiar with a nice set of template tags called "humanize". In essence it turns data such as "13 April 2012" into "2 days ago" or "1000000000" into "1.0 billion". JS Humanizer is a jQuery counterpart to the Django humanize template tags we wrote and thought that others might find it useful too.
( 19 comments | 40 points | on HN )
Re:JS Humanize (Give data a human touch) by callumjones on Sunday April 15, 2012 @09:02PM (#3845405)
Is it possible to have these in a stand alone library/namespace without the need for bringing the whole of jQuery with it? Not everyone uses jQuery in their JS application and it certainly would aid those who perform server-side JS.
Re:JS Humanize (Give data a human touch) by chetan51 on Sunday April 15, 2012 @10:50PM (#3845681)
I've removed the jQuery dependency and submitted a pull request (https://github.com/milanvrekic/JS-humanize/pull/1).
Re:JS Humanize (Give data a human touch) by Groxx on Sunday April 15, 2012 @09:31PM (#3845498)
IMO it's especially heinous because I didn't see a single piece of jQuery use in the code (I may have missed it, though). All they seem to be requiring jQuery for is to add to the $ namespace.

That pedantry aside, it seems like a decent enough chunk of code, and it's very easy to read. More libraries should be this clean.

Re:JS Humanize (Give data a human touch) by TamDenholm on Sunday April 15, 2012 @10:14PM (#3845599)
This is perhaps an inelegant solution but i've found it useful sometimes, you can get standalone functions of most of these by using a JS port of PHP functions called phpjs (phpjs.org), while i understand PHP is as cool as toe fungus on HN right now, its a solution.

EDIT: Actually having now looked at the code, it seems a lot of it is specifically using the phpjs library.

Re:JS Humanize (Give data a human touch) by mvrekic on Monday April 16, 2012 @12:13AM (#3845867)
jQuery bit was originally intended to make it compatible with require.js loading; chetan51, Groxx - thanks for pointing it out. We removed that bit but we missed the dependancy, removed now thanks to chetan51
Re:JS Humanize (Give data a human touch) by dominicrodger on Monday April 16, 2012 @01:22AM (#3846022)
Nice work! Might want to update the README - it's no longer a jQuery plugin.
Re:JS Humanize (Give data a human touch) by callumjones on Monday April 16, 2012 @02:35AM (#3846196)
Sweet! Might be needing this library very soon!
Re:JS Humanize (Give data a human touch) by ctz on Monday April 16, 2012 @03:58AM (#3846427)

  filesizeformat (1234567890 = 1.15 Gb)
This should be either 9.20 Gb or 1.15 GB.
Re:JS Humanize (Give data a human touch) by sirn on Monday April 16, 2012 @01:16AM (#3846011)
Nice idea, but seems to lack of any input escaping.

    => Humanize.truncatechars("<script>alert('yo');<\/script>", 30)
       "<script>alert('yo');</script>"

    => Humanize.linebreaks("<script>alert('yo');<\/script>")
       "<p><script>alert('yo');</script></p>"
Bug filed.
Re:JS Humanize (Give data a human touch) by mvrekic on Monday April 16, 2012 @01:24AM (#3846026)
Thanks. I am on it.
Re:JS Humanize (Give data a human touch) by whalesalad on Monday April 16, 2012 @12:13AM (#3845868)
This is great! I love how it aligns with the Django templating system as well =) Although I do agree that this might be better off a something that is framework independent. A while ago I posted an example on Forrst of how you might write a simple and pure-js shorten function: https://forrst.com/posts/JavaScript_Shorten_Function-1up
Re:JS Humanize (Give data a human touch) by chrisbroadfoot on Monday April 16, 2012 @05:21AM (#3846635)
Two things:

1) Gb should be GiB

2) it would be lovely to see some internationalisation support.

Re:JS Humanize (Give data a human touch) by chris_wot on Sunday April 15, 2012 @10:55PM (#3845694)
This might need some internationalization support...
Re:JS Humanize (Give data a human touch) by _pferreir_ on Monday April 16, 2012 @08:50AM (#3847114)
I totally agree. i.e. plural should be handled by i18n libraries. Same thing for the thousands separator, different locales use different separators/styles.
Re:JS Humanize (Give data a human touch) by alastairpat on Monday April 16, 2012 @01:55AM (#3846106)
pferreir, just a heads-up - you've been hellbanned. This means all of your comments are automatically marked as [dead], but you can't see this.
Re:JS Humanize (Give data a human touch) by Estragon on Monday April 16, 2012 @05:40AM (#3846672)
Pferreir, hellbanning is where your posts appear normally when rendered for your account, but do not show up for anyone else. Try viewing this thread using a freshly created HN account.

Don't know why this has happened to you...

Re:JS Humanize (Give data a human touch) by alastairpat on Monday April 16, 2012 @07:16AM (#3846878)
You'll probably need to start a new account, unfortunately – there doesn't appear to be any protocol for being 'unhellbanned'.
Re:JS Humanize (Give data a human touch) by alastairpat on Monday April 16, 2012 @08:23PM (#3850251)
I'm just the messenger – by no means am I in any way associated with HN.
Re:JS Humanize (Give data a human touch) by mahmud on Monday April 16, 2012 @05:35AM (#3846662)
All of them are built into Play Framework, fwiw.