Sha256: 7ca58ac1d6c81ebebf16449528542da29515074064c0a01c1ab89ddad9377ba3

Contents?: true

Size: 1.93 KB

Versions: 8

Compression:

Stored size: 1.93 KB

Contents

import { daysToYears, yearsToDays } 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 + daysToYears(days) * 12;
        return units === 'month' ? months : months / 12;
    } else {
        // handle milliseconds separately because of floating point math errors (issue #1867)
        days = this._days + Math.round(yearsToDays(this._months / 12));
        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 * 24 * 60      + milliseconds / 6e4;
            case 'second' : return days * 24 * 60 * 60 + milliseconds / 1000;
            // Math.floor prevents floating point math errors here
            case 'millisecond': return Math.floor(days * 24 * 60 * 60 * 1000) + 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

8 entries across 8 versions & 2 rubygems

Version Path
date_picker-0.0.6 test/dummy/vendor/assets/components/moment/src/lib/duration/as.js
date_picker-0.0.5 test/dummy/vendor/assets/components/moment/src/lib/duration/as.js
date_picker-0.0.4 test/dummy/vendor/assets/components/moment/src/lib/duration/as.js
date_picker-0.0.3 test/dummy/vendor/assets/components/moment/src/lib/duration/as.js
date_picker-0.0.2 test/dummy/vendor/assets/components/moment/src/lib/duration/as.js
date_picker-0.0.1 test/dummy/vendor/assets/components/moment/src/lib/duration/as.js
puppet-herald-0.8.1 lib/puppet-herald/public/bower_components/moment/src/lib/duration/as.js
puppet-herald-0.8.0 lib/puppet-herald/public/bower_components/moment/src/lib/duration/as.js