/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

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