]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Adding support for `_date.add('seconds', 2)`
authorTim Wood <washwithcare@gmail.com>
Fri, 14 Oct 2011 17:58:47 +0000 (10:58 -0700)
committerTim Wood <washwithcare@gmail.com>
Fri, 14 Oct 2011 17:58:47 +0000 (10:58 -0700)
Added more robustness to the `.add` function. Now you can do all of the
following:
    _date.add('s', 1);
    _date.add('seconds', 1);
    _date.add({s:1});
    _date.add({seconds:1});

test/date.js
underscore.date.js
underscore.date.min.js

index c1f6a5e0c29cd3b3a1d819568c19eaa5fe253bec..86d4a3813d41ba18657e168c684207bd8310337a 100755 (executable)
@@ -91,7 +91,7 @@ test("string with format - years", 2, function() {
 module("add and subtract");
 
 
-test("add and subtract", 11, function() {
+test("add and subtract short", 11, function() {
     var a = _date();
     a.year(2011);
     a.month(9);
@@ -117,6 +117,59 @@ test("add and subtract", 11, function() {
     equal(c.date(), 28, 'subtract month, feb 28th to jan 28th');
 });
 
+test("add and subtract long", 7, function() {
+    var a = _date();
+    a.year(2011);
+    a.month(9);
+    a.date(12);
+    a.hours(6);
+    a.minutes(7);
+    a.seconds(8);
+
+    equal(a.add({milliseconds:1000}).seconds(), 9, 'Add milliseconds');
+    equal(a.add({seconds:1}).seconds(), 10, 'Add seconds');
+    equal(a.add({minutes:1}).minutes(), 8, 'Add minutes');
+    equal(a.add({hours:1}).hours(), 7, 'Add hours');
+    equal(a.add({days:1}).date(), 13, 'Add date');
+    equal(a.add({months:1}).month(), 10, 'Add month');
+    equal(a.add({years:1}).year(), 2012, 'Add year');
+});
+
+test("add and subtract string short", 7, function() {
+    var a = _date();
+    a.year(2011);
+    a.month(9);
+    a.date(12);
+    a.hours(6);
+    a.minutes(7);
+    a.seconds(8);
+
+    equal(a.add('milliseconds', 1000).seconds(), 9, 'Add milliseconds');
+    equal(a.add('seconds', 1).seconds(), 10, 'Add seconds');
+    equal(a.add('minutes', 1).minutes(), 8, 'Add minutes');
+    equal(a.add('hours', 1).hours(), 7, 'Add hours');
+    equal(a.add('days', 1).date(), 13, 'Add date');
+    equal(a.add('months', 1).month(), 10, 'Add month');
+    equal(a.add('years', 1).year(), 2012, 'Add year');
+});
+
+test("add and subtract string short", 7, function() {
+    var a = _date();
+    a.year(2011);
+    a.month(9);
+    a.date(12);
+    a.hours(6);
+    a.minutes(7);
+    a.seconds(8);
+
+    equal(a.add('ms', 1000).seconds(), 9, 'Add milliseconds');
+    equal(a.add('s', 1).seconds(), 10, 'Add seconds');
+    equal(a.add('m', 1).minutes(), 8, 'Add minutes');
+    equal(a.add('h', 1).hours(), 7, 'Add hours');
+    equal(a.add('d', 1).date(), 13, 'Add date');
+    equal(a.add('M', 1).month(), 10, 'Add month');
+    equal(a.add('y', 1).year(), 2012, 'Add year');
+});
 
 module("diff");
 
index 7a738a7a516004768103bdf66b480f534e5735ea..2e08f12d015244dd77894b68d6ffdd254ffbca55 100644 (file)
     }
 
     // helper function for _.addTime and _.subtractTime
-    function dateAddRemove(date, input, adding) {
-        var ms = (input.ms || 0) +
-            (input.s  || 0) * 1e3 + // 1000
-            (input.m  || 0) * 6e4 + // 1000 * 60
-            (input.h  || 0) * 36e5 + // 1000 * 60 * 60
-            (input.d  || 0) * 864e5 + // 1000 * 60 * 60 * 24
-            (input.w  || 0) * 6048e5, // 1000 * 60 * 60 * 24 * 7
-            M = (input.M || 0) +
-            (input.y || 0) * 12,
-            currentDate;
+    function dateAddRemove(date, _input, adding, val) {
+        var isString = (typeof _input === 'string'),
+            input = isString ? {} : _input,
+            ms, M, currentDate;
+        if (isString && val) {
+            input[_input] = val;
+        }
+        ms = (input.ms || input.milliseconds || 0) +
+            (input.s || input.seconds || 0) * 1e3 + // 1000
+            (input.m || input.minutes || 0) * 6e4 + // 1000 * 60
+            (input.h || input.hours || 0) * 36e5 + // 1000 * 60 * 60
+            (input.d || input.days || 0) * 864e5 + // 1000 * 60 * 60 * 24
+            (input.w || input.weeks || 0) * 6048e5; // 1000 * 60 * 60 * 24 * 7
+        M = (input.M || input.months || 0) +
+            (input.y || input.years || 0) * 12;
         if (ms) {
             date.setMilliseconds(date.getMilliseconds() + ms * adding);
         }
             return formatDate(this._d, inputString);
         },
 
-        add : function (input) {
-            this._d = dateAddRemove(this._d, input, 1);
+        add : function (input, val) {
+            this._d = dateAddRemove(this._d, input, 1, val);
             return this;
         },
 
-        subtract : function (input) {
-            this._d = dateAddRemove(this._d, input, -1);
+        subtract : function (input, val) {
+            this._d = dateAddRemove(this._d, input, -1, val);
             return this;
         },
 
index 0359ac3132a257ed683bb4c59d76fb19951e2d15..aca93837f49c8dff09c6ea2e80412847e03cebdc 100755 (executable)
@@ -1 +1 @@
-(function(a,b){function j(a,b){var c=a+"";while(c.length<b)c="0"+c;return c}function k(b,c,d){var e=(c.ms||0)+(c.s||0)*1e3+(c.m||0)*6e4+(c.h||0)*36e5+(c.d||0)*864e5+(c.w||0)*6048e5,f=(c.M||0)+(c.y||0)*12,g;return e&&b.setMilliseconds(b.getMilliseconds()+e*d),f&&(g=b.getDate(),b.setDate(1),b.setMonth(b.getMonth()+f*d),b.setDate(Math.min((new a(b.getFullYear(),b.getMonth()+1,0)).getDate(),g))),b}function l(a){return Object.prototype.toString.call(a)==="[object Array]"}function m(b){return new a(b[0],b[1]||0,b[2]||1,b[3]||0,b[4]||0,b[5]||0,b[6]||0)}function n(b,d){function p(d){var m,q;switch(d){case"M":return e+1;case"Mo":return e+1+c.ordinal(e+1);case"MM":return j(e+1,2);case"MMM":return c.monthsShort[e];case"MMMM":return c.months[e];case"D":return f;case"Do":return f+c.ordinal(f);case"DD":return j(f,2);case"DDD":return m=new a(g,e,f),q=new a(g,0,1),~~((m-q)/864e5+1.5);case"DDDo":return m=p("DDD"),m+c.ordinal(m);case"DDDD":return j(p("DDD"),3);case"d":return h;case"do":return h+c.ordinal(h);case"ddd":return c.weekdaysShort[h];case"dddd":return c.weekdays[h];case"w":return m=new a(g,e,f-h+5),q=new a(m.getFullYear(),0,4),~~((m-q)/864e5/7+1.5);case"wo":return m=p("w"),m+c.ordinal(m);case"ww":return j(p("w"),2);case"YY":return(g+"").slice(-2);case"YYYY":return g;case"a":return i>11?"pm":"am";case"A":return i>11?"PM":"AM";case"H":return i;case"HH":return j(i,2);case"h":return i%12||12;case"hh":return j(i%12||12,2);case"m":return k;case"mm":return j(k,2);case"s":return l;case"ss":return j(l,2);case"zz":case"z":return(b.toString().match(o)||[""])[0].replace(n,"");default:return d.replace("\\","")}}var e=b.getMonth(),f=b.getDate(),g=b.getFullYear(),h=b.getDay(),i=b.getHours(),k=b.getMinutes(),l=b.getSeconds(),m=/(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|zz?)/g,n=/[^A-Z]/g,o=/\([A-Za-z ]+\)|:[0-9]{2} [A-Z]{3} /g;return d.replace(m,p)}function o(a,b){function i(a,b){switch(a){case"M":case"MM":c[1]=~~b-1;break;case"D":case"DD":case"DDD":case"DDDD":c[2]=~~b;break;case"YY":b=~~b,c[0]=b+(b>70?1900:2e3);break;case"YYYY":c[0]=~~b;break;case"a":case"A":h=b.toLowerCase()==="pm";break;case"H":case"HH":case"h":case"hh":c[3]=~~b;break;case"m":case"mm":c[4]=~~b;break;case"s":case"ss":c[5]=~~b}}var c=[0],d=/[0-9a-zA-Z]+/g,e=a.match(d),f=b.match(d),g,h;for(g=0;g<f.length;g++)i(f[g],e[g]);return h&&c[3]<12&&(c[3]+=12),m(c)}function p(a,b){var c=Math.min(a.length,b.length),d=Math.abs(a.length-b.length),e=0,f;for(f=0;f<c;f++)~~a[f]!==~~b[f]&&e++;return e+d}function q(a,b){var c,d=/[0-9a-zA-Z]+/g,e=a.match(d),f=[],g=99,h,i,j;for(h=0;h<b.length;h++)i=o(a,b[h]),j=p(e,n(i,b[h]).match(d)),j<g&&(g=j,c=i);return c}function r(c,d){c&&c._d instanceof a?this._d=c._d:d?l(d)?this._d=q(c,d):this._d=o(c,d):this._d=c===b?new a:c instanceof a?c:l(c)?m(c):new a(c)}function s(a,b){return c.relativeTime[a].replace(/%d/i,b||1)}function t(a){var b=Math.abs(a)/1e3,c=b/60,e=c/60,f=e/24,g=f/365;return b<45&&s("s",d(b))||d(c)===1&&s("m")||c<45&&s("mm",d(c))||d(e)===1&&s("h")||e<22&&s("hh",d(e))||d(f)===1&&s("d")||f<25&&s("dd",d(f))||f<45&&s("M")||f<345&&s("MM",d(f/30))||d(g)===1&&s("y")||s("yy",d(g))}function u(a,b){c.fn[a]=function(a){return a?(this._d["set"+b](a),this):this._d["get"+b]()}}var c,d=Math.round,e={},f=typeof window=="undefined"&&typeof module!="undefined",g="months|monthsShort|weekdays|weekdaysShort|relativeTime|ordinal".split("|"),h,i="Month|Date|Hours|Minutes|Seconds".split("|");c=function(a,b){return new r(a,b)},c.lang=function(a,b){var d,h,i;b&&(e[a]=b);if(e[a])for(d=0;d<g.length;d++)h=g[d],c[h]=e[a][h]||c[h];else f&&(i=require("./lang/"+a),c.lang(a,i))},c.lang("en",{months:"January_February_March_April_May_June_July_August_September_October_November_December".split("_"),monthsShort:"Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec".split("_"),weekdays:"Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),weekdaysShort:"Sun_Mon_Tue_Wed_Thu_Fri_Sat".split("_"),relativeTime:{future:"in %s",past:"%s ago",s:"seconds",m:"a minute",mm:"%d minutes",h:"an hour",hh:"%d hours",d:"a day",dd:"%d days",M:"a month",MM:"%d months",y:"a year",yy:"%d years"},ordinal:function(a){var b=a%10;return~~(a%100/10)===1?"th":b===1?"st":b===2?"nd":b===3?"rd":"th"}}),c.fn=r.prototype={valueOf:function(){return+this._d},format:function(a){return n(this._d,a)},add:function(a){return this._d=k(this._d,a,1),this},subtract:function(a){return this._d=k(this._d,a,-1),this},diff:function(a,b){return this._d-c(a,b)._d},from:function(a,b){var d=this.diff(a),e=d<0?c.relativeTime.past:c.relativeTime.future,f=t(d);return b?f:e.replace(/%s/i,f)},fromNow:function(a){return this.from(new r,a)},isLeapYear:function(){var a=this._d.getFullYear();return a%4===0&&a%100!==0||a%400===0}};for(h=0;h<i.length;h++)u(i[h].toLowerCase(),i[h]);u("year","FullYear"),c.fn.day=function(){return this._d.getDay()},f?module.exports=c:(this._!==b&&this._.mixin!==b&&this._.mixin({date:c}),this._date=c)})(Date)
\ No newline at end of file
+(function(a,b){function j(a,b){var c=a+"";while(c.length<b)c="0"+c;return c}function k(b,c,d,e){var f=typeof c=="string",g=f?{}:c,h,i,j;return f&&e&&(g[c]=e),h=(g.ms||g.milliseconds||0)+(g.s||g.seconds||0)*1e3+(g.m||g.minutes||0)*6e4+(g.h||g.hours||0)*36e5+(g.d||g.days||0)*864e5+(g.w||g.weeks||0)*6048e5,i=(g.M||g.months||0)+(g.y||g.years||0)*12,h&&b.setMilliseconds(b.getMilliseconds()+h*d),i&&(j=b.getDate(),b.setDate(1),b.setMonth(b.getMonth()+i*d),b.setDate(Math.min((new a(b.getFullYear(),b.getMonth()+1,0)).getDate(),j))),b}function l(a){return Object.prototype.toString.call(a)==="[object Array]"}function m(b){return new a(b[0],b[1]||0,b[2]||1,b[3]||0,b[4]||0,b[5]||0,b[6]||0)}function n(b,d){function p(d){var m,q;switch(d){case"M":return e+1;case"Mo":return e+1+c.ordinal(e+1);case"MM":return j(e+1,2);case"MMM":return c.monthsShort[e];case"MMMM":return c.months[e];case"D":return f;case"Do":return f+c.ordinal(f);case"DD":return j(f,2);case"DDD":return m=new a(g,e,f),q=new a(g,0,1),~~((m-q)/864e5+1.5);case"DDDo":return m=p("DDD"),m+c.ordinal(m);case"DDDD":return j(p("DDD"),3);case"d":return h;case"do":return h+c.ordinal(h);case"ddd":return c.weekdaysShort[h];case"dddd":return c.weekdays[h];case"w":return m=new a(g,e,f-h+5),q=new a(m.getFullYear(),0,4),~~((m-q)/864e5/7+1.5);case"wo":return m=p("w"),m+c.ordinal(m);case"ww":return j(p("w"),2);case"YY":return(g+"").slice(-2);case"YYYY":return g;case"a":return i>11?"pm":"am";case"A":return i>11?"PM":"AM";case"H":return i;case"HH":return j(i,2);case"h":return i%12||12;case"hh":return j(i%12||12,2);case"m":return k;case"mm":return j(k,2);case"s":return l;case"ss":return j(l,2);case"zz":case"z":return(b.toString().match(o)||[""])[0].replace(n,"");default:return d.replace("\\","")}}var e=b.getMonth(),f=b.getDate(),g=b.getFullYear(),h=b.getDay(),i=b.getHours(),k=b.getMinutes(),l=b.getSeconds(),m=/(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|zz?)/g,n=/[^A-Z]/g,o=/\([A-Za-z ]+\)|:[0-9]{2} [A-Z]{3} /g;return d.replace(m,p)}function o(a,b){function i(a,b){switch(a){case"M":case"MM":c[1]=~~b-1;break;case"D":case"DD":case"DDD":case"DDDD":c[2]=~~b;break;case"YY":b=~~b,c[0]=b+(b>70?1900:2e3);break;case"YYYY":c[0]=~~b;break;case"a":case"A":h=b.toLowerCase()==="pm";break;case"H":case"HH":case"h":case"hh":c[3]=~~b;break;case"m":case"mm":c[4]=~~b;break;case"s":case"ss":c[5]=~~b}}var c=[0],d=/[0-9a-zA-Z]+/g,e=a.match(d),f=b.match(d),g,h;for(g=0;g<f.length;g++)i(f[g],e[g]);return h&&c[3]<12&&(c[3]+=12),m(c)}function p(a,b){var c=Math.min(a.length,b.length),d=Math.abs(a.length-b.length),e=0,f;for(f=0;f<c;f++)~~a[f]!==~~b[f]&&e++;return e+d}function q(a,b){var c,d=/[0-9a-zA-Z]+/g,e=a.match(d),f=[],g=99,h,i,j;for(h=0;h<b.length;h++)i=o(a,b[h]),j=p(e,n(i,b[h]).match(d)),j<g&&(g=j,c=i);return c}function r(c,d){c&&c._d instanceof a?this._d=c._d:d?l(d)?this._d=q(c,d):this._d=o(c,d):this._d=c===b?new a:c instanceof a?c:l(c)?m(c):new a(c)}function s(a,b){return c.relativeTime[a].replace(/%d/i,b||1)}function t(a){var b=Math.abs(a)/1e3,c=b/60,e=c/60,f=e/24,g=f/365;return b<45&&s("s",d(b))||d(c)===1&&s("m")||c<45&&s("mm",d(c))||d(e)===1&&s("h")||e<22&&s("hh",d(e))||d(f)===1&&s("d")||f<25&&s("dd",d(f))||f<45&&s("M")||f<345&&s("MM",d(f/30))||d(g)===1&&s("y")||s("yy",d(g))}function u(a,b){c.fn[a]=function(a){return a?(this._d["set"+b](a),this):this._d["get"+b]()}}var c,d=Math.round,e={},f=typeof window=="undefined"&&typeof module!="undefined",g="months|monthsShort|weekdays|weekdaysShort|relativeTime|ordinal".split("|"),h,i="Month|Date|Hours|Minutes|Seconds".split("|");c=function(a,b){return new r(a,b)},c.lang=function(a,b){var d,h,i;b&&(e[a]=b);if(e[a])for(d=0;d<g.length;d++)h=g[d],c[h]=e[a][h]||c[h];else f&&(i=require("./lang/"+a),c.lang(a,i))},c.lang("en",{months:"January_February_March_April_May_June_July_August_September_October_November_December".split("_"),monthsShort:"Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec".split("_"),weekdays:"Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),weekdaysShort:"Sun_Mon_Tue_Wed_Thu_Fri_Sat".split("_"),relativeTime:{future:"in %s",past:"%s ago",s:"seconds",m:"a minute",mm:"%d minutes",h:"an hour",hh:"%d hours",d:"a day",dd:"%d days",M:"a month",MM:"%d months",y:"a year",yy:"%d years"},ordinal:function(a){var b=a%10;return~~(a%100/10)===1?"th":b===1?"st":b===2?"nd":b===3?"rd":"th"}}),c.fn=r.prototype={valueOf:function(){return+this._d},format:function(a){return n(this._d,a)},add:function(a,b){return this._d=k(this._d,a,1,b),this},subtract:function(a,b){return this._d=k(this._d,a,-1,b),this},diff:function(a,b){return this._d-c(a,b)._d},from:function(a,b){var d=this.diff(a),e=d<0?c.relativeTime.past:c.relativeTime.future,f=t(d);return b?f:e.replace(/%s/i,f)},fromNow:function(a){return this.from(new r,a)},isLeapYear:function(){var a=this._d.getFullYear();return a%4===0&&a%100!==0||a%400===0}};for(h=0;h<i.length;h++)u(i[h].toLowerCase(),i[h]);u("year","FullYear"),c.fn.day=function(){return this._d.getDay()},f?module.exports=c:(this._!==b&&this._.mixin!==b&&this._.mixin({date:c}),this._date=c)})(Date)
\ No newline at end of file