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

fix(csv-stringify): quote_match with empty string pattern quotes empty strings #345

Merged
merged 2 commits into from Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion packages/csv-stringify/dist/cjs/index.cjs
Expand Up @@ -436,7 +436,22 @@ const stringifier = function(options, state, info){
return [Error(`Invalid Casting Value: returned value must return a string, an object, null or undefined, got ${JSON.stringify(value)}`)];
}
const {delimiter, escape, quote, quoted, quoted_empty, quoted_string, quoted_match, record_delimiter} = options;
if(value){
if('' === value && '' === field){
let quotedMatch = quoted_match && quoted_match.filter(quoted_match => {
if(typeof quoted_match === 'string'){
return value.indexOf(quoted_match) !== -1;
}else {
return quoted_match.test(value);
}
});
quotedMatch = quotedMatch && quotedMatch.length > 0;
const shouldQuote = quotedMatch || true === quoted_empty ||
(true === quoted_string && false !== quoted_empty);
if(shouldQuote === true){
value = quote + value + quote;
}
csvrecord += value;
}else if(value){
if(typeof value !== 'string'){
return [Error(`Formatter must return a string, null or undefined, got ${JSON.stringify(value)}`)];
}
Expand Down
17 changes: 16 additions & 1 deletion packages/csv-stringify/dist/cjs/sync.cjs
Expand Up @@ -434,7 +434,22 @@ const stringifier = function(options, state, info){
return [Error(`Invalid Casting Value: returned value must return a string, an object, null or undefined, got ${JSON.stringify(value)}`)];
}
const {delimiter, escape, quote, quoted, quoted_empty, quoted_string, quoted_match, record_delimiter} = options;
if(value){
if('' === value && '' === field){
let quotedMatch = quoted_match && quoted_match.filter(quoted_match => {
if(typeof quoted_match === 'string'){
return value.indexOf(quoted_match) !== -1;
}else {
return quoted_match.test(value);
}
});
quotedMatch = quotedMatch && quotedMatch.length > 0;
const shouldQuote = quotedMatch || true === quoted_empty ||
(true === quoted_string && false !== quoted_empty);
if(shouldQuote === true){
value = quote + value + quote;
}
csvrecord += value;
}else if(value){
if(typeof value !== 'string'){
return [Error(`Formatter must return a string, null or undefined, got ${JSON.stringify(value)}`)];
}
Expand Down
17 changes: 16 additions & 1 deletion packages/csv-stringify/dist/esm/index.js
Expand Up @@ -5492,7 +5492,22 @@ const stringifier = function(options, state, info){
return [Error(`Invalid Casting Value: returned value must return a string, an object, null or undefined, got ${JSON.stringify(value)}`)];
}
const {delimiter, escape, quote, quoted, quoted_empty, quoted_string, quoted_match, record_delimiter} = options;
if(value){
if('' === value && '' === field){
let quotedMatch = quoted_match && quoted_match.filter(quoted_match => {
if(typeof quoted_match === 'string'){
return value.indexOf(quoted_match) !== -1;
}else {
return quoted_match.test(value);
}
});
quotedMatch = quotedMatch && quotedMatch.length > 0;
const shouldQuote = quotedMatch || true === quoted_empty ||
(true === quoted_string && false !== quoted_empty);
if(shouldQuote === true){
value = quote + value + quote;
}
csvrecord += value;
}else if(value){
if(typeof value !== 'string'){
return [Error(`Formatter must return a string, null or undefined, got ${JSON.stringify(value)}`)];
}
Expand Down
17 changes: 16 additions & 1 deletion packages/csv-stringify/dist/esm/sync.js
Expand Up @@ -2402,7 +2402,22 @@ const stringifier = function(options, state, info){
return [Error(`Invalid Casting Value: returned value must return a string, an object, null or undefined, got ${JSON.stringify(value)}`)];
}
const {delimiter, escape, quote, quoted, quoted_empty, quoted_string, quoted_match, record_delimiter} = options;
if(value){
if('' === value && '' === field){
let quotedMatch = quoted_match && quoted_match.filter(quoted_match => {
if(typeof quoted_match === 'string'){
return value.indexOf(quoted_match) !== -1;
}else {
return quoted_match.test(value);
}
});
quotedMatch = quotedMatch && quotedMatch.length > 0;
const shouldQuote = quotedMatch || true === quoted_empty ||
(true === quoted_string && false !== quoted_empty);
if(shouldQuote === true){
value = quote + value + quote;
}
csvrecord += value;
}else if(value){
if(typeof value !== 'string'){
return [Error(`Formatter must return a string, null or undefined, got ${JSON.stringify(value)}`)];
}
Expand Down
17 changes: 16 additions & 1 deletion packages/csv-stringify/dist/iife/index.js
Expand Up @@ -5495,7 +5495,22 @@ var csv_stringify = (function (exports) {
return [Error(`Invalid Casting Value: returned value must return a string, an object, null or undefined, got ${JSON.stringify(value)}`)];
}
const {delimiter, escape, quote, quoted, quoted_empty, quoted_string, quoted_match, record_delimiter} = options;
if(value){
if('' === value && '' === field){
let quotedMatch = quoted_match && quoted_match.filter(quoted_match => {
if(typeof quoted_match === 'string'){
return value.indexOf(quoted_match) !== -1;
}else {
return quoted_match.test(value);
}
});
quotedMatch = quotedMatch && quotedMatch.length > 0;
const shouldQuote = quotedMatch || true === quoted_empty ||
(true === quoted_string && false !== quoted_empty);
if(shouldQuote === true){
value = quote + value + quote;
}
csvrecord += value;
}else if(value){
if(typeof value !== 'string'){
return [Error(`Formatter must return a string, null or undefined, got ${JSON.stringify(value)}`)];
}
Expand Down
17 changes: 16 additions & 1 deletion packages/csv-stringify/dist/iife/sync.js
Expand Up @@ -2405,7 +2405,22 @@ var csv_stringify_sync = (function (exports) {
return [Error(`Invalid Casting Value: returned value must return a string, an object, null or undefined, got ${JSON.stringify(value)}`)];
}
const {delimiter, escape, quote, quoted, quoted_empty, quoted_string, quoted_match, record_delimiter} = options;
if(value){
if('' === value && '' === field){
let quotedMatch = quoted_match && quoted_match.filter(quoted_match => {
if(typeof quoted_match === 'string'){
return value.indexOf(quoted_match) !== -1;
}else {
return quoted_match.test(value);
}
});
quotedMatch = quotedMatch && quotedMatch.length > 0;
const shouldQuote = quotedMatch || true === quoted_empty ||
(true === quoted_string && false !== quoted_empty);
if(shouldQuote === true){
value = quote + value + quote;
}
csvrecord += value;
}else if(value){
if(typeof value !== 'string'){
return [Error(`Formatter must return a string, null or undefined, got ${JSON.stringify(value)}`)];
}
Expand Down
17 changes: 16 additions & 1 deletion packages/csv-stringify/dist/umd/index.js
Expand Up @@ -5498,7 +5498,22 @@
return [Error(`Invalid Casting Value: returned value must return a string, an object, null or undefined, got ${JSON.stringify(value)}`)];
}
const {delimiter, escape, quote, quoted, quoted_empty, quoted_string, quoted_match, record_delimiter} = options;
if(value){
if('' === value && '' === field){
let quotedMatch = quoted_match && quoted_match.filter(quoted_match => {
if(typeof quoted_match === 'string'){
return value.indexOf(quoted_match) !== -1;
}else {
return quoted_match.test(value);
}
});
quotedMatch = quotedMatch && quotedMatch.length > 0;
const shouldQuote = quotedMatch || true === quoted_empty ||
(true === quoted_string && false !== quoted_empty);
if(shouldQuote === true){
value = quote + value + quote;
}
csvrecord += value;
}else if(value){
if(typeof value !== 'string'){
return [Error(`Formatter must return a string, null or undefined, got ${JSON.stringify(value)}`)];
}
Expand Down
17 changes: 16 additions & 1 deletion packages/csv-stringify/dist/umd/sync.js
Expand Up @@ -2408,7 +2408,22 @@
return [Error(`Invalid Casting Value: returned value must return a string, an object, null or undefined, got ${JSON.stringify(value)}`)];
}
const {delimiter, escape, quote, quoted, quoted_empty, quoted_string, quoted_match, record_delimiter} = options;
if(value){
if('' === value && '' === field){
let quotedMatch = quoted_match && quoted_match.filter(quoted_match => {
if(typeof quoted_match === 'string'){
return value.indexOf(quoted_match) !== -1;
}else {
return quoted_match.test(value);
}
});
quotedMatch = quotedMatch && quotedMatch.length > 0;
const shouldQuote = quotedMatch || true === quoted_empty ||
(true === quoted_string && false !== quoted_empty);
if(shouldQuote === true){
value = quote + value + quote;
}
csvrecord += value;
}else if(value){
if(typeof value !== 'string'){
return [Error(`Formatter must return a string, null or undefined, got ${JSON.stringify(value)}`)];
}
Expand Down
17 changes: 16 additions & 1 deletion packages/csv-stringify/lib/api/index.js
Expand Up @@ -126,7 +126,22 @@ const stringifier = function(options, state, info){
return [Error(`Invalid Casting Value: returned value must return a string, an object, null or undefined, got ${JSON.stringify(value)}`)];
}
const {delimiter, escape, quote, quoted, quoted_empty, quoted_string, quoted_match, record_delimiter} = options;
if(value){
if('' === value && '' === field){
let quotedMatch = quoted_match && quoted_match.filter(quoted_match => {
if(typeof quoted_match === 'string'){
return value.indexOf(quoted_match) !== -1;
}else{
return quoted_match.test(value);
}
});
quotedMatch = quotedMatch && quotedMatch.length > 0;
const shouldQuote = quotedMatch || true === quoted_empty ||
(true === quoted_string && false !== quoted_empty);
if(shouldQuote === true){
value = quote + value + quote;
}
csvrecord += value;
}else if(value){
if(typeof value !== 'string'){
return [Error(`Formatter must return a string, null or undefined, got ${JSON.stringify(value)}`)];
}
Expand Down
55 changes: 55 additions & 0 deletions packages/csv-stringify/test/option.quoted_match.coffee
Expand Up @@ -35,3 +35,58 @@ describe 'Option `quoted_match`', ->
ab,"cd","efg"
''' unless err
next err

it 'an empty string regex with no other "quoted" options (#344)', (next) ->
count = 0
data = ''
stringifier = stringify [
['a', null, undefined, '', 'b']
], quoted_match: /^$/, eof: false, (err, data) ->
data.should.eql '''
a,,,"",b
''' unless err
next err

it 'an empty string regex with all other "quoted" options set to false (#344)', (next) ->
count = 0
data = ''
stringifier = stringify [
['a', null, undefined, '', 'b']
], quoted: false, quoted_empty: false, quoted_string: false, quoted_match: /^$/, eof: false, (err, data) ->
data.should.eql '''
a,,,"",b
''' unless err
next err

it 'an empty string regex has higher priority than the "quoted" option', (next) ->
count = 0
data = ''
stringifier = stringify [
['a', null, undefined, '', 'b']
], quoted: true, quoted_match: /^$/, eof: false, (err, data) ->
data.should.eql '''
"a",,,"","b"
''' unless err
next err

it "an empty string regex does not conflict with quoted_string set to true", (next) ->
count = 0
data = ''
stringifier = stringify [
['a', null, undefined, '', 'b']
], quoted_string: true, quoted_match: /^$/, eof: false, (err, data) ->
data.should.eql '''
"a",,,"","b"
''' unless err
next err

it "an empty string regex does not conflict with quoted_empty set to true", (next) ->
count = 0
data = ''
stringifier = stringify [
['a', null, undefined, '' , 'b']
], quoted_empty: true, quoted_match: /^$/, eof: false, (err, data) ->
data.should.eql '''
a,"","","",b
''' unless err
next err
17 changes: 16 additions & 1 deletion packages/csv/dist/cjs/index.cjs
Expand Up @@ -2195,7 +2195,22 @@ const stringifier = function(options, state, info){
return [Error(`Invalid Casting Value: returned value must return a string, an object, null or undefined, got ${JSON.stringify(value)}`)];
}
const {delimiter, escape, quote, quoted, quoted_empty, quoted_string, quoted_match, record_delimiter} = options;
if(value){
if('' === value && '' === field){
let quotedMatch = quoted_match && quoted_match.filter(quoted_match => {
if(typeof quoted_match === 'string'){
return value.indexOf(quoted_match) !== -1;
}else {
return quoted_match.test(value);
}
});
quotedMatch = quotedMatch && quotedMatch.length > 0;
const shouldQuote = quotedMatch || true === quoted_empty ||
(true === quoted_string && false !== quoted_empty);
if(shouldQuote === true){
value = quote + value + quote;
}
csvrecord += value;
}else if(value){
if(typeof value !== 'string'){
return [Error(`Formatter must return a string, null or undefined, got ${JSON.stringify(value)}`)];
}
Expand Down
17 changes: 16 additions & 1 deletion packages/csv/dist/cjs/sync.cjs
Expand Up @@ -1974,7 +1974,22 @@ const stringifier = function(options, state, info){
return [Error(`Invalid Casting Value: returned value must return a string, an object, null or undefined, got ${JSON.stringify(value)}`)];
}
const {delimiter, escape, quote, quoted, quoted_empty, quoted_string, quoted_match, record_delimiter} = options;
if(value){
if('' === value && '' === field){
let quotedMatch = quoted_match && quoted_match.filter(quoted_match => {
if(typeof quoted_match === 'string'){
return value.indexOf(quoted_match) !== -1;
}else {
return quoted_match.test(value);
}
});
quotedMatch = quotedMatch && quotedMatch.length > 0;
const shouldQuote = quotedMatch || true === quoted_empty ||
(true === quoted_string && false !== quoted_empty);
if(shouldQuote === true){
value = quote + value + quote;
}
csvrecord += value;
}else if(value){
if(typeof value !== 'string'){
return [Error(`Formatter must return a string, null or undefined, got ${JSON.stringify(value)}`)];
}
Expand Down
17 changes: 16 additions & 1 deletion packages/csv/dist/esm/index.js
Expand Up @@ -7315,7 +7315,22 @@ const stringifier = function(options, state, info){
return [Error(`Invalid Casting Value: returned value must return a string, an object, null or undefined, got ${JSON.stringify(value)}`)];
}
const {delimiter, escape, quote, quoted, quoted_empty, quoted_string, quoted_match, record_delimiter} = options;
if(value){
if('' === value && '' === field){
let quotedMatch = quoted_match && quoted_match.filter(quoted_match => {
if(typeof quoted_match === 'string'){
return value.indexOf(quoted_match) !== -1;
}else {
return quoted_match.test(value);
}
});
quotedMatch = quotedMatch && quotedMatch.length > 0;
const shouldQuote = quotedMatch || true === quoted_empty ||
(true === quoted_string && false !== quoted_empty);
if(shouldQuote === true){
value = quote + value + quote;
}
csvrecord += value;
}else if(value){
if(typeof value !== 'string'){
return [Error(`Formatter must return a string, null or undefined, got ${JSON.stringify(value)}`)];
}
Expand Down
17 changes: 16 additions & 1 deletion packages/csv/dist/esm/sync.js
Expand Up @@ -7094,7 +7094,22 @@ const stringifier = function(options, state, info){
return [Error(`Invalid Casting Value: returned value must return a string, an object, null or undefined, got ${JSON.stringify(value)}`)];
}
const {delimiter, escape, quote, quoted, quoted_empty, quoted_string, quoted_match, record_delimiter} = options;
if(value){
if('' === value && '' === field){
let quotedMatch = quoted_match && quoted_match.filter(quoted_match => {
if(typeof quoted_match === 'string'){
return value.indexOf(quoted_match) !== -1;
}else {
return quoted_match.test(value);
}
});
quotedMatch = quotedMatch && quotedMatch.length > 0;
const shouldQuote = quotedMatch || true === quoted_empty ||
(true === quoted_string && false !== quoted_empty);
if(shouldQuote === true){
value = quote + value + quote;
}
csvrecord += value;
}else if(value){
if(typeof value !== 'string'){
return [Error(`Formatter must return a string, null or undefined, got ${JSON.stringify(value)}`)];
}
Expand Down
17 changes: 16 additions & 1 deletion packages/csv/dist/iife/index.js
Expand Up @@ -7318,7 +7318,22 @@ var csv = (function (exports) {
return [Error(`Invalid Casting Value: returned value must return a string, an object, null or undefined, got ${JSON.stringify(value)}`)];
}
const {delimiter, escape, quote, quoted, quoted_empty, quoted_string, quoted_match, record_delimiter} = options;
if(value){
if('' === value && '' === field){
let quotedMatch = quoted_match && quoted_match.filter(quoted_match => {
if(typeof quoted_match === 'string'){
return value.indexOf(quoted_match) !== -1;
}else {
return quoted_match.test(value);
}
});
quotedMatch = quotedMatch && quotedMatch.length > 0;
const shouldQuote = quotedMatch || true === quoted_empty ||
(true === quoted_string && false !== quoted_empty);
if(shouldQuote === true){
value = quote + value + quote;
}
csvrecord += value;
}else if(value){
if(typeof value !== 'string'){
return [Error(`Formatter must return a string, null or undefined, got ${JSON.stringify(value)}`)];
}
Expand Down