Comparing version 2.3.0 to 2.5.2
420
countdown.js
@@ -0,4 +1,5 @@ | ||
/*global window */ | ||
/** | ||
* @license countdown.js v2.3.0 http://countdownjs.org | ||
* Copyright (c)2006-2012 Stephen M. McKamey. | ||
* @license countdown.js v2.5.2 http://countdownjs.org | ||
* Copyright (c)2006-2014 Stephen M. McKamey. | ||
* Licensed under The MIT License. | ||
@@ -210,3 +211,3 @@ */ | ||
// increment month by shift | ||
ref.setUTCMonth( ref.getUTCMonth() + shift ); | ||
ref.setMonth( ref.getMonth() + shift ); | ||
@@ -227,3 +228,3 @@ // this is the trickiest since months vary in length | ||
var b = new Date(a); | ||
b.setUTCMonth( ref.getUTCMonth() + 1 ); | ||
b.setMonth( ref.getMonth() + 1 ); | ||
@@ -244,3 +245,3 @@ // this is the trickiest since months vary in length | ||
var b = new Date(a); | ||
b.setUTCFullYear( ref.getUTCFullYear() + 1 ); | ||
b.setFullYear( ref.getFullYear() + 1 ); | ||
@@ -252,14 +253,208 @@ // this is the trickiest since years (periodically) vary in length | ||
/** | ||
* Applies the Timespan to the given date. | ||
* | ||
* @private | ||
* @param {Timespan} ts | ||
* @param {Date=} date | ||
* @return {Date} | ||
*/ | ||
function addToDate(ts, date) { | ||
date = (date instanceof Date) || ((date !== null) && isFinite(date)) ? new Date(+date) : new Date(); | ||
if (!ts) { | ||
return date; | ||
} | ||
// if there is a value field, use it directly | ||
var value = +ts.value || 0; | ||
if (value) { | ||
date.setTime(date.getTime() + value); | ||
return date; | ||
} | ||
value = +ts.milliseconds || 0; | ||
if (value) { | ||
date.setMilliseconds(date.getMilliseconds() + value); | ||
} | ||
value = +ts.seconds || 0; | ||
if (value) { | ||
date.setSeconds(date.getSeconds() + value); | ||
} | ||
value = +ts.minutes || 0; | ||
if (value) { | ||
date.setMinutes(date.getMinutes() + value); | ||
} | ||
value = +ts.hours || 0; | ||
if (value) { | ||
date.setHours(date.getHours() + value); | ||
} | ||
value = +ts.weeks || 0; | ||
if (value) { | ||
value *= DAYS_PER_WEEK; | ||
} | ||
value += +ts.days || 0; | ||
if (value) { | ||
date.setDate(date.getDate() + value); | ||
} | ||
value = +ts.months || 0; | ||
if (value) { | ||
date.setMonth(date.getMonth() + value); | ||
} | ||
value = +ts.millennia || 0; | ||
if (value) { | ||
value *= CENTURIES_PER_MILLENNIUM; | ||
} | ||
value += +ts.centuries || 0; | ||
if (value) { | ||
value *= DECADES_PER_CENTURY; | ||
} | ||
value += +ts.decades || 0; | ||
if (value) { | ||
value *= YEARS_PER_DECADE; | ||
} | ||
value += +ts.years || 0; | ||
if (value) { | ||
date.setFullYear(date.getFullYear() + value); | ||
} | ||
return date; | ||
} | ||
/** | ||
* @private | ||
* @const | ||
* @type {number} | ||
*/ | ||
var LABEL_MILLISECONDS = 0; | ||
/** | ||
* @private | ||
* @const | ||
* @type {number} | ||
*/ | ||
var LABEL_SECONDS = 1; | ||
/** | ||
* @private | ||
* @const | ||
* @type {number} | ||
*/ | ||
var LABEL_MINUTES = 2; | ||
/** | ||
* @private | ||
* @const | ||
* @type {number} | ||
*/ | ||
var LABEL_HOURS = 3; | ||
/** | ||
* @private | ||
* @const | ||
* @type {number} | ||
*/ | ||
var LABEL_DAYS = 4; | ||
/** | ||
* @private | ||
* @const | ||
* @type {number} | ||
*/ | ||
var LABEL_WEEKS = 5; | ||
/** | ||
* @private | ||
* @const | ||
* @type {number} | ||
*/ | ||
var LABEL_MONTHS = 6; | ||
/** | ||
* @private | ||
* @const | ||
* @type {number} | ||
*/ | ||
var LABEL_YEARS = 7; | ||
/** | ||
* @private | ||
* @const | ||
* @type {number} | ||
*/ | ||
var LABEL_DECADES = 8; | ||
/** | ||
* @private | ||
* @const | ||
* @type {number} | ||
*/ | ||
var LABEL_CENTURIES = 9; | ||
/** | ||
* @private | ||
* @const | ||
* @type {number} | ||
*/ | ||
var LABEL_MILLENNIA = 10; | ||
/** | ||
* @private | ||
* @type {Array} | ||
*/ | ||
var LABELS_SINGLUAR; | ||
/** | ||
* @private | ||
* @type {Array} | ||
*/ | ||
var LABELS_PLURAL; | ||
/** | ||
* @private | ||
* @type {string} | ||
*/ | ||
var LABEL_LAST; | ||
/** | ||
* @private | ||
* @type {string} | ||
*/ | ||
var LABEL_DELIM; | ||
/** | ||
* @private | ||
* @type {string} | ||
*/ | ||
var LABEL_NOW; | ||
/** | ||
* Formats a number as a string | ||
* | ||
* @private | ||
* @param {number} value | ||
* @param {string} singular | ||
* @param {string} plural | ||
* @return {string} | ||
*/ | ||
function plurality(value, singular, plural) { | ||
return value+' '+((value === 1) ? singular : plural); | ||
var formatNumber; | ||
/** | ||
* @private | ||
* @param {number} value | ||
* @param {number} unit unit index into label list | ||
* @return {string} | ||
*/ | ||
function plurality(value, unit) { | ||
return formatNumber(value)+((value === 1) ? LABELS_SINGLUAR[unit] : LABELS_PLURAL[unit]); | ||
} | ||
/** | ||
* Formats the entries as English labels | ||
* Formats the entries with singular or plural labels | ||
* | ||
@@ -282,8 +477,8 @@ * @private | ||
/** | ||
* Formats the Timespan as a sentance | ||
* Formats the Timespan as a sentence | ||
* | ||
* @private | ||
* @param {string=} emptyLabel the string to use when no values returned | ||
* @return {string} | ||
*/ | ||
Timespan.prototype.toString = function() { | ||
Timespan.prototype.toString = function(emptyLabel) { | ||
var label = formatList(this); | ||
@@ -293,18 +488,20 @@ | ||
if (!count) { | ||
return ''; | ||
return emptyLabel ? ''+emptyLabel : LABEL_NOW; | ||
} | ||
if (count > 1) { | ||
label[count-1] = 'and '+label[count-1]; | ||
if (count === 1) { | ||
return label[0]; | ||
} | ||
return label.join(', '); | ||
var last = LABEL_LAST+label.pop(); | ||
return label.join(LABEL_DELIM)+last; | ||
}; | ||
/** | ||
* Formats the Timespan as HTML | ||
* Formats the Timespan as a sentence in HTML | ||
* | ||
* @private | ||
* @param {string} tag HTML tag name to wrap each value | ||
* @param {string=} tag HTML tag name to wrap each value | ||
* @param {string=} emptyLabel the string to use when no values returned | ||
* @return {string} | ||
*/ | ||
Timespan.prototype.toHTML = function(tag) { | ||
Timespan.prototype.toHTML = function(tag, emptyLabel) { | ||
tag = tag || 'span'; | ||
@@ -315,3 +512,4 @@ var label = formatList(this); | ||
if (!count) { | ||
return ''; | ||
emptyLabel = emptyLabel || LABEL_NOW; | ||
return emptyLabel ? '<'+tag+'>'+emptyLabel+'</'+tag+'>' : emptyLabel; | ||
} | ||
@@ -322,9 +520,21 @@ for (var i=0; i<count; i++) { | ||
} | ||
if (--count) { | ||
label[count] = 'and '+label[count]; | ||
if (count === 1) { | ||
return label[0]; | ||
} | ||
return label.join(', '); | ||
var last = LABEL_LAST+label.pop(); | ||
return label.join(LABEL_DELIM)+last; | ||
}; | ||
/** | ||
* Applies the Timespan to the given date | ||
* | ||
* @param {Date=} date the date to which the timespan is added. | ||
* @return {Date} | ||
*/ | ||
Timespan.prototype.addTo = function(date) { | ||
return addToDate(this, date); | ||
}; | ||
/** | ||
* Formats the entries as English labels | ||
@@ -341,3 +551,3 @@ * | ||
if (value) { | ||
list.push(plurality(value, 'millennium', 'millennia')); | ||
list.push(plurality(value, LABEL_MILLENNIA)); | ||
} | ||
@@ -347,3 +557,3 @@ | ||
if (value) { | ||
list.push(plurality(value, 'century', 'centuries')); | ||
list.push(plurality(value, LABEL_CENTURIES)); | ||
} | ||
@@ -353,3 +563,3 @@ | ||
if (value) { | ||
list.push(plurality(value, 'decade', 'decades')); | ||
list.push(plurality(value, LABEL_DECADES)); | ||
} | ||
@@ -359,3 +569,3 @@ | ||
if (value) { | ||
list.push(plurality(value, 'year', 'years')); | ||
list.push(plurality(value, LABEL_YEARS)); | ||
} | ||
@@ -365,3 +575,3 @@ | ||
if (value) { | ||
list.push(plurality(value, 'month', 'months')); | ||
list.push(plurality(value, LABEL_MONTHS)); | ||
} | ||
@@ -371,3 +581,3 @@ | ||
if (value) { | ||
list.push(plurality(value, 'week', 'weeks')); | ||
list.push(plurality(value, LABEL_WEEKS)); | ||
} | ||
@@ -377,3 +587,3 @@ | ||
if (value) { | ||
list.push(plurality(value, 'day', 'days')); | ||
list.push(plurality(value, LABEL_DAYS)); | ||
} | ||
@@ -383,3 +593,3 @@ | ||
if (value) { | ||
list.push(plurality(value, 'hour', 'hours')); | ||
list.push(plurality(value, LABEL_HOURS)); | ||
} | ||
@@ -389,3 +599,3 @@ | ||
if (value) { | ||
list.push(plurality(value, 'minute', 'minutes')); | ||
list.push(plurality(value, LABEL_MINUTES)); | ||
} | ||
@@ -395,3 +605,3 @@ | ||
if (value) { | ||
list.push(plurality(value, 'second', 'seconds')); | ||
list.push(plurality(value, LABEL_SECONDS)); | ||
} | ||
@@ -401,3 +611,3 @@ | ||
if (value) { | ||
list.push(plurality(value, 'millisecond', 'milliseconds')); | ||
list.push(plurality(value, LABEL_MILLISECONDS)); | ||
} | ||
@@ -805,4 +1015,4 @@ | ||
* @param {Timespan} ts | ||
* @param {Date} start the starting date | ||
* @param {Date} end the ending date | ||
* @param {?Date} start the starting date | ||
* @param {?Date} end the ending date | ||
* @param {number} units the units to populate | ||
@@ -813,4 +1023,6 @@ * @param {number} max number of labels to output | ||
function populate(ts, start, end, units, max, digits) { | ||
ts.start = start; | ||
ts.end = end; | ||
var now = new Date(); | ||
ts.start = start = start || now; | ||
ts.end = end = end || now; | ||
ts.units = units; | ||
@@ -821,9 +1033,9 @@ | ||
// swap if reversed | ||
var temp = end; | ||
var tmp = end; | ||
end = start; | ||
start = temp; | ||
start = tmp; | ||
} | ||
// reference month for determining days in month | ||
ts.refMonth = new Date(start.getFullYear(), start.getMonth(), 15); | ||
ts.refMonth = new Date(start.getFullYear(), start.getMonth(), 15, 12, 0, 0); | ||
try { | ||
@@ -834,10 +1046,10 @@ // reset to initial deltas | ||
ts.decades = 0; | ||
ts.years = end.getUTCFullYear() - start.getUTCFullYear(); | ||
ts.months = end.getUTCMonth() - start.getUTCMonth(); | ||
ts.years = end.getFullYear() - start.getFullYear(); | ||
ts.months = end.getMonth() - start.getMonth(); | ||
ts.weeks = 0; | ||
ts.days = end.getUTCDate() - start.getUTCDate(); | ||
ts.hours = end.getUTCHours() - start.getUTCHours(); | ||
ts.minutes = end.getUTCMinutes() - start.getUTCMinutes(); | ||
ts.seconds = end.getUTCSeconds() - start.getUTCSeconds(); | ||
ts.milliseconds = end.getUTCMilliseconds() - start.getUTCMilliseconds(); | ||
ts.days = end.getDate() - start.getDate(); | ||
ts.hours = end.getHours() - start.getHours(); | ||
ts.minutes = end.getMinutes() - start.getMinutes(); | ||
ts.seconds = end.getSeconds() - start.getSeconds(); | ||
ts.milliseconds = end.getMilliseconds() - start.getMilliseconds(); | ||
@@ -895,7 +1107,7 @@ ripple(ts); | ||
* @public | ||
* @param {Date|number|null|function(Timespan)} start the starting date | ||
* @param {Date|number|null|function(Timespan)} end the ending date | ||
* @param {number} units the units to populate | ||
* @param {number} max number of labels to output | ||
* @param {number} digits max number of decimal digits to output | ||
* @param {Date|number|Timespan|null|function(Timespan,number)} start the starting date | ||
* @param {Date|number|Timespan|null|function(Timespan,number)} end the ending date | ||
* @param {number=} units the units to populate | ||
* @param {number=} max number of labels to output | ||
* @param {number=} digits max number of decimal digits to output | ||
* @return {Timespan|number} | ||
@@ -914,2 +1126,3 @@ */ | ||
// ensure start date | ||
var startTS = null; | ||
if ('function' === typeof start) { | ||
@@ -920,6 +1133,14 @@ callback = start; | ||
} else if (!(start instanceof Date)) { | ||
start = (start !== null && isFinite(start)) ? new Date(start) : null; | ||
if ((start !== null) && isFinite(start)) { | ||
start = new Date(+start); | ||
} else { | ||
if ('object' === typeof startTS) { | ||
startTS = /** @type{Timespan} */(start); | ||
} | ||
start = null; | ||
} | ||
} | ||
// ensure end date | ||
var endTS = null; | ||
if ('function' === typeof end) { | ||
@@ -930,5 +1151,20 @@ callback = end; | ||
} else if (!(end instanceof Date)) { | ||
end = (end !== null && isFinite(end)) ? new Date(end) : null; | ||
if ((end !== null) && isFinite(end)) { | ||
end = new Date(+end); | ||
} else { | ||
if ('object' === typeof end) { | ||
endTS = /** @type{Timespan} */(end); | ||
} | ||
end = null; | ||
} | ||
} | ||
// must wait to interpret timespans until after resolving dates | ||
if (startTS) { | ||
start = addToDate(startTS, end); | ||
} | ||
if (endTS) { | ||
end = addToDate(endTS, start); | ||
} | ||
if (!start && !end) { | ||
@@ -940,15 +1176,17 @@ // used for unit testing | ||
if (!callback) { | ||
return populate(new Timespan(), /** @type{Date} */(start||new Date()), /** @type{Date} */(end||new Date()), units, max, digits); | ||
return populate(new Timespan(), /** @type{Date} */(start), /** @type{Date} */(end), /** @type{number} */(units), /** @type{number} */(max), /** @type{number} */(digits)); | ||
} | ||
// base delay off units | ||
var delay = getDelay(units); | ||
var fn = function() { | ||
callback( | ||
populate(new Timespan(), /** @type{Date} */(start||new Date()), /** @type{Date} */(end||new Date()), units, max, digits) | ||
); | ||
}; | ||
var delay = getDelay(units), | ||
timerId, | ||
fn = function() { | ||
callback( | ||
populate(new Timespan(), /** @type{Date} */(start), /** @type{Date} */(end), /** @type{number} */(units), /** @type{number} */(max), /** @type{number} */(digits)), | ||
timerId | ||
); | ||
}; | ||
fn(); | ||
return setInterval(fn, delay); | ||
return (timerId = setInterval(fn, delay)); | ||
} | ||
@@ -1047,4 +1285,56 @@ | ||
/** | ||
* Override the unit labels | ||
* @public | ||
* @param {string|Array=} singular a pipe ('|') delimited list of singular unit name overrides | ||
* @param {string|Array=} plural a pipe ('|') delimited list of plural unit name overrides | ||
* @param {string=} last a delimiter before the last unit (default: ' and ') | ||
* @param {string=} delim a delimiter to use between all other units (default: ', ') | ||
* @param {string=} empty a label to use when all units are zero (default: '') | ||
* @param {function(number):string=} formatter a function which formats numbers as a string | ||
*/ | ||
countdown.setLabels = function(singular, plural, last, delim, empty, formatter) { | ||
singular = singular || []; | ||
if (singular.split) { | ||
singular = singular.split('|'); | ||
} | ||
plural = plural || []; | ||
if (plural.split) { | ||
plural = plural.split('|'); | ||
} | ||
for (var i=LABEL_MILLISECONDS; i<=LABEL_MILLENNIA; i++) { | ||
// override any specified units | ||
LABELS_SINGLUAR[i] = singular[i] || LABELS_SINGLUAR[i]; | ||
LABELS_PLURAL[i] = plural[i] || LABELS_PLURAL[i]; | ||
} | ||
LABEL_LAST = ('string' === typeof last) ? last : LABEL_LAST; | ||
LABEL_DELIM = ('string' === typeof delim) ? delim : LABEL_DELIM; | ||
LABEL_NOW = ('string' === typeof empty) ? empty : LABEL_NOW; | ||
formatNumber = ('function' === typeof formatter) ? formatter : formatNumber; | ||
}; | ||
/** | ||
* Revert to the default unit labels | ||
* @public | ||
*/ | ||
var resetLabels = countdown.resetLabels = function() { | ||
LABELS_SINGLUAR = ' millisecond| second| minute| hour| day| week| month| year| decade| century| millennium'.split('|'); | ||
LABELS_PLURAL = ' milliseconds| seconds| minutes| hours| days| weeks| months| years| decades| centuries| millennia'.split('|'); | ||
LABEL_LAST = ' and '; | ||
LABEL_DELIM = ', '; | ||
LABEL_NOW = ''; | ||
formatNumber = function(value) { return value; }; | ||
}; | ||
resetLabels(); | ||
if (module && module.exports) { | ||
module.exports = countdown; | ||
} else if (typeof window.define === 'function' && typeof window.define.amd !== 'undefined') { | ||
window.define('countdown', [], function() { | ||
return countdown; | ||
}); | ||
} | ||
@@ -1051,0 +1341,0 @@ |
/* | ||
countdown.js v2.3.0 http://countdownjs.org | ||
Copyright (c)2006-2012 Stephen M. McKamey. | ||
countdown.js v2.5.2 http://countdownjs.org | ||
Copyright (c)2006-2014 Stephen M. McKamey. | ||
Licensed under The MIT License. | ||
*/ | ||
var module,countdown=function(n){function r(a,b){var c=a.getTime();a.setUTCMonth(a.getUTCMonth()+b);return Math.round((a.getTime()-c)/864E5)}function q(a){var b=a.getTime(),c=new Date(b);c.setUTCMonth(a.getUTCMonth()+1);return Math.round((c.getTime()-b)/864E5)}function i(a,b,c){return a+" "+(1===a?b:c)}function m(){}function k(a,b,c,d,f,j){0<=a[c]&&(b+=a[c],delete a[c]);b/=f;if(1>=b+1)return 0;if(0<=a[d]){a[d]=+(a[d]+b).toFixed(j);switch(d){case "seconds":if(60!==a.seconds||isNaN(a.minutes))break; | ||
a.minutes++;a.seconds=0;case "minutes":if(60!==a.minutes||isNaN(a.hours))break;a.hours++;a.minutes=0;case "hours":if(24!==a.hours||isNaN(a.days))break;a.days++;a.hours=0;case "days":if(7!==a.days||isNaN(a.weeks))break;a.weeks++;a.days=0;case "weeks":if(a.weeks!==q(a.refMonth)/7||isNaN(a.months))break;a.months++;a.weeks=0;case "months":if(12!==a.months||isNaN(a.years))break;a.years++;a.months=0;case "years":if(10!==a.years||isNaN(a.decades))break;a.decades++;a.years=0;case "decades":if(10!==a.decades|| | ||
isNaN(a.centuries))break;a.centuries++;a.decades=0;case "centuries":if(10!==a.centuries||isNaN(a.millennia))break;a.millennia++;a.centuries=0}return 0}return b}function s(a,b,c,d,f,j){a.start=b;a.end=c;a.units=d;a.value=c.getTime()-b.getTime();if(0>a.value)var e=c,c=b,b=e;a.refMonth=new Date(b.getFullYear(),b.getMonth(),15);try{a.millennia=0;a.centuries=0;a.decades=0;a.years=c.getUTCFullYear()-b.getUTCFullYear();a.months=c.getUTCMonth()-b.getUTCMonth();a.weeks=0;a.days=c.getUTCDate()-b.getUTCDate(); | ||
a.hours=c.getUTCHours()-b.getUTCHours();a.minutes=c.getUTCMinutes()-b.getUTCMinutes();a.seconds=c.getUTCSeconds()-b.getUTCSeconds();a.milliseconds=c.getUTCMilliseconds()-b.getUTCMilliseconds();var h;0>a.milliseconds?(h=p(-a.milliseconds/1E3),a.seconds-=h,a.milliseconds+=1E3*h):1E3<=a.milliseconds&&(a.seconds+=l(a.milliseconds/1E3),a.milliseconds%=1E3);0>a.seconds?(h=p(-a.seconds/60),a.minutes-=h,a.seconds+=60*h):60<=a.seconds&&(a.minutes+=l(a.seconds/60),a.seconds%=60);0>a.minutes?(h=p(-a.minutes/ | ||
60),a.hours-=h,a.minutes+=60*h):60<=a.minutes&&(a.hours+=l(a.minutes/60),a.minutes%=60);0>a.hours?(h=p(-a.hours/24),a.days-=h,a.hours+=24*h):24<=a.hours&&(a.days+=l(a.hours/24),a.hours%=24);for(;0>a.days;)a.months--,a.days+=r(a.refMonth,1);7<=a.days&&(a.weeks+=l(a.days/7),a.days%=7);0>a.months?(h=p(-a.months/12),a.years-=h,a.months+=12*h):12<=a.months&&(a.years+=l(a.months/12),a.months%=12);10<=a.years&&(a.decades+=l(a.years/10),a.years%=10,10<=a.decades&&(a.centuries+=l(a.decades/10),a.decades%= | ||
10,10<=a.centuries&&(a.millennia+=l(a.centuries/10),a.centuries%=10)));b=0;!(d&1024)||b>=f?(a.centuries+=10*a.millennia,delete a.millennia):a.millennia&&b++;!(d&512)||b>=f?(a.decades+=10*a.centuries,delete a.centuries):a.centuries&&b++;!(d&256)||b>=f?(a.years+=10*a.decades,delete a.decades):a.decades&&b++;!(d&128)||b>=f?(a.months+=12*a.years,delete a.years):a.years&&b++;!(d&64)||b>=f?(a.months&&(a.days+=r(a.refMonth,a.months)),delete a.months,7<=a.days&&(a.weeks+=l(a.days/7),a.days%=7)):a.months&& | ||
b++;!(d&32)||b>=f?(a.days+=7*a.weeks,delete a.weeks):a.weeks&&b++;!(d&16)||b>=f?(a.hours+=24*a.days,delete a.days):a.days&&b++;!(d&8)||b>=f?(a.minutes+=60*a.hours,delete a.hours):a.hours&&b++;!(d&4)||b>=f?(a.seconds+=60*a.minutes,delete a.minutes):a.minutes&&b++;!(d&2)||b>=f?(a.milliseconds+=1E3*a.seconds,delete a.seconds):a.seconds&&b++;if(!(d&1)||b>=f){var g=k(a,0,"milliseconds","seconds",1E3,j);if(g&&(g=k(a,g,"seconds","minutes",60,j)))if(g=k(a,g,"minutes","hours",60,j))if(g=k(a,g,"hours","days", | ||
24,j))if(g=k(a,g,"days","weeks",7,j))if(g=k(a,g,"weeks","months",q(a.refMonth)/7,j)){var d=g,i,m=a.refMonth,n=m.getTime(),o=new Date(n);o.setUTCFullYear(m.getUTCFullYear()+1);i=Math.round((o.getTime()-n)/864E5);if(g=k(a,d,"months","years",i/q(a.refMonth),j))if(g=k(a,g,"years","decades",10,j))if(g=k(a,g,"decades","centuries",10,j))if(g=k(a,g,"centuries","millennia",10,j))throw Error("Fractional unit overflow");}}}finally{delete a.refMonth}return a}function e(a,b,c,d,f){var e,c=+c||222,d=0<d?d:NaN, | ||
f=0<f?20>f?Math.round(f):20:0;"function"===typeof a?(e=a,a=null):a instanceof Date||(a=null!==a&&isFinite(a)?new Date(a):null);"function"===typeof b?(e=b,b=null):b instanceof Date||(b=null!==b&&isFinite(b)?new Date(b):null);if(!a&&!b)return new m;if(!e)return s(new m,a||new Date,b||new Date,c,d,f);var i;i=c&1?1E3/30:c&2?1E3:c&4?6E4:c&8?36E5:c&16?864E5:6048E5;var h=function(){e(s(new m,a||new Date,b||new Date,c,d,f))};h();return setInterval(h,i)}var p=Math.ceil,l=Math.floor,o;m.prototype.toString= | ||
function(){var a=o(this),b=a.length;if(!b)return"";1<b&&(a[b-1]="and "+a[b-1]);return a.join(", ")};m.prototype.toHTML=function(a){var a=a||"span",b=o(this),c=b.length;if(!c)return"";for(var d=0;d<c;d++)b[d]="<"+a+">"+b[d]+"</"+a+">";--c&&(b[c]="and "+b[c]);return b.join(", ")};o=function(a){var b=[],c=a.millennia;c&&b.push(i(c,"millennium","millennia"));(c=a.centuries)&&b.push(i(c,"century","centuries"));(c=a.decades)&&b.push(i(c,"decade","decades"));(c=a.years)&&b.push(i(c,"year","years"));(c=a.months)&& | ||
b.push(i(c,"month","months"));(c=a.weeks)&&b.push(i(c,"week","weeks"));(c=a.days)&&b.push(i(c,"day","days"));(c=a.hours)&&b.push(i(c,"hour","hours"));(c=a.minutes)&&b.push(i(c,"minute","minutes"));(c=a.seconds)&&b.push(i(c,"second","seconds"));(c=a.milliseconds)&&b.push(i(c,"millisecond","milliseconds"));return b};e.MILLISECONDS=1;e.SECONDS=2;e.MINUTES=4;e.HOURS=8;e.DAYS=16;e.WEEKS=32;e.MONTHS=64;e.YEARS=128;e.DECADES=256;e.CENTURIES=512;e.MILLENNIA=1024;e.DEFAULTS=222;e.ALL=2047;n&&n.exports&&(n.exports= | ||
e);return e}(module); | ||
var module,countdown=function(y){function C(a,b){var c=a.getTime();a.setMonth(a.getMonth()+b);return Math.round((a.getTime()-c)/864E5)}function z(a){var b=a.getTime(),c=new Date(b);c.setMonth(a.getMonth()+1);return Math.round((c.getTime()-b)/864E5)}function A(a,b){b=b instanceof Date||null!==b&&isFinite(b)?new Date(+b):new Date;if(!a)return b;var c=+a.value||0;if(c)return b.setTime(b.getTime()+c),b;(c=+a.milliseconds||0)&&b.setMilliseconds(b.getMilliseconds()+c);(c=+a.seconds||0)&&b.setSeconds(b.getSeconds()+ | ||
c);(c=+a.minutes||0)&&b.setMinutes(b.getMinutes()+c);(c=+a.hours||0)&&b.setHours(b.getHours()+c);(c=+a.weeks||0)&&(c*=7);(c+=+a.days||0)&&b.setDate(b.getDate()+c);(c=+a.months||0)&&b.setMonth(b.getMonth()+c);(c=+a.millennia||0)&&(c*=10);(c+=+a.centuries||0)&&(c*=10);(c+=+a.decades||0)&&(c*=10);(c+=+a.years||0)&&b.setFullYear(b.getFullYear()+c);return b}function l(a,b){return v(a)+(1===a?w[b]:x[b])}function q(){}function n(a,b,c,d,f,m){0<=a[c]&&(b+=a[c],delete a[c]);b/=f;if(1>=b+1)return 0;if(0<=a[d]){a[d]= | ||
+(a[d]+b).toFixed(m);switch(d){case "seconds":if(60!==a.seconds||isNaN(a.minutes))break;a.minutes++;a.seconds=0;case "minutes":if(60!==a.minutes||isNaN(a.hours))break;a.hours++;a.minutes=0;case "hours":if(24!==a.hours||isNaN(a.days))break;a.days++;a.hours=0;case "days":if(7!==a.days||isNaN(a.weeks))break;a.weeks++;a.days=0;case "weeks":if(a.weeks!==z(a.refMonth)/7||isNaN(a.months))break;a.months++;a.weeks=0;case "months":if(12!==a.months||isNaN(a.years))break;a.years++;a.months=0;case "years":if(10!== | ||
a.years||isNaN(a.decades))break;a.decades++;a.years=0;case "decades":if(10!==a.decades||isNaN(a.centuries))break;a.centuries++;a.decades=0;case "centuries":if(10!==a.centuries||isNaN(a.millennia))break;a.millennia++;a.centuries=0}return 0}return b}function D(a,b,c,d,f,m){var k=new Date;a.start=b=b||k;a.end=c=c||k;a.units=d;a.value=c.getTime()-b.getTime();0>a.value&&(k=c,c=b,b=k);a.refMonth=new Date(b.getFullYear(),b.getMonth(),15,12,0,0);try{a.millennia=0;a.centuries=0;a.decades=0;a.years=c.getFullYear()- | ||
b.getFullYear();a.months=c.getMonth()-b.getMonth();a.weeks=0;a.days=c.getDate()-b.getDate();a.hours=c.getHours()-b.getHours();a.minutes=c.getMinutes()-b.getMinutes();a.seconds=c.getSeconds()-b.getSeconds();a.milliseconds=c.getMilliseconds()-b.getMilliseconds();var g;0>a.milliseconds?(g=s(-a.milliseconds/1E3),a.seconds-=g,a.milliseconds+=1E3*g):1E3<=a.milliseconds&&(a.seconds+=p(a.milliseconds/1E3),a.milliseconds%=1E3);0>a.seconds?(g=s(-a.seconds/60),a.minutes-=g,a.seconds+=60*g):60<=a.seconds&&(a.minutes+= | ||
p(a.seconds/60),a.seconds%=60);0>a.minutes?(g=s(-a.minutes/60),a.hours-=g,a.minutes+=60*g):60<=a.minutes&&(a.hours+=p(a.minutes/60),a.minutes%=60);0>a.hours?(g=s(-a.hours/24),a.days-=g,a.hours+=24*g):24<=a.hours&&(a.days+=p(a.hours/24),a.hours%=24);for(;0>a.days;)a.months--,a.days+=C(a.refMonth,1);7<=a.days&&(a.weeks+=p(a.days/7),a.days%=7);0>a.months?(g=s(-a.months/12),a.years-=g,a.months+=12*g):12<=a.months&&(a.years+=p(a.months/12),a.months%=12);10<=a.years&&(a.decades+=p(a.years/10),a.years%= | ||
10,10<=a.decades&&(a.centuries+=p(a.decades/10),a.decades%=10,10<=a.centuries&&(a.millennia+=p(a.centuries/10),a.centuries%=10)));b=0;!(d&1024)||b>=f?(a.centuries+=10*a.millennia,delete a.millennia):a.millennia&&b++;!(d&512)||b>=f?(a.decades+=10*a.centuries,delete a.centuries):a.centuries&&b++;!(d&256)||b>=f?(a.years+=10*a.decades,delete a.decades):a.decades&&b++;!(d&128)||b>=f?(a.months+=12*a.years,delete a.years):a.years&&b++;!(d&64)||b>=f?(a.months&&(a.days+=C(a.refMonth,a.months)),delete a.months, | ||
7<=a.days&&(a.weeks+=p(a.days/7),a.days%=7)):a.months&&b++;!(d&32)||b>=f?(a.days+=7*a.weeks,delete a.weeks):a.weeks&&b++;!(d&16)||b>=f?(a.hours+=24*a.days,delete a.days):a.days&&b++;!(d&8)||b>=f?(a.minutes+=60*a.hours,delete a.hours):a.hours&&b++;!(d&4)||b>=f?(a.seconds+=60*a.minutes,delete a.minutes):a.minutes&&b++;!(d&2)||b>=f?(a.milliseconds+=1E3*a.seconds,delete a.seconds):a.seconds&&b++;if(!(d&1)||b>=f){var h=n(a,0,"milliseconds","seconds",1E3,m);if(h&&(h=n(a,h,"seconds","minutes",60,m))&&(h= | ||
n(a,h,"minutes","hours",60,m))&&(h=n(a,h,"hours","days",24,m))&&(h=n(a,h,"days","weeks",7,m))&&(h=n(a,h,"weeks","months",z(a.refMonth)/7,m))){d=h;var e,l=a.refMonth,q=l.getTime(),r=new Date(q);r.setFullYear(l.getFullYear()+1);e=Math.round((r.getTime()-q)/864E5);if(h=n(a,d,"months","years",e/z(a.refMonth),m))if(h=n(a,h,"years","decades",10,m))if(h=n(a,h,"decades","centuries",10,m))if(h=n(a,h,"centuries","millennia",10,m))throw Error("Fractional unit overflow");}}}finally{delete a.refMonth}return a} | ||
function e(a,b,c,d,f){var e;c=+c||222;d=0<d?d:NaN;f=0<f?20>f?Math.round(f):20:0;var k=null;"function"===typeof a?(e=a,a=null):a instanceof Date||(null!==a&&isFinite(a)?a=new Date(+a):("object"===typeof k&&(k=a),a=null));var g=null;"function"===typeof b?(e=b,b=null):b instanceof Date||(null!==b&&isFinite(b)?b=new Date(+b):("object"===typeof b&&(g=b),b=null));k&&(a=A(k,b));g&&(b=A(g,a));if(!a&&!b)return new q;if(!e)return D(new q,a,b,c,d,f);var k=c&1?1E3/30:c&2?1E3:c&4?6E4:c&8?36E5:c&16?864E5:6048E5, | ||
h,g=function(){e(D(new q,a,b,c,d,f),h)};g();return h=setInterval(g,k)}var s=Math.ceil,p=Math.floor,w,x,r,t,u,v,B;q.prototype.toString=function(a){var b=B(this),c=b.length;if(!c)return a?""+a:u;if(1===c)return b[0];a=r+b.pop();return b.join(t)+a};q.prototype.toHTML=function(a,b){a=a||"span";var c=B(this),d=c.length;if(!d)return(b=b||u)?"\x3c"+a+"\x3e"+b+"\x3c/"+a+"\x3e":b;for(var f=0;f<d;f++)c[f]="\x3c"+a+"\x3e"+c[f]+"\x3c/"+a+"\x3e";if(1===d)return c[0];d=r+c.pop();return c.join(t)+d};q.prototype.addTo= | ||
function(a){return A(this,a)};B=function(a){var b=[],c=a.millennia;c&&b.push(l(c,10));(c=a.centuries)&&b.push(l(c,9));(c=a.decades)&&b.push(l(c,8));(c=a.years)&&b.push(l(c,7));(c=a.months)&&b.push(l(c,6));(c=a.weeks)&&b.push(l(c,5));(c=a.days)&&b.push(l(c,4));(c=a.hours)&&b.push(l(c,3));(c=a.minutes)&&b.push(l(c,2));(c=a.seconds)&&b.push(l(c,1));(c=a.milliseconds)&&b.push(l(c,0));return b};e.MILLISECONDS=1;e.SECONDS=2;e.MINUTES=4;e.HOURS=8;e.DAYS=16;e.WEEKS=32;e.MONTHS=64;e.YEARS=128;e.DECADES=256; | ||
e.CENTURIES=512;e.MILLENNIA=1024;e.DEFAULTS=222;e.ALL=2047;e.setLabels=function(a,b,c,d,f,e){a=a||[];a.split&&(a=a.split("|"));b=b||[];b.split&&(b=b.split("|"));for(var k=0;10>=k;k++)w[k]=a[k]||w[k],x[k]=b[k]||x[k];r="string"===typeof c?c:r;t="string"===typeof d?d:t;u="string"===typeof f?f:u;v="function"===typeof e?e:v};(e.resetLabels=function(){w=" millisecond; second; minute; hour; day; week; month; year; decade; century; millennium".split(";");x=" milliseconds; seconds; minutes; hours; days; weeks; months; years; decades; centuries; millennia".split(";"); | ||
r=" and ";t=", ";u="";v=function(a){return a}})();y&&y.exports?y.exports=e:"function"===typeof window.define&&"undefined"!==typeof window.define.amd&&window.define("countdown",[],function(){return e});return e}(module); |
21
demo.js
@@ -22,9 +22,16 @@ (function() { | ||
// adjust demo time for local timezone | ||
byId('hours').value -= new Date().getTimezoneOffset()/60; | ||
// Mayan Calendar: 1356088271111 | ||
// https://groups.google.com/group/alt.hypertext/msg/06dad279804cb3ba?dmode=source | ||
var DEMO_MS = 681490580000, | ||
DEMO_PAST = 'The World Wide Web debuted', | ||
DEMO_FUTURE = 'The World Wide Web debuts'; | ||
// adjust initial demo time for local timezone | ||
byId('hours').value -= new Date(DEMO_MS).getTimezoneOffset() / 60; | ||
function update() { | ||
var units = ~countdown.ALL, | ||
chx = byId('countdown-units').getElementsByTagName('input'), | ||
empty = byId('empty-label').value || '', | ||
empty = byId('empty-label').value || null, | ||
max = +(byId('max-units').value), | ||
@@ -52,8 +59,8 @@ digits = +(byId('frac-digits').value); | ||
timespan = byId('timespan'), | ||
msg = ts.toHTML('strong') || empty; | ||
msg = ts.toHTML('strong', empty); | ||
if (start.getTime() === 1356088271111) { | ||
if (start.getTime() === DEMO_MS) { | ||
msg = (ts.value > 0) ? | ||
'The world ended '+msg+' ago!?!' : | ||
'The world ends in '+msg+'!'; | ||
DEMO_PAST+' '+msg+' ago!' : | ||
DEMO_FUTURE+' in '+msg+'!'; | ||
} | ||
@@ -60,0 +67,0 @@ |
{ | ||
"name": "countdown", | ||
"description": "A simple JavaScript API for producing an accurate, intuitive description of the timespan between two Date instances.", | ||
"version": "2.3.0", | ||
"version": "2.5.2", | ||
"homepage": "http://countdownjs.org", | ||
"author": "Stephen McKamey <stephen@mckamey.com>", | ||
"repository": { | ||
"type": "hg", | ||
"url": "https://bitbucket.org/mckamey/countdown.js" | ||
"type": "git", | ||
"url": "https://github.com/mckamey/countdownjs" | ||
}, | ||
@@ -11,0 +11,0 @@ "tags": ["countdown", "timespan", "date", "time", "clock"], |
@@ -5,2 +5,43 @@ try{ | ||
test('Default empty label override', function() { | ||
var input = countdown(0, 0, countdown.ALL); | ||
countdown.setLabels(null, null, null, null, 'Now.'); | ||
var expected = 'Now.'; | ||
var actual = input.toString(); | ||
countdown.resetLabels(); | ||
same(actual, expected, ''); | ||
}); | ||
test('Empty label override', function() { | ||
var input = countdown(0, 0, countdown.ALL); | ||
var expected = 'Now!'; | ||
var actual = input.toString('Now!'); | ||
same(actual, expected, ''); | ||
}); | ||
test('Default empty label override, overridden', function() { | ||
var input = countdown(0, 0, countdown.ALL); | ||
countdown.setLabels(null, null, null, null, 'Now.'); | ||
var expected = 'Right now!'; | ||
var actual = input.toString('Right now!'); | ||
countdown.resetLabels(); | ||
same(actual, expected, ''); | ||
}); | ||
test('Zero', function() { | ||
@@ -43,3 +84,3 @@ | ||
var expected = '1 second, and 2 milliseconds'; | ||
var expected = '1 second and 2 milliseconds'; | ||
@@ -55,3 +96,3 @@ var actual = input.toString(); | ||
var expected = '2 seconds, and 1 millisecond'; | ||
var expected = '2 seconds and 1 millisecond'; | ||
@@ -65,3 +106,6 @@ var actual = input.toString(); | ||
var input = countdown(24 * 60 * 60 * 1000, 0, countdown.ALL); | ||
var start = new Date(1970, 0, 1, 0, 0, 0, 0); | ||
var end = new Date(1970, 0, 1, 0, 0, 0, 0); | ||
end.setDate( end.getDate()+1 ); | ||
var input = countdown(end, start, countdown.ALL); | ||
@@ -77,6 +121,10 @@ var expected = '1 day'; | ||
var input = countdown(15 * 24 * 60 * 60 * 1000, 0, countdown.ALL); | ||
var start = new Date(1970, 0, 1, 0, 0, 0, 0); | ||
var end = new Date(1970, 0, 1, 0, 0, 0, 0); | ||
end.setDate( end.getDate()+15 ); | ||
var expected = '2 weeks, and 1 day'; | ||
var input = countdown(end, start, countdown.ALL); | ||
var expected = '2 weeks and 1 day'; | ||
var actual = input.toString(); | ||
@@ -89,6 +137,10 @@ | ||
var input = countdown(32 * 24 * 60 * 60 * 1000, 0, countdown.ALL); | ||
var start = new Date(1970, 0, 1, 0, 0, 0, 0); | ||
var end = new Date(1970, 0, 1, 0, 0, 0, 0); | ||
end.setDate( end.getDate()+32 ); | ||
var expected = '1 month, and 1 day'; | ||
var input = countdown(end, start, countdown.ALL); | ||
var expected = '1 month and 1 day'; | ||
var actual = input.toString(); | ||
@@ -99,8 +151,15 @@ | ||
test('millennium, week', function() { | ||
test('Millennium, week', function() { | ||
var input = countdown(0, 10 * 100 * 365.25 * 24 * 60 * 60 * 1000, countdown.ALL); | ||
var start = new Date(0); | ||
var end = new Date(10 * 100 * 365.25 * 24 * 60 * 60 * 1000);// millennium, week | ||
var expected = '1 millennium, and 1 week'; | ||
// account for local timezone | ||
start.setMinutes(start.getMinutes() + start.getTimezoneOffset()); | ||
end.setMinutes(end.getMinutes() + end.getTimezoneOffset()); | ||
var input = countdown(start, end, countdown.ALL); | ||
var expected = '1 millennium and 1 week'; | ||
var actual = input.toString(); | ||
@@ -111,5 +170,6 @@ | ||
test('one of each', function() { | ||
test('One of each', function() { | ||
var input = countdown(0, | ||
var start = new Date(0); | ||
var end = new Date( | ||
(11 * 100) * (365.25 * 24 * 60 * 60 * 1000) + // millennium, century, week, day | ||
@@ -121,7 +181,12 @@ (365 * 24 * 60 * 60 * 1000) + // year | ||
1000 + // sec | ||
1, // ms | ||
countdown.ALL); | ||
1); // ms | ||
var expected = '1 millennium, 1 century, 1 year, 1 month, 1 week, 1 day, 1 hour, 1 minute, 1 second, and 1 millisecond'; | ||
// account for local timezone | ||
start.setMinutes(start.getMinutes() + start.getTimezoneOffset()); | ||
end.setMinutes(end.getMinutes() + end.getTimezoneOffset()); | ||
var input = countdown(start, end, countdown.ALL); | ||
var expected = '1 millennium, 1 century, 1 year, 1 month, 1 week, 1 day, 1 hour, 1 minute, 1 second and 1 millisecond'; | ||
var actual = input.toString(); | ||
@@ -132,8 +197,40 @@ | ||
test('Two of each', function() { | ||
var start = new Date(0); | ||
var end = new Date( | ||
(22 * 100) * (365.25 * 24 * 60 * 60 * 1000) + // millennium, century, week, day | ||
(2 * 365 * 24 * 60 * 60 * 1000) + // year | ||
(31 + 29) * (24 * 60 * 60 * 1000) + // month | ||
(2 * 60 * 60 * 1000) + // hour | ||
(2 * 60 * 1000) + // min | ||
(2 * 1000) + // sec | ||
2); // ms | ||
// account for local timezone | ||
start.setMinutes(start.getMinutes() + start.getTimezoneOffset()); | ||
end.setMinutes(end.getMinutes() + end.getTimezoneOffset()); | ||
var input = countdown(start, end, countdown.ALL); | ||
var expected = '2 millennia, 2 centuries, 2 years, 2 months, 2 weeks, 2 days, 2 hours, 2 minutes, 2 seconds and 2 milliseconds'; | ||
var actual = input.toString(); | ||
same(actual, expected, ''); | ||
}); | ||
module('Timespan.toString(number)'); | ||
test('millennium, week; 1 max', function() { | ||
test('Millennium, week; 1 max', function() { | ||
var input = countdown(0, 10 * 100 * 365.25 * 24 * 60 * 60 * 1000, countdown.ALL, 1); | ||
var start = new Date(0); | ||
var end = new Date(10 * 100 * 365.25 * 24 * 60 * 60 * 1000); | ||
// account for local timezone | ||
start.setMinutes(start.getMinutes() + start.getTimezoneOffset()); | ||
end.setMinutes(end.getMinutes() + end.getTimezoneOffset()); | ||
var input = countdown(start, end, countdown.ALL, 1); | ||
var expected = '1 millennium'; | ||
@@ -146,5 +243,24 @@ | ||
test('one of each; 3 max', function() { | ||
test('Millennium, week; 2 max', function() { | ||
var input = countdown(0, | ||
var start = new Date(0); | ||
var end = new Date(10 * 100 * 365.25 * 24 * 60 * 60 * 1000); | ||
// account for local timezone | ||
start.setMinutes(start.getMinutes() + start.getTimezoneOffset()); | ||
end.setMinutes(end.getMinutes() + end.getTimezoneOffset()); | ||
var input = countdown(start, end, countdown.ALL, 2); | ||
var expected = '1 millennium and 1 week'; | ||
var actual = input.toString(); | ||
same(actual, expected, ''); | ||
}); | ||
test('One of each; 3 max', function() { | ||
var start = new Date(0); | ||
var end = new Date( | ||
(11 * 100) * (365.25 * 24 * 60 * 60 * 1000) + // millennium, century, week, day | ||
@@ -156,8 +272,12 @@ (365 * 24 * 60 * 60 * 1000) + // year | ||
1000 + // sec | ||
1, // ms | ||
countdown.ALL, | ||
3); | ||
1); // ms | ||
var expected = '1 millennium, 1 century, and 1 year'; | ||
// account for local timezone | ||
start.setMinutes(start.getMinutes() + start.getTimezoneOffset()); | ||
end.setMinutes(end.getMinutes() + end.getTimezoneOffset()); | ||
var input = countdown(start, end, countdown.ALL, 3); | ||
var expected = '1 millennium, 1 century and 1 year'; | ||
var actual = input.toString(); | ||
@@ -168,5 +288,6 @@ | ||
test('one of each; zero max', function() { | ||
test('One of each; zero max', function() { | ||
var input = countdown(0, | ||
var start = new Date(0); | ||
var end = new Date( | ||
(11 * 100) * (365.25 * 24 * 60 * 60 * 1000) + // millennium, century, week, day | ||
@@ -178,8 +299,12 @@ (365 * 24 * 60 * 60 * 1000) + // year | ||
1000 + // sec | ||
1, // ms | ||
countdown.ALL, | ||
0); | ||
1); // ms | ||
var expected = '1 millennium, 1 century, 1 year, 1 month, 1 week, 1 day, 1 hour, 1 minute, 1 second, and 1 millisecond'; | ||
// account for local timezone | ||
start.setMinutes(start.getMinutes() + start.getTimezoneOffset()); | ||
end.setMinutes(end.getMinutes() + end.getTimezoneOffset()); | ||
var input = countdown(start, end, countdown.ALL, 0); | ||
var expected = '1 millennium, 1 century, 1 year, 1 month, 1 week, 1 day, 1 hour, 1 minute, 1 second and 1 millisecond'; | ||
var actual = input.toString(); | ||
@@ -190,5 +315,6 @@ | ||
test('one of each; -2 max', function() { | ||
test('One of each; -2 max', function() { | ||
var input = countdown(0, | ||
var start = new Date(0); | ||
var end = new Date( | ||
(11 * 100) * (365.25 * 24 * 60 * 60 * 1000) + // millennium, century, week, day | ||
@@ -200,8 +326,12 @@ (365 * 24 * 60 * 60 * 1000) + // year | ||
1000 + // sec | ||
1, // ms | ||
countdown.ALL, | ||
-2); | ||
1); // ms | ||
var expected = '1 millennium, 1 century, 1 year, 1 month, 1 week, 1 day, 1 hour, 1 minute, 1 second, and 1 millisecond'; | ||
// account for local timezone | ||
start.setMinutes(start.getMinutes() + start.getTimezoneOffset()); | ||
end.setMinutes(end.getMinutes() + end.getTimezoneOffset()); | ||
var input = countdown(start, end, countdown.ALL, -2); | ||
var expected = '1 millennium, 1 century, 1 year, 1 month, 1 week, 1 day, 1 hour, 1 minute, 1 second and 1 millisecond'; | ||
var actual = input.toString(); | ||
@@ -216,3 +346,3 @@ | ||
var expected = "1 minute, and 59.999 seconds"; | ||
var expected = "1 minute and 59.999 seconds"; | ||
@@ -237,2 +367,43 @@ var actual = input.toString(); | ||
test('Default empty label override', function() { | ||
var input = countdown(0, 0, countdown.ALL); | ||
countdown.setLabels(null, null, null, null, 'Now.'); | ||
var expected = '<span>Now.</span>'; | ||
var actual = input.toHTML(); | ||
countdown.resetLabels(); | ||
same(actual, expected, '<span>Now.</span>'); | ||
}); | ||
test('Empty label override', function() { | ||
var input = countdown(0, 0, countdown.ALL); | ||
var expected = '<span>Now!</span>'; | ||
var actual = input.toHTML(null, 'Now!'); | ||
same(actual, expected, ''); | ||
}); | ||
test('Default empty label override, overridden', function() { | ||
var input = countdown(0, 0, countdown.ALL); | ||
countdown.setLabels(null, null, null, null, 'Now.'); | ||
var expected = '<span>Right now!</span>'; | ||
var actual = input.toHTML(null, 'Right now!'); | ||
countdown.resetLabels(); | ||
same(actual, expected, ''); | ||
}); | ||
test('Zero', function() { | ||
@@ -249,6 +420,24 @@ | ||
test('Empty label and tag override', function() { | ||
var input = countdown(0, 0, countdown.ALL); | ||
var expected = '<em>Now!</em>'; | ||
var actual = input.toHTML('em', 'Now!'); | ||
same(actual, expected, ''); | ||
}); | ||
test('1 ms', function() { | ||
var input = countdown(0, 1, countdown.ALL); | ||
var start = new Date(0); | ||
var end = new Date(1); | ||
// account for local timezone | ||
start.setMinutes(start.getMinutes() + start.getTimezoneOffset()); | ||
end.setMinutes(end.getMinutes() + end.getTimezoneOffset()); | ||
var input = countdown(start, end, countdown.ALL); | ||
var expected = '<span>1 millisecond</span>'; | ||
@@ -263,4 +452,11 @@ | ||
var input = countdown(2 * 24 * 60 * 60 * 1000, 0, countdown.ALL); | ||
var start = new Date(2 * 24 * 60 * 60 * 1000); | ||
var end = new Date(0); | ||
// account for local timezone | ||
start.setMinutes(start.getMinutes() + start.getTimezoneOffset()); | ||
end.setMinutes(end.getMinutes() + end.getTimezoneOffset()); | ||
var input = countdown(start, end, countdown.ALL); | ||
var expected = '<span>2 days</span>'; | ||
@@ -275,6 +471,13 @@ | ||
var input = countdown(0, 8 * 24 * 60 * 60 * 1000, countdown.ALL); | ||
var start = new Date(0); | ||
var end = new Date(8 * 24 * 60 * 60 * 1000); | ||
var expected = '<span>1 week</span>, and <span>1 day</span>'; | ||
// account for local timezone | ||
start.setMinutes(start.getMinutes() + start.getTimezoneOffset()); | ||
end.setMinutes(end.getMinutes() + end.getTimezoneOffset()); | ||
var input = countdown(start, end, countdown.ALL); | ||
var expected = '<span>1 week</span> and <span>1 day</span>'; | ||
var actual = input.toHTML(); | ||
@@ -287,6 +490,13 @@ | ||
var input = countdown(0, 70 * 24 * 60 * 60 * 1000, countdown.ALL); | ||
var start = new Date(0); | ||
var end = new Date(70 * 24 * 60 * 60 * 1000); | ||
var expected = '<span>2 months</span>, <span>1 week</span>, and <span>4 days</span>'; | ||
// account for local timezone | ||
start.setMinutes(start.getMinutes() + start.getTimezoneOffset()); | ||
end.setMinutes(end.getMinutes() + end.getTimezoneOffset()); | ||
var input = countdown(start, end, countdown.ALL); | ||
var expected = '<span>2 months</span>, <span>1 week</span> and <span>4 days</span>'; | ||
var actual = input.toHTML(); | ||
@@ -299,6 +509,13 @@ | ||
var input = countdown(0, 366 * 24 * 60 * 60 * 1000, countdown.ALL); | ||
var start = new Date(0); | ||
var end = new Date(366 * 24 * 60 * 60 * 1000); | ||
var expected = '<span>1 year</span>, and <span>1 day</span>'; | ||
// account for local timezone | ||
start.setMinutes(start.getMinutes() + start.getTimezoneOffset()); | ||
end.setMinutes(end.getMinutes() + end.getTimezoneOffset()); | ||
var input = countdown(start, end, countdown.ALL); | ||
var expected = '<span>1 year</span> and <span>1 day</span>'; | ||
var actual = input.toHTML(); | ||
@@ -312,5 +529,10 @@ | ||
var start = new Date(2000, 0, 1); | ||
var end = new Date(start.getTime() + 366 * 24 * 60 * 60 * 1000); | ||
var input = countdown(start, start.getTime() + 366 * 24 * 60 * 60 * 1000, countdown.ALL); | ||
// account for local timezone | ||
start.setMinutes(start.getMinutes() + start.getTimezoneOffset()); | ||
end.setMinutes(end.getMinutes() + end.getTimezoneOffset()); | ||
var input = countdown(start, end, countdown.ALL); | ||
var expected = '<span>1 year</span>'; | ||
@@ -323,5 +545,6 @@ | ||
test('one of each', function() { | ||
test('One of each', function() { | ||
var input = countdown(0, | ||
var start = new Date(0); | ||
var end = new Date( | ||
(11 * 100) * (365.25 * 24 * 60 * 60 * 1000) + // millennium, century, week, day | ||
@@ -333,5 +556,10 @@ (365 * 24 * 60 * 60 * 1000) + // year | ||
1000 + // sec | ||
1, // ms | ||
countdown.ALL); | ||
1); // ms | ||
// account for local timezone | ||
start.setMinutes(start.getMinutes() + start.getTimezoneOffset()); | ||
end.setMinutes(end.getMinutes() + end.getTimezoneOffset()); | ||
var input = countdown(start, end, countdown.ALL); | ||
var expected = | ||
@@ -346,3 +574,3 @@ '<em>1 millennium</em>, ' + | ||
'<em>1 minute</em>, ' + | ||
'<em>1 second</em>, and ' + | ||
'<em>1 second</em> and ' + | ||
'<em>1 millisecond</em>'; | ||
@@ -355,2 +583,297 @@ | ||
test('Singular overrides', function() { | ||
var start = new Date(0); | ||
var end = new Date( | ||
(11 * 100) * (365.25 * 24 * 60 * 60 * 1000) + // millennium, century, week, day | ||
(365 * 24 * 60 * 60 * 1000) + // year | ||
(31 * 24 * 60 * 60 * 1000) + // month | ||
(60 * 60 * 1000) + // hour | ||
(60 * 1000) + // min | ||
1000 + // sec | ||
1); // ms | ||
// account for local timezone | ||
start.setMinutes(start.getMinutes() + start.getTimezoneOffset()); | ||
end.setMinutes(end.getMinutes() + end.getTimezoneOffset()); | ||
var input = countdown(start, end, countdown.ALL); | ||
countdown.setLabels(' a| b| c| d| e| f| g| h| i| j| k'); | ||
var expected = | ||
'<em>1 k</em>, ' + | ||
'<em>1 j</em>, ' + | ||
'<em>1 h</em>, ' + | ||
'<em>1 g</em>, ' + | ||
'<em>1 f</em>, ' + | ||
'<em>1 e</em>, ' + | ||
'<em>1 d</em>, ' + | ||
'<em>1 c</em>, ' + | ||
'<em>1 b</em> and ' + | ||
'<em>1 a</em>'; | ||
var actual = input.toHTML('em'); | ||
countdown.resetLabels(); | ||
same(actual, expected, ''); | ||
expected = | ||
'<em>1 millennium</em>, ' + | ||
'<em>1 century</em>, ' + | ||
'<em>1 year</em>, ' + | ||
'<em>1 month</em>, ' + | ||
'<em>1 week</em>, ' + | ||
'<em>1 day</em>, ' + | ||
'<em>1 hour</em>, ' + | ||
'<em>1 minute</em>, ' + | ||
'<em>1 second</em> and ' + | ||
'<em>1 millisecond</em>'; | ||
actual = input.toHTML('em'); | ||
same(actual, expected, ''); | ||
}); | ||
test('Plural overrides', function() { | ||
var start = new Date(0); | ||
var end = new Date( | ||
(22 * 100) * (365.25 * 24 * 60 * 60 * 1000) + // millennium, century, week, day | ||
(2 * 365 * 24 * 60 * 60 * 1000) + // year | ||
(31 + 29) * (24 * 60 * 60 * 1000) + // month | ||
(2 * 60 * 60 * 1000) + // hour | ||
(2 * 60 * 1000) + // min | ||
(2 * 1000) + // sec | ||
2); // ms | ||
// account for local timezone | ||
start.setMinutes(start.getMinutes() + start.getTimezoneOffset()); | ||
end.setMinutes(end.getMinutes() + end.getTimezoneOffset()); | ||
var input = countdown(start, end, countdown.ALL); | ||
countdown.setLabels(null, ' A| B| C| D| E| F| G| H| I| J| K', ' & ', ' & '); | ||
var expected = | ||
'<em>2 K</em> & ' + | ||
'<em>2 J</em> & ' + | ||
'<em>2 H</em> & ' + | ||
'<em>2 G</em> & ' + | ||
'<em>2 F</em> & ' + | ||
'<em>2 E</em> & ' + | ||
'<em>2 D</em> & ' + | ||
'<em>2 C</em> & ' + | ||
'<em>2 B</em> & ' + | ||
'<em>2 A</em>'; | ||
var actual = input.toHTML('em'); | ||
countdown.resetLabels(); | ||
same(actual, expected, ''); | ||
expected = | ||
'<em>2 millennia</em>, ' + | ||
'<em>2 centuries</em>, ' + | ||
'<em>2 years</em>, ' + | ||
'<em>2 months</em>, ' + | ||
'<em>2 weeks</em>, ' + | ||
'<em>2 days</em>, ' + | ||
'<em>2 hours</em>, ' + | ||
'<em>2 minutes</em>, ' + | ||
'<em>2 seconds</em> and ' + | ||
'<em>2 milliseconds</em>'; | ||
actual = input.toHTML('em'); | ||
same(actual, expected, ''); | ||
}); | ||
test('Partial singular overrides', function() { | ||
var start = new Date(0); | ||
var end = new Date( | ||
(11 * 100) * (365.25 * 24 * 60 * 60 * 1000) + // millennium, century, week, day | ||
(365 * 24 * 60 * 60 * 1000) + // year | ||
(31 * 24 * 60 * 60 * 1000) + // month | ||
(60 * 60 * 1000) + // hour | ||
(60 * 1000) + // min | ||
1000 + // sec | ||
1); // ms | ||
// account for local timezone | ||
start.setMinutes(start.getMinutes() + start.getTimezoneOffset()); | ||
end.setMinutes(end.getMinutes() + end.getTimezoneOffset()); | ||
var input = countdown(start, end, countdown.ALL); | ||
countdown.setLabels(' a|| c|| e|| g|| i|| k', '| B|| D|| F|| H|| J|', ' + finally ', ' + '); | ||
var expected = | ||
'<em>1 k</em> + ' + | ||
'<em>1 century</em> + ' + | ||
'<em>1 year</em> + ' + | ||
'<em>1 g</em> + ' + | ||
'<em>1 week</em> + ' + | ||
'<em>1 e</em> + ' + | ||
'<em>1 hour</em> + ' + | ||
'<em>1 c</em> + ' + | ||
'<em>1 second</em> + finally ' + | ||
'<em>1 a</em>'; | ||
var actual = input.toHTML('em'); | ||
countdown.resetLabels(); | ||
same(actual, expected, ''); | ||
expected = | ||
'<em>1 millennium</em>, ' + | ||
'<em>1 century</em>, ' + | ||
'<em>1 year</em>, ' + | ||
'<em>1 month</em>, ' + | ||
'<em>1 week</em>, ' + | ||
'<em>1 day</em>, ' + | ||
'<em>1 hour</em>, ' + | ||
'<em>1 minute</em>, ' + | ||
'<em>1 second</em> and ' + | ||
'<em>1 millisecond</em>'; | ||
actual = input.toHTML('em'); | ||
same(actual, expected, ''); | ||
}); | ||
test('Partial plural overrides', function() { | ||
var start = new Date(0); | ||
var end = new Date( | ||
(22 * 100) * (365.25 * 24 * 60 * 60 * 1000) + // millennium, century, week, day | ||
(2 * 365 * 24 * 60 * 60 * 1000) + // year | ||
(31 + 29) * (24 * 60 * 60 * 1000) + // month | ||
(2 * 60 * 60 * 1000) + // hour | ||
(2 * 60 * 1000) + // min | ||
(2 * 1000) + // sec | ||
2); // ms | ||
// account for local timezone | ||
start.setMinutes(start.getMinutes() + start.getTimezoneOffset()); | ||
end.setMinutes(end.getMinutes() + end.getTimezoneOffset()); | ||
var input = countdown(start, end, countdown.ALL); | ||
countdown.setLabels(' a|| c|| e|| g|| i|| k', '| B|| D|| F|| H|| J|', ', '); | ||
var expected = | ||
'<em>2 millennia</em>, ' + | ||
'<em>2 J</em>, ' + | ||
'<em>2 H</em>, ' + | ||
'<em>2 months</em>, ' + | ||
'<em>2 F</em>, ' + | ||
'<em>2 days</em>, ' + | ||
'<em>2 D</em>, ' + | ||
'<em>2 minutes</em>, ' + | ||
'<em>2 B</em>, ' + | ||
'<em>2 milliseconds</em>'; | ||
var actual = input.toHTML('em'); | ||
countdown.resetLabels(); | ||
same(actual, expected, ''); | ||
expected = | ||
'<em>2 millennia</em>, ' + | ||
'<em>2 centuries</em>, ' + | ||
'<em>2 years</em>, ' + | ||
'<em>2 months</em>, ' + | ||
'<em>2 weeks</em>, ' + | ||
'<em>2 days</em>, ' + | ||
'<em>2 hours</em>, ' + | ||
'<em>2 minutes</em>, ' + | ||
'<em>2 seconds</em> and ' + | ||
'<em>2 milliseconds</em>'; | ||
actual = input.toHTML('em'); | ||
same(actual, expected, ''); | ||
}); | ||
test('Custom number formatter', function() { | ||
var sillyFormatter = function(num) { | ||
num = ''+num; | ||
var str = ''; | ||
for (var i=0, len=num.length; i<len; i++) { | ||
switch (num.charAt(i)) { | ||
case '0': str += 'zero'; break; | ||
case '1': str += 'one'; break; | ||
case '2': str += 'two'; break; | ||
case '3': str += 'three'; break; | ||
case '4': str += 'four'; break; | ||
case '5': str += 'five'; break; | ||
case '6': str += 'six'; break; | ||
case '7': str += 'seven'; break; | ||
case '8': str += 'eight'; break; | ||
case '9': str += 'nine'; break; | ||
case '.': str += 'dot'; break; | ||
case '-': str += 'dash'; break; | ||
default: str += '?!?'; break; | ||
} | ||
} | ||
return str; | ||
}; | ||
var start = new Date(0); | ||
var end = new Date( | ||
(87 * 100) * (365.25 * 24 * 60 * 60 * 1000) + // millennia, centuries, weeks, days | ||
(2 * 365 * 24 * 60 * 60 * 1000) + // years | ||
(4 * 31 * 24 * 60 * 60 * 1000) + // months | ||
(4 * 60 * 60 * 1000) + // hours | ||
(31 * 60 * 1000) + // mins | ||
(55 * 1000) + // secs | ||
900); // ms | ||
// account for local timezone | ||
start.setMinutes(start.getMinutes() + start.getTimezoneOffset()); | ||
end.setMinutes(end.getMinutes() + end.getTimezoneOffset()); | ||
var input = countdown(start, end, countdown.ALL); | ||
countdown.setLabels(null, null, null, null, null, sillyFormatter); | ||
var expected = | ||
'<em>eight millennia</em>, ' + | ||
'<em>seven centuries</em>, ' + | ||
'<em>two years</em>, ' + | ||
'<em>six months</em>, ' + | ||
'<em>one week</em>, ' + | ||
'<em>four hours</em>, ' + | ||
'<em>threeone minutes</em>, ' + | ||
'<em>fivefive seconds</em> and ' + | ||
'<em>ninezerozero milliseconds</em>'; | ||
var actual = input.toHTML('em'); | ||
countdown.resetLabels(); | ||
same(actual, expected, ''); | ||
expected = | ||
'<em>8 millennia</em>, ' + | ||
'<em>7 centuries</em>, ' + | ||
'<em>2 years</em>, ' + | ||
'<em>6 months</em>, ' + | ||
'<em>1 week</em>, ' + | ||
'<em>4 hours</em>, ' + | ||
'<em>31 minutes</em>, ' + | ||
'<em>55 seconds</em> and ' + | ||
'<em>900 milliseconds</em>'; | ||
actual = input.toHTML('em'); | ||
same(actual, expected, ''); | ||
}); | ||
}catch(ex){alert(ex);} |
@@ -10,3 +10,3 @@ try{ | ||
*/ | ||
countdown.clone = function(map) { | ||
countdown.mock = function(map) { | ||
var ts = countdown(); | ||
@@ -21,2 +21,20 @@ for (var key in map) { | ||
var formatTZ = function(date) { | ||
var tz = -(date instanceof Date ? date : new Date()).getTimezoneOffset(); | ||
if (!tz) { | ||
return 'UTC'; | ||
} | ||
var tzStr = ''+Math.abs(tz % 60); | ||
if (tzStr.length < 2) { | ||
tzStr = '0'+tzStr; | ||
} | ||
tzStr = Math.abs(tz / 60) + tzStr; | ||
if (tzStr.length < 4) { | ||
tzStr = '0'+tzStr; | ||
} | ||
return 'UTC'+((tz < 0) ? '-' : '+') +tzStr; | ||
}; | ||
module('countdown(...)'); | ||
@@ -29,3 +47,3 @@ | ||
var expected = countdown.clone({ | ||
var expected = countdown.mock({ | ||
start: new Date(0), | ||
@@ -58,3 +76,3 @@ end: new Date(0), | ||
var expected = countdown.clone({ | ||
var expected = countdown.mock({ | ||
start: new Date(0), | ||
@@ -87,3 +105,3 @@ end: new Date(1), | ||
var expected = countdown.clone({ | ||
var expected = countdown.mock({ | ||
start: new Date(10000), | ||
@@ -116,3 +134,3 @@ end: new Date(11000), | ||
var expected = countdown.clone({ | ||
var expected = countdown.mock({ | ||
start: new Date(5 * 60 * 1000), | ||
@@ -140,8 +158,7 @@ end: new Date(0), | ||
test('constant 1 month span, daily over 5 years', function() { | ||
test('Constant 1 month span, daily over 5 years', function() { | ||
var daySpan = 24 * 60 * 60 * 1000; | ||
var start = new Date(1999, 10, 1, 12, 0, 0); | ||
var expected = countdown.clone({ | ||
var expected = countdown.mock({ | ||
start: start, | ||
@@ -167,6 +184,6 @@ end: start, | ||
var end = new Date(start.getTime()); | ||
end.setUTCMonth( end.getUTCMonth()+1 ); | ||
end.setMonth( end.getMonth()+1 ); | ||
// skip situations like 'Jan 31st + month' | ||
if (end.getUTCMonth() === start.getUTCMonth()+1) { | ||
if (end.getMonth() === start.getMonth()+1) { | ||
expected.start = start; | ||
@@ -182,18 +199,17 @@ expected.end = end; | ||
// add a day | ||
start = new Date( start.getTime()+daySpan ); | ||
start.setDate( start.getDate()+1 ); | ||
} | ||
}); | ||
test('contiguous daily countdown over 83 weeks', function() { | ||
test('Contiguous daily countdown over 83 weeks', function() { | ||
var daySpan = 24 * 60 * 60 * 1000; | ||
var units = countdown.WEEKS | countdown.DAYS | countdown.HOURS; | ||
var UNITS = countdown.WEEKS | countdown.DAYS | countdown.HOURS; | ||
var start = new Date(2007, 10, 10, 5, 30, 0); | ||
var end = new Date(2009, 5, 14, 16, 0, 0); | ||
var expected = { weeks: 83, days: 1, hours: 9.5 }; | ||
var expected = { weeks: 83, days: 1, hours: 10.5 }; | ||
while (start.getTime() < end.getTime()) { | ||
var actual = countdown(start, end, units, 0, 20); | ||
var actual = countdown(start, end, UNITS, 0, 20); | ||
actual = { | ||
@@ -208,3 +224,3 @@ weeks: actual.weeks, | ||
// add a day | ||
start = new Date( start.getTime()+daySpan ); | ||
start.setDate( start.getDate()+1 ); | ||
@@ -228,14 +244,14 @@ // store | ||
test('contiguous daily countdown over 1 year 7 months', function() { | ||
test('Contiguous daily countdown over 1 year 7 months', function() { | ||
var daySpan = 24 * 60 * 60 * 1000; | ||
var units = countdown.MONTHS | countdown.DAYS | countdown.HOURS; | ||
var DAY_IN_MS = 24 * 60 * 60 * 1000; | ||
var UNITS = countdown.MONTHS | countdown.DAYS | countdown.HOURS; | ||
var start = new Date(2006, 10, 10, 5, 30, 0); | ||
var end = new Date(2008, 5, 14, 16, 0, 0); | ||
var expected = { months: 19, days: 4, hours: 9.5 }; | ||
var expected = { months: 19, days: 4, hours: 10.5 }; | ||
while (start.getTime() < end.getTime()) { | ||
var actual = countdown(start, end, units, 0, 1); | ||
var actual = countdown(start, end, UNITS, 0, 1); | ||
actual = { | ||
@@ -250,3 +266,3 @@ months: actual.months, | ||
// add a day | ||
start = new Date( start.getTime()+daySpan ); | ||
start.setDate( start.getDate()+1 ); | ||
@@ -261,3 +277,3 @@ // set up next iteration | ||
} else { | ||
var daysInStart = Math.round((new Date(start.getFullYear(), start.getMonth()+1, 15).getTime() - new Date(start.getFullYear(), start.getMonth(), 15).getTime()) / (24 * 60 * 60 * 1000)); | ||
var daysInStart = Math.round((new Date(start.getFullYear(), start.getMonth()+1, 15).getTime() - new Date(start.getFullYear(), start.getMonth(), 15).getTime()) / DAY_IN_MS); | ||
expected = { | ||
@@ -273,6 +289,6 @@ months: actual.months-1, | ||
test('contiguous weekly countdown over 7 months, out of bounds max & digits', function() { | ||
test('Contiguous weekly countdown over 7 months, out of bounds max & digits', function() { | ||
var daySpan = 24 * 60 * 60 * 1000; | ||
var units = countdown.MONTHS | countdown.WEEKS | countdown.DAYS | countdown.HOURS; | ||
var DAY_IN_MS = 24 * 60 * 60 * 1000; | ||
var UNITS = countdown.MONTHS | countdown.WEEKS | countdown.DAYS | countdown.HOURS; | ||
var start = new Date(1999, 10, 10, 5, 30, 0); | ||
@@ -282,7 +298,7 @@ var end = new Date(2001, 5, 14, 16, 0, 0); | ||
// calc by days since easier | ||
var prev = { months: 19, days: 4, hours: 9.5 }; | ||
var prev = { months: 19, days: 4, hours: 10.5 }; | ||
while (start.getTime() < end.getTime()) { | ||
var actual = countdown(start, end, units, -1, 100); | ||
var actual = countdown(start, end, UNITS, -1, 100); | ||
actual = { | ||
@@ -306,3 +322,3 @@ months: actual.months, | ||
// add a day | ||
start = new Date( start.getTime()+daySpan ); | ||
start.setDate( start.getDate()+1 ); | ||
@@ -317,3 +333,3 @@ // set up next iteration | ||
} else { | ||
var daysInStart = Math.round((new Date(start.getFullYear(), start.getMonth()+1, 15).getTime() - new Date(start.getFullYear(), start.getMonth(), 15).getTime()) / (24 * 60 * 60 * 1000)); | ||
var daysInStart = Math.round((new Date(start.getFullYear(), start.getMonth()+1, 15).getTime() - new Date(start.getFullYear(), start.getMonth(), 15).getTime()) / DAY_IN_MS); | ||
prev = { | ||
@@ -329,6 +345,6 @@ months: prev.months-1, | ||
test('contiguous daily count up over 10 years', function() { | ||
test('Contiguous daily count up over 10 years', function() { | ||
var daySpan = 24 * 60 * 60 * 1000; | ||
var units = countdown.MONTHS | countdown.DAYS; | ||
var DAY_IN_MS = 24 * 60 * 60 * 1000; | ||
var UNITS = countdown.MONTHS | countdown.DAYS; | ||
var start = new Date(1995, 0, 1, 12, 0, 0); | ||
@@ -342,3 +358,3 @@ var end = new Date(start.getTime()); | ||
var actual = countdown(start, end, units); | ||
var actual = countdown(start, end, UNITS); | ||
actual = { | ||
@@ -351,6 +367,6 @@ months: actual.months, | ||
var daysInEnd = Math.round((new Date(end.getFullYear(), end.getMonth()+1, 15).getTime() - new Date(end.getFullYear(), end.getMonth(), 15).getTime()) / (24 * 60 * 60 * 1000)); | ||
var daysInEnd = Math.round((new Date(end.getFullYear(), end.getMonth()+1, 15).getTime() - new Date(end.getFullYear(), end.getMonth(), 15).getTime()) / DAY_IN_MS); | ||
// add a day | ||
end = new Date( end.getTime()+daySpan ); | ||
end.setDate( end.getDate + 1 ); | ||
@@ -373,10 +389,10 @@ // set up next iteration | ||
test('Underflow bug', function() { | ||
test('Underflow bug ('+formatTZ(2011, 11, 1)+')', function() { | ||
var start = new Date(2011, 11, 1); | ||
var start = new Date(2011, 11, 1, 0, 0, 0, 0); | ||
var end = new Date(2011, 11, 31, 23, 59, 59, 999); | ||
var expected = countdown.clone({ | ||
start: new Date(2011, 11, 1), | ||
end: new Date(2011, 11, 31, 23, 59, 59, 999), | ||
var expected = countdown.mock({ | ||
start: new Date(start.getTime()), | ||
end: new Date(end.getTime()), | ||
units: countdown.ALL, | ||
@@ -402,8 +418,92 @@ value: end.getTime() - start.getTime(), | ||
test('Extra day bug ('+formatTZ(2014, 6, 3)+')', function() { | ||
var start = new Date(2014, 6, 1, 0, 0, 0, 0); | ||
var end = new Date(2014, 6, 3, 17, 52, 49, 209); | ||
var expected = countdown.mock({ | ||
start: new Date(start.getTime()), | ||
end: new Date(end.getTime()), | ||
units: countdown.ALL, | ||
value: end.getTime() - start.getTime(), | ||
millennia: 0, | ||
centuries: 0, | ||
decades: 0, | ||
years: 0, | ||
months: 0, | ||
weeks: 0, | ||
days: 2, | ||
hours: 17, | ||
minutes: 52, | ||
seconds: 49, | ||
milliseconds: 209 | ||
}); | ||
var actual = countdown(start, end, countdown.ALL); | ||
same(actual, expected, ''+start+' => '+end); | ||
}); | ||
test('Daylight Savings Time ('+formatTZ(1318451880000)+')', function() { | ||
var start = new Date(2011, 9, 12, 13, 38, 0); | ||
var end = new Date(2013, 11, 2, 14, 0, 0); | ||
var expected = countdown.mock({ | ||
start: new Date(start.getTime()), | ||
end: new Date(end.getTime()), | ||
units: countdown.ALL, | ||
value: end.getTime() - start.getTime(), | ||
millennia: 0, | ||
centuries: 0, | ||
decades: 0, | ||
years: 2, | ||
months: 1, | ||
weeks: 3, | ||
days: 0, | ||
hours: 0, | ||
minutes: 22, | ||
seconds: 0, | ||
milliseconds: 0 | ||
}); | ||
var actual = countdown(start, end, countdown.ALL); | ||
same(actual, expected, ''+start+' => '+end); | ||
}); | ||
test('Reference month ordering', function() { | ||
var start = new Date(2015, 1, 1, 0, 0, 0); | ||
var end = new Date(2014, 9, 27, 12, 00, 0); | ||
var expected = countdown.mock({ | ||
start: new Date(start.getTime()), | ||
end: new Date(end.getTime()), | ||
units: countdown.ALL, | ||
value: end.getTime() - start.getTime(), | ||
millennia: 0, | ||
centuries: 0, | ||
decades: 0, | ||
years: 0, | ||
months: 3, | ||
weeks: 0, | ||
days: 4, | ||
hours: 12, | ||
minutes: 0, | ||
seconds: 0, | ||
milliseconds: 0 | ||
}); | ||
var actual = countdown(start, end, countdown.ALL); | ||
same(actual, expected, ''+start+' => '+end); | ||
}); | ||
test('Before leap day', function() { | ||
var start = new Date(2012, 1, 28, 13, 14, 30, 109); | ||
var end = new Date(2012, 1, 29, 17, 46, 22, 111);// Leap day 2012 | ||
var end = new Date(2012, 1, 29, 17, 46, 22, 111); // Leap day 2012 | ||
var expected = countdown.clone({ | ||
var expected = countdown.mock({ | ||
start: new Date(2012, 1, 28, 13, 14, 30, 109), | ||
@@ -433,6 +533,6 @@ end: new Date(2012, 1, 29, 17, 46, 22, 111), | ||
var start = new Date(2012, 1, 29, 17, 46, 22, 111);// Leap day 2012 | ||
var start = new Date(2012, 1, 29, 17, 46, 22, 111); // Leap day 2012 | ||
var end = new Date(2012, 2, 1, 13, 14, 30, 109); | ||
var expected = countdown.clone({ | ||
var expected = countdown.mock({ | ||
start: new Date(2012, 1, 29, 17, 46, 22, 111), | ||
@@ -462,6 +562,6 @@ end: new Date(2012, 2, 1, 13, 14, 30, 109), | ||
var start = new Date(1330537582111);// Leap day 2012 | ||
var end = new Date(1330607670109); | ||
var start = new Date(1330537582111); // 2012-02-29T17:46:22.111Z, Leap day 2012 | ||
var end = new Date(1330607670109); // 2012-03-01T13:14:30.109Z | ||
var expected = countdown.clone({ | ||
var expected = countdown.mock({ | ||
start: new Date(1330537582111), | ||
@@ -491,6 +591,6 @@ end: new Date(1330607670109), | ||
var start = new Date(915220800000); | ||
var end = new Date(915220919999); | ||
var start = new Date(915220800000); // 1999-01-01T20:00:00.000Z | ||
var end = new Date(915220919999); // 1999-01-01T20:01:59.999Z | ||
var expected = countdown.clone({ | ||
var expected = countdown.mock({ | ||
start: new Date(915220800000), | ||
@@ -515,6 +615,6 @@ end: new Date(915220919999), | ||
var start = new Date(915220800000); | ||
var end = new Date(915220919999); | ||
var start = new Date(915220800000); // 1999-01-01T20:00:00.000Z | ||
var end = new Date(915220919999); // 1999-01-01T20:01:59.999Z | ||
var expected = countdown.clone({ | ||
var expected = countdown.mock({ | ||
start: new Date(915220800000), | ||
@@ -539,6 +639,6 @@ end: new Date(915220919999), | ||
var start = new Date(915220800000); | ||
var end = new Date(915220919999); | ||
var start = new Date(915220800000); // 1999-01-01T20:00:00.000Z | ||
var end = new Date(915220919999); // 1999-01-01T20:01:59.999Z | ||
var expected = countdown.clone({ | ||
var expected = countdown.mock({ | ||
start: new Date(915220800000), | ||
@@ -561,2 +661,156 @@ end: new Date(915220919999), | ||
module('Timespan.addTo(date)'); | ||
test('Zero', function() { | ||
var timespan = countdown.mock({ | ||
millennia: 0, | ||
centuries: 0, | ||
decades: 0, | ||
years: 0, | ||
months: 0, | ||
weeks: 0, | ||
days: 0, | ||
hours: 0, | ||
minutes: 0, | ||
seconds: 0, | ||
milliseconds: 0 | ||
}); | ||
var expected = 0; | ||
var actual = timespan.addTo(0).getTime(); | ||
same(actual, expected, ''); | ||
}); | ||
test('1 ms', function() { | ||
var timespan = countdown.mock({ | ||
millennia: 0, | ||
centuries: 0, | ||
decades: 0, | ||
years: 0, | ||
months: 0, | ||
weeks: 0, | ||
days: 0, | ||
hours: 0, | ||
minutes: 0, | ||
seconds: 0, | ||
milliseconds: 1 | ||
}); | ||
var expected = 1; | ||
var actual = timespan.addTo(0).getTime(); | ||
same(actual, expected, ''); | ||
}); | ||
test('1 sec', function() { | ||
var timespan = countdown.mock({ | ||
millennia: 0, | ||
centuries: 0, | ||
decades: 0, | ||
years: 0, | ||
months: 0, | ||
weeks: 0, | ||
days: 0, | ||
hours: 0, | ||
minutes: 0, | ||
seconds: 1, | ||
milliseconds: 0 | ||
}); | ||
var expected = 11000; | ||
var actual = timespan.addTo(10000).getTime(); | ||
same(actual, expected, ''); | ||
}); | ||
test('1 sec, value', function() { | ||
var timespan = countdown.mock({ | ||
value: 1000 | ||
}); | ||
var expected = 11000; | ||
var actual = timespan.addTo(10000).getTime(); | ||
same(actual, expected, ''); | ||
}); | ||
test('5 min, reversed', function() { | ||
var timespan = countdown.mock({ | ||
millennia: 0, | ||
centuries: 0, | ||
decades: 0, | ||
years: 0, | ||
months: 0, | ||
weeks: 0, | ||
days: 0, | ||
hours: 0, | ||
minutes: -5, | ||
seconds: 0, | ||
milliseconds: 0 | ||
}); | ||
var expected = 0; | ||
var actual = timespan.addTo((5 * 60 * 1000)).getTime(); | ||
same(actual, expected, ''); | ||
}); | ||
test('5 min, reversed, value', function() { | ||
var timespan = countdown.mock({ | ||
value: -(5 * 60 * 1000) | ||
}); | ||
var expected = 0; | ||
var actual = timespan.addTo((5 * 60 * 1000)).getTime(); | ||
same(actual, expected, ''); | ||
}); | ||
test('Daylight Savings Time ('+formatTZ(1318451880000)+')', function() { | ||
var start = new Date(2011, 9, 12, 13, 38, 0); | ||
var end = new Date(2013, 11, 2, 14, 0, 0); | ||
var timespan = countdown.mock({ | ||
millennia: 0, | ||
centuries: 0, | ||
decades: 0, | ||
years: 2, | ||
months: 1, | ||
weeks: 3, | ||
days: 0, | ||
hours: 0, | ||
minutes: 22, | ||
seconds: 0, | ||
milliseconds: 0 | ||
}); | ||
var expected = end.getTime(); | ||
var actual = timespan.addTo(start).getTime(); | ||
same(actual, expected, ''+start+' => '+end); | ||
}); | ||
test('Daylight Savings Time ('+formatTZ(1318451880000)+'), value', function() { | ||
var start = new Date(2011, 9, 12, 13, 38, 0); | ||
var end = new Date(2013, 11, 2, 14, 0, 0); | ||
var timespan = countdown.mock({ | ||
value: end.getTime() - start.getTime() | ||
}); | ||
var expected = end.getTime(); | ||
var actual = timespan.addTo(start).getTime(); | ||
same(actual, expected, ''+start+' => '+end); | ||
}); | ||
}catch(ex){alert(ex);} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
8015388
10021
292
0
40
1