PageRenderTime 22ms CodeModel.GetById 10ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/moment/src/lib/moment/add-subtract.js

https://bitbucket.org/coleman333/smartsite
JavaScript | 55 lines | 43 code | 9 blank | 3 comment | 9 complexity | 283d8a97a647ee095ceca63807aab0bf MD5 | raw file
 1import { get, set } from './get-set';
 2import { setMonth } from '../units/month';
 3import { createDuration } from '../duration/create';
 4import { deprecateSimple } from '../utils/deprecate';
 5import { hooks } from '../utils/hooks';
 6import absRound from '../utils/abs-round';
 7
 8
 9// TODO: remove 'name' arg after deprecation is removed
10function createAdder(direction, name) {
11    return function (val, period) {
12        var dur, tmp;
13        //invert the arguments, but complain about it
14        if (period !== null && !isNaN(+period)) {
15            deprecateSimple(name, 'moment().' + name  + '(period, number) is deprecated. Please use moment().' + name + '(number, period). ' +
16            'See http://momentjs.com/guides/#/warnings/add-inverted-param/ for more info.');
17            tmp = val; val = period; period = tmp;
18        }
19
20        val = typeof val === 'string' ? +val : val;
21        dur = createDuration(val, period);
22        addSubtract(this, dur, direction);
23        return this;
24    };
25}
26
27export function addSubtract (mom, duration, isAdding, updateOffset) {
28    var milliseconds = duration._milliseconds,
29        days = absRound(duration._days),
30        months = absRound(duration._months);
31
32    if (!mom.isValid()) {
33        // No op
34        return;
35    }
36
37    updateOffset = updateOffset == null ? true : updateOffset;
38
39    if (months) {
40        setMonth(mom, get(mom, 'Month') + months * isAdding);
41    }
42    if (days) {
43        set(mom, 'Date', get(mom, 'Date') + days * isAdding);
44    }
45    if (milliseconds) {
46        mom._d.setTime(mom._d.valueOf() + milliseconds * isAdding);
47    }
48    if (updateOffset) {
49        hooks.updateOffset(mom, days || months);
50    }
51}
52
53export var add      = createAdder(1, 'add');
54export var subtract = createAdder(-1, 'subtract');
55