Skip to content

Commit

Permalink
docs(csv-stringify): custom quote sample
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Mar 3, 2023
1 parent 7633d7f commit d4ed6bc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
11 changes: 11 additions & 0 deletions packages/csv-stringify/samples/option.quote.custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

import { stringify } from 'csv-stringify/sync';
import assert from 'assert';

const records = stringify([
['a,b']
], {
quote: '|'
});

assert.equal(records, '|a,b|\n');
8 changes: 4 additions & 4 deletions packages/csv-stringify/samples/option.quoted.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

import { stringify } from 'csv-stringify';
import { stringify } from 'csv-stringify/sync';
import assert from 'assert';

stringify([
const records = stringify([
['1', ''],
[false, '2'],
['3', null],
[undefined, '4']
], {
quoted: true
}, function(err, records){
assert.equal(records, '"1",\n,"2"\n"3",\n,"4"\n');
});

assert.equal(records, '"1",\n,"2"\n"3",\n,"4"\n');
32 changes: 24 additions & 8 deletions packages/csv-stringify/test/option.quote.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { stringify } from '../lib/index.js'

describe 'Option `quote`', ->

it 'default', (next) ->
stringifier = stringify [], ->
stringifier.options.quote.should.eql('"')
next()

it 'validation', ->
stringify [], quote: ''
stringify [], quote: '"'
Expand All @@ -25,7 +30,18 @@ describe 'Option `quote`', ->
"""
next()

it 'with separator inside fields', (next) ->
it 'custom value with quoted', (next) ->
stringify [
[ 'a','','b' ]
[ '','c','' ]
], eof: false, quote: '|', quoted: true, (err, data) ->
data.should.eql """
|a|,,|b|
,|c|,
"""
next()

it 'fields with separator inside fields', (next) ->
stringify [
[ '20322051544','1979.0','8.8017226E7','ABC,45','2000-01-01' ]
[ '28392898392','1974.0','8.8392926E7','DEF','23','2050-11-27' ]
Expand All @@ -36,7 +52,7 @@ describe 'Option `quote`', ->
"""
next()

it 'with values containing delimiters', (next) ->
it 'fields containing delimiters', (next) ->
stringify [
[ '20322051544',',1979.0,8.8017226E7,ABC,45,2000-01-01' ]
[ '28392898392','1974.0','8.8392926E7','DEF','23','2050-11-27' ]
Expand All @@ -49,7 +65,7 @@ describe 'Option `quote`', ->
"""
next()

it 'with fields containing quotes', (next) ->
it 'fields containing quotes', (next) ->
stringify [
[ '20322051544','1979.0','8.801"7226E7','ABC','45','2000-01-01' ]
[ '28392898392','1974.0','8.8392926E7','DEF','2"3','2050-11-27' ]
Expand All @@ -60,7 +76,7 @@ describe 'Option `quote`', ->
"""
next()

it 'empty value', (next) ->
it 'empty fields', (next) ->
stringify [
[ '20322051544','','8.8017226E7','45','' ]
[ '','1974','8.8392926E7','','' ]
Expand All @@ -71,7 +87,7 @@ describe 'Option `quote`', ->
"""
next()

it 'values containing quotes and double quotes escape', (next) ->
it 'fields containing quotes and double quotes escape', (next) ->
stringify [
[ '20322051544','"','8.8017226E7',45,'"ok"' ]
[ '','1974','8.8392926E7','','' ]
Expand All @@ -82,7 +98,7 @@ describe 'Option `quote`', ->
"""
next()

it 'line breaks inside quotes', (next) ->
it 'fields with line breaks inside quotes', (next) ->
stringify [
[ '20322051544','\n',',8.8017226E7',45,'\nok\n' ]
[ '\n','1974','8.8392926E7','','\n' ]
Expand All @@ -98,7 +114,7 @@ describe 'Option `quote`', ->
"""
next()

it 'values where quote string is empty', (next) ->
it 'field where quote string is empty', (next) ->
stringify [
[ '20322051544','"','8.8017226E7',45,'"ok"' ]
[ '','1974','8.8392926E7','','' ]
Expand All @@ -109,7 +125,7 @@ describe 'Option `quote`', ->
"""
next()

it 'values with linebreaks and different record delimiter', (next) ->
it 'fields with linebreaks and different record delimiter', (next) ->
stringify [
[ '123\n456', 789]
[ '','1974' ]
Expand Down

0 comments on commit d4ed6bc

Please sign in to comment.