From fbbd3ebfbce8326dfc5d3203e54ad96c1ce923ed Mon Sep 17 00:00:00 2001 From: Isaac Cambron Date: Sun, 6 Jul 2014 03:43:11 -0400 Subject: [PATCH] deprecate pre-2.0 add/subtract --- moment.js | 90 +++++++++++++++--------------- test/lang/az.js | 2 +- test/lang/tr.js | 2 +- test/moment/add_subtract.js | 30 +++++----- test/moment/lang.js | 8 +-- test/moment/preparse_postformat.js | 2 +- test/moment/relative_time.js | 36 ++++++------ test/moment/zones.js | 24 ++++---- 8 files changed, 98 insertions(+), 96 deletions(-) diff --git a/moment.js b/moment.js index a7cbd43c8..4a5e6b55d 100644 --- a/moment.js +++ b/moment.js @@ -290,6 +290,8 @@ } }, + deprecations = {}, + lists = ['months', 'monthsShort', 'weekdays', 'weekdaysShort', 'weekdaysMin']; // Pick the first defined of two or three arguments. dfl comes from @@ -319,23 +321,31 @@ }; } + 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); @@ -487,7 +497,23 @@ 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, @@ -1651,7 +1677,7 @@ daysToDayOfWeek += 7; } - adjustedMoment = moment(mom).add('d', daysToDayOfWeek); + adjustedMoment = moment(mom).add(daysToDayOfWeek, 'd'); return { week: Math.ceil(adjustedMoment.dayOfYear() / 7), year: adjustedMoment.year() @@ -2057,33 +2083,9 @@ 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), @@ -2155,7 +2157,7 @@ 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; } @@ -2163,7 +2165,7 @@ 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. @@ -2208,7 +2210,7 @@ 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) { @@ -2320,7 +2322,7 @@ 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) { @@ -2329,27 +2331,27 @@ 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) { diff --git a/test/lang/az.js b/test/lang/az.js index 11bbf14bb..d9c5cf9a4 100644 --- a/test/lang/az.js +++ b/test/lang/az.js @@ -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(); }, diff --git a/test/lang/tr.js b/test/lang/tr.js index bfe4208e8..069efef25 100644 --- a/test/lang/tr.js +++ b/test/lang/tr.js @@ -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(); }, diff --git a/test/moment/add_subtract.js b/test/moment/add_subtract.js index 2bc32a15b..e1147d32b 100644 --- a/test/moment/add_subtract.js +++ b/test/moment/add_subtract.js @@ -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'); diff --git a/test/moment/lang.js b/test/moment/lang.js index a0e32d5a9..84aa5ba62 100644 --- a/test/moment/lang.js +++ b/test/moment/lang.js @@ -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(); }, diff --git a/test/moment/preparse_postformat.js b/test/moment/preparse_postformat.js index 3dd0841f2..4542ef25b 100644 --- a/test/moment/preparse_postformat.js +++ b/test/moment/preparse_postformat.js @@ -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(); diff --git a/test/moment/relative_time.js b/test/moment/relative_time.js index 23258733b..7191be1ed 100644 --- a/test/moment/relative_time.js +++ b/test/moment/relative_time.js @@ -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(); diff --git a/test/moment/zones.js b/test/moment/zones.js index dc5a43f30..0f6ed9253 100644 --- a/test/moment/zones.js +++ b/test/moment/zones.js @@ -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"); -- 2.47.2