/node_modules/moment/src/lib/moment/min-max.js

https://bitbucket.org/coleman333/smartsite · JavaScript · 63 lines · 50 code · 7 blank · 6 comment · 12 complexity · 6eb12e940d8616cf7e7407f2d9fa48b0 MD5 · raw file

  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. export var 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. }