From: Rocky Meza Date: Sun, 15 Apr 2012 06:20:21 +0000 (-0600) Subject: Added getters to the Duration prototype. X-Git-Tag: 1.6.0~1^2~6^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a5bc5577ce220a97d0ebf4a9fc8914b671183266;p=thirdparty%2Fmoment.git Added getters to the Duration prototype. These can serve as encapsulation to protect from future possible changes in internal representation. --- diff --git a/moment.js b/moment.js index 4b6413aeb..476b29845 100644 --- a/moment.js +++ b/moment.js @@ -27,7 +27,8 @@ ], timezoneParseRegex = /([\+\-]|\d\d)/gi, VERSION = "1.5.0", - shortcuts = 'Month|Date|Hours|Minutes|Seconds|Milliseconds'.split('|'); + shortcuts = 'Month|Date|Hours|Minutes|Seconds|Milliseconds'.split('|'), + durationGetters = 'years|months|weeks|days|hours|minutes|seconds|milliseconds'.split('|'); // Moment prototype object function Moment(date, isUTC) { @@ -37,14 +38,15 @@ // Duration Constructor function Duration(duration) { - this.years = duration.years || duration.y || 0; - this.months = duration.months || duration.M || 0; - this.weeks = duration.weeks || duration.w || 0; - this.days = duration.days || duration.d || 0; - this.hours = duration.hours || duration.h || 0; - this.minutes = duration.minutes || duration.m || 0; - this.seconds = duration.seconds || duration.s || 0; - this.milliseconds = duration.milliseconds || duration.ms || 0; + var data = this._data = {}; + data.years = duration.years || duration.y || 0; + data.months = duration.months || duration.M || 0; + data.weeks = duration.weeks || duration.w || 0; + data.days = duration.days || duration.d || 0; + data.hours = duration.hours || duration.h || 0; + data.minutes = duration.minutes || duration.m || 0; + data.seconds = duration.seconds || duration.s || 0; + data.milliseconds = duration.milliseconds || duration.ms || 0; } // left zero fill a number @@ -68,14 +70,14 @@ input = moment.duration(_input); } - ms = input.milliseconds + - (input.seconds) * 1e3 + // 1000 - (input.minutes) * 6e4 + // 1000 * 60 - (input.hours) * 36e5; // 1000 * 60 * 60 - d = (input.days) + - (input.weeks) * 7; - M = (input.months) + - (input.years) * 12; + ms = input.milliseconds() + + (input.seconds()) * 1e3 + // 1000 + (input.minutes()) * 6e4 + // 1000 * 60 + (input.hours()) * 36e5; // 1000 * 60 * 60 + d = (input.days()) + + (input.weeks()) * 7; + M = (input.months()) + + (input.years()) * 12; if (ms) { date.setTime(+date + ms * adding); } @@ -713,16 +715,37 @@ } }; + // helper for adding shortcuts + function makeShortcut(name, key) { + moment.fn[name] = function (input) { + var utc = this._isUTC ? 'UTC' : ''; + if (input != null) { + this._d['set' + utc + key](input); + return this; + } else { + return this._d['get' + utc + key](); + } + }; + } + + // loop through and add shortcuts (Month, Date, Hours, Minutes, Seconds, Milliseconds) + for (i = 0; i < shortcuts.length; i ++) { + makeShortcut(shortcuts[i].toLowerCase(), shortcuts[i]); + } + + // add shortcut for year (uses different syntax than the getter/setter 'year' == 'FullYear') + makeShortcut('year', 'FullYear'); + moment.duration.fn = Duration.prototype = { valueOf : function () { - return this.milliseconds + - (this.seconds * 1000) + // 1000 - (this.minutes * 60000) + // 60 * 1000 - (this.hours * 3600000) + // 60 * 60 * 1000 - (this.days * 86400000) + // 24 * 60 * 60 * 1000 - (this.weeks * 604800000) + // 7 * 24 * 60 * 60 * 1000 - (this.months * 2592000000) + // 30 * 24 * 60 * 60 * 1000 - (this.years * 31536000000); // 365 * 24 * 60 * 60 * 1000 + return this._data.milliseconds + + (this._data.seconds * 1000) + // 1000 + (this._data.minutes * 60000) + // 60 * 1000 + (this._data.hours * 3600000) + // 60 * 60 * 1000 + (this._data.days * 86400000) + // 24 * 60 * 60 * 1000 + (this._data.weeks * 604800000) + // 7 * 24 * 60 * 60 * 1000 + (this._data.months * 2592000000) + // 30 * 24 * 60 * 60 * 1000 + (this._data.years * 31536000000); // 365 * 24 * 60 * 60 * 1000 }, humanize : function (withSuffix) { @@ -738,28 +761,16 @@ } }; - - // helper for adding shortcuts - function makeShortcut(name, key) { - moment.fn[name] = function (input) { - var utc = this._isUTC ? 'UTC' : ''; - if (input != null) { - this._d['set' + utc + key](input); - return this; - } else { - return this._d['get' + utc + key](); - } + function makeDurationGetter(name) { + moment.duration.fn[name] = function () { + return this._data[name]; }; } - // loop through and add shortcuts (Month, Date, Hours, Minutes, Seconds, Milliseconds) - for (i = 0; i < shortcuts.length; i ++) { - makeShortcut(shortcuts[i].toLowerCase(), shortcuts[i]); + for (i = 0; i < durationGetters.length; i++) { + makeDurationGetter(durationGetters[i]); } - // add shortcut for year (uses different syntax than the getter/setter 'year' == 'FullYear') - makeShortcut('year', 'FullYear'); - // CommonJS module is defined if (hasModule) { module.exports = moment; diff --git a/test/moment/duration.js b/test/moment/duration.js index ecd0cfcc9..6cab93a37 100644 --- a/test/moment/duration.js +++ b/test/moment/duration.js @@ -14,54 +14,54 @@ exports.duration = { }); test.expect(8); - test.equal(d.years, 2, "years"); - test.equal(d.months, 3, "months"); - test.equal(d.weeks, 4, "weeks"); - test.equal(d.days, 1, "days"); - test.equal(d.hours, 8, "hours"); - test.equal(d.minutes, 9, "minutes"); - test.equal(d.seconds, 20, "seconds"); - test.equal(d.milliseconds, 12, "milliseconds"); + test.equal(d.years(), 2, "years"); + test.equal(d.months(), 3, "months"); + test.equal(d.weeks(), 4, "weeks"); + test.equal(d.days(), 1, "days"); + test.equal(d.hours(), 8, "hours"); + test.equal(d.minutes(), 9, "minutes"); + test.equal(d.seconds(), 20, "seconds"); + test.equal(d.milliseconds(), 12, "milliseconds"); test.done(); }, "milliseconds instantiation" : function(test) { test.expect(1); - test.equal(moment.duration(72).milliseconds, 72, "milliseconds"); + test.equal(moment.duration(72).milliseconds(), 72, "milliseconds"); test.done(); }, "instantiation by type" : function(test) { test.expect(16); - test.equal(moment.duration(1, "years").years, 1, "years"); - test.equal(moment.duration(1, "y").years, 1, "y"); - test.equal(moment.duration(2, "months").months, 2, "months"); - test.equal(moment.duration(2, "M").months, 2, "M"); - test.equal(moment.duration(3, "weeks").weeks, 3, "weeks"); - test.equal(moment.duration(3, "w").weeks, 3, "weeks"); - test.equal(moment.duration(4, "days").days, 4, "days"); - test.equal(moment.duration(4, "d").days, 4, "d"); - test.equal(moment.duration(5, "hours").hours, 5, "hours"); - test.equal(moment.duration(5, "h").hours, 5, "h"); - test.equal(moment.duration(6, "minutes").minutes, 6, "minutes"); - test.equal(moment.duration(6, "m").minutes, 6, "m"); - test.equal(moment.duration(7, "seconds").seconds, 7, "seconds"); - test.equal(moment.duration(7, "s").seconds, 7, "s"); - test.equal(moment.duration(8, "milliseconds").milliseconds, 8, "milliseconds"); - test.equal(moment.duration(8, "ms").milliseconds, 8, "ms"); + test.equal(moment.duration(1, "years").years(), 1, "years"); + test.equal(moment.duration(1, "y").years(), 1, "y"); + test.equal(moment.duration(2, "months").months(), 2, "months"); + test.equal(moment.duration(2, "M").months(), 2, "M"); + test.equal(moment.duration(3, "weeks").weeks(), 3, "weeks"); + test.equal(moment.duration(3, "w").weeks(), 3, "weeks"); + test.equal(moment.duration(4, "days").days(), 4, "days"); + test.equal(moment.duration(4, "d").days(), 4, "d"); + test.equal(moment.duration(5, "hours").hours(), 5, "hours"); + test.equal(moment.duration(5, "h").hours(), 5, "h"); + test.equal(moment.duration(6, "minutes").minutes(), 6, "minutes"); + test.equal(moment.duration(6, "m").minutes(), 6, "m"); + test.equal(moment.duration(7, "seconds").seconds(), 7, "seconds"); + test.equal(moment.duration(7, "s").seconds(), 7, "s"); + test.equal(moment.duration(8, "milliseconds").milliseconds(), 8, "milliseconds"); + test.equal(moment.duration(8, "ms").milliseconds(), 8, "ms"); test.done(); }, "shortcuts" : function(test) { test.expect(8); - test.equal(moment.duration({y: 1}).years, 1, "years = y"); - test.equal(moment.duration({M: 2}).months, 2, "months = M"); - test.equal(moment.duration({w: 3}).weeks, 3, "weeks = w"); - test.equal(moment.duration({d: 4}).days, 4, "days = d"); - test.equal(moment.duration({h: 5}).hours, 5, "hours = h"); - test.equal(moment.duration({m: 6}).minutes, 6, "minutes = m"); - test.equal(moment.duration({s: 7}).seconds, 7, "seconds = s"); - test.equal(moment.duration({ms: 8}).milliseconds, 8, "milliseconds = ms"); + test.equal(moment.duration({y: 1}).years(), 1, "years = y"); + test.equal(moment.duration({M: 2}).months(), 2, "months = M"); + test.equal(moment.duration({w: 3}).weeks(), 3, "weeks = w"); + test.equal(moment.duration({d: 4}).days(), 4, "days = d"); + test.equal(moment.duration({h: 5}).hours(), 5, "hours = h"); + test.equal(moment.duration({m: 6}).minutes(), 6, "minutes = m"); + test.equal(moment.duration({s: 7}).seconds(), 7, "seconds = s"); + test.equal(moment.duration({ms: 8}).milliseconds(), 8, "milliseconds = ms"); test.done(); },