-
Notifications
You must be signed in to change notification settings - Fork 279
Description
Summary
Provide an easy way to specify a string value that represents null value.
This issue is related to:
csv-parsecsv-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 representsnull.unquoted_null: boolean | undefined - should work only withnull_stringspecified. Ifunquoted_nullis not false,- with
parse, the value specified bynull_stringis treated as null only when it is unquoted. - with
stringify, null is emitted as an unquotednull_stringvalue. A string value ofnull_stringis emitted with quotes. This option overridesquoted_empty,quoted_string,quoted_match.- If
null_stringcontains delimiter or some special string,unquoted_nullwon't work.
- If
- with
undefined_as_null: boolean | undefined - should work only withnull_stringspecified. Ifundefied_as_nullis not false,- with
stringify,undefinedis emitted asnull.
- with
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.quoting ? 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,It 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.