All files / date intervalToRestrictiveDuration.ts

93.33% Statements 28/30
93.48% Branches 43/46
100% Functions 1/1
93.33% Lines 28/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128                                                                              8x 8x   8x     8x       8x                   8x   8x   8x 2x     8x 3x 3x     8x 4x 4x     8x           5x 5x     8x             6x 6x     8x               7x 7x     8x                 8x 8x         8x    
import {
  compareAsc,
  differenceInDays,
  differenceInHours,
  differenceInMilliseconds,
  differenceInMinutes,
  differenceInMonths,
  differenceInSeconds,
  differenceInYears,
  isValid,
  sub,
} from 'date-fns'
import { anyToDate } from './anyToDate'
 
export type IntervalToRestrictiveDurationTarget =
  | 'year'
  | 'month'
  | 'day'
  | 'hour'
  | 'minute'
  | 'second'
  | 'millisecond'
 
export interface IntervalToRestrictiveDurationResult {
  years: number
  months: number
  days: number
  hours: number
  minutes: number
  seconds: number
  milliseconds: number
}
 
// ref: https://github.com/date-fns/date-fns/blob/master/src/intervalToDuration/index.js
export function intervalToRestrictiveDuration(
  start: string | number | Date,
  end: string | number | Date,
  target: IntervalToRestrictiveDurationTarget = 'year',
): IntervalToRestrictiveDurationResult {
  const dateLeft = anyToDate(start)
  const dateRight = anyToDate(end)
 
  Iif (!isValid(dateLeft)) {
    throw new RangeError('Start Date is invalid')
  }
  Iif (!isValid(dateRight)) {
    throw new RangeError('End Date is invalid')
  }
 
  const duration: IntervalToRestrictiveDurationResult = {
    years: 0,
    months: 0,
    days: 0,
    hours: 0,
    minutes: 0,
    seconds: 0,
    milliseconds: 0,
  }
 
  let remaining: Date = dateLeft
 
  const sign = compareAsc(dateLeft, dateRight)
 
  if (target === 'year') {
    duration.years = Math.abs(differenceInYears(remaining, dateRight))
  }
 
  if (target === 'year' || target === 'month') {
    remaining = sub(remaining, { years: sign * duration.years })
    duration.months = Math.abs(differenceInMonths(remaining, dateRight))
  }
 
  if (target === 'year' || target === 'month' || target === 'day') {
    remaining = sub(remaining, { months: sign * duration.months })
    duration.days = Math.abs(differenceInDays(remaining, dateRight))
  }
 
  if (
    target === 'year' ||
    target === 'month' ||
    target === 'day' ||
    target === 'hour'
  ) {
    remaining = sub(remaining, { days: sign * duration.days })
    duration.hours = Math.abs(differenceInHours(remaining, dateRight))
  }
 
  if (
    target === 'year' ||
    target === 'month' ||
    target === 'day' ||
    target === 'hour' ||
    target === 'minute'
  ) {
    remaining = sub(remaining, { hours: sign * duration.hours })
    duration.minutes = Math.abs(differenceInMinutes(remaining, dateRight))
  }
 
  if (
    target === 'year' ||
    target === 'month' ||
    target === 'day' ||
    target === 'hour' ||
    target === 'minute' ||
    target === 'second'
  ) {
    remaining = sub(remaining, { minutes: sign * duration.minutes })
    duration.seconds = Math.abs(differenceInSeconds(remaining, dateRight))
  }
 
  if (
    target === 'year' ||
    target === 'month' ||
    target === 'day' ||
    target === 'hour' ||
    target === 'minute' ||
    target === 'second' ||
    target === 'millisecond'
  ) {
    remaining = sub(remaining, { seconds: sign * duration.seconds })
    duration.milliseconds = Math.abs(
      differenceInMilliseconds(remaining, dateRight),
    )
  }
 
  return duration
}