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