MCPcopy
hub / github.com/adaltas/node-csv / normalize_columns

Function normalize_columns

packages/csv-stringify/lib/api/normalize_columns.js:1–48  ·  view source on GitHub ↗
(columns)

Source from the content-addressed store, hash-verified

1const normalize_columns = function (columns) {
2 if (columns === undefined || columns === null) {
3 return [undefined, undefined];
4 }
5 if (typeof columns !== "object") {
6 return [Error('Invalid option "columns": expect an array or an object')];
7 }
8 if (!Array.isArray(columns)) {
9 const newcolumns = [];
10 for (const k in columns) {
11 newcolumns.push({
12 key: k,
13 header: columns[k],
14 });
15 }
16 columns = newcolumns;
17 } else {
18 const newcolumns = [];
19 for (const column of columns) {
20 if (typeof column === "string") {
21 newcolumns.push({
22 key: column,
23 header: column,
24 });
25 } else if (
26 typeof column === "object" &&
27 column !== null &&
28 !Array.isArray(column)
29 ) {
30 if (!column.key) {
31 return [
32 Error('Invalid column definition: property "key" is required'),
33 ];
34 }
35 if (column.header === undefined) {
36 column.header = column.key;
37 }
38 newcolumns.push(column);
39 } else {
40 return [
41 Error("Invalid column definition: expect a string or an object"),
42 ];
43 }
44 }
45 columns = newcolumns;
46 }
47 return [undefined, columns];
48};
49
50export { normalize_columns };

Callers 2

stringifierFunction · 0.90
normalize_optionsFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected