369d9cb9b914b8cff573353b60907bbdebc8134c1208dedd50d5f84aa8cad71f28d93893bd10cc58bf5bcaa17905332f13a774f9cf59d18125fbe66869fbb6 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { deprecate } from '../utils/deprecate';
  2. import isArray from '../utils/is-array';
  3. import { createLocal } from '../create/local';
  4. import { createInvalid } from '../create/valid';
  5. export var prototypeMin = deprecate(
  6. 'moment().min is deprecated, use moment.max instead. http://momentjs.com/guides/#/warnings/min-max/',
  7. function () {
  8. var other = createLocal.apply(null, arguments);
  9. if (this.isValid() && other.isValid()) {
  10. return other < this ? this : other;
  11. } else {
  12. return createInvalid();
  13. }
  14. }
  15. ),
  16. prototypeMax = deprecate(
  17. 'moment().max is deprecated, use moment.min instead. http://momentjs.com/guides/#/warnings/min-max/',
  18. function () {
  19. var other = createLocal.apply(null, arguments);
  20. if (this.isValid() && other.isValid()) {
  21. return other > this ? this : other;
  22. } else {
  23. return createInvalid();
  24. }
  25. }
  26. );
  27. // Pick a moment m from moments so that m[fn](other) is true for all
  28. // other. This relies on the function fn to be transitive.
  29. //
  30. // moments should either be an array of moment objects or an array, whose
  31. // first element is an array of moment objects.
  32. function pickBy(fn, moments) {
  33. var res, i;
  34. if (moments.length === 1 && isArray(moments[0])) {
  35. moments = moments[0];
  36. }
  37. if (!moments.length) {
  38. return createLocal();
  39. }
  40. res = moments[0];
  41. for (i = 1; i < moments.length; ++i) {
  42. if (!moments[i].isValid() || moments[i][fn](res)) {
  43. res = moments[i];
  44. }
  45. }
  46. return res;
  47. }
  48. // TODO: Use [].sort instead?
  49. export function min() {
  50. var args = [].slice.call(arguments, 0);
  51. return pickBy('isBefore', args);
  52. }
  53. export function max() {
  54. var args = [].slice.call(arguments, 0);
  55. return pickBy('isAfter', args);
  56. }