]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
deprecate pre-2.0 add/subtract
authorIsaac Cambron <isaac@isaaccambron.com>
Sun, 6 Jul 2014 07:43:11 +0000 (03:43 -0400)
committerIsaac Cambron <isaac@isaaccambron.com>
Sun, 6 Jul 2014 07:43:11 +0000 (03:43 -0400)
moment.js
test/lang/az.js
test/lang/tr.js
test/moment/add_subtract.js
test/moment/lang.js
test/moment/preparse_postformat.js
test/moment/relative_time.js
test/moment/zones.js

index a7cbd43c8ceb2363d3fb98ca6647683bf2d8e6ab..4a5e6b55d98408805eab73f24b44e9ddc238f244 100644 (file)
--- a/moment.js
+++ b/moment.js
             }
         },
 
+        deprecations = {},
+
         lists = ['months', 'monthsShort', 'weekdays', 'weekdaysShort', 'weekdaysMin'];
 
     // Pick the first defined of two or three arguments. dfl comes from
         };
     }
 
+    function printMsg(msg) {
+        if (moment.suppressDeprecationWarnings === false &&
+                typeof console !== 'undefined' && console.warn) {
+            console.warn("Deprecation warning: " + msg);
+        }
+    }
+
     function deprecate(msg, fn) {
         var firstTime = true;
-        function printMsg() {
-            if (moment.suppressDeprecationWarnings === false &&
-                    typeof console !== 'undefined' && console.warn) {
-                console.warn("Deprecation warning: " + msg);
-            }
-        }
         return extend(function () {
             if (firstTime) {
-                printMsg();
+                printMsg(msg);
                 firstTime = false;
             }
             return fn.apply(this, arguments);
         }, fn);
     }
 
+    function deprecateSimple(name, msg) {
+        if (!deprecations[name]) {
+            printMsg(msg);
+            deprecations[name] = true;
+        }
+    }
+
     function padToken(func, count) {
         return function (a) {
             return leftZeroFill(func.call(this, a), count);
         return res;
     }
 
-    // helper function for _.addTime and _.subtractTime
+    //todo: remove 'name' arg after deprecation is removed
+    function createAdder(direction, name) {
+        return function (val, period) {
+            var dur, tmp;
+            //invert the arguments, but complain about it
+            if (period !== null && !isNaN(+period)) {
+                deprecateSimple(name, "moment()." + name  + "(period, number) is deprecated. Please use moment()." + name + "(number, period).");
+                tmp = val; val = period; period = tmp;
+            }
+
+            val = typeof val === 'string' ? +val : val;
+            dur = moment.duration(val, period);
+            addOrSubtractDurationFromMoment(this, dur, direction);
+            return this;
+        };
+    }
+
     function addOrSubtractDurationFromMoment(mom, duration, isAdding, updateOffset) {
         var milliseconds = duration._milliseconds,
             days = duration._days,
             daysToDayOfWeek += 7;
         }
 
-        adjustedMoment = moment(mom).add('d', daysToDayOfWeek);
+        adjustedMoment = moment(mom).add(daysToDayOfWeek, 'd');
         return {
             week: Math.ceil(adjustedMoment.dayOfYear() / 7),
             year: adjustedMoment.year()
             return this.lang().postformat(output);
         },
 
-        add : function (input, val) {
-            var dur;
-            // switch args to support add('s', 1) and add(1, 's')
-            if (typeof input === 'string' && typeof val === 'string') {
-                dur = moment.duration(isNaN(+val) ? +input : +val, isNaN(+val) ? val : input);
-            } else if (typeof input === 'string') {
-                dur = moment.duration(+val, input);
-            } else {
-                dur = moment.duration(input, val);
-            }
-            addOrSubtractDurationFromMoment(this, dur, 1);
-            return this;
-        },
+        add : createAdder(1, 'add'),
 
-        subtract : function (input, val) {
-            var dur;
-            // switch args to support subtract('s', 1) and subtract(1, 's')
-            if (typeof input === 'string' && typeof val === 'string') {
-                dur = moment.duration(isNaN(+val) ? +input : +val, isNaN(+val) ? val : input);
-            } else if (typeof input === 'string') {
-                dur = moment.duration(+val, input);
-            } else {
-                dur = moment.duration(input, val);
-            }
-            addOrSubtractDurationFromMoment(this, dur, -1);
-            return this;
-        },
+        subtract : createAdder(-1, 'subtract'),
 
         diff : function (input, units, asFloat) {
             var that = makeAs(input, this),
             var day = this._isUTC ? this._d.getUTCDay() : this._d.getDay();
             if (input != null) {
                 input = parseWeekday(input, this.lang());
-                return this.add(input - day, 'days');
+                return this.add(input - day, 'd');
             } else {
                 return day;
             }
 
         month : makeAccessor('Month', true),
 
-        startOf: function (units) {
+        startOf : function (units) {
             units = normalizeUnits(units);
             // the following switch intentionally omits break keywords
             // to utilize falling through the cases.
 
         endOf: function (units) {
             units = normalizeUnits(units);
-            return this.startOf(units).add((units === 'isoWeek' ? 'week' : units), 1).subtract('ms', 1);
+            return this.startOf(units).add(1, (units === 'isoWeek' ? 'week' : units)).subtract(1, 'ms');
         },
 
         isAfter: function (input, units) {
 
         dayOfYear : function (input) {
             var dayOfYear = round((moment(this).startOf('day') - moment(this).startOf('year')) / 864e5) + 1;
-            return input == null ? dayOfYear : this.add("d", (input - dayOfYear));
+            return input == null ? dayOfYear : this.add((input - dayOfYear), 'd');
         },
 
         quarter : function (input) {
 
         weekYear : function (input) {
             var year = weekOfYear(this, this.lang()._week.dow, this.lang()._week.doy).year;
-            return input == null ? year : this.add("y", (input - year));
+            return input == null ? year : this.add((input - year), 'y');
         },
 
         isoWeekYear : function (input) {
             var year = weekOfYear(this, 1, 4).year;
-            return input == null ? year : this.add("y", (input - year));
+            return input == null ? year : this.add((input - year), 'y');
         },
 
         week : function (input) {
             var week = this.lang().week(this);
-            return input == null ? week : this.add("d", (input - week) * 7);
+            return input == null ? week : this.add((input - week) * 7, 'd');
         },
 
         isoWeek : function (input) {
             var week = weekOfYear(this, 1, 4).week;
-            return input == null ? week : this.add("d", (input - week) * 7);
+            return input == null ? week : this.add((input - week) * 7, 'd');
         },
 
         weekday : function (input) {
             var weekday = (this.day() + 7 - this.lang()._week.dow) % 7;
-            return input == null ? weekday : this.add("d", input - weekday);
+            return input == null ? weekday : this.add(input - weekday, 'd');
         },
 
         isoWeekday : function (input) {
index 11bbf14bbdb9bd6c2245ed078890eab65adc93d6..d9c5cf9a4fb3f31061a36a26469d26fa18164115 100644 (file)
@@ -80,7 +80,7 @@ exports["lang:az"] = {
         }
         for (i = 0; i < DDDo.length; i++) {
             DDDoDt = moment([2010]);
-            test.equal(DDDoDt.add('days', DDDo[i][0]).format('DDDo'), DDDo[i][1], DDDo[i][0] + ' ---> ' + DDDo[i][1]);
+            test.equal(DDDoDt.add(DDDo[i][0], 'days').format('DDDo'), DDDo[i][1], DDDo[i][0] + ' ---> ' + DDDo[i][1]);
         }
         test.done();
     },
index bfe4208e85447063e065e9e97a2378efb916f0dc..069efef25fee9af487ec85442d63b2c85992e0d0 100644 (file)
@@ -77,7 +77,7 @@ exports["lang:tr"] = {
         }
         for (i = 0; i < DDDo.length; i++) {
             DDDoDt = moment([2010]);
-            test.equal(DDDoDt.add('days', DDDo[i][0]).format('DDDo'), DDDo[i][1], DDDo[i][0] + ' ---> ' + DDDo[i][1]);
+            test.equal(DDDoDt.add(DDDo[i][0], 'days').format('DDDo'), DDDo[i][1], DDDo[i][0] + ' ---> ' + DDDo[i][1]);
         }
         test.done();
     },
index 2bc32a15bbd8cdc62a06395be7e012ec0b616208..e1147d32b584006c2b220f3002fc3f079cdb2517 100644 (file)
@@ -8,7 +8,7 @@ exports.add = {
         done();
     },
 
-    "add short" : function (test) {
+    "add short reverse args" : function (test) {
         test.expect(16);
 
         var a = moment(), b, c, d;
@@ -44,7 +44,7 @@ exports.add = {
         test.done();
     },
 
-    "add long" : function (test) {
+    "add long reverse args" : function (test) {
         test.expect(9);
 
         var a = moment();
@@ -68,7 +68,7 @@ exports.add = {
         test.done();
     },
 
-    "add long singular" : function (test) {
+    "add long singular reverse args" : function (test) {
         test.expect(9);
 
         var a = moment();
@@ -92,7 +92,7 @@ exports.add = {
         test.done();
     },
 
-    "add string long" : function (test) {
+    "add string long reverse args" : function (test) {
         test.expect(10);
 
         var a = moment(), b;
@@ -119,7 +119,7 @@ exports.add = {
         test.done();
     },
 
-    "add string long singular" : function (test) {
+    "add string long singular reverse args" : function (test) {
         test.expect(10);
 
         var a = moment(), b;
@@ -146,7 +146,7 @@ exports.add = {
         test.done();
     },
 
-    "add string short" : function (test) {
+    "add string short reverse args" : function (test) {
         test.expect(9);
 
         var a = moment();
@@ -170,7 +170,7 @@ exports.add = {
         test.done();
     },
 
-    "add string long reverse args" : function (test) {
+    "add string long" : function (test) {
         test.expect(9);
 
         var a = moment();
@@ -194,7 +194,7 @@ exports.add = {
         test.done();
     },
 
-    "add string long singular reverse args" : function (test) {
+    "add string long singular" : function (test) {
         test.expect(9);
 
         var a = moment();
@@ -218,7 +218,7 @@ exports.add = {
         test.done();
     },
 
-    "add string short reverse args" : function (test) {
+    "add string short" : function (test) {
         test.expect(9);
 
         var a = moment();
@@ -290,7 +290,7 @@ exports.add = {
         test.done();
     },
 
-    "add strings string short reverse args" : function (test) {
+    "add strings string short" : function (test) {
         test.expect(9);
 
         var a = moment();
@@ -314,7 +314,7 @@ exports.add = {
         test.done();
     },
 
-    "subtract strings string short reverse args" : function (test) {
+    "subtract strings string short" : function (test) {
         test.expect(9);
 
         var a = moment();
@@ -351,10 +351,10 @@ exports.add = {
             c = moment(new Date(2011, 2, 12, 5, 0, 0)),
             d = moment(new Date(2011, 2, 12, 5, 0, 0)),
             e = moment(new Date(2011, 2, 12, 5, 0, 0));
-        a.add('days', 1);
-        b.add('hours', 24);
-        c.add('months', 1);
-        e.add('quarter', 1);
+        a.add(1, 'days');
+        b.add(24, 'hours');
+        c.add(1, 'months');
+        e.add(1, 'quarter');
         test.equal(a.hours(), 5, 'adding days over DST difference should result in the same hour');
         if (b.isDST() && !d.isDST()) {
             test.equal(b.hours(), 6, 'adding hours over DST difference should result in a different hour');
index a0e32d5a988322ee558ccdf01595236c5ec3c773..84aa5ba62bce70e495615b6401ee69b1d082d4d1 100644 (file)
@@ -103,10 +103,10 @@ exports.lang = {
         moment.lang('es');
 
         test.equal(moment().lang(lang).calendar(), "sameDay -LT-", "Should use instance lang in LT formatting");
-        test.equal(moment().add('days', 1).lang(lang).calendar(), "nextDay -L-", "Should use instance lang in L formatting");
-        test.equal(moment().add('days', -1).lang(lang).calendar(), "lastDay -LLL-", "Should use instance lang in LL formatting");
-        test.equal(moment().add('days', 4).lang(lang).calendar(), "nextWeek -LL-", "Should use instance lang in LLL formatting");
-        test.equal(moment().add('days', -4).lang(lang).calendar(), "lastWeek -LLLL-", "Should use instance lang in LLLL formatting");
+        test.equal(moment().add(1, 'days').lang(lang).calendar(), "nextDay -L-", "Should use instance lang in L formatting");
+        test.equal(moment().add(-1, 'days').lang(lang).calendar(), "lastDay -LLL-", "Should use instance lang in LL formatting");
+        test.equal(moment().add(4, 'days').lang(lang).calendar(), "nextWeek -LL-", "Should use instance lang in LLL formatting");
+        test.equal(moment().add(-4, 'days').lang(lang).calendar(), "lastWeek -LLLL-", "Should use instance lang in LLLL formatting");
 
         test.done();
     },
index 3dd0841f25967eff14acc90e9a3cad30d4414408..4542ef25baf38a87cca7628c6c4d16ac2c3e70ce 100644 (file)
@@ -71,7 +71,7 @@ exports.preparsePostformat = {
         var start = moment([2007, 1, 28]);
 
         test.equal(start.from(moment([2007, 1, 28]).add({s: 90}), true), "@ minutes", "postformat should work on moment.fn.from");
-        test.equal(moment().add('d', 6).fromNow(true), "^ days", "postformat should work on moment.fn.fromNow");
+        test.equal(moment().add(6, 'd').fromNow(true), "^ days", "postformat should work on moment.fn.fromNow");
         test.equal(moment.duration(10, "h").humanize(), "!) hours", "postformat should work on moment.duration.fn.humanize");
 
         test.done();
index 23258733bffe8d6fd83696da864c7174bd87a5b0..7191be1ed8789d62a6f6be04acae03ceb0e161c0 100644 (file)
@@ -5,30 +5,30 @@ exports.relativeTime = {
         var a = moment();
 
         // Seconds to minutes threshold
-        a.subtract('seconds', 44);
+        a.subtract(44, 'seconds');
         test.equal(a.fromNow(), "a few seconds ago", "Below default seconds to minutes threshold");
-        a.subtract('seconds', 1);
+        a.subtract(1, 'seconds');
         test.equal(a.fromNow(), "a minute ago", "Above default seconds to minutes threshold");
 
         // Minutes to hours threshold
         a = moment();
-        a.subtract('minutes', 44);
+        a.subtract(44, 'minutes');
         test.equal(a.fromNow(), "44 minutes ago", "Below default minute to hour threshold");
-        a.subtract('minutes', 1);
+        a.subtract(1, 'minutes');
         test.equal(a.fromNow(), "an hour ago", "Above default minute to hour threshold");
 
         // Hours to days threshold
         a = moment();
-        a.subtract('hours', 21);
+        a.subtract(21, 'hours');
         test.equal(a.fromNow(), "21 hours ago", "Below default hours to day threshold");
-        a.subtract('hours', 1);
+        a.subtract(1, 'hours');
         test.equal(a.fromNow(), "a day ago", "Above default hours to day threshold");
 
         // Days to month threshold
         a = moment();
-        a.subtract('days', 25);
+        a.subtract(25, 'days');
         test.equal(a.fromNow(), "25 days ago", "Below default days to month (singular) threshold");
-        a.subtract('days', 1);
+        a.subtract(1, 'days');
         test.equal(a.fromNow(), "a month ago", "Above default days to month (singular) threshold");
 
         // months to year threshold
@@ -46,9 +46,9 @@ exports.relativeTime = {
         moment.relativeTimeThreshold('s', 55);
 
         var a = moment();
-        a.subtract('seconds', 54);
+        a.subtract(54, 'seconds');
         test.equal(a.fromNow(), "a few seconds ago", "Below custom seconds to minutes threshold");
-        a.subtract('seconds', 1);
+        a.subtract(1, 'seconds');
         test.equal(a.fromNow(), "a minute ago", "Above custom seconds to minutes threshold");
 
         moment.relativeTimeThreshold('s', 45);
@@ -56,36 +56,36 @@ exports.relativeTime = {
         // Minutes to hours threshold
         moment.relativeTimeThreshold('m', 55);
         a = moment();
-        a.subtract('minutes', 54);
+        a.subtract(54, 'minutes');
         test.equal(a.fromNow(), "54 minutes ago", "Below custom minutes to hours threshold");
-        a.subtract('minutes', 1);
+        a.subtract(1, 'minutes');
         test.equal(a.fromNow(), "an hour ago", "Above custom minutes to hours threshold");
         moment.relativeTimeThreshold('m', 45);
 
         // Hours to days threshold
         moment.relativeTimeThreshold('h', 24);
         a = moment();
-        a.subtract('hours', 23);
+        a.subtract(23, 'hours');
         test.equal(a.fromNow(), "23 hours ago", "Below custom hours to days threshold");
-        a.subtract('hours', 1);
+        a.subtract(1, 'hours');
         test.equal(a.fromNow(), "a day ago", "Above custom hours to days threshold");
         moment.relativeTimeThreshold('h', 22);
 
         // Days to month threshold
         moment.relativeTimeThreshold('d', 28);
         a = moment();
-        a.subtract('days', 27);
+        a.subtract(27, 'days');
         test.equal(a.fromNow(), "27 days ago", "Below custom days to month (singular) threshold");
-        a.subtract('days', 1);
+        a.subtract(1, 'days');
         test.equal(a.fromNow(), "a month ago", "Above custom days to month (singular) threshold");
         moment.relativeTimeThreshold('d', 26);
 
         // months to years threshold
         moment.relativeTimeThreshold('M', 9);
         a = moment();
-        a.subtract('months', 8);
+        a.subtract(8, 'months');
         test.equal(a.fromNow(), "8 months ago", "Below custom days to years threshold");
-        a.subtract('months', 1);
+        a.subtract(1, 'months');
         test.equal(a.fromNow(), "a year ago", "Above custom days to years threshold");
         moment.relativeTimeThreshold('M', 11);
         test.done();
index dc5a43f303b24226f4cce2d7c7183ff09e8a5644..0f6ed92531789b9b20bda91c969e1f88945a4aed 100644 (file)
@@ -145,12 +145,12 @@ exports.zones = {
         test.equal(m.format("HH:mm"), "00:00", "should start 12AM at +0000 timezone");
 
         m.__doChange = true;
-        m.add('h', 1);
+        m.add(1, 'h');
 
         test.equal(m.format("ZZ"), "-0200", "should be at -0200");
         test.equal(m.format("HH:mm"), "23:00", "1AM at +0000 should be 11PM at -0200 timezone");
 
-        m.subtract('h', 1);
+        m.subtract(1, 'h');
 
         test.equal(m.format("ZZ"), "-0100", "should be at -0100");
         test.equal(m.format("HH:mm"), "23:00", "12AM at +0000 should be 11PM at -0100 timezone");
@@ -202,7 +202,7 @@ exports.zones = {
             zoneB = moment(zoneA).zone(720),
             zoneC = moment(zoneA).zone(360),
             zoneD = moment(zoneA).zone(-690),
-            other = moment(zoneA).add('m', 35);
+            other = moment(zoneA).add(35, 'm');
 
         test.equal(zoneA.from(other), zoneB.from(other), "moment#from should be the same in all zones");
         test.equal(zoneA.from(other), zoneC.from(other), "moment#from should be the same in all zones");
@@ -216,7 +216,7 @@ exports.zones = {
             zoneB = moment(zoneA).zone(720),
             zoneC = moment(zoneA).zone(360),
             zoneD = moment(zoneA).zone(-690),
-            other = moment(zoneA).add('m', 35);
+            other = moment(zoneA).add(35, 'm');
 
         test.equal(zoneA.diff(other), zoneB.diff(other), "moment#diff should be the same in all zones");
         test.equal(zoneA.diff(other), zoneC.diff(other), "moment#diff should be the same in all zones");
@@ -314,7 +314,7 @@ exports.zones = {
         test.ok(zoneA.isSame(zoneB, 'hour'), "two moments with different offsets should be the same hour");
         test.ok(zoneA.isSame(zoneC, 'hour'), "two moments with different offsets should be the same hour");
 
-        zoneA.add('hour', 1);
+        zoneA.add(1, 'hour');
 
         test.ok(zoneA.isAfter(zoneB), "isAfter should work with two moments with different offsets");
         test.ok(zoneA.isAfter(zoneC), "isAfter should work with two moments with different offsets");
@@ -322,7 +322,7 @@ exports.zones = {
         test.ok(zoneA.isAfter(zoneB, 'hour'), "isAfter:hour should work with two moments with different offsets");
         test.ok(zoneA.isAfter(zoneC, 'hour'), "isAfter:hour should work with two moments with different offsets");
 
-        zoneA.subtract('hour', 2);
+        zoneA.subtract(2, 'hour');
 
         test.ok(zoneA.isBefore(zoneB), "isBefore should work with two moments with different offsets");
         test.ok(zoneA.isBefore(zoneC), "isBefore should work with two moments with different offsets");
@@ -347,27 +347,27 @@ exports.zones = {
 
         test.equal(m.hour(), 3, "should start at 00:00");
 
-        m.add('hour', 24);
+        m.add(24, 'hour');
 
         test.equal(m.hour(), 4, "adding 24 hours should disregard dst");
 
-        m.subtract('hour', 24);
+        m.subtract(24, 'hour');
 
         test.equal(m.hour(), 3, "subtracting 24 hours should disregard dst");
 
-        m.add('day', 1);
+        m.add(1, 'day');
 
         test.equal(m.hour(), 3, "adding 1 day should have the same hour");
 
-        m.subtract('day', 1);
+        m.subtract(1, 'day');
 
         test.equal(m.hour(), 3, "subtracting 1 day should have the same hour");
 
-        m.add('month', 1);
+        m.add(1, 'month');
 
         test.equal(m.hour(), 3, "adding 1 month should have the same hour");
 
-        m.subtract('month', 1);
+        m.subtract(1, 'month');
 
         test.equal(m.hour(), 3, "subtracting 1 month should have the same hour");