Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

unit tests and bug fixes for Date.prototype.diff

  • Loading branch information...
commit 293b66a7ba2883efcb9bf201fa4f7351930f0d72 1 parent d80eb16
@constantology authored
Showing with 28 additions and 23 deletions.
  1. +2 −5 d8.js
  2. +1 −1  d8.min.js
  3. +2 −4 src/diff.js
  4. +23 −13 test/d8.test.js
View
7 d8.js
@@ -300,8 +300,7 @@
return ddiiff;
}
// round up or down depending on what's available
- if ( calcs[i + 1] ) return ddiiff;
- else if ( calc = calcs[i - 1] ) {
+ if ( ( !calcs[i + 1] || ddiiff.excl[calcs[i + 1][0]] ) && ( calc = calcs[i - 1] ) ) {
time = ddiiff.__ms__ / calc[1];
ddiiff.__ms__ = ( Math.round( time ) * calc[1] ) + ( ( ( ddiiff.__ms__ / calcs[i][1] ) % 1 ) * calcs[i][1] );
return diff_eval( ddiiff, calc, i - 1, [] );
@@ -350,12 +349,10 @@
case '-' : excl[prop] = true; break;
case '+' : excl[prop] = false; break;
case '>' :
- diff_calc.map( diff_excl_iter, excl, { excl : excl, prop : prop, val : true } );
- delete excl.SET_VALID;
+ diff_calc.map( diff_excl_iter, { excl : excl, prop : prop, val : true } );
break;
case '<' :
diff_calc.slice().reverse().map( diff_excl_iter, { excl : excl, prop : prop, val : false } );
- delete excl.SET_VALID;
break;
default : excl[val] = false;
}
View
2  d8.min.js
@@ -1 +1 @@
-!function(e){"use strict";e.x.cache("Date",function(t){function n(e,t){var n=e.indexOf(t);return n==-1?null:n}function r(e){return e.toLowerCase()}function i(e){return e.substring(0,3)}function s(e){return e.toUpperCase()}function o(e,t){return e.reduce(function(e,n,r){return e[t[r]]=n,e},{})}function u(e,t,n){return t<=e&&e<=n}function a(e,t,n){var r=-1,i=e.toString(n||10);t-=i.length;while(++r<t)i="0"+i;return i}function f(e,t){return e.reduce(function(e,n){return!(t in n)||e.push(n[t]),e},[])}function l(e,t){return e+t}function c(e,t){return(e=Number(e))<12&&r(t.ampm)==r(G.PM)?e+=12:e}function h(e,t,n){return e.adjust(n,t)}function p(e){return Math.floor((e-C.call(e))/et)}function d(e){var t=!!e.indexOf("-"),n=e.match(Xt),r=(Number(n[1])+n[2]/60)*3600;return t?r:-r}function v(e){return Math.floor(Math.abs(p(e)/7))}function m(e,t){return isNaN(t)?Number(e)-1:t==e?0:Number(t)}function g(n,r){if(e.ntype(n)=="object")return Object.reduce(n,h,this),this;var i,s=Nt[n.toLowerCase()],o;return!s||r===0?this:(G.setLeapYear(this),s==Nt.month&&(i=this.getDate(),i<28||this.setDate(Math.min(i,A.call(S.call(this).adjust(t.MONTH,r)).getDate()))),s!=Nt.week||(o=this.getDay()),this[s[1]](this[s[0]]()+r),!o||this.setDate(this.getDate()+o),this)}function y(e,t){return this>=e&&this<=t}function b(){return this.setHours(0),this.setMinutes(0),this.setSeconds(0),this.setMilliseconds(0),this}function w(){return new t(this.getTime())}function E(){return G.setLeapYear(this),G.day_count.slice(0,this.getMonth()).reduce(l,0)+this.getDate()-1}function S(){return new t(this.getFullYear(),this.getMonth(),1)}function x(e){var t=this.getTimezoneOffset();return[t>0?"-":"+",a(Math.floor(Math.abs(t)/60),2),e?":":"",a(Math.abs(t%60),2)].join("")}function T(){return this.getDay()||7}function N(){return Math.ceil((C.call(new t(this.getFullYear()+1,0,1))-C.call(this))/et)}function C(){var e=this.getFullYear();return new t(e,0,Z[(new t(e,0,1)).getDay()])}function k(){var e,n=this.getFullYear();return this>=C.call(new t(n+1,0,1))?1:(e=Math.floor((E.call(this)-T.call(this)+10)/7),e==0?L.call(new t(n-1,0,1))-v(this):e)}function L(){return Math.round((C.call(new t(this.getFullYear()+1,0,1))-C.call(this))/st)}function A(){var e=this.getMonth();return G.setLeapYear(this),new t(this.getFullYear(),e,G.day_count[e])}function O(){return Math.floor(E.call(this)/7)}function M(){return(new t(this.getFullYear(),0,1)).getTimezoneOffset()!=this.getTimezoneOffset()}function _(){return G.isLeapYear(this.getFullYear())}function D(e){return this.setMonth(0),this.setDate(1),this.adjust(t.DAY,e*7).getTime()}function P(){var e=this.toString().split(" ");return e.splice(4,e.length).join(" ").replace(zt,"$1").replace(Wt,"")}function H(e){if(Ct[e])return Ct[e];var t=["var out=[];"],n=-1,r,i=e.replace(jt,xt).replace(Bt,Tt).split(Ut),s=/^[^A-Za-z]*$/g,o=i.length;while(++n<o)r=i[n],r==St?(t.push(F(i[++n])),++n):s.test(r)?t.push(F(r)):t.push(j(r));return t.push('return out.join( "" );\n//@ sourceURL=d8/format/'+e),Ct[e]=new Function("filter","date",t.join("\n"))}function B(e){return H(e)(Mt,this)}function j(e){return e.replace(Ft,function(e,t,n,r){return F(t+"', filter."+n+"( date ), '"+r)})}function F(e){return"out.push( '"+e+"' );"}function I(t){if(kt[t])return kt[t];var n={},r=[],i=-1,s,u=t.replace(jt,xt).replace(Bt,Tt).split(Ut),a=u.length,l=[];while(++i<a){s=u[i];if(s==St){l.push(u[++i]),++i;continue}s.replace(Ft,function(t,i,s,u){var a,c,h;if(!(h=Vt[s]))return;h.k&&(r.push(h.k),h.fn&&(n[h.k]=h.fn)),h.combo&&(c=f(h.combo,"k"),a=o(f(h.combo,"fn"),c),r.push.apply(r,c),e.copy(n,a,!0)),h.re&&l.push(i,h.re,u)})}return kt[t]=q.bind(null,new RegExp(l.join("")),r,n)}function q(e,n,r,i){var s=new t,u=i.match(e),a=o(u.slice(1),n);return Object.reduce(a,function(e,t,n){return typeof t=="string"&&r[n]&&(a[n]=r[n](t,a)),e},null),isNaN(a[bt])?(U(s,a[pt],a[vt],a[gt],a[dt]),R(s,a),z(s,a[yt])):s.setTime(a[bt]),s}function R(t,n){var r,i=-1,s,o,a;if(Lt.every(e.has.bind(null,n)))return;isNaN(n[Et])&&(n[Et]=t.getFullYear());if(isNaN(n[mt])){o=G.isLeapYear(n[Et])?1:0,a=G.ordinal_day_count[o],s=a.length,n[mt]=0,n[wt]&&!n[ht]&&(r=n[ct],r=isNaN(r)?0:r?r:7,n[ht]=n[wt]*7-(4-r));if(!isNaN(n[ht])){n[ht]>a[a.length-1]&&(n[ht]-=a[a.length-1],++n[Et]);while(++i<s)if(u(n[ht],a[i],a[i+1])){n[mt]=i,n[lt]=a[i]==0?n[ht]:n[ht]-a[i];break}}}isNaN(n[lt])&&(n[lt]=1),t.setYear(n[Et]),t.setMonth(n[mt]),t.setDate(n[lt])}function U(e,t,n,r,i){e.setHours(t||0),e.setMinutes(n||0),e.setSeconds(r||0),e.setMilliseconds(i||0)}function z(e,n){!u(n,-43200,50400)||e.adjust(t.SECOND,-e.getTimezoneOffset()*60-n)}function W(e,t){return I(t)(e)}function X(t,n){switch(e.ntype(t)){case"string":if(!isNaN(+(new Date(t)))){t=new Date(t);break}if(n){t=Date.now();break};case"array":case"object":n=t,t=Date.now();break;case"date":if(!isNaN(+(new Date(t))))break;default:t=Date.now()}var r,i=t- +this,s=i<0?1:i>0?-1:0;return r=s===0?e.obj():$(Math.abs(i),J(n)),r.tense=s,r}function V(e,t,n,r){var i;return e.__ms__?e.excl[t[0]]?(t=r[n+1])||(t=r[n-1])?(i=e.__ms__/t[1],e.__ms__=Math.round(i)*t[1]+e.__ms__/r[n][1]%1*r[n][1],V(e,t,n-1,[])):e:(e.__ms__>=t[1]&&(i=e.__ms__/t[1],t[0]in e.val?(i=Math.floor(i),e.__ms__-=i*t[1],e.val[t[0]]+=i):(e.__ms__=i%1*t[1],e.val[t[0]]=Math.floor(i))),e):e}function $(t,n){var r=At.reduce(V,{__ms__:t,excl:n,val:e.obj()}).val;return r.value=t,r}function J(t){var n=e.obj(),r=!0;if(t)switch(e.ntype(t)){case"object":r=!1;break;case"string":t=t.split(" ");case"array":t=t.reduce(K,n),r=!!e.len(n)}return Ot.map(function(e){e in this||(this[e]=!r)},n),n}function K(e,t){var n=(t=String(t).toLowerCase()).substring(1);switch(t.charAt(0)){case"-":e[n]=!0;break;case"+":e[n]=!1;break;case">":At.map(Q,e,{excl:e,prop:n,val:!0}),delete e.SET_VALID;break;case"<":At.slice().reverse().map(Q,{excl:e,prop:n,val:!1}),delete e.SET_VALID;break;default:e[t]=!1}return e}function Q(e){e[0]===this.prop&&(this.SET_VALID=!0),this.SET_VALID&&(this.excl[e[0]]=this.val)}var G=t.locale,Y,Z=[9,1,0,-1,-2,4,3],et=864e5,tt=36e5,nt=6e4,rt=2592e6,it=1e3,st=6048e5,ot=31536e6,ut=G.days.map(i),at=G.months.map(i),ft="ampm",lt="day",ct="dayweek",ht="dayyear",pt="hour",dt="ms",vt="minute",mt="month",gt="second",yt="timezone",bt="unix",wt="week",Et="year",St="NOREPLACE",xt="<"+St+"<",Tt=">END"+St+">",Nt={day:["getDate","setDate"],hr:["getHours","setHours"],min:["getMinutes","setMinutes"],month:["getMonth","setMonth"],ms:["getMilliseconds","setMilliseconds"],sec:["getSeconds","setSeconds"],week:["getWeek","setWeek"],year:["getFullYear","setFullYear"]},Ct={},kt={},Lt=[lt,ct,ht,mt,wt,Et],At=[[Et+"s",ot],[mt+"s",rt],[wt+"s",st],[lt+"s",et],[pt+"s",tt],[vt+"s",nt],[gt+"s",it],[dt,1]],Ot=At.map(function(e){return e[0]}),Mt={d:function(e){return a(e.getDate(),2)},D:function(e){return G.days[e.getDay()].substring(0,3)},j:function(e){return e.getDate()},l:function(e){return G.days[e.getDay()]},N:function(e){return T.call(e)},S:function(e){return G.getOrdinal(e.getDate())},w:function(e){return e.getDay()},z:function(e){return e.getDayOfYear()},W:function(e){return k.call(e)},F:function(e){return G.months[e.getMonth()]},m:function(e){return a(e.getMonth()+1,2)},M:function(e){return G.months[e.getMonth()].substring(0,3)},n:function(e){return e.getMonth()+1},t:function(e){return G.setLeapYear(e),G.day_count[e.getMonth()]},L:function(e){return e.isLeapYear()?1:0},o:function(e){var t=e.getMonth(),n=k.call(e);return e.getFullYear()+(n==1&&t>0?1:n>=52&&t<11?-1:0)},Y:function(e){return e.getFullYear()},y:function(e){return String(e.getFullYear()).substring(2,4)},a:function(e){return r(e.getHours()<12?G.AM:G.PM)},A:function(e){return s(e.getHours()<12?G.AM:G.PM)},g:function(e){return e.getHours()%12||12},G:function(e){return e.getHours()},h:function(e){return a(Mt.g(e),2)},H:function(e){return a(Mt.G(e),2)},i:function(e){return a(e.getMinutes(),2)},s:function(e){return a(e.getSeconds(),2)},u:function(e){return a(e.getMilliseconds(),3)},O:function(e){return x.call(e)},P:function(e){return x.call(e,!0)},T:function(e){return P.call(e)},Z:function(e){return e.getTimezoneOffset()*-60},c:function(e){return B.call(e,Dt.ISO_8601)},r:function(e){return B.call(e,Dt.RFC_2822)},U:function(e){return e.getTime()}},_t=Object.keys(Mt).sort().join(""),Dt=e.copy({ISO_8601:"Y-m-d<T>H:i:s.u<Z>",ISO_8601_SHORT:"Y-m-d",RFC_850:"l, d-M-y H:i:s T",RFC_2822:"D, d M Y H:i:s O",sortable:"Y-m-d H:i:sO"},G.formats),Pt,Ht="(am|pm)",Bt=/>/g,jt=/</g,Ft=new RegExp("([^"+_t+"]*)(["+_t+"])([^"+_t+"]*)","g"),It="([0-9]{1,2})",qt="([0-9]{2})",Rt="([0-9]{4})",Ut=/[<>]/,zt=/[^\(]*\(([^\)]+)\)/g,Wt=/[^A-Z]+/g,Xt=/[\+-]?([0-9]{2}):?([0-9]{2})/,Vt={d:{k:lt,fn:Number,re:qt},D:{k:ct,fn:n.bind(null,ut),re:"("+ut.join("|")+")"},j:{k:lt,fn:Number,re:It},l:{k:ct,fn:n.bind(null,G.days),re:"("+G.days.join("|")+")"},N:{k:ct,fn:m.bind(null,7),re:"([1-7])"},S:{re:"(?:"+G.ordinal.join("|")+")"},w:{k:ct,fn:Number,re:"([0-6])"},z:{k:ht,fn:Number,re:"([0-9]{1,3})"},W:{k:wt,fn:Number,re:qt},F:{k:mt,fn:n.bind(null,G.months),re:"("+G.months.join("|")+")"},m:{k:mt,fn:m,re:qt},M:{k:mt,fn:n.bind(null,at),re:"("+at.join("|")+")"},n:{k:mt,fn:m,re:It},t:{re:"[0-9]{2}"},L:{re:"(?:0|1)"},o:{k:Et,fn:Number,re:Rt},Y:{k:Et,fn:Number,re:Rt},y:{k:Et,fn:function(e){return e=Number(e),e+=e<30?2e3:1900},re:qt},a:{k:ft,fn:e,re:Ht},A:{k:ft,fn:r,re:s(Ht)},g:{k:pt,fn:c,re:It},G:{k:pt,fn:Number,re:It},h:{k:pt,fn:c,re:qt},H:{k:pt,fn:Number,re:qt},i:{k:vt,fn:Number,re:qt},s:{k:gt,fn:Number,re:qt},u:{k:dt,fn:Number,re:"([0-9]{1,})"},O:{k:yt,fn:d,re:"([\\+-][0-9]{4})"},P:{k:yt,fn:d,re:"([\\+-][0-9]{2}:[0-9]{2})"},T:{re:"[A-Z]{1,4}"},Z:{k:yt,fn:Number,re:"([\\+-]?[0-9]{5})"},U:{k:bt,fn:Number,re:"(-?[0-9]{1,})"}};Dt.atom=Dt.ISO_8601,Dt.cookie=Dt.RFC_850,Dt.rss=Dt.RFC_2822,Vt.c={combo:[Vt.Y,Vt.m,Vt.d,Vt.H,Vt.i,Vt.s,Vt.u,Vt.P],re:[Vt.Y.re,"-",Vt.m.re,"-",Vt.d.re,"T",Vt.H.re,":",Vt.i.re,":",Vt.s.re,"(?:\\.",Vt.u.re,"){0,1}",Vt.P.re,"{0,1}"].join("")},Vt.r={combo:[Vt.D,Vt.d,Vt.M,Vt.Y,Vt.H,Vt.i,Vt.s,Vt.O],re:[Vt.D.re,", ",Vt.d.re," ",Vt.M.re," ",Vt.Y.re," ",Vt.H.re,":",Vt.i.re,":",Vt.s.re," ",Vt.O.re].join("")},e.defs(t.prototype,{adjust:g,between:y,clearTime:b,clone:w,diff:X,format:B,getDayOfYear:E,getFirstOfTheMonth:S,getGMTOffset:x,getISODay:T,getISODaysInYear:N,getISOFirstMondayOfYear:C,getISOWeek:k,getISOWeeksInYear:L,getLastOfTheMonth:A,getWeek:O,isDST:M,isLeapYear:_,setWeek:D,timezone:P},"r"),e.defs(t,{DAY:lt,HOUR:"hr",MINUTE:vt.substring(0,3),MILLISECOND:dt,MONTH:mt,SECOND:gt.substring(0,3),WEEK:wt,YEAR:Et,MS_DAY:et,MS_HOUR:tt,MS_MINUTE:nt,MS_MONTH:rt,MS_SECOND:it,MS_WEEK:st,MS_YEAR:ot,filters:{value:Mt},formats:{value:Dt},parsers:{value:Vt},getOrdinal:G.getOrdinal,isLeapYear:G.isLeapYear,setLeapYear:G.setLeapYear,toDate:W},"r")}).x(Date)}(typeof m8!="undefined"?m8:typeof require!="undefined"?require("m8"):null)
+!function(e){"use strict";e.x.cache("Date",function(t){function n(e,t){var n=e.indexOf(t);return n==-1?null:n}function r(e){return e.toLowerCase()}function i(e){return e.substring(0,3)}function s(e){return e.toUpperCase()}function o(e,t){return e.reduce(function(e,n,r){return e[t[r]]=n,e},{})}function u(e,t,n){return t<=e&&e<=n}function a(e,t,n){var r=-1,i=e.toString(n||10);t-=i.length;while(++r<t)i="0"+i;return i}function f(e,t){return e.reduce(function(e,n){return!(t in n)||e.push(n[t]),e},[])}function l(e,t){return e+t}function c(e,t){return(e=Number(e))<12&&r(t.ampm)==r(G.PM)?e+=12:e}function h(e,t,n){return e.adjust(n,t)}function p(e){return Math.floor((e-C.call(e))/et)}function d(e){var t=!!e.indexOf("-"),n=e.match(Xt),r=(Number(n[1])+n[2]/60)*3600;return t?r:-r}function v(e){return Math.floor(Math.abs(p(e)/7))}function m(e,t){return isNaN(t)?Number(e)-1:t==e?0:Number(t)}function g(n,r){if(e.ntype(n)=="object")return Object.reduce(n,h,this),this;var i,s=Nt[n.toLowerCase()],o;return!s||r===0?this:(G.setLeapYear(this),s==Nt.month&&(i=this.getDate(),i<28||this.setDate(Math.min(i,A.call(S.call(this).adjust(t.MONTH,r)).getDate()))),s!=Nt.week||(o=this.getDay()),this[s[1]](this[s[0]]()+r),!o||this.setDate(this.getDate()+o),this)}function y(e,t){return this>=e&&this<=t}function b(){return this.setHours(0),this.setMinutes(0),this.setSeconds(0),this.setMilliseconds(0),this}function w(){return new t(this.getTime())}function E(){return G.setLeapYear(this),G.day_count.slice(0,this.getMonth()).reduce(l,0)+this.getDate()-1}function S(){return new t(this.getFullYear(),this.getMonth(),1)}function x(e){var t=this.getTimezoneOffset();return[t>0?"-":"+",a(Math.floor(Math.abs(t)/60),2),e?":":"",a(Math.abs(t%60),2)].join("")}function T(){return this.getDay()||7}function N(){return Math.ceil((C.call(new t(this.getFullYear()+1,0,1))-C.call(this))/et)}function C(){var e=this.getFullYear();return new t(e,0,Z[(new t(e,0,1)).getDay()])}function k(){var e,n=this.getFullYear();return this>=C.call(new t(n+1,0,1))?1:(e=Math.floor((E.call(this)-T.call(this)+10)/7),e==0?L.call(new t(n-1,0,1))-v(this):e)}function L(){return Math.round((C.call(new t(this.getFullYear()+1,0,1))-C.call(this))/st)}function A(){var e=this.getMonth();return G.setLeapYear(this),new t(this.getFullYear(),e,G.day_count[e])}function O(){return Math.floor(E.call(this)/7)}function M(){return(new t(this.getFullYear(),0,1)).getTimezoneOffset()!=this.getTimezoneOffset()}function _(){return G.isLeapYear(this.getFullYear())}function D(e){return this.setMonth(0),this.setDate(1),this.adjust(t.DAY,e*7).getTime()}function P(){var e=this.toString().split(" ");return e.splice(4,e.length).join(" ").replace(zt,"$1").replace(Wt,"")}function H(e){if(Ct[e])return Ct[e];var t=["var out=[];"],n=-1,r,i=e.replace(jt,xt).replace(Bt,Tt).split(Ut),s=/^[^A-Za-z]*$/g,o=i.length;while(++n<o)r=i[n],r==St?(t.push(F(i[++n])),++n):s.test(r)?t.push(F(r)):t.push(j(r));return t.push('return out.join( "" );\n//@ sourceURL=d8/format/'+e),Ct[e]=new Function("filter","date",t.join("\n"))}function B(e){return H(e)(Mt,this)}function j(e){return e.replace(Ft,function(e,t,n,r){return F(t+"', filter."+n+"( date ), '"+r)})}function F(e){return"out.push( '"+e+"' );"}function I(t){if(kt[t])return kt[t];var n={},r=[],i=-1,s,u=t.replace(jt,xt).replace(Bt,Tt).split(Ut),a=u.length,l=[];while(++i<a){s=u[i];if(s==St){l.push(u[++i]),++i;continue}s.replace(Ft,function(t,i,s,u){var a,c,h;if(!(h=Vt[s]))return;h.k&&(r.push(h.k),h.fn&&(n[h.k]=h.fn)),h.combo&&(c=f(h.combo,"k"),a=o(f(h.combo,"fn"),c),r.push.apply(r,c),e.copy(n,a,!0)),h.re&&l.push(i,h.re,u)})}return kt[t]=q.bind(null,new RegExp(l.join("")),r,n)}function q(e,n,r,i){var s=new t,u=i.match(e),a=o(u.slice(1),n);return Object.reduce(a,function(e,t,n){return typeof t=="string"&&r[n]&&(a[n]=r[n](t,a)),e},null),isNaN(a[bt])?(U(s,a[pt],a[vt],a[gt],a[dt]),R(s,a),z(s,a[yt])):s.setTime(a[bt]),s}function R(t,n){var r,i=-1,s,o,a;if(Lt.every(e.has.bind(null,n)))return;isNaN(n[Et])&&(n[Et]=t.getFullYear());if(isNaN(n[mt])){o=G.isLeapYear(n[Et])?1:0,a=G.ordinal_day_count[o],s=a.length,n[mt]=0,n[wt]&&!n[ht]&&(r=n[ct],r=isNaN(r)?0:r?r:7,n[ht]=n[wt]*7-(4-r));if(!isNaN(n[ht])){n[ht]>a[a.length-1]&&(n[ht]-=a[a.length-1],++n[Et]);while(++i<s)if(u(n[ht],a[i],a[i+1])){n[mt]=i,n[lt]=a[i]==0?n[ht]:n[ht]-a[i];break}}}isNaN(n[lt])&&(n[lt]=1),t.setYear(n[Et]),t.setMonth(n[mt]),t.setDate(n[lt])}function U(e,t,n,r,i){e.setHours(t||0),e.setMinutes(n||0),e.setSeconds(r||0),e.setMilliseconds(i||0)}function z(e,n){!u(n,-43200,50400)||e.adjust(t.SECOND,-e.getTimezoneOffset()*60-n)}function W(e,t){return I(t)(e)}function X(t,n){switch(e.ntype(t)){case"string":if(!isNaN(+(new Date(t)))){t=new Date(t);break}if(n){t=Date.now();break};case"array":case"object":n=t,t=Date.now();break;case"date":if(!isNaN(+(new Date(t))))break;default:t=Date.now()}var r,i=t- +this,s=i<0?1:i>0?-1:0;return r=s===0?e.obj():$(Math.abs(i),J(n)),r.tense=s,r}function V(e,t,n,r){var i;return e.__ms__?e.excl[t[0]]?(!r[n+1]||e.excl[r[n+1][0]])&&(t=r[n-1])?(i=e.__ms__/t[1],e.__ms__=Math.round(i)*t[1]+e.__ms__/r[n][1]%1*r[n][1],V(e,t,n-1,[])):e:(e.__ms__>=t[1]&&(i=e.__ms__/t[1],t[0]in e.val?(i=Math.floor(i),e.__ms__-=i*t[1],e.val[t[0]]+=i):(e.__ms__=i%1*t[1],e.val[t[0]]=Math.floor(i))),e):e}function $(t,n){var r=At.reduce(V,{__ms__:t,excl:n,val:e.obj()}).val;return r.value=t,r}function J(t){var n=e.obj(),r=!0;if(t)switch(e.ntype(t)){case"object":r=!1;break;case"string":t=t.split(" ");case"array":t=t.reduce(K,n),r=!!e.len(n)}return Ot.map(function(e){e in this||(this[e]=!r)},n),n}function K(e,t){var n=(t=String(t).toLowerCase()).substring(1);switch(t.charAt(0)){case"-":e[n]=!0;break;case"+":e[n]=!1;break;case">":At.map(Q,{excl:e,prop:n,val:!0});break;case"<":At.slice().reverse().map(Q,{excl:e,prop:n,val:!1});break;default:e[t]=!1}return e}function Q(e){e[0]===this.prop&&(this.SET_VALID=!0),this.SET_VALID&&(this.excl[e[0]]=this.val)}var G=t.locale,Y,Z=[9,1,0,-1,-2,4,3],et=864e5,tt=36e5,nt=6e4,rt=2592e6,it=1e3,st=6048e5,ot=31536e6,ut=G.days.map(i),at=G.months.map(i),ft="ampm",lt="day",ct="dayweek",ht="dayyear",pt="hour",dt="ms",vt="minute",mt="month",gt="second",yt="timezone",bt="unix",wt="week",Et="year",St="NOREPLACE",xt="<"+St+"<",Tt=">END"+St+">",Nt={day:["getDate","setDate"],hr:["getHours","setHours"],min:["getMinutes","setMinutes"],month:["getMonth","setMonth"],ms:["getMilliseconds","setMilliseconds"],sec:["getSeconds","setSeconds"],week:["getWeek","setWeek"],year:["getFullYear","setFullYear"]},Ct={},kt={},Lt=[lt,ct,ht,mt,wt,Et],At=[[Et+"s",ot],[mt+"s",rt],[wt+"s",st],[lt+"s",et],[pt+"s",tt],[vt+"s",nt],[gt+"s",it],[dt,1]],Ot=At.map(function(e){return e[0]}),Mt={d:function(e){return a(e.getDate(),2)},D:function(e){return G.days[e.getDay()].substring(0,3)},j:function(e){return e.getDate()},l:function(e){return G.days[e.getDay()]},N:function(e){return T.call(e)},S:function(e){return G.getOrdinal(e.getDate())},w:function(e){return e.getDay()},z:function(e){return e.getDayOfYear()},W:function(e){return k.call(e)},F:function(e){return G.months[e.getMonth()]},m:function(e){return a(e.getMonth()+1,2)},M:function(e){return G.months[e.getMonth()].substring(0,3)},n:function(e){return e.getMonth()+1},t:function(e){return G.setLeapYear(e),G.day_count[e.getMonth()]},L:function(e){return e.isLeapYear()?1:0},o:function(e){var t=e.getMonth(),n=k.call(e);return e.getFullYear()+(n==1&&t>0?1:n>=52&&t<11?-1:0)},Y:function(e){return e.getFullYear()},y:function(e){return String(e.getFullYear()).substring(2,4)},a:function(e){return r(e.getHours()<12?G.AM:G.PM)},A:function(e){return s(e.getHours()<12?G.AM:G.PM)},g:function(e){return e.getHours()%12||12},G:function(e){return e.getHours()},h:function(e){return a(Mt.g(e),2)},H:function(e){return a(Mt.G(e),2)},i:function(e){return a(e.getMinutes(),2)},s:function(e){return a(e.getSeconds(),2)},u:function(e){return a(e.getMilliseconds(),3)},O:function(e){return x.call(e)},P:function(e){return x.call(e,!0)},T:function(e){return P.call(e)},Z:function(e){return e.getTimezoneOffset()*-60},c:function(e){return B.call(e,Dt.ISO_8601)},r:function(e){return B.call(e,Dt.RFC_2822)},U:function(e){return e.getTime()}},_t=Object.keys(Mt).sort().join(""),Dt=e.copy({ISO_8601:"Y-m-d<T>H:i:s.u<Z>",ISO_8601_SHORT:"Y-m-d",RFC_850:"l, d-M-y H:i:s T",RFC_2822:"D, d M Y H:i:s O",sortable:"Y-m-d H:i:sO"},G.formats),Pt,Ht="(am|pm)",Bt=/>/g,jt=/</g,Ft=new RegExp("([^"+_t+"]*)(["+_t+"])([^"+_t+"]*)","g"),It="([0-9]{1,2})",qt="([0-9]{2})",Rt="([0-9]{4})",Ut=/[<>]/,zt=/[^\(]*\(([^\)]+)\)/g,Wt=/[^A-Z]+/g,Xt=/[\+-]?([0-9]{2}):?([0-9]{2})/,Vt={d:{k:lt,fn:Number,re:qt},D:{k:ct,fn:n.bind(null,ut),re:"("+ut.join("|")+")"},j:{k:lt,fn:Number,re:It},l:{k:ct,fn:n.bind(null,G.days),re:"("+G.days.join("|")+")"},N:{k:ct,fn:m.bind(null,7),re:"([1-7])"},S:{re:"(?:"+G.ordinal.join("|")+")"},w:{k:ct,fn:Number,re:"([0-6])"},z:{k:ht,fn:Number,re:"([0-9]{1,3})"},W:{k:wt,fn:Number,re:qt},F:{k:mt,fn:n.bind(null,G.months),re:"("+G.months.join("|")+")"},m:{k:mt,fn:m,re:qt},M:{k:mt,fn:n.bind(null,at),re:"("+at.join("|")+")"},n:{k:mt,fn:m,re:It},t:{re:"[0-9]{2}"},L:{re:"(?:0|1)"},o:{k:Et,fn:Number,re:Rt},Y:{k:Et,fn:Number,re:Rt},y:{k:Et,fn:function(e){return e=Number(e),e+=e<30?2e3:1900},re:qt},a:{k:ft,fn:e,re:Ht},A:{k:ft,fn:r,re:s(Ht)},g:{k:pt,fn:c,re:It},G:{k:pt,fn:Number,re:It},h:{k:pt,fn:c,re:qt},H:{k:pt,fn:Number,re:qt},i:{k:vt,fn:Number,re:qt},s:{k:gt,fn:Number,re:qt},u:{k:dt,fn:Number,re:"([0-9]{1,})"},O:{k:yt,fn:d,re:"([\\+-][0-9]{4})"},P:{k:yt,fn:d,re:"([\\+-][0-9]{2}:[0-9]{2})"},T:{re:"[A-Z]{1,4}"},Z:{k:yt,fn:Number,re:"([\\+-]?[0-9]{5})"},U:{k:bt,fn:Number,re:"(-?[0-9]{1,})"}};Dt.atom=Dt.ISO_8601,Dt.cookie=Dt.RFC_850,Dt.rss=Dt.RFC_2822,Vt.c={combo:[Vt.Y,Vt.m,Vt.d,Vt.H,Vt.i,Vt.s,Vt.u,Vt.P],re:[Vt.Y.re,"-",Vt.m.re,"-",Vt.d.re,"T",Vt.H.re,":",Vt.i.re,":",Vt.s.re,"(?:\\.",Vt.u.re,"){0,1}",Vt.P.re,"{0,1}"].join("")},Vt.r={combo:[Vt.D,Vt.d,Vt.M,Vt.Y,Vt.H,Vt.i,Vt.s,Vt.O],re:[Vt.D.re,", ",Vt.d.re," ",Vt.M.re," ",Vt.Y.re," ",Vt.H.re,":",Vt.i.re,":",Vt.s.re," ",Vt.O.re].join("")},e.defs(t.prototype,{adjust:g,between:y,clearTime:b,clone:w,diff:X,format:B,getDayOfYear:E,getFirstOfTheMonth:S,getGMTOffset:x,getISODay:T,getISODaysInYear:N,getISOFirstMondayOfYear:C,getISOWeek:k,getISOWeeksInYear:L,getLastOfTheMonth:A,getWeek:O,isDST:M,isLeapYear:_,setWeek:D,timezone:P},"r"),e.defs(t,{DAY:lt,HOUR:"hr",MINUTE:vt.substring(0,3),MILLISECOND:dt,MONTH:mt,SECOND:gt.substring(0,3),WEEK:wt,YEAR:Et,MS_DAY:et,MS_HOUR:tt,MS_MINUTE:nt,MS_MONTH:rt,MS_SECOND:it,MS_WEEK:st,MS_YEAR:ot,filters:{value:Mt},formats:{value:Dt},parsers:{value:Vt},getOrdinal:G.getOrdinal,isLeapYear:G.isLeapYear,setLeapYear:G.setLeapYear,toDate:W},"r")}).x(Date)}(typeof m8!="undefined"?m8:typeof require!="undefined"?require("m8"):null)
View
6 src/diff.js
@@ -61,7 +61,7 @@
return ddiiff;
}
// round up or down depending on what's available
- if ( ( calc = calcs[i + 1] ) || ( calc = calcs[i - 1] ) ) {
+ if ( ( !calcs[i + 1] || ddiiff.excl[calcs[i + 1][0]] ) && ( calc = calcs[i - 1] ) ) {
time = ddiiff.__ms__ / calc[1];
ddiiff.__ms__ = ( Math.round( time ) * calc[1] ) + ( ( ( ddiiff.__ms__ / calcs[i][1] ) % 1 ) * calcs[i][1] );
return diff_eval( ddiiff, calc, i - 1, [] );
@@ -110,12 +110,10 @@
case '-' : excl[prop] = true; break;
case '+' : excl[prop] = false; break;
case '>' :
- diff_calc.map( diff_excl_iter, excl, { excl : excl, prop : prop, val : true } );
- delete excl.SET_VALID;
+ diff_calc.map( diff_excl_iter, { excl : excl, prop : prop, val : true } );
break;
case '<' :
diff_calc.slice().reverse().map( diff_excl_iter, { excl : excl, prop : prop, val : false } );
- delete excl.SET_VALID;
break;
default : excl[val] = false;
}
View
36 test/d8.test.js
@@ -217,7 +217,6 @@ suite( 'd8', function() {
expect( diff.ms ).to.eql( 99 );
diff = date_2.diff( date_1 );
- expect( diff.value ).to.eql( Math.abs( +date_2 - +date_1 ) );
expect( diff.tense ).to.eql( 1 );
expect( diff.years ).to.eql( 1 );
expect( diff.months ).to.eql( 1 );
@@ -236,7 +235,7 @@ suite( 'd8', function() {
date_1 = new Date( 2012, 11, 10, 9, 8, 7, 600 );
date_2 = date_1.clone().adjust( { year : 1, month : 1, week : 1, day : 1, hr : 1, min : 1, sec : 1, ms : 100 } );
- diff = date_1.diff( date_2, '-weeks' );
+ diff = date_1.diff( date_2, '-weeks >hours' );
expect( diff.value ).to.eql( Math.abs( +date_1 - +date_2 ) );
expect( diff.tense ).to.eql( -1 );
@@ -244,22 +243,33 @@ suite( 'd8', function() {
expect( diff.months ).to.eql( 1 );
expect( diff.weeks ).to.be.undefined;
expect( diff.days ).to.eql( 12 );
- expect( diff.hours ).to.eql( 1 );
- expect( diff.minutes ).to.eql( 1 );
- expect( diff.seconds ).to.eql( 1 );
- expect( diff.ms ).to.eql( 99 );
+ expect( diff.hours ).to.be.undefined;
+ expect( diff.minutes ).to.be.undefined;
+ expect( diff.seconds ).to.be.undefined;
+ expect( diff.ms ).to.be.undefined;
- diff = date_2.diff( date_1 );
- expect( diff.value ).to.eql( Math.abs( +date_2 - +date_1 ) );
+ diff = date_2.diff( date_1, '-weeks >minutes' );
expect( diff.tense ).to.eql( 1 );
expect( diff.years ).to.eql( 1 );
expect( diff.months ).to.eql( 1 );
- expect( diff.weeks ).to.eql( 1 );
- expect( diff.days ).to.eql( 5 );
+ expect( diff.weeks ).to.be.undefined;
+ expect( diff.days ).to.eql( 12 );
expect( diff.hours ).to.eql( 1 );
- expect( diff.minutes ).to.eql( 1 );
- expect( diff.seconds ).to.eql( 1 );
- expect( diff.ms ).to.eql( 99 );
+ expect( diff.minutes ).to.be.undefined;
+ expect( diff.seconds ).to.be.undefined;
+ expect( diff.ms ).to.be.undefined;
+
+ date_1 = new Date( 2012, 11, 10, 9, 8, 7, 600 );
+ date_2 = date_1.clone().adjust( { month : 11 } );
+ diff = date_1.diff( date_2, '>months' );
+
+ expect( diff.value ).to.eql( Math.abs( +date_2 - +date_1 ) );
+ expect( diff.tense ).to.eql( -1 );
+ expect( diff.years ).to.eql( 1 );
+
+ diff = date_1.diff( date_2, '-years -months -weeks' );
+ expect( diff.tense ).to.eql( -1 );
+ expect( diff.days ).to.eql( 370 );
done();
} );
Please sign in to comment.
Something went wrong with that request. Please try again.