* Formats an array for output as psql would. * * See `mz_repr::strconv::format_array` for the implementation in Materialize.
( vals: unknown, formatValue: (_: unknown) => string = formatCellDefault, )
| 63 | * See `mz_repr::strconv::format_array` for the implementation in Materialize. |
| 64 | */ |
| 65 | function formatArray( |
| 66 | vals: unknown, |
| 67 | formatValue: (_: unknown) => string = formatCellDefault, |
| 68 | ) { |
| 69 | assert(Array.isArray(vals)); |
| 70 | |
| 71 | let out = "{"; |
| 72 | let firstValue = true; |
| 73 | for (const val of vals) { |
| 74 | if (!firstValue) { |
| 75 | out += ","; |
| 76 | } |
| 77 | |
| 78 | // Format the value. |
| 79 | const formattedVal = formatValue(val); |
| 80 | |
| 81 | // If the formatted value contains special characters, the value needs |
| 82 | // to be escaped. Otherwise it can be emitted directly. |
| 83 | if (!Array.isArray(val) && /[\s{},\\"]/.test(formattedVal)) { |
| 84 | out += '"'; |
| 85 | // Backslash-escape any backslashes or double quotes inside the value. |
| 86 | out += formattedVal.replace(/["|\\]/g, "\\$&"); |
| 87 | out += '"'; |
| 88 | } else { |
| 89 | out += formattedVal; |
| 90 | } |
| 91 | |
| 92 | firstValue = false; |
| 93 | } |
| 94 | out += "}"; |
| 95 | return out; |
| 96 | } |
| 97 | |
| 98 | type FormatFn = (val: unknown) => string; |
| 99 |
no test coverage detected