]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Add moment.fn.to and moment.fn.toNow with tests (redo #2120)
authorIskren Chernev <iskren.chernev@gmail.com>
Mon, 4 May 2015 16:35:12 +0000 (09:35 -0700)
committerIskren Chernev <iskren.chernev@gmail.com>
Mon, 4 May 2015 16:35:12 +0000 (09:35 -0700)
src/lib/moment/prototype.js
src/lib/moment/to.js [new file with mode: 0644]
src/test/moment/from_to.js [new file with mode: 0644]

index b31b80e77f2e0479da4b7f1ceeb89cd7f60ff2df..2e77b6dfc69292a0b079e135b641e1ca28fac98f 100644 (file)
@@ -9,6 +9,7 @@ import { isBefore, isBetween, isSame, isAfter } from './compare';
 import { diff } from './diff';
 import { format, toString, toISOString } from './format';
 import { from, fromNow } from './from';
+import { to, toNow } from './to';
 import { getSet } from './get-set';
 import { locale, localeData, lang } from './locale';
 import { prototypeMin, prototypeMax } from './min-max';
@@ -24,6 +25,8 @@ proto.endOf        = endOf;
 proto.format       = format;
 proto.from         = from;
 proto.fromNow      = fromNow;
+proto.to           = to;
+proto.toNow        = fromNow;
 proto.get          = getSet;
 proto.invalidAt    = invalidAt;
 proto.isAfter      = isAfter;
diff --git a/src/lib/moment/to.js b/src/lib/moment/to.js
new file mode 100644 (file)
index 0000000..d9eccfd
--- /dev/null
@@ -0,0 +1,13 @@
+import { createDuration } from '../duration/create';
+import { createLocal } from '../create/local';
+
+export function to (time, withoutSuffix) {
+    if (!this.isValid()) {
+        return this.localeData().invalidDate();
+    }
+    return createDuration({from: this, to: time}).locale(this.locale()).humanize(!withoutSuffix);
+}
+
+export function toNow (withoutSuffix) {
+    return this.to(createLocal(), withoutSuffix);
+}
diff --git a/src/test/moment/from_to.js b/src/test/moment/from_to.js
new file mode 100644 (file)
index 0000000..6eeb3fa
--- /dev/null
@@ -0,0 +1,53 @@
+import { module, test } from '../qunit';
+import moment from '../../moment';
+
+module('from_to');
+
+test('from', function (assert) {
+    var start = moment();
+    moment.locale('en');
+    assert.equal(start.from(start.clone().add(5, 'seconds')),  'a few seconds ago', '5 seconds = a few seconds ago');
+    assert.equal(start.from(start.clone().add(1, 'minute')),  'a minute ago', '1 minute = a minute ago');
+    assert.equal(start.from(start.clone().add(5, 'minutes')),  '5 minutes ago', '5 minutes = 5 minutes ago');
+
+    assert.equal(start.from(start.clone().subtract(5, 'seconds')),  'in a few seconds', '5 seconds = in a few seconds');
+    assert.equal(start.from(start.clone().subtract(1, 'minute')),  'in a minute', '1 minute = in a minute');
+    assert.equal(start.from(start.clone().subtract(5, 'minutes')),  'in 5 minutes', '5 minutes = in 5 minutes');
+});
+
+test('from with absolute duration', function (assert) {
+    var start = moment();
+    moment.locale('en');
+    assert.equal(start.from(start.clone().add(5, 'seconds'), true),  'a few seconds', '5 seconds = a few seconds');
+    assert.equal(start.from(start.clone().add(1, 'minute'), true),  'a minute', '1 minute = a minute');
+    assert.equal(start.from(start.clone().add(5, 'minutes'), true),  '5 minutes', '5 minutes = 5 minutes');
+
+    assert.equal(start.from(start.clone().subtract(5, 'seconds'), true),  'a few seconds', '5 seconds = a few seconds');
+    assert.equal(start.from(start.clone().subtract(1, 'minute'), true),  'a minute', '1 minute = a minute');
+    assert.equal(start.from(start.clone().subtract(5, 'minutes'), true),  '5 minutes', '5 minutes = 5 minutes');
+});
+
+test('to', function (assert) {
+    var start = moment();
+    moment.locale('en');
+    assert.equal(start.to(start.clone().subtract(5, 'seconds')),  'a few seconds ago', '5 seconds = a few seconds ago');
+    assert.equal(start.to(start.clone().subtract(1, 'minute')),  'a minute ago', '1 minute = a minute ago');
+    assert.equal(start.to(start.clone().subtract(5, 'minutes')),  '5 minutes ago', '5 minutes = 5 minutes ago');
+
+    assert.equal(start.to(start.clone().add(5, 'seconds')),  'in a few seconds', '5 seconds = in a few seconds');
+    assert.equal(start.to(start.clone().add(1, 'minute')),  'in a minute', '1 minute = in a minute');
+    assert.equal(start.to(start.clone().add(5, 'minutes')),  'in 5 minutes', '5 minutes = in 5 minutes');
+});
+
+test('to with absolute duration', function (assert) {
+    var start = moment();
+    moment.locale('en');
+    assert.equal(start.to(start.clone().add(5, 'seconds'), true),  'a few seconds', '5 seconds = a few seconds');
+    assert.equal(start.to(start.clone().add(1, 'minute'), true),  'a minute', '1 minute = a minute');
+    assert.equal(start.to(start.clone().add(5, 'minutes'), true),  '5 minutes', '5 minutes = 5 minutes');
+
+    assert.equal(start.to(start.clone().subtract(5, 'seconds'), true),  'a few seconds', '5 seconds = a few seconds');
+    assert.equal(start.to(start.clone().subtract(1, 'minute'), true),  'a minute', '1 minute = a minute');
+    assert.equal(start.to(start.clone().subtract(5, 'minutes'), true),  '5 minutes', '5 minutes = 5 minutes');
+});
+