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

CSV - option to automatically infer headers #3857

Closed
sergeysolovev opened this issue Nov 25, 2023 · 10 comments · Fixed by #6122
Closed

CSV - option to automatically infer headers #3857

sergeysolovev opened this issue Nov 25, 2023 · 10 comments · Fixed by #6122
Labels
csv PR welcome A pull request for this issue would be welcome

Comments

@sergeysolovev
Copy link

It would be amazing if this code worked, probably by passing something like inferHeaders option (suggested by @crowlKats).

import { stringify } from 'https://deno.land/[email protected]/csv/mod.ts';
const arr = [{ a: 1, b: 2 }, { a: 3, b: 4 }];
console.log(stringify(arr));
@sigmaSd
Copy link
Contributor

sigmaSd commented Mar 16, 2024

just want to mention that this works if you pass the columns options

console.log(stringify(arr, { columns: ["a", "b"] }));

without it it errors with

error: Uncaught (in promise) StringifyError: No property accessor function was provided for object
      throw new StringifyError(

@sergeysolovev
Copy link
Author

sergeysolovev commented Mar 17, 2024

@sigmaSd thank you for the suggestion! It helped me to simplify my little wrapper to auto-stringify an array of objects

export class Csv {
  static stringify(array: Record<string, unknown>[]) {
    const columns = Object.keys(array[0]);
    return stringify(array, { columns }).trim();
  }
}

@iuioiua
Copy link
Contributor

iuioiua commented Mar 18, 2024

I like this idea! Thoughts, @kt3k?

@sigmaSd
Copy link
Contributor

sigmaSd commented Mar 19, 2024

In the mean while, shouldn't columns property be mandatory instead of optional ? because currently it feels weird that if you just use stringify(data) it throws

@timreichen
Copy link
Contributor

Related: #2660

@iuioiua
Copy link
Contributor

iuioiua commented Mar 19, 2024

Closing as duplicate of #2660.

@iuioiua iuioiua closed this as not planned Won't fix, can't repro, duplicate, stale Mar 19, 2024
@dandv
Copy link
Contributor

dandv commented May 25, 2024

@iuioiua This is a feature request for auto-inferring headers, the way mature CSV libraries do.

#2660 is an issue about marking the columns option as mandatory. If this feature (#3857) is implemented, then #2660 can be closed. Until then, #2660 can be resolved independently. We should reopen this issue (#3857).

@sergeysolovev: can you please rename this issue to "CSV - option to automatically infer headers" or something similar?

@sergeysolovev sergeysolovev changed the title Csv stringify support of an array of objects CSV - option to automatically infer headers May 25, 2024
@sergeysolovev
Copy link
Author

@dandv alright, ready

@kt3k kt3k reopened this May 25, 2024
@timreichen
Copy link
Contributor

Strange that csv is now stable but these issues are not resolved yet.

@iuioiua
Copy link
Contributor

iuioiua commented Aug 8, 2024

This is a feature request, not a bug. It can be added as a non-breaking change, so it wasn't a blocker for stabilization. Also, PRs are welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
csv PR welcome A pull request for this issue would be welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants