1
0

getInsetInputValueFromInsetInputStr.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. case 'monthRange':
  36. insetInputValue.monthLeft.dateInput = inputValue;
  37. break;
  38. case 'dateRange':
  39. [leftDateInput = '', rightDateInput = ''] = inputValue.split(rangeSeparator);
  40. insetInputValue.monthLeft.dateInput = leftDateInput;
  41. insetInputValue.monthRight.dateInput = rightDateInput;
  42. break;
  43. case 'dateTime':
  44. [leftDateInput = '', leftTimeInput = ''] = inputValue.split(timeSeparator);
  45. insetInputValue.monthLeft.dateInput = leftDateInput;
  46. insetInputValue.monthLeft.timeInput = leftTimeInput;
  47. break;
  48. case 'dateTimeRange':
  49. const [leftInput = '', rightInput = ''] = inputValue.split(rangeSeparator);
  50. [leftDateInput = '', leftTimeInput = ''] = leftInput.split(timeSeparator);
  51. [rightDateInput = '', rightTimeInput = ''] = rightInput.split(timeSeparator);
  52. insetInputValue.monthLeft.dateInput = leftDateInput;
  53. insetInputValue.monthLeft.timeInput = leftTimeInput;
  54. insetInputValue.monthRight.dateInput = rightDateInput;
  55. insetInputValue.monthRight.timeInput = rightTimeInput;
  56. break;
  57. }
  58. return insetInputValue;
  59. }