Sha256: 87588446bececfd383d1d09dc0a46ff1d4d918dd272bdaa6bb57b368e7c07cac

Contents?: true

Size: 1.88 KB

Versions: 18

Compression:

Stored size: 1.88 KB

Contents

import { daysToMonths, monthsToDays } from './bubble';
import { normalizeUnits } from '../units/aliases';
import toInt from '../utils/to-int';

export function as (units) {
    var days;
    var months;
    var milliseconds = this._milliseconds;

    units = normalizeUnits(units);

    if (units === 'month' || units === 'year') {
        days   = this._days   + milliseconds / 864e5;
        months = this._months + daysToMonths(days);
        return units === 'month' ? months : months / 12;
    } else {
        // handle milliseconds separately because of floating point math errors (issue #1867)
        days = this._days + Math.round(monthsToDays(this._months));
        switch (units) {
            case 'week'   : return days / 7     + milliseconds / 6048e5;
            case 'day'    : return days         + milliseconds / 864e5;
            case 'hour'   : return days * 24    + milliseconds / 36e5;
            case 'minute' : return days * 1440  + milliseconds / 6e4;
            case 'second' : return days * 86400 + milliseconds / 1000;
            // Math.floor prevents floating point math errors here
            case 'millisecond': return Math.floor(days * 864e5) + milliseconds;
            default: throw new Error('Unknown unit ' + units);
        }
    }
}

// TODO: Use this.as('ms')?
export function valueOf () {
    return (
        this._milliseconds +
        this._days * 864e5 +
        (this._months % 12) * 2592e6 +
        toInt(this._months / 12) * 31536e6
    );
}

function makeAs (alias) {
    return function () {
        return this.as(alias);
    };
}

export var asMilliseconds = makeAs('ms');
export var asSeconds      = makeAs('s');
export var asMinutes      = makeAs('m');
export var asHours        = makeAs('h');
export var asDays         = makeAs('d');
export var asWeeks        = makeAs('w');
export var asMonths       = makeAs('M');
export var asYears        = makeAs('y');

Version data entries

18 entries across 18 versions & 4 rubygems

Version Path
lanes-0.8.0 node_modules/moment/src/lib/duration/as.js
active_record_survey_api-0.0.19 spec/test_app/vendor/assets/bower_components/moment/src/lib/duration/as.js
active_record_survey_api-0.0.18 spec/test_app/vendor/assets/bower_components/moment/src/lib/duration/as.js
active_record_survey_api-0.0.17 spec/test_app/vendor/assets/bower_components/moment/src/lib/duration/as.js
active_record_survey_api-0.0.14 spec/test_app/vendor/assets/bower_components/moment/src/lib/duration/as.js
date_picker-0.0.9 test/dummy/vendor/assets/components/moment/src/lib/duration/as.js
active_record_survey_api-0.0.12 spec/test_app/vendor/assets/bower_components/moment/src/lib/duration/as.js
locomotivecms-3.2.0.rc1 vendor/assets/components/locomotive_sources/moment/src/lib/duration/as.js
locomotivecms-3.1.1 vendor/assets/components/locomotive_sources/moment/src/lib/duration/as.js
locomotivecms-3.1.0 vendor/assets/components/locomotive_sources/moment/src/lib/duration/as.js
active_record_survey_api-0.0.11 spec/test_app/vendor/assets/bower_components/moment/src/lib/duration/as.js
locomotivecms-3.1.0.rc3 vendor/assets/components/locomotive_sources/moment/src/lib/duration/as.js
locomotivecms-3.1.0.rc2 vendor/assets/components/locomotive_sources/moment/src/lib/duration/as.js
locomotivecms-3.1.0.rc1 vendor/assets/components/locomotive_sources/moment/src/lib/duration/as.js
active_record_survey_api-0.0.7 spec/test_app/vendor/assets/bower_components/moment/src/lib/duration/as.js
active_record_survey_api-0.0.6 spec/test_app/vendor/assets/bower_components/moment/src/lib/duration/as.js
date_picker-0.0.8 test/dummy/vendor/assets/components/moment/src/lib/duration/as.js
date_picker-0.0.7 test/dummy/vendor/assets/components/moment/src/lib/duration/as.js