Skip to content

Commit

Permalink
BREAKING(encoding/csv): make column argument optional (#2168)
Browse files Browse the repository at this point in the history
  • Loading branch information
luk3skyw4lker authored Sep 7, 2022
1 parent 3d802b2 commit d3b14b2
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 88 deletions.
4 changes: 2 additions & 2 deletions encoding/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ let columns: Column[] = [
"age",
];
console.log(await stringify(data, columns));
console.log(await stringify(data, { columns }));
// first,age
// Rick,70
// Morty,14
Expand All @@ -245,7 +245,7 @@ columns = [
},
];
console.log(await stringify(data, columns, { separator: "\t" }));
console.log(await stringify(data, { separator: "\t", columns }));
// name is_adult
// Rick Sanchez true
// Morty Smith false
Expand Down
54 changes: 31 additions & 23 deletions encoding/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,36 @@ async function getValuesFromItem(
): Promise<unknown[]> {
const values: unknown[] = [];

for (const column of normalizedColumns) {
let value: unknown = item;

for (const prop of column.prop) {
if (typeof value !== "object" || value === null) continue;
if (Array.isArray(value)) {
if (typeof prop === "number") value = value[prop];
else {
throw new StringifyError('Property accessor is not of type "number"');
}
} // I think this assertion is safe. Confirm?
else value = (value as ObjectWithStringPropertyKeys)[prop];
}
if (normalizedColumns.length) {
for (const column of normalizedColumns) {
let value: unknown = item;

for (const prop of column.prop) {
if (typeof value !== "object" || value === null) continue;
if (Array.isArray(value)) {
if (typeof prop === "number") value = value[prop];
else {
throw new StringifyError(
'Property accessor is not of type "number"',
);
}
} // I think this assertion is safe. Confirm?
else value = (value as ObjectWithStringPropertyKeys)[prop];
}

if (typeof column.fn === "function") value = await column.fn(value);
values.push(value);
if (typeof column.fn === "function") value = await column.fn(value);
values.push(value);
}
} else {
if (Array.isArray(item)) {
values.push(...item);
} else if (typeof item === "object") {
throw new StringifyError(
"No property accessor function was provided for object",
);
} else {
values.push(item);
}
}

return values;
Expand All @@ -145,23 +159,17 @@ async function getValuesFromItem(
export type StringifyOptions = {
headers?: boolean;
separator?: string;
columns?: Column[];
};

/**
* @param data The array of objects to encode
* @param columns Array of values specifying which data to include in the output
* @param options Output formatting options
*/
export async function stringify(
data: DataItem[],
columns: Column[],
options: StringifyOptions = {},
{ headers = true, separator: sep = ",", columns = [] }: StringifyOptions = {},
): Promise<string> {
const { headers, separator: sep } = {
headers: true,
separator: ",",
...options,
};
if (sep.includes(QUOTE) || sep.includes(NEWLINE)) {
const message = [
"Separator cannot include the following strings:",
Expand Down
Loading

0 comments on commit d3b14b2

Please sign in to comment.