Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(encoding): making column argument optional #2168

Merged
merged 8 commits into from
Sep 7, 2022
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