]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Add moment.inspect() for friendly console output
authorGlen Mailer <glenjamin@gmail.com>
Sat, 11 Jun 2016 17:40:27 +0000 (18:40 +0100)
committerIskren Chernev <iskren.chernev@gmail.com>
Sun, 6 Nov 2016 10:10:55 +0000 (02:10 -0800)
This gets used automatically by node's util.inspect function
and a number of similar libraries. This also means that it
will be used by the NodeJS REPL

moment.d.ts
src/lib/moment/format.js
src/lib/moment/prototype.js
src/test/moment/format.js

index e0a77e7b24a12624d2e835d4a2aa1d05a040ae7d..23048bbfbf18d2ac4cfb7662e6be2e895a0e74a0 100644 (file)
@@ -535,6 +535,7 @@ declare namespace moment {
     toArray(): number[];
     toDate(): Date;
     toISOString(): string;
+    inspect(): string;
     toJSON(): string;
     unix(): number;
 
index c7150e4f58b34e61707ebf726b946cf72a7df389..539f6c8e1a0344330474d14f66862dced1e55759 100644 (file)
@@ -23,6 +23,30 @@ export function toISOString () {
     }
 }
 
+/**
+ * Return a human readable representation of a moment that can
+ * also be evaluated to get a new moment which is the same
+ *
+ * @link https://nodejs.org/dist/latest/docs/api/util.html#util_custom_inspect_function_on_objects
+ */
+export function inspect () {
+    if (!this.isValid()) {
+        return 'moment.invalid(/* ' + this._i + ' */)';
+    }
+    var func = 'moment';
+    var zone = '';
+    if (!this.isLocal()) {
+        func = this.utcOffset() === 0 ? 'moment.utc' : 'moment.parseZone';
+        zone = 'Z';
+    }
+    var prefix = '[' + func + '("]';
+    var year = (0 < this.year() && this.year() <= 9999) ? 'YYYY' : 'YYYYYY';
+    var datetime = '-MM-DD[T]HH:mm:ss.SSS';
+    var suffix = zone + '[")]';
+
+    return this.format(prefix + year + datetime + suffix);
+}
+
 export function format (inputString) {
     if (!inputString) {
         inputString = this.isUtc() ? hooks.defaultFormatUtc : hooks.defaultFormat;
index cf7a0dc8e879f925201b643bd134589a7e2a2402..bd8fff79c201e4e588c824e97296cffa14d79981 100644 (file)
@@ -7,7 +7,7 @@ import { calendar, getCalendarFormat } from './calendar';
 import { clone } from './clone';
 import { isBefore, isBetween, isSame, isAfter, isSameOrAfter, isSameOrBefore } from './compare';
 import { diff } from './diff';
-import { format, toString, toISOString } from './format';
+import { format, toString, toISOString, inspect } from './format';
 import { from, fromNow } from './from';
 import { to, toNow } from './to';
 import { stringGet, stringSet } from './get-set';
@@ -50,6 +50,7 @@ proto.toArray           = toArray;
 proto.toObject          = toObject;
 proto.toDate            = toDate;
 proto.toISOString       = toISOString;
+proto.inspect           = inspect;
 proto.toJSON            = toJSON;
 proto.toString          = toString;
 proto.unix              = unix;
index d3084d014967b1360c6efbb0e6cda41325071421..3d9804bb2f4646d9da209624c754d617fb7c0e51 100644 (file)
@@ -150,6 +150,57 @@ test('toISOString', function (assert) {
     assert.equal(date.toISOString(), '-020123-10-09T20:30:40.678Z', 'ISO8601 format on big negative year');
 });
 
+// See https://nodejs.org/dist/latest/docs/api/util.html#util_custom_inspect_function_on_objects
+test('inspect', function (assert) {
+    function roundtrip(m) {
+        /*jshint evil:true */
+        return eval(m.inspect());
+    }
+    function testInspect(date, string) {
+        var inspected = date.inspect();
+        assert.equal(inspected, string);
+        assert.ok(date.isSame(roundtrip(date)), 'Tried to parse ' + inspected);
+    }
+
+    testInspect(
+        moment('2012-10-09T20:30:40.678'),
+        'moment("2012-10-09T20:30:40.678")'
+    );
+    testInspect(
+        moment('+020123-10-09T20:30:40.678'),
+        'moment("+020123-10-09T20:30:40.678")'
+    );
+    testInspect(
+        moment.utc('2012-10-09T20:30:40.678'),
+        'moment.utc("2012-10-09T20:30:40.678+00:00")'
+    );
+    testInspect(
+        moment.utc('+020123-10-09T20:30:40.678'),
+        'moment.utc("+020123-10-09T20:30:40.678+00:00")'
+    );
+    testInspect(
+        moment.utc('+020123-10-09T20:30:40.678+01:00'),
+        'moment.utc("+020123-10-09T19:30:40.678+00:00")'
+    );
+    testInspect(
+        moment.parseZone('2016-06-11T17:30:40.678+0430'),
+        'moment.parseZone("2016-06-11T17:30:40.678+04:30")'
+    );
+    testInspect(
+        moment.parseZone('+112016-06-11T17:30:40.678+0430'),
+        'moment.parseZone("+112016-06-11T17:30:40.678+04:30")'
+    );
+
+    assert.equal(
+        moment(new Date('nope')).inspect(),
+        'moment.invalid(/* Invalid Date */)'
+    );
+    assert.equal(
+        moment('blah', 'YYYY').inspect(),
+        'moment.invalid(/* blah */)'
+    );
+});
+
 test('long years', function (assert) {
     assert.equal(moment.utc().year(2).format('YYYYYY'), '+000002', 'small year with YYYYYY');
     assert.equal(moment.utc().year(2012).format('YYYYYY'), '+002012', 'regular year with YYYYYY');