index.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { format, parse } from 'date-fns';
  2. import { toNumber } from 'lodash';
  3. import { strings } from '../constants';
  4. import isNullOrUndefined from '../../utils/isNullOrUndefined';
  5. import { zhCN as defaultLocale } from 'date-fns/locale';
  6. /**
  7. *
  8. * @param {string|Date|number} input
  9. * @param {string} formatToken
  10. * @param {object} dateFnsLocale
  11. * @returns {Date}
  12. */
  13. export const parseToDate = (input: string | Date | number, formatToken = strings.DEFAULT_FORMAT, dateFnsLocale = defaultLocale) => {
  14. if (input instanceof Date) {
  15. return input;
  16. } else if (typeof input === 'number') {
  17. return new Date(toNumber(input));
  18. } else if (typeof input === 'string') {
  19. let curDate = new Date();
  20. // console.log(input, formatToken);
  21. curDate = parse(input, formatToken, curDate, { locale: dateFnsLocale });
  22. // console.log(curDate, formatToken);
  23. return curDate;
  24. } else if (typeof input === 'undefined') {
  25. return undefined;
  26. }
  27. return new Date();
  28. };
  29. /**
  30. *
  31. * @param {string|Date|number} input
  32. * @returns {number}
  33. */
  34. export const parseToTimestamp = (input: string | Date | number, formatToken = strings.DEFAULT_FORMAT, dateFnsLocale = defaultLocale) => Number(parseToDate(input, formatToken, dateFnsLocale));
  35. /**
  36. *
  37. * @param {Date|number} dateOrTimestamp
  38. * @param {string} formatToken
  39. * @returns {string}
  40. */
  41. export const formatToString = (dateOrTimestamp: Date | number, formatToken = strings.DEFAULT_FORMAT, dateFnsLocale = defaultLocale) => format(dateOrTimestamp, formatToken, { locale: dateFnsLocale });
  42. export const hourIsDisabled = (disabledHours: () => boolean, hour: number) => {
  43. if (typeof disabledHours === 'function') {
  44. const disabledOptions = disabledHours();
  45. if (
  46. Array.isArray(disabledOptions) &&
  47. !isNullOrUndefined(hour) &&
  48. disabledOptions.some(v => toNumber(v) === toNumber(hour))
  49. ) {
  50. return true;
  51. }
  52. }
  53. return false;
  54. };
  55. export const minuteIsDisabled = (disabledMinutes: (hour: number) => number[], hour: number, minute: number) => {
  56. if (typeof disabledMinutes === 'function') {
  57. const disabledOptions = disabledMinutes(hour);
  58. if (
  59. Array.isArray(disabledOptions) &&
  60. !isNullOrUndefined(hour) &&
  61. !isNullOrUndefined(minute) &&
  62. disabledOptions.some(v => toNumber(v) === toNumber(minute))
  63. ) {
  64. return true;
  65. }
  66. }
  67. return false;
  68. };
  69. export const secondIsDisabled = (disabledSeconds: (hour: number, minute: number) => number[], hour: number, minute: number, second: number) => {
  70. if (typeof disabledSeconds === 'function') {
  71. const disabledOptions = disabledSeconds(hour, minute);
  72. if (
  73. Array.isArray(disabledOptions) &&
  74. !isNullOrUndefined(hour) &&
  75. !isNullOrUndefined(minute) &&
  76. !isNullOrUndefined(second) &&
  77. disabledOptions.some(v => toNumber(v) === toNumber(second))
  78. ) {
  79. return true;
  80. }
  81. }
  82. return false;
  83. };
  84. export const transformToArray = (value: any) => {
  85. if (!Array.isArray(value)) {
  86. return [];
  87. } else {
  88. return [...value];
  89. }
  90. };
  91. /**
  92. * Determine whether the time length is the same as the format
  93. * e.g.
  94. * format | time | return
  95. * HH:mm | 12:00 | true
  96. * HH:mm:ss | 12:00:00 | true
  97. * yyyy HH:mm | 2021 12:00| true
  98. * HH | 1 | false
  99. * HH:mm | 12:0 | false
  100. * HH | 1 | false
  101. * HH:mm:ss | 12:00:0 | false
  102. * @param {String} time e.g. 12:0
  103. * @param {String} formatToken e.g. HH:mm
  104. * @returns {Boolean}
  105. */
  106. export const isTimeFormatLike = (time: string, formatToken: string) => {
  107. let isLike = true;
  108. const dateFnsSupportFormatCh = 'BDEGHKLMOPQRSTXYabcehimopqstuwxyz'; // dateFns support format character
  109. const formatSupportChReg = new RegExp(`[${dateFnsSupportFormatCh}]`, 'g');
  110. const formatNotSupportChReg = new RegExp(`[^${dateFnsSupportFormatCh}]`, 'g');
  111. const hmsReg = /[H|m|s]{1,2}/;
  112. const formatSplitted = formatToken.split(formatNotSupportChReg); // => ['HH', 'mm'];
  113. const timeSeparator = formatToken.replace(formatSupportChReg, ''); // => :
  114. const timeReg = new RegExp(`[${timeSeparator}]`, 'g'); // => /[:]/g
  115. const timeSplitted = time.split(timeReg); // => ['12', '0]
  116. if (formatSplitted.length !== timeSplitted.length) {
  117. isLike = false;
  118. } else {
  119. for (let i = 0, len = timeSplitted.length; i < len; i++) {
  120. const formatStr = formatSplitted[i];
  121. const timeStr = timeSplitted[i];
  122. // Returns false if the current character corresponds to minutes and seconds and the length is less than format
  123. // when i=1 => '0'.length < 'mm'.length
  124. if (hmsReg.test(formatStr) && timeStr.length < formatStr.length) {
  125. isLike = false;
  126. break;
  127. }
  128. }
  129. }
  130. return isLike;
  131. };