MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / formatArray

Function formatArray

console/src/platform/shell/formatRows.ts:65–96  ·  view source on GitHub ↗

* 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,
)

Source from the content-addressed store, hash-verified

63 * See `mz_repr::strconv::format_array` for the implementation in Materialize.
64 */
65function 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
98type FormatFn = (val: unknown) => string;
99

Callers 2

formatRows.tsFile · 0.85
formatCellDefaultFunction · 0.85

Calls 4

assertFunction · 0.90
formatValueFunction · 0.50
testMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected