]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Allow calendar to use functions
authorKushan Joshi <0o3ko0@gmail.com>
Wed, 21 Oct 2015 08:18:57 +0000 (13:48 +0530)
committerIskren Chernev <iskren.chernev@gmail.com>
Mon, 9 Nov 2015 05:24:31 +0000 (21:24 -0800)
src/lib/moment/calendar.js
src/test/moment/calendar.js [new file with mode: 0644]

index c9df8031123d3938581b3d2e314258013050e617..6480c862ebfd4fce18b720ec59ced8b639f89c69 100644 (file)
@@ -13,5 +13,8 @@ export function calendar (time, formats) {
             diff < 1 ? 'sameDay' :
             diff < 2 ? 'nextDay' :
             diff < 7 ? 'nextWeek' : 'sameElse';
-    return this.format(formats && formats[format] || this.localeData().calendar(format, this, createLocal(now)));
+
+    var output = formats && (typeof formats[format] === 'function' ? formats[format]() : formats[format]);
+
+    return this.format(output || this.localeData().calendar(format, this, createLocal(now)));
 }
diff --git a/src/test/moment/calendar.js b/src/test/moment/calendar.js
new file mode 100644 (file)
index 0000000..d388c41
--- /dev/null
@@ -0,0 +1,17 @@
+// These tests are for locale independent features
+// locale dependent tests would be in locale test folder
+import { module, test } from '../qunit';
+import moment from '../../moment';
+
+module('calendar');
+
+test('passing a function', function (assert) {
+    var a  = moment().hours(2).minutes(0).seconds(0);
+    assert.equal(moment(a).calendar(null, {
+        'sameDay': function () {
+            return 'h:mmA';
+        }
+    }), '2:00AM', 'should equate');
+});
+
+