]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Started on the Duration object feature.
authorRocky Meza <rocky@fusionbox.com>
Sat, 7 Apr 2012 15:05:49 +0000 (09:05 -0600)
committerRocky Meza <rocky@fusionbox.com>
Sat, 7 Apr 2012 15:05:49 +0000 (09:05 -0600)
moment.duration provides an instantiation method for Duration objects
that handles three input types: an Object, key/value, or just
milliseconds.  I moved the functionality of moment.humanizeDuration to
moment.duration.fn.humanize.  I left a deprecated
moment.humanizeDuration method which relies on moment.duration.
moment.fn.from has been updated to use moment.duration.

moment.js
test/moment/duration.js [new file with mode: 0644]
test/moment/humanize_duration.js

index f2f07cd92ecd9f90f1c9b102cb61459a68bee3ab..a52e4f30e455e31e062a2b1cec6d77309d30b5f9 100644 (file)
--- a/moment.js
+++ b/moment.js
         this._isUTC = !!isUTC;
     }
 
+    // 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;
+    }
+
     // left zero fill a number
     // see http://jsperf.com/left-zero-filling for performance comparison
     function leftZeroFill(number, targetLength) {
         return moment(input * 1000);
     };
 
+    // duration
+    moment.duration = function (input, key) {
+        var isNumber = (typeof input === 'number'),
+            duration = isNumber ? {} : input;
+
+        if (isNumber) {
+            if (key) {
+                duration[key] = input;
+            } else {
+                duration.milliseconds = input;
+            }
+        }
+
+        return new Duration(duration);
+    };
+
     // humanizeDuration
+    // This method is deprecated in favor of the new Duration object.  Please
+    // see the moment.duration method.
     moment.humanizeDuration = function (num, type, withSuffix) {
-        var difference = +num,
-            rel = moment.relativeTime,
-            output;
-        switch (type) {
-        case "seconds" :
-            difference *= 1000; // 1000
-            break;
-        case "minutes" :
-            difference *= 60000; // 60 * 1000
-            break;
-        case "hours" :
-            difference *= 3600000; // 60 * 60 * 1000
-            break;
-        case "days" :
-            difference *= 86400000; // 24 * 60 * 60 * 1000
-            break;
-        case "weeks" :
-            difference *= 604800000; // 7 * 24 * 60 * 60 * 1000
-            break;
-        case "months" :
-            difference *= 2592000000; // 30 * 24 * 60 * 60 * 1000
-            break;
-        case "years" :
-            difference *= 31536000000; // 365 * 24 * 60 * 60 * 1000
-            break;
-        default :
-            withSuffix = !!type;
-            break;
-        }
-        output = relativeTime(difference, !withSuffix);
-        return withSuffix ? (difference <= 0 ? rel.past : rel.future).replace(/%s/i, output) : output;
+        return moment.duration(num, type).humanize(withSuffix);
     };
 
     // version number
         },
 
         from : function (time, withoutSuffix) {
-            return moment.humanizeDuration(this.diff(time), !withoutSuffix);
+            return moment.duration(this.diff(time)).humanize(!withoutSuffix);
         },
 
         fromNow : function (withoutSuffix) {
         }
     };
 
+    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
+        },
+
+        humanize : function (withSuffix) {
+            var difference = +this,
+                rel = moment.relativeTime,
+                output = relativeTime(difference, !withSuffix);
+
+            if (withSuffix) {
+                output = (difference <= 0 ? rel.past : rel.future).replace(/%s/i, output);
+            }
+
+            return output;
+        }
+    };
+
+
     // helper for adding shortcuts
     function makeShortcut(name, key) {
         moment.fn[name] = function (input) {
diff --git a/test/moment/duration.js b/test/moment/duration.js
new file mode 100644 (file)
index 0000000..ecd0cfc
--- /dev/null
@@ -0,0 +1,113 @@
+var moment = require("../../moment");
+
+exports.duration = {
+    "object instantiation" : function(test) {
+        var d = moment.duration({
+            years: 2,
+            months: 3,
+            weeks: 4,
+            days: 1,
+            hours: 8,
+            minutes: 9,
+            seconds: 20,
+            milliseconds: 12
+        });
+
+        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.done();
+    },
+
+    "milliseconds instantiation" : function(test) {
+        test.expect(1);
+        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.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.done();
+    },
+
+    "humanize" : function(test) {
+        test.expect(32);
+        moment.lang('en');
+        test.equal(moment.duration({seconds: 44}).humanize(),  "a few seconds", "44 seconds = a few seconds");
+        test.equal(moment.duration({seconds: 45}).humanize(),  "a minute",      "45 seconds = a minute");
+        test.equal(moment.duration({seconds: 89}).humanize(),  "a minute",      "89 seconds = a minute");
+        test.equal(moment.duration({seconds: 90}).humanize(),  "2 minutes",     "90 seconds = 2 minutes");
+        test.equal(moment.duration({minutes: 44}).humanize(),  "44 minutes",    "44 minutes = 44 minutes");
+        test.equal(moment.duration({minutes: 45}).humanize(),  "an hour",       "45 minutes = an hour");
+        test.equal(moment.duration({minutes: 89}).humanize(),  "an hour",       "89 minutes = an hour");
+        test.equal(moment.duration({minutes: 90}).humanize(),  "2 hours",       "90 minutes = 2 hours");
+        test.equal(moment.duration({hours: 5}).humanize(),     "5 hours",       "5 hours = 5 hours");
+        test.equal(moment.duration({hours: 21}).humanize(),    "21 hours",      "21 hours = 21 hours");
+        test.equal(moment.duration({hours: 22}).humanize(),    "a day",         "22 hours = a day");
+        test.equal(moment.duration({hours: 35}).humanize(),    "a day",         "35 hours = a day");
+        test.equal(moment.duration({hours: 36}).humanize(),    "2 days",        "36 hours = 2 days");
+        test.equal(moment.duration({days: 1}).humanize(),      "a day",         "1 day = a day");
+        test.equal(moment.duration({days: 5}).humanize(),      "5 days",        "5 days = 5 days");
+        test.equal(moment.duration({weeks: 1}).humanize(),     "7 days",        "1 week = 7 days");
+        test.equal(moment.duration({days: 25}).humanize(),     "25 days",       "25 days = 25 days");
+        test.equal(moment.duration({days: 26}).humanize(),     "a month",       "26 days = a month");
+        test.equal(moment.duration({days: 30}).humanize(),     "a month",       "30 days = a month");
+        test.equal(moment.duration({days: 45}).humanize(),     "a month",       "45 days = a month");
+        test.equal(moment.duration({days: 46}).humanize(),     "2 months",      "46 days = 2 months");
+        test.equal(moment.duration({days: 74}).humanize(),     "2 months",      "75 days = 2 months");
+        test.equal(moment.duration({days: 76}).humanize(),     "3 months",      "76 days = 3 months");
+        test.equal(moment.duration({months: 1}).humanize(),    "a month",       "1 month = a month");
+        test.equal(moment.duration({months: 5}).humanize(),    "5 months",      "5 months = 5 months");
+        test.equal(moment.duration({days: 344}).humanize(),    "11 months",     "344 days = 11 months");
+        test.equal(moment.duration({days: 345}).humanize(),    "a year",        "345 days = a year");
+        test.equal(moment.duration({days: 547}).humanize(),    "a year",        "547 days = a year");
+        test.equal(moment.duration({days: 548}).humanize(),    "2 years",       "548 days = 2 years");
+        test.equal(moment.duration({years: 1}).humanize(),     "a year",        "1 year = a year");
+        test.equal(moment.duration({years: 5}).humanize(),     "5 years",       "5 years = 5 years");
+        test.equal(moment.duration(7200000).humanize(),        "2 hours",       "7200000 = 2 minutes");
+        test.done();
+    },
+
+    "humanize duration with suffix" : function(test) {
+        test.expect(2);
+        moment.lang('en');
+        test.equal(moment.duration({seconds:  44}).humanize(true),  "in a few seconds", "44 seconds = a few seconds");
+        test.equal(moment.duration({seconds: -44}).humanize(true),  "a few seconds ago", "44 seconds = a few seconds");
+        test.done();
+    }
+};
index 149ee0c64d4c005ecd75c87146c1f8b52a975c15..a5f775241b561854bf201408f6037e8dac3fb5e0 100644 (file)
@@ -4,6 +4,8 @@ exports.humanize_duration = {
     "humanize duration" : function(test) {
         test.expect(32);
         moment.lang('en');
+        // this syntax is deprecated.
+        // see moment.duration instead.
         test.equal(moment.humanizeDuration(44, "seconds"),  "a few seconds", "44 seconds = a few seconds");
         test.equal(moment.humanizeDuration(45, "seconds"),  "a minute",      "45 seconds = a minute");
         test.equal(moment.humanizeDuration(89, "seconds"),  "a minute",      "89 seconds = a minute");