+import { Duration } from './constructor';
+
var mathAbs = Math.abs;
export function abs () {
- var data = this._data;
-
- this._milliseconds = mathAbs(this._milliseconds);
- this._days = mathAbs(this._days);
- this._months = mathAbs(this._months);
-
- data.milliseconds = mathAbs(data.milliseconds);
- data.seconds = mathAbs(data.seconds);
- data.minutes = mathAbs(data.minutes);
- data.hours = mathAbs(data.hours);
- data.months = mathAbs(data.months);
- data.years = mathAbs(data.years);
-
- return this;
+ if (!this.isValid()) {
+ return this;
+ }
+ return new Duration({
+ ms: mathAbs(this._milliseconds),
+ d: mathAbs(this._days),
+ M: mathAbs(this._months)
+ });
}
assert.equal(d._milliseconds, 3 * 60 * 60 * 1000 + 1 * 60 * 1000 - 10000, 'Subtract hour:minute');
});
+test('abs', function (assert) {
+ var d = moment.duration({months: 2, weeks: 2, hours: 5});
+ assert.equal(+d.abs(), +d);
+
+ d = moment.duration({months: -2, weeks: -2, hours: -5});
+ assert.equal(+d.abs(), -d);
+});
+
test('JSON.stringify duration', function (assert) {
var d = moment.duration(1024, 'h');
module('mutable');
-test('manipulation methods', function (assert) {
+test('moment manipulation methods', function (assert) {
var m = moment([2017, 2, 21, 0, 6, 54]);
assert.notEqual(m, moment(m), 'constructor should return a new moment');
var utc = m.utc();
assert.notEqual(utc, utc.local(), 'local() should return a new moment');
});
+
+test('duration manipulation methods', function (assert) {
+ var d = moment.duration({months: 2, weeks: 2, days: 0, hours: 5});
+
+ assert.notEqual(d, moment.duration(d), 'constructor should return a new duration');
+ assert.notEqual(d, d.abs(), 'abs() should return a new duration');
+ assert.notEqual(d, d.add(1, 'days'), 'add() should return a new duration');
+ assert.notEqual(d, d.subtract(2, 'years'), 'subtract() should return a new duration');
+});