All files / src/apis/shared time.ts

100% Statements 30/30
100% Branches 7/7
100% Functions 6/6
100% Lines 30/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 129 130 131 132 133 134 135 136 137 138 139 1406x                       6x 9x 9x   9x                           6x 3x 3x     3x     3x     6x 6x 6x 6x 6x 6x 6x 6x 6x     6x                                                                           11x   11x 9x     11x 11x   11x                               6x         8x                       6x         3x    
import { getOwnProperty } from "./get";
 
type DateValue = Date | number | string;
 
/**
 * Форматирует дату в понятную для QIWI строку:
 *
 * `ГГГГ-ММ-ДДTЧЧ:ММ:СС+\-ЧЧ:ММ`
 *
 * @param {DateValue} dateTime Аргумент для конструктора
 * @return {string}
 */
export function formatDate(dateTime: DateValue): string {
  const date = new Date(dateTime);
  const base = date.toISOString().split(".")[0];
 
  return `${base}+00:00`;
}
 
/**
 * **Используется только для параметра `lifetime` при создании ссылки на форму оплаты**
 *
 * Форматирует дату в понятную для QIWI строку:
 *
 * `ГГГГ-ММ-ДДTччмм`
 *
 * @export
 * @param {DateValue} dateTime
 * @return {string} string
 */
export function formatAltLifetimeDate(dateTime: DateValue): string {
  const date = new Date(dateTime);
  const base = date.toISOString().split("T")[0];
 
  const time =
    date.getHours().toString().padStart(2, "0") +
    date.getMinutes().toString().padStart(2, "0");
 
  return `${base}T${time}`;
}
 
export enum TimeSpan {
  Millisecond = 1,
  Second = Millisecond * 1000,
  Minute = Second * 60,
  Hour = Minute * 60,
  Day = Hour * 24,
  Week = Day * 7,
  Month = Day * 30,
  Year = Day * 365
}
 
export const TimeSpanMapping = Object.freeze({
  ms: TimeSpan.Millisecond,
  msec: TimeSpan.Millisecond,
  millisecond: TimeSpan.Millisecond,
  s: TimeSpan.Second,
  sec: TimeSpan.Second,
  second: TimeSpan.Second,
  m: TimeSpan.Minute,
  min: TimeSpan.Minute,
  minute: TimeSpan.Minute,
  h: TimeSpan.Hour,
  hr: TimeSpan.Hour,
  hour: TimeSpan.Hour,
  d: TimeSpan.Day,
  day: TimeSpan.Day,
  w: TimeSpan.Week,
  week: TimeSpan.Week,
  mn: TimeSpan.Month,
  mon: TimeSpan.Month,
  month: TimeSpan.Month,
  y: TimeSpan.Year,
  yr: TimeSpan.Year,
  year: TimeSpan.Year
});
export type TimeSpanMapping = typeof TimeSpanMapping;
export type TimeSpanKeys = keyof TimeSpanMapping;
 
type TimeUnit = TimeSpan | number | TimeSpanKeys;
 
/**
 *
 *
 * @param {number} amount
 * @param {TimeUnit} unit
 * @param {Date} currentDate
 * @return {Date}
 */
function offsetDate(amount: number, unit: TimeUnit, currentDate: Date): Date {
  const date = new Date(currentDate);
 
  if (typeof unit !== "number") {
    unit = getOwnProperty(TimeSpanMapping, unit);
  }
 
  const time = Math.round(date.getTime() + amount * unit);
  date.setTime(time);
 
  return date;
}
 
/**
 * **Используется только для параметра `lifetime` при создании ссылки на форму оплаты**
 *
 * Форматирует дату в понятную для QIWI строку:
 *
 * `ГГГГ-ММ-ДДTччмм`
 *
 * @export
 * @param {number} amount
 * @param {(TimeSpan | number | TimeSpanKeys)} unit
 * @param {Date} [currentDate=new Date()]
 * @return {string} string
 */
export function formatOffsetDate(
  amount: number,
  unit: TimeUnit = TimeSpan.Millisecond,
  currentDate = new Date()
): string {
  return formatDate(offsetDate(amount, unit, currentDate));
}
 
/**
 *
 *
 * @export
 * @param {number} amount
 * @param {(TimeSpan | number | TimeSpanKeys)} unit
 * @param {Date} [currentDate=new Date()]
 * @return {string} string
 */
export function formatOffsetAltLifetimeDate(
  amount: number,
  unit: TimeUnit = TimeSpan.Millisecond,
  currentDate = new Date()
): string {
  return formatAltLifetimeDate(offsetDate(amount, unit, currentDate));
}