5df7c92694e917bc09050e263dcec45b2a5c4f21173dacb853e986236d9599bbd38d5b301fc31662534893c07eb29340bfe9f24fba82cd4e9ac1efbc7484ab 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import hasOwnProp from '../utils/has-own-prop';
  2. import toInt from '../utils/to-int';
  3. import indexOf from '../utils/index-of';
  4. import { createDuration } from './create';
  5. var ordering = [
  6. 'year',
  7. 'quarter',
  8. 'month',
  9. 'week',
  10. 'day',
  11. 'hour',
  12. 'minute',
  13. 'second',
  14. 'millisecond',
  15. ];
  16. export default function isDurationValid(m) {
  17. var key,
  18. unitHasDecimal = false,
  19. i,
  20. orderLen = ordering.length;
  21. for (key in m) {
  22. if (
  23. hasOwnProp(m, key) &&
  24. !(
  25. indexOf.call(ordering, key) !== -1 &&
  26. (m[key] == null || !isNaN(m[key]))
  27. )
  28. ) {
  29. return false;
  30. }
  31. }
  32. for (i = 0; i < orderLen; ++i) {
  33. if (m[ordering[i]]) {
  34. if (unitHasDecimal) {
  35. return false; // only allow non-integers for smallest unit
  36. }
  37. if (parseFloat(m[ordering[i]]) !== toInt(m[ordering[i]])) {
  38. unitHasDecimal = true;
  39. }
  40. }
  41. }
  42. return true;
  43. }
  44. export function isValid() {
  45. return this._isValid;
  46. }
  47. export function createInvalid() {
  48. return createDuration(NaN);
  49. }