All files / src utils.ts

87.95% Statements 73/83
84.74% Branches 50/59
25% Functions 5/20
87.95% Lines 73/83

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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 1546x 6x           6x 152x           6x       140x 140x 140x     140x 140x 140x 140x     140x 5x 135x       8x   127x   140x 6x     134x 8x     140x           6x       10x 10x 10x 10x                   10x 10x 10x 10x 10x 10x 10x   10x 2x   8x     8x     8x     8x 4x   4x     4x 1x   3x 1x   2x               2x     2x 1x   1x     1x     6x 6x 6x 6x 6x 74x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x             6x       4x 4x 4x    
import momentLib from 'moment';
import 'moment-duration-format';
import type { MomentProps, MomentSettings } from './types';
 
/**
 * Returns the active moment module reference from settings.
 */
export function getMomentRef(settings: MomentSettings): typeof momentLib {
  return settings.moment ?? momentLib;
}
 
/**
 * Returns a moment instance computed from the given props.
 */
export function getDatetime(
  props: MomentProps,
  settings: MomentSettings
): momentLib.Moment {
  const { utc, unix } = props;
  const dateInput = (props.date ?? props.children) as momentLib.MomentInput;
  const parse = (props.parse ?? settings.parse ?? undefined) as
    | momentLib.MomentFormatSpecification
    | undefined;
  const tz = props.tz ?? settings.timezone ?? undefined;
  const local = props.local ?? settings.local ?? false;
  const momentRef = getMomentRef(settings);
  const locale = props.locale || settings.locale || momentRef.locale();
 
  let datetime: momentLib.Moment;
  if (utc) {
    datetime = momentRef.utc(dateInput, parse, locale);
  } else if (unix) {
    // moment#unix fails because of a deprecation,
    // but since moment#unix(s) is implemented as moment(s * 1000),
    // this works equivalently
    datetime = momentRef((dateInput as number) * 1000, parse, locale);
  } else {
    datetime = momentRef(dateInput, parse, locale);
  }
  if (tz) {
    datetime = (datetime as momentLib.Moment & {
      tz: (zone: string) => momentLib.Moment;
    }).tz(tz);
  } else if (local) {
    datetime = datetime.local();
  }
 
  return datetime;
}
 
/**
 * Returns a compact relative time string without mutating locale settings.
 */
export function getShortRelativeTime(
  datetime: momentLib.Moment,
  settings: MomentSettings
): string {
  const momentRef = getMomentRef(settings);
  const round = momentRef.relativeTimeRounding();
  const duration = momentRef.duration(momentRef().diff(datetime)).abs();
  const thresholds = {
    ss: momentRef.relativeTimeThreshold('ss') as number,
    s: momentRef.relativeTimeThreshold('s') as number,
    m: momentRef.relativeTimeThreshold('m') as number,
    h: momentRef.relativeTimeThreshold('h') as number,
    d: momentRef.relativeTimeThreshold('d') as number,
    w: momentRef.relativeTimeThreshold('w') as number | null,
    M: momentRef.relativeTimeThreshold('M') as number
  };
 
  const seconds = round(duration.as('s'));
  const minutes = round(duration.as('m'));
  const hours = round(duration.as('h'));
  const days = round(duration.as('d'));
  const weeks = round(duration.as('w'));
  const months = round(duration.as('M'));
  const years = round(duration.as('y'));
 
  if (seconds <= thresholds.ss) {
    return '1s';
  }
  Iif (seconds < thresholds.s) {
    return `${seconds}s`;
  }
  Iif (minutes <= 1) {
    return '1m';
  }
  Iif (minutes < thresholds.m) {
    return `${minutes}m`;
  }
  if (hours <= 1) {
    return '1h';
  }
  Iif (hours < thresholds.h) {
    return `${hours}h`;
  }
  if (days <= 1) {
    return '1d';
  }
  if (days < thresholds.d) {
    return `${days}d`;
  }
  Iif (thresholds.w !== null) {
    Iif (weeks <= 1) {
      return '1w';
    }
    Iif (weeks < thresholds.w) {
      return `${weeks}w`;
    }
  }
  Iif (months <= 1) {
    return '1mo';
  }
  if (months < thresholds.M) {
    return `${months}mo`;
  }
  Iif (years <= 1) {
    return '1y';
  }
  return `${years}y`;
}
 
export {
  applyContentFilter,
  applyDateAdjustments,
  contentRenderers,
  formatDurationContent,
  getContent,
  renderCalendar,
  renderDefault,
  renderDiff,
  renderDuration,
  renderDurationFromNow,
  renderFormat,
  renderFrom,
  renderFromNow,
  renderFromNowShort,
  renderTo,
  renderToNow
} from './renderers';
export type { ContentContext, ContentRenderer } from './renderers';
 
/**
 * Returns the title to use when `withTitle` is set.
 */
export function getTitle(
  props: MomentProps,
  settings: MomentSettings
): string {
  const datetime = getDatetime(props, settings);
  const format = props.titleFormat || settings.format || '';
  return datetime.format(format);
}