| 51 | * @returns The value of the key path, or the default value if the key path does not exist. |
| 52 | */ |
| 53 | export const valueForKeyPath = ( |
| 54 | obj: JSONObject | string | string[] | JSONObject[], |
| 55 | pathString: string, |
| 56 | defaultValue?: string, |
| 57 | ): string => { |
| 58 | const path: string[] = []; |
| 59 | |
| 60 | // Split the key path string into an array of keys |
| 61 | pathString |
| 62 | .trim() |
| 63 | .split(".") |
| 64 | .forEach(function (item) { |
| 65 | item.split(/\[([^}]+)\]/g).forEach(function (key) { |
| 66 | if (key.length > 0) { |
| 67 | path.push(key); |
| 68 | } |
| 69 | }); |
| 70 | }); |
| 71 | |
| 72 | let current = obj; |
| 73 | if (typeof current == "object") { |
| 74 | for (let i = 0; i < path.length; i++) { |
| 75 | if (!(current as JSONObject)[path[i]]) return defaultValue || ""; |
| 76 | current = (current as JSONObject)[path[i]]; |
| 77 | } |
| 78 | } |
| 79 | return current as string; |
| 80 | }; |
| 81 | |
| 82 | /** |
| 83 | * Gets the final schema to send to the model endpoint. |