multiOptions.ts 910 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { VariableOption } from 'app/features/variables/types';
  2. export const alignCurrentWithMulti = (current: VariableOption, value: boolean): VariableOption => {
  3. if (!current) {
  4. return current;
  5. }
  6. if (value && !Array.isArray(current.value)) {
  7. return {
  8. ...current,
  9. value: convertToMulti(current.value),
  10. text: convertToMulti(current.text),
  11. };
  12. }
  13. if (!value && Array.isArray(current.value)) {
  14. return {
  15. ...current,
  16. value: convertToSingle(current.value),
  17. text: convertToSingle(current.text),
  18. };
  19. }
  20. return current;
  21. };
  22. const convertToSingle = (value: string | string[]): string => {
  23. if (!Array.isArray(value)) {
  24. return value;
  25. }
  26. if (value.length > 0) {
  27. return value[0];
  28. }
  29. return '';
  30. };
  31. const convertToMulti = (value: string | string[]): string[] => {
  32. if (Array.isArray(value)) {
  33. return value;
  34. }
  35. return [value];
  36. };