Functions.utils.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { isEmpty } from 'lodash'
  2. /**
  3. * convert argument_types = "a integer, b integer"
  4. * to args = {value: [{name:'a', type:'integer'}, {name:'b', type:'integer'}]}
  5. */
  6. export function convertArgumentTypes(value: string) {
  7. const items = value?.split(',').map((item) => item.trim())
  8. if (isEmpty(value) || !items || items.length === 0) return { value: [] }
  9. const temp = items
  10. .map((x) => {
  11. const regex = /(\w+)\s+([\w\[\]]+)(?:\s+DEFAULT\s+(.*))?/i
  12. const match = x.match(regex)
  13. if (match) {
  14. const [, name, type, defaultValue] = match
  15. let parsedDefaultValue = defaultValue ? defaultValue.trim() : undefined
  16. if (
  17. ['timestamp', 'time', 'timetz', 'timestamptz'].includes(type.toLowerCase()) &&
  18. parsedDefaultValue
  19. ) {
  20. parsedDefaultValue = `'${parsedDefaultValue}'`
  21. }
  22. return { name, type, defaultValue: parsedDefaultValue }
  23. } else {
  24. console.error('Error while trying to parse function arguments', x)
  25. return null
  26. }
  27. })
  28. .filter(Boolean) as { name: string; type: string; defaultValue?: string }[]
  29. return { value: temp }
  30. }
  31. /**
  32. * convert config_params = {search_path: "auth, public"}
  33. * to {value: [{name: 'search_path', value: 'auth, public'}]}
  34. */
  35. export function convertConfigParams(value: Record<string, string> | null | undefined) {
  36. const temp = []
  37. if (value) {
  38. for (var key in value) {
  39. temp.push({ name: key, value: value[key] })
  40. }
  41. }
  42. return { value: temp }
  43. }
  44. export function hasWhitespace(value: string) {
  45. return /\s/.test(value)
  46. }