getInsetInputValueFromInsetInputStr.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { strings } from '../constants';
  2. /**
  3. * 从 insetInputStr 字符串解析出 insetInputValue 对象
  4. * Parse the insetInputValue object from the insetInputStr string
  5. *
  6. * @example
  7. * ```
  8. * '2022-02-01' => { monthLeft: { dateInput: '2022-02-01' } }
  9. * '2022-02-01 00:00:00' => { monthLeft: { dateInput: '2022-02-01', timeInput: '00:00:00' } }
  10. * '2022-02-01 00:00:00 ~ 2022-02-15 00:00:00' => { monthLeft: { dateInput: '2022-02-01', timeInput: '00:00:00'}, monthRight: { dateInput: '2022-02-15', timeInput: '00:00:00' } }
  11. *
  12. * '2022-0' => { monthLeft: { dateInput: '2022-0' } }
  13. * '2022-02-01 00:00:' => { monthLeft: { dateInput: '2022-02-01', timeInput: '00:00:' } }
  14. * '2022-02-01 00:00:00 ~ ' => { monthLeft: { dateInput: '2022-02-01', timeInput: '00:00:00'}, monthRight: { dateInput: '', timeInput: '' } }
  15. * ' ~ 2022-02-15 00:00:00' => { monthLeft: { dateInput: '', timeInput: '' }, monthRight: { dateInput: '2022-02-15', timeInput: '00:00:00' } }
  16. * ```
  17. */
  18. export default function getInsetInputValueFromInsetInputStr(options: { inputValue: string; rangeSeparator: string; type: typeof strings.TYPE_SET[number] }) {
  19. const timeSeparator = ' ';
  20. const { inputValue = '', rangeSeparator, type } = options;
  21. let leftDateInput, leftTimeInput, rightDateInput, rightTimeInput;
  22. const insetInputValue = {
  23. monthLeft: {
  24. dateInput: '',
  25. timeInput: '',
  26. },
  27. monthRight: {
  28. dateInput: '',
  29. timeInput: '',
  30. }
  31. };
  32. switch (type) {
  33. case 'date':
  34. case 'month':
  35. insetInputValue.monthLeft.dateInput = inputValue;
  36. break;
  37. case 'dateRange':
  38. [leftDateInput = '', rightDateInput = ''] = inputValue.split(rangeSeparator);
  39. insetInputValue.monthLeft.dateInput = leftDateInput;
  40. insetInputValue.monthRight.dateInput = rightDateInput;
  41. break;
  42. case 'dateTime':
  43. [leftDateInput = '', leftTimeInput = ''] = inputValue.split(timeSeparator);
  44. insetInputValue.monthLeft.dateInput = leftDateInput;
  45. insetInputValue.monthLeft.timeInput = leftTimeInput;
  46. break;
  47. case 'dateTimeRange':
  48. const [leftInput = '', rightInput = ''] = inputValue.split(rangeSeparator);
  49. [leftDateInput = '', leftTimeInput = ''] = leftInput.split(timeSeparator);
  50. [rightDateInput = '', rightTimeInput = ''] = rightInput.split(timeSeparator);
  51. insetInputValue.monthLeft.dateInput = leftDateInput;
  52. insetInputValue.monthLeft.timeInput = leftTimeInput;
  53. insetInputValue.monthRight.dateInput = rightDateInput;
  54. insetInputValue.monthRight.timeInput = rightTimeInput;
  55. break;
  56. }
  57. return insetInputValue;
  58. }