Browse Source

Initial go at the code

master
Andrew Chilton 10 years ago
parent
commit
f0e8eb5a51
3 changed files with 78 additions and 0 deletions
  1. +5
    -0
      Makefile
  2. +71
    -0
      jquery.humanify.js
  3. +2
    -0
      jquery.humanify.min.js

+ 5
- 0
Makefile View File

@@ -0,0 +1,5 @@
minimised:
js-min.pl jquery.humanify.js > jquery.humanify.min.js

copy-to-cdn:
cp jquery.humanify.js jquery.humanify.min.js ~/work/cdn/htdocs/js/

+ 71
- 0
jquery.humanify.js View File

@@ -0,0 +1,71 @@
// Style copied from: http://www.learningjquery.com/2007/10/a-plugin-development-pattern
//
// To use try the following:
//
// <span class="date" data-date="2010-09-05 23:11:44.330282+12">2010-09-05 23:11:44.330282+12</span>
//
// Then, in your JS:
//
// $('.date').humanify();
//
// which should produce something like:
//
// <span class="date" data-date="2010-09-05 23:11:44.330282+12">2 mins ago</span>
//
// Credits:
// * Written by Andrew Chilton for AppsAttic.com, Copyright (c) 2010
// * Pretty date routine copied from: http://ejohn.org/blog/javascript-pretty-date/
// * Style copied from: http://www.learningjquery.com/2007/10/a-plugin-development-pattern
//
(function($) {

// plugin 'humanify'
$.fn.humanify = function(options) {

// build main options
var opts = $.extend({}, $.fn.humanify.defaults, options);

return this.each(function(){
var $this = $(this);

// firstly, read the date from the data attribute
var date = $this.attr('data-date');

// grep out some information regarding the date

// set the date as a substr of what we just saw
// $this.text( date.substr(0, 19) );
// $this.text( prettyDate(date.substr(0, 19)) );
$this.text( $.fn.humanify.pretty(date.substr(0, 19)) );
});
};

// private functions
// none

// public functions
$.fn.humanify.pretty = function(time) {
var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," "));
var diff = (((new Date()).getTime() - date.getTime()) / 1000);
var day_diff = Math.floor(diff / 86400);

if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
return;

return day_diff == 0 && (
diff < 60 && "just now" ||
diff < 120 && "1 minute ago" ||
diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
diff < 7200 && "1 hour ago" ||
diff < 86400 && Math.floor( diff / 3600 ) + " hours ago"
) ||
day_diff == 1 && "Yesterday" ||
day_diff < 7 && day_diff + " days ago" ||
day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago"
;
};


// plugin defaults
$.fn.humanify.defaults = {};
})(jQuery);

+ 2
- 0
jquery.humanify.min.js View File

@@ -0,0 +1,2 @@
(function(c){c.fn.humanify=function(a){c.extend({},c.fn.humanify.defaults,a);return this.each(function(){var b=c(this),d=b.attr("data-date");b.text(c.fn.humanify.pretty(d.substr(0,19)))})};c.fn.humanify.pretty=function(a){a=new Date((a||"").replace(/-/g,"/").replace(/[TZ]/g," "));a=((new Date).getTime()-a.getTime())/1E3;var b=Math.floor(a/86400);if(!(isNaN(b)||b<0||b>=31))return b==0&&(a<60&&"just now"||a<120&&"1 minute ago"||a<3600&&Math.floor(a/60)+" minutes ago"||a<7200&&"1 hour ago"||a<86400&&
Math.floor(a/3600)+" hours ago")||b==1&&"Yesterday"||b<7&&b+" days ago"||b<31&&Math.ceil(b/7)+" weeks ago"};c.fn.humanify.defaults={}})(jQuery);

Loading…
Cancel
Save