@@ -35,7 +35,7 @@ function () {
3535 // second, minute, hour, day, week, month, year(365 days)
3636 SEC_ARRAY = [ 60 , 60 , 24 , 7 , 365 / 7 / 12 , 12 ] ,
3737 SEC_ARRAY_LEN = 6 ,
38- ATTR_DATETIME = 'datetime' ,
38+ ATTR_DATETIME = 'datetime' ;
3939
4040 /**
4141 * timeago: the function to get `timeago` instance.
@@ -45,21 +45,19 @@ function () {
4545 * How to use it?
4646 * var timeagoLib = require('timeago.js');
4747 * var timeago = timeagoLib(); // all use default.
48- * var timeago = timeagoLib('zh_CN '); // set default locale is `zh_CN` .
49- * var timeago = timeagoLib(null, '2016-09-10 '); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago .
50- * var timeago = timeagoLib('zh_CN', ' 2016-09-10'); // the relative date is 2016-09-10, and locale is zh_CN, so the 2016-09-11 will be 1天前.
48+ * var timeago = timeagoLib('2016-09-10 '); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago .
49+ * var timeago = timeagoLib(null, 'zh_CN '); // set default locale is `zh_CN` .
50+ * var timeago = timeagoLib('2016-09-10', 'zh_CN '); // the relative date is 2016-09-10, and locale is zh_CN, so the 2016-09-11 will be 1天前.
5151 **/
52- timeago = function ( defaultLocale , nowDate ) {
52+ function timeago ( nowDate , defaultLocale ) {
5353 var timers = { } ; // real-time render timers
5454 // if do not set the defaultLocale, set it with `en`
55- if ( ! defaultLocale ) {
56- defaultLocale = 'en' ; // use default build-in locale
57- }
55+ if ( ! defaultLocale ) defaultLocale = 'en' ; // use default build-in locale
5856 // calculate the diff second between date to be formated an now date.
5957 function diffSec ( date ) {
6058 var now = new Date ( ) ;
6159 if ( nowDate ) now = toDate ( nowDate ) ;
62- return ( now . getTime ( ) - toDate ( date ) . getTime ( ) ) / 1000 ;
60+ return ( now - toDate ( date ) ) / 1000 ;
6361 }
6462 // format the diff second to *** time ago, with setting locale
6563 function formatDiff ( diff , locale ) {
@@ -136,14 +134,14 @@ function () {
136134 // return leftSec(d, rst);
137135 d = d % rst ;
138136 d = d ? rst - d : rst ;
139- return Math . ceil ( diff ) ;
137+ return Math . ceil ( d ) ;
140138 // }; // for dev test
141139 }
142140 // what the timer will do
143141 function doRender ( node , date , locale , cnt ) {
144142 var diff = diffSec ( date ) ;
145143 node . innerHTML = formatDiff ( diff , locale ) ;
146- // 通过diff来判断下一次执行的时间
144+ // waiting %s seconds, do the next render
147145 timers [ 'k' + cnt ] = setTimeout ( function ( ) {
148146 doRender ( node , date , locale , cnt ) ;
149147 } , nextInterval ( diff ) * 1000 ) ;
@@ -165,7 +163,7 @@ function () {
165163 * // 2. use jQuery selector
166164 * timeago.render($('.need_to_be_rendered'), 'pl');
167165 *
168- * Notice: please be sure the dom has attribute `data-timeago `.
166+ * Notice: please be sure the dom has attribute `datetime `.
169167 **/
170168 this . render = function ( nodes , locale ) {
171169 if ( nodes . length === undefined ) nodes = [ nodes ] ;
@@ -199,7 +197,22 @@ function () {
199197 defaultLocale = locale ;
200198 } ;
201199 return this ;
202- } ;
200+ }
201+ /**
202+ * timeago: the function to get `timeago` instance.
203+ * - nowDate: the relative date, default is new Date().
204+ * - defaultLocale: the default locale, default is en. if your set it, then the `locale` parameter of format is not needed of you.
205+ *
206+ * How to use it?
207+ * var timeagoLib = require('timeago.js');
208+ * var timeago = timeagoLib(); // all use default.
209+ * var timeago = timeagoLib('2016-09-10'); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago.
210+ * var timeago = timeagoLib(null, 'zh_CN'); // set default locale is `zh_CN`.
211+ * var timeago = timeagoLib('2016-09-10', 'zh_CN'); // the relative date is 2016-09-10, and locale is zh_CN, so the 2016-09-11 will be 1天前.
212+ **/
213+ function timeagoFactory ( nowDate , defaultLocale ) {
214+ return new timeago ( nowDate , defaultLocale ) ;
215+ }
203216 /**
204217 * register: register a new language locale
205218 * - locale: locale name, e.g. en / zh_CN, notice the standard.
@@ -211,10 +224,10 @@ function () {
211224 * timeagoLib.register('the locale name', the_locale_func);
212225 * // or
213226 * timeagoLib.register('pl', require('timeago.js/locales/pl'));
214- **/
215- timeago . register = function ( locale , localeFunc ) {
227+ **/
228+ timeagoFactory . register = function ( locale , localeFunc ) {
216229 locales [ locale ] = localeFunc ;
217230 } ;
218231
219- return timeago ;
232+ return timeagoFactory ;
220233} ) ;
0 commit comments