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

Provide an easy way to handle null value #440

Open
somnicattus opened this issue Sep 21, 2024 · 0 comments
Open

Provide an easy way to handle null value #440

somnicattus opened this issue Sep 21, 2024 · 0 comments

Comments

@somnicattus
Copy link

somnicattus commented Sep 21, 2024

Summary

Provide an easy way to specify a string value that represents null value.

This issue is related to:

  • csv-parse
  • csv-stringify

Motivation

I utilized these packages to imitate COPY command in SQL and found that there is no official way to handle with null values documented.

Most SQL provides COPY command with an option to specify a string value that represents NULL.

And PostgreSQL treats the value as null only when it is unquoted.

Alternative

No alternative considered.

Draft

Provide following options:

  • null_string: string | undefined - specifies a string value that represents null.
  • unquoted_null: boolean | undefined - should work only with null_string specified. If unquoted_null is not false,
    • with parse, the value specified by null_string is treated as null only when it is unquoted.
    • with stringify, null is emitted as an unquoted null_string value. A string value of null_string is emitted with quotes. This option overrides quoted_empty, quoted_string, quoted_match.
  • undefined_as_null: boolean | undefined - should work only with null_string specified. If undefied_as_null is not false,
    • with stringify, undefined is emitted as null.

Additional context

I managed to handle null values in a same way as PostgreSQL does, by using cast and quoted_string.

parse({ cast: (v, c) => v === '' && !c.quoted ? null : v, columns: ['id', 'data'] })

// input: id,data\n0,foo\n1,""\n2,
// output: [{ id: '0', data: 'foo' }, { id: '1', data: '' }, { id: '2', data: null }]
stringify({ cast: { string: v => { value: v, quoted_string: v === '', columns: ['id', 'data'] }  } })

// input: [{ id: 0, data: 'foo' }, { id: 1, data: '' }, { id: 2, data: null }]
// output: id,data\n0,foo\n1,""\n2,

Iit was a little tricky, and stringify could not emit null as unquoted non-empty string. If there was cast.null option for stringify provided, it would be possible.

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

No branches or pull requests

1 participant