RefactorMyCode.com has now been (publicly) live for a week. It was amazing! Who would have though a simple site like this could attract more then 100 000 visits in a week ?
But what surprises me the most is how much the community is alive and healty. I haven’t had any spam really yet, well maybe 3-4. And 3 other posts from people saying “you suck”. But most of the time when people get upset in there, they apologies right after and people are really thankful when they get help, amazing! How different from forums and websites where people tell you to search or RTMF before thinking about helping you ? But RefactorMyCode is not for support anyway, it’s for challenging the mind ratter then fixing problems. Hackers like solving small well defined problems, I think that’s why the site is so successful.
To celebrate RmC first week anniversary I’d like to highlight one of the refactoring from this week.
Chris Lamothe submitted this one: Beautify JS Date to how recently the event occured.
This adds a .when() method to Date() object allowing you do things such as “Posted ” + someDate.when() + ” ago.” and get output such as “Posted 10 minutes ago.” I’ve refactored it twice but would like to see it even smaller.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/*** * Beautify date to how recent the event occurred compared to now. * Some examples: 1 second, 4 hours, 10 days, 1 year. * Example: * var oneFiftyMin = new Date(new Date().getTime() - 60000 * 150); * alert(oneFiftyMin.when()); // will display "2 hours" * * Handy for things like "Item posted " + someDate.when() + " ago." */ Date.prototype.when = function() { var diff = new Date().getTime() - this.getTime(); var when; // our return value //TODO: what if the time is in the future? //if (diff < 0) throw new Error ("Date is in future, check timezone?"; //one or more of these will be non-zero, but we only care about the biggest one (in scale of time) var secondsOld = Math.floor(diff / 1000); var minutesOld = Math.floor(diff / 60000); var hoursOld = Math.floor(diff / 3600000); var daysOld = Math.floor(diff / 86400000); var monthsOld = Math.floor(diff / 2592000000); var yearsOld = Math.floor(diff / (2592000000 * 12)); //your content is old! //determine which value is non-zero and assign appropriate text if (yearsOld > 0) { when = yearsOld + " year"; } else if (monthsOld > 0) { when = monthsOld + " month"; } else if (daysOld > 0) { when = daysOld + " day"; } else if (hoursOld > 0) { when = hoursOld + " hour"; } else if (minutesOld > 0) { when = minutesOld + " minute"; } else { when = Math.round(diff /1000) + " second"; } //add plural if necessary return (0 == when.indexOf("1 ")) ? when : when + "s"; }
With the help of typefreak, Andre Steenveld and Tomasz Kołodziejski they finally end up with this code, that does exactly the same thing in less then one-third the line count.
1 2 3 4 5 6 7 8 9 10 11 12
Date.prototype.when = function(){ var diff = (new Date() - this)/1e3, u = [ "second", "minute", "hour", "day", "weeks", "month", "year" ], s = [ 1, 60, 60, 24, 7, 4.333, 12, 1e9], // again fast hack - 1e9 i=0; for(;;i++){ if((diff/=s[i])<1){ return ~~(diff*=s[i])+‘ ‘+u[i-1]+(diff>1?‘s‘:0); } } }
What can you say ? WOW !
Thanks to all people who’ve taken the time to write me an email to report a bug, suggestion or simply to say thanks, it means a lot to me!










Recent Comments