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

stringObjs support #14

Closed
MultiMote opened this issue Sep 19, 2024 · 4 comments
Closed

stringObjs support #14

MultiMote opened this issue Sep 19, 2024 · 4 comments

Comments

@MultiMote
Copy link

Hello!
I need to convert CSV to key:value dictionary. Values need to be string-typed.

From the example, I realized that there are two options:

  • Parse to string arrays
  • Parse to key:value, but values are typed
// native format (fastest)
let stringArrs = parser.stringArrs(csvStr); // [ ['1','2','3'], ['4','5','6'] ]
// typed formats (internally converted from native)
let typedObjs  = parser.typedObjs(csvStr);  // [ {a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6} ]

There is no way to parse csv to objects where values are strings.

@leeoniya
Copy link
Owner

worth adding, i think

@leeoniya
Copy link
Owner

leeoniya commented Sep 23, 2024

just realized the solution is to add one line to modify the inferred schema to set all columns to string type:

https://jsfiddle.net/zv89euo5/

const { inferSchema, initParser } = uDSV;

const csvStr = `
a,b
1,50000
10,5
`.trim();

const schema = inferSchema(csvStr);

// set all columns as strings
schema.cols.forEach(c => { c.type = 's'; });

const parser = initParser(schema);

const data = parser.typedObjs(csvStr);

console.log(data);

@mz8i
Copy link
Contributor

mz8i commented Nov 2, 2024

This is obviously a very simple fix, thanks @leeoniya ! Though it definitely feels like the interface of the library should allow this more explicitly, or that at least it would be worth giving this as an example explicitly in the docs. I was initially confused and wondering if I was missing something in the docs about such a functionality, until I found this issue.

The main use case where this comes in handy is where I don't know the list of columns, but I know that all of the should be read as strings. But also, it's easiest for me to just get an array of objects from the parser. This feels like a very common case?

In terms of an interface, if parser.stringObjs() is not desirable for some reason, it would make most sense to have an option for inferSchema, something like:

inferSchema(csvText, { type: 's' })

@leeoniya
Copy link
Owner

leeoniya commented Nov 3, 2024

added in 55686c5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants