From 74334cf0e85e005a878c0597b3300f4762116a0d Mon Sep 17 00:00:00 2001 From: David Worms Date: Mon, 8 Nov 2021 21:15:21 +0100 Subject: [PATCH] refactor(csv-parse)!: rename group_columns_by_name option --- packages/csv-parse/ROADMAP.md | 1 - packages/csv-parse/dist/cjs/index.cjs | 22 +++++++++---------- packages/csv-parse/dist/cjs/index.d.ts | 6 ++--- packages/csv-parse/dist/cjs/sync.cjs | 22 +++++++++---------- packages/csv-parse/dist/esm/index.d.ts | 6 ++--- packages/csv-parse/dist/esm/index.js | 22 +++++++++---------- packages/csv-parse/dist/esm/sync.js | 22 +++++++++---------- packages/csv-parse/dist/iife/index.js | 22 +++++++++---------- packages/csv-parse/dist/iife/sync.js | 22 +++++++++---------- packages/csv-parse/dist/umd/index.js | 22 +++++++++---------- packages/csv-parse/dist/umd/sync.js | 22 +++++++++---------- packages/csv-parse/lib/index.d.ts | 6 ++--- packages/csv-parse/lib/index.js | 22 +++++++++---------- ...ray.js => option.group_columns_by_name.js} | 2 +- packages/csv-parse/test/api.types.ts | 10 ++++----- packages/csv-parse/test/option.cast.coffee | 6 ++--- .../option.columns_duplicates_to_array.coffee | 18 +++++++-------- 17 files changed, 126 insertions(+), 127 deletions(-) rename packages/csv-parse/samples/{option.columns_duplicates_to_array.js => option.group_columns_by_name.js} (91%) diff --git a/packages/csv-parse/ROADMAP.md b/packages/csv-parse/ROADMAP.md index fd7a00115..7a0e395cf 100644 --- a/packages/csv-parse/ROADMAP.md +++ b/packages/csv-parse/ROADMAP.md @@ -8,6 +8,5 @@ We invite you to join and contribute but create an issue before engaging any wor * promise: new API module (medium) * errors: finish normalisation of all errors (easy) * encoding: new encoding_input and encoding_output options (medium) -* `columns_duplicates_to_array`: this is just too long but I don't have much insipiration for a better name * `info`: remove the `parser.info` object and move its properties to `state` * `info`: rename the `info` related properties and functions to `context` diff --git a/packages/csv-parse/dist/cjs/index.cjs b/packages/csv-parse/dist/cjs/index.cjs index 68ea1de24..f2d584886 100644 --- a/packages/csv-parse/dist/cjs/index.cjs +++ b/packages/csv-parse/dist/cjs/index.cjs @@ -5193,18 +5193,18 @@ class Parser extends Transform { `got ${JSON.stringify(options.columns)}` ], options); } - // Normalize option `columns_duplicates_to_array` - if(options.columns_duplicates_to_array === undefined || options.columns_duplicates_to_array === null || options.columns_duplicates_to_array === false){ - options.columns_duplicates_to_array = false; - }else if(options.columns_duplicates_to_array !== true){ - throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', [ - 'Invalid option columns_duplicates_to_array:', + // Normalize option `group_columns_by_name` + if(options.group_columns_by_name === undefined || options.group_columns_by_name === null || options.group_columns_by_name === false){ + options.group_columns_by_name = false; + }else if(options.group_columns_by_name !== true){ + throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [ + 'Invalid option group_columns_by_name:', 'expect an boolean,', - `got ${JSON.stringify(options.columns_duplicates_to_array)}` + `got ${JSON.stringify(options.group_columns_by_name)}` ], options); }else if(options.columns === false){ - throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', [ - 'Invalid option columns_duplicates_to_array:', + throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [ + 'Invalid option group_columns_by_name:', 'the `columns` mode must be activated.' ], options); } @@ -5860,7 +5860,7 @@ class Parser extends Transform { } } __onRecord(){ - const {columns, columns_duplicates_to_array, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_records_with_empty_values} = this.options; + const {columns, group_columns_by_name, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_records_with_empty_values} = this.options; const {enabled, record} = this.state; if(enabled === false){ return this.__resetRecord(); @@ -5924,7 +5924,7 @@ class Parser extends Transform { for(let i = 0, l = record.length; i < l; i++){ if(columns[i] === undefined || columns[i].disabled) continue; // Turn duplicate columns into an array - if (columns_duplicates_to_array === true && obj[columns[i].name] !== undefined) { + if (group_columns_by_name === true && obj[columns[i].name] !== undefined) { if (Array.isArray(obj[columns[i].name])) { obj[columns[i].name] = obj[columns[i].name].concat(record[i]); } else { diff --git a/packages/csv-parse/dist/cjs/index.d.ts b/packages/csv-parse/dist/cjs/index.d.ts index 78114f792..f16b9d314 100644 --- a/packages/csv-parse/dist/cjs/index.d.ts +++ b/packages/csv-parse/dist/cjs/index.d.ts @@ -82,8 +82,8 @@ export interface Options { * Convert values into an array of values when columns are activated and * when multiple columns of the same name are found. */ - columns_duplicates_to_array?: boolean; - columnsDuplicatesToArray?: boolean; + group_columns_by_name?: boolean; + groupColumnsByName?: boolean; /** * Treat all the characters after this one as a comment, default to '' (disabled). */ @@ -241,7 +241,7 @@ export type CsvErrorCode = | 'CSV_INVALID_OPTION_CAST' | 'CSV_INVALID_OPTION_CAST_DATE' | 'CSV_INVALID_OPTION_COLUMNS' - | 'CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY' + | 'CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME' | 'CSV_INVALID_OPTION_COMMENT' | 'CSV_INVALID_OPTION_DELIMITER' | 'CSV_INVALID_OPTION_ON_RECORD' diff --git a/packages/csv-parse/dist/cjs/sync.cjs b/packages/csv-parse/dist/cjs/sync.cjs index ba56de82f..d3e3ff1c2 100644 --- a/packages/csv-parse/dist/cjs/sync.cjs +++ b/packages/csv-parse/dist/cjs/sync.cjs @@ -5193,18 +5193,18 @@ class Parser extends Transform { `got ${JSON.stringify(options.columns)}` ], options); } - // Normalize option `columns_duplicates_to_array` - if(options.columns_duplicates_to_array === undefined || options.columns_duplicates_to_array === null || options.columns_duplicates_to_array === false){ - options.columns_duplicates_to_array = false; - }else if(options.columns_duplicates_to_array !== true){ - throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', [ - 'Invalid option columns_duplicates_to_array:', + // Normalize option `group_columns_by_name` + if(options.group_columns_by_name === undefined || options.group_columns_by_name === null || options.group_columns_by_name === false){ + options.group_columns_by_name = false; + }else if(options.group_columns_by_name !== true){ + throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [ + 'Invalid option group_columns_by_name:', 'expect an boolean,', - `got ${JSON.stringify(options.columns_duplicates_to_array)}` + `got ${JSON.stringify(options.group_columns_by_name)}` ], options); }else if(options.columns === false){ - throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', [ - 'Invalid option columns_duplicates_to_array:', + throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [ + 'Invalid option group_columns_by_name:', 'the `columns` mode must be activated.' ], options); } @@ -5860,7 +5860,7 @@ class Parser extends Transform { } } __onRecord(){ - const {columns, columns_duplicates_to_array, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_records_with_empty_values} = this.options; + const {columns, group_columns_by_name, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_records_with_empty_values} = this.options; const {enabled, record} = this.state; if(enabled === false){ return this.__resetRecord(); @@ -5924,7 +5924,7 @@ class Parser extends Transform { for(let i = 0, l = record.length; i < l; i++){ if(columns[i] === undefined || columns[i].disabled) continue; // Turn duplicate columns into an array - if (columns_duplicates_to_array === true && obj[columns[i].name] !== undefined) { + if (group_columns_by_name === true && obj[columns[i].name] !== undefined) { if (Array.isArray(obj[columns[i].name])) { obj[columns[i].name] = obj[columns[i].name].concat(record[i]); } else { diff --git a/packages/csv-parse/dist/esm/index.d.ts b/packages/csv-parse/dist/esm/index.d.ts index 78114f792..f16b9d314 100644 --- a/packages/csv-parse/dist/esm/index.d.ts +++ b/packages/csv-parse/dist/esm/index.d.ts @@ -82,8 +82,8 @@ export interface Options { * Convert values into an array of values when columns are activated and * when multiple columns of the same name are found. */ - columns_duplicates_to_array?: boolean; - columnsDuplicatesToArray?: boolean; + group_columns_by_name?: boolean; + groupColumnsByName?: boolean; /** * Treat all the characters after this one as a comment, default to '' (disabled). */ @@ -241,7 +241,7 @@ export type CsvErrorCode = | 'CSV_INVALID_OPTION_CAST' | 'CSV_INVALID_OPTION_CAST_DATE' | 'CSV_INVALID_OPTION_COLUMNS' - | 'CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY' + | 'CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME' | 'CSV_INVALID_OPTION_COMMENT' | 'CSV_INVALID_OPTION_DELIMITER' | 'CSV_INVALID_OPTION_ON_RECORD' diff --git a/packages/csv-parse/dist/esm/index.js b/packages/csv-parse/dist/esm/index.js index 526b69edd..e7ad99045 100644 --- a/packages/csv-parse/dist/esm/index.js +++ b/packages/csv-parse/dist/esm/index.js @@ -5189,18 +5189,18 @@ class Parser extends Transform { `got ${JSON.stringify(options.columns)}` ], options); } - // Normalize option `columns_duplicates_to_array` - if(options.columns_duplicates_to_array === undefined || options.columns_duplicates_to_array === null || options.columns_duplicates_to_array === false){ - options.columns_duplicates_to_array = false; - }else if(options.columns_duplicates_to_array !== true){ - throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', [ - 'Invalid option columns_duplicates_to_array:', + // Normalize option `group_columns_by_name` + if(options.group_columns_by_name === undefined || options.group_columns_by_name === null || options.group_columns_by_name === false){ + options.group_columns_by_name = false; + }else if(options.group_columns_by_name !== true){ + throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [ + 'Invalid option group_columns_by_name:', 'expect an boolean,', - `got ${JSON.stringify(options.columns_duplicates_to_array)}` + `got ${JSON.stringify(options.group_columns_by_name)}` ], options); }else if(options.columns === false){ - throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', [ - 'Invalid option columns_duplicates_to_array:', + throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [ + 'Invalid option group_columns_by_name:', 'the `columns` mode must be activated.' ], options); } @@ -5856,7 +5856,7 @@ class Parser extends Transform { } } __onRecord(){ - const {columns, columns_duplicates_to_array, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_records_with_empty_values} = this.options; + const {columns, group_columns_by_name, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_records_with_empty_values} = this.options; const {enabled, record} = this.state; if(enabled === false){ return this.__resetRecord(); @@ -5920,7 +5920,7 @@ class Parser extends Transform { for(let i = 0, l = record.length; i < l; i++){ if(columns[i] === undefined || columns[i].disabled) continue; // Turn duplicate columns into an array - if (columns_duplicates_to_array === true && obj[columns[i].name] !== undefined) { + if (group_columns_by_name === true && obj[columns[i].name] !== undefined) { if (Array.isArray(obj[columns[i].name])) { obj[columns[i].name] = obj[columns[i].name].concat(record[i]); } else { diff --git a/packages/csv-parse/dist/esm/sync.js b/packages/csv-parse/dist/esm/sync.js index c164f5cb0..0e7fe4220 100644 --- a/packages/csv-parse/dist/esm/sync.js +++ b/packages/csv-parse/dist/esm/sync.js @@ -5189,18 +5189,18 @@ class Parser extends Transform { `got ${JSON.stringify(options.columns)}` ], options); } - // Normalize option `columns_duplicates_to_array` - if(options.columns_duplicates_to_array === undefined || options.columns_duplicates_to_array === null || options.columns_duplicates_to_array === false){ - options.columns_duplicates_to_array = false; - }else if(options.columns_duplicates_to_array !== true){ - throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', [ - 'Invalid option columns_duplicates_to_array:', + // Normalize option `group_columns_by_name` + if(options.group_columns_by_name === undefined || options.group_columns_by_name === null || options.group_columns_by_name === false){ + options.group_columns_by_name = false; + }else if(options.group_columns_by_name !== true){ + throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [ + 'Invalid option group_columns_by_name:', 'expect an boolean,', - `got ${JSON.stringify(options.columns_duplicates_to_array)}` + `got ${JSON.stringify(options.group_columns_by_name)}` ], options); }else if(options.columns === false){ - throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', [ - 'Invalid option columns_duplicates_to_array:', + throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [ + 'Invalid option group_columns_by_name:', 'the `columns` mode must be activated.' ], options); } @@ -5856,7 +5856,7 @@ class Parser extends Transform { } } __onRecord(){ - const {columns, columns_duplicates_to_array, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_records_with_empty_values} = this.options; + const {columns, group_columns_by_name, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_records_with_empty_values} = this.options; const {enabled, record} = this.state; if(enabled === false){ return this.__resetRecord(); @@ -5920,7 +5920,7 @@ class Parser extends Transform { for(let i = 0, l = record.length; i < l; i++){ if(columns[i] === undefined || columns[i].disabled) continue; // Turn duplicate columns into an array - if (columns_duplicates_to_array === true && obj[columns[i].name] !== undefined) { + if (group_columns_by_name === true && obj[columns[i].name] !== undefined) { if (Array.isArray(obj[columns[i].name])) { obj[columns[i].name] = obj[columns[i].name].concat(record[i]); } else { diff --git a/packages/csv-parse/dist/iife/index.js b/packages/csv-parse/dist/iife/index.js index 421f0e132..74c979c1f 100644 --- a/packages/csv-parse/dist/iife/index.js +++ b/packages/csv-parse/dist/iife/index.js @@ -5192,18 +5192,18 @@ var csv_parse = (function (exports) { `got ${JSON.stringify(options.columns)}` ], options); } - // Normalize option `columns_duplicates_to_array` - if(options.columns_duplicates_to_array === undefined || options.columns_duplicates_to_array === null || options.columns_duplicates_to_array === false){ - options.columns_duplicates_to_array = false; - }else if(options.columns_duplicates_to_array !== true){ - throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', [ - 'Invalid option columns_duplicates_to_array:', + // Normalize option `group_columns_by_name` + if(options.group_columns_by_name === undefined || options.group_columns_by_name === null || options.group_columns_by_name === false){ + options.group_columns_by_name = false; + }else if(options.group_columns_by_name !== true){ + throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [ + 'Invalid option group_columns_by_name:', 'expect an boolean,', - `got ${JSON.stringify(options.columns_duplicates_to_array)}` + `got ${JSON.stringify(options.group_columns_by_name)}` ], options); }else if(options.columns === false){ - throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', [ - 'Invalid option columns_duplicates_to_array:', + throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [ + 'Invalid option group_columns_by_name:', 'the `columns` mode must be activated.' ], options); } @@ -5859,7 +5859,7 @@ var csv_parse = (function (exports) { } } __onRecord(){ - const {columns, columns_duplicates_to_array, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_records_with_empty_values} = this.options; + const {columns, group_columns_by_name, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_records_with_empty_values} = this.options; const {enabled, record} = this.state; if(enabled === false){ return this.__resetRecord(); @@ -5923,7 +5923,7 @@ var csv_parse = (function (exports) { for(let i = 0, l = record.length; i < l; i++){ if(columns[i] === undefined || columns[i].disabled) continue; // Turn duplicate columns into an array - if (columns_duplicates_to_array === true && obj[columns[i].name] !== undefined) { + if (group_columns_by_name === true && obj[columns[i].name] !== undefined) { if (Array.isArray(obj[columns[i].name])) { obj[columns[i].name] = obj[columns[i].name].concat(record[i]); } else { diff --git a/packages/csv-parse/dist/iife/sync.js b/packages/csv-parse/dist/iife/sync.js index 14cbfc1ad..5f5de7299 100644 --- a/packages/csv-parse/dist/iife/sync.js +++ b/packages/csv-parse/dist/iife/sync.js @@ -5192,18 +5192,18 @@ var csv_parse_sync = (function (exports) { `got ${JSON.stringify(options.columns)}` ], options); } - // Normalize option `columns_duplicates_to_array` - if(options.columns_duplicates_to_array === undefined || options.columns_duplicates_to_array === null || options.columns_duplicates_to_array === false){ - options.columns_duplicates_to_array = false; - }else if(options.columns_duplicates_to_array !== true){ - throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', [ - 'Invalid option columns_duplicates_to_array:', + // Normalize option `group_columns_by_name` + if(options.group_columns_by_name === undefined || options.group_columns_by_name === null || options.group_columns_by_name === false){ + options.group_columns_by_name = false; + }else if(options.group_columns_by_name !== true){ + throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [ + 'Invalid option group_columns_by_name:', 'expect an boolean,', - `got ${JSON.stringify(options.columns_duplicates_to_array)}` + `got ${JSON.stringify(options.group_columns_by_name)}` ], options); }else if(options.columns === false){ - throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', [ - 'Invalid option columns_duplicates_to_array:', + throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [ + 'Invalid option group_columns_by_name:', 'the `columns` mode must be activated.' ], options); } @@ -5859,7 +5859,7 @@ var csv_parse_sync = (function (exports) { } } __onRecord(){ - const {columns, columns_duplicates_to_array, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_records_with_empty_values} = this.options; + const {columns, group_columns_by_name, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_records_with_empty_values} = this.options; const {enabled, record} = this.state; if(enabled === false){ return this.__resetRecord(); @@ -5923,7 +5923,7 @@ var csv_parse_sync = (function (exports) { for(let i = 0, l = record.length; i < l; i++){ if(columns[i] === undefined || columns[i].disabled) continue; // Turn duplicate columns into an array - if (columns_duplicates_to_array === true && obj[columns[i].name] !== undefined) { + if (group_columns_by_name === true && obj[columns[i].name] !== undefined) { if (Array.isArray(obj[columns[i].name])) { obj[columns[i].name] = obj[columns[i].name].concat(record[i]); } else { diff --git a/packages/csv-parse/dist/umd/index.js b/packages/csv-parse/dist/umd/index.js index 9901a1805..6adbb90ae 100644 --- a/packages/csv-parse/dist/umd/index.js +++ b/packages/csv-parse/dist/umd/index.js @@ -5195,18 +5195,18 @@ `got ${JSON.stringify(options.columns)}` ], options); } - // Normalize option `columns_duplicates_to_array` - if(options.columns_duplicates_to_array === undefined || options.columns_duplicates_to_array === null || options.columns_duplicates_to_array === false){ - options.columns_duplicates_to_array = false; - }else if(options.columns_duplicates_to_array !== true){ - throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', [ - 'Invalid option columns_duplicates_to_array:', + // Normalize option `group_columns_by_name` + if(options.group_columns_by_name === undefined || options.group_columns_by_name === null || options.group_columns_by_name === false){ + options.group_columns_by_name = false; + }else if(options.group_columns_by_name !== true){ + throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [ + 'Invalid option group_columns_by_name:', 'expect an boolean,', - `got ${JSON.stringify(options.columns_duplicates_to_array)}` + `got ${JSON.stringify(options.group_columns_by_name)}` ], options); }else if(options.columns === false){ - throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', [ - 'Invalid option columns_duplicates_to_array:', + throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [ + 'Invalid option group_columns_by_name:', 'the `columns` mode must be activated.' ], options); } @@ -5862,7 +5862,7 @@ } } __onRecord(){ - const {columns, columns_duplicates_to_array, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_records_with_empty_values} = this.options; + const {columns, group_columns_by_name, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_records_with_empty_values} = this.options; const {enabled, record} = this.state; if(enabled === false){ return this.__resetRecord(); @@ -5926,7 +5926,7 @@ for(let i = 0, l = record.length; i < l; i++){ if(columns[i] === undefined || columns[i].disabled) continue; // Turn duplicate columns into an array - if (columns_duplicates_to_array === true && obj[columns[i].name] !== undefined) { + if (group_columns_by_name === true && obj[columns[i].name] !== undefined) { if (Array.isArray(obj[columns[i].name])) { obj[columns[i].name] = obj[columns[i].name].concat(record[i]); } else { diff --git a/packages/csv-parse/dist/umd/sync.js b/packages/csv-parse/dist/umd/sync.js index 87a0c9b9b..c4a57ff4f 100644 --- a/packages/csv-parse/dist/umd/sync.js +++ b/packages/csv-parse/dist/umd/sync.js @@ -5195,18 +5195,18 @@ `got ${JSON.stringify(options.columns)}` ], options); } - // Normalize option `columns_duplicates_to_array` - if(options.columns_duplicates_to_array === undefined || options.columns_duplicates_to_array === null || options.columns_duplicates_to_array === false){ - options.columns_duplicates_to_array = false; - }else if(options.columns_duplicates_to_array !== true){ - throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', [ - 'Invalid option columns_duplicates_to_array:', + // Normalize option `group_columns_by_name` + if(options.group_columns_by_name === undefined || options.group_columns_by_name === null || options.group_columns_by_name === false){ + options.group_columns_by_name = false; + }else if(options.group_columns_by_name !== true){ + throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [ + 'Invalid option group_columns_by_name:', 'expect an boolean,', - `got ${JSON.stringify(options.columns_duplicates_to_array)}` + `got ${JSON.stringify(options.group_columns_by_name)}` ], options); }else if(options.columns === false){ - throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', [ - 'Invalid option columns_duplicates_to_array:', + throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [ + 'Invalid option group_columns_by_name:', 'the `columns` mode must be activated.' ], options); } @@ -5862,7 +5862,7 @@ } } __onRecord(){ - const {columns, columns_duplicates_to_array, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_records_with_empty_values} = this.options; + const {columns, group_columns_by_name, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_records_with_empty_values} = this.options; const {enabled, record} = this.state; if(enabled === false){ return this.__resetRecord(); @@ -5926,7 +5926,7 @@ for(let i = 0, l = record.length; i < l; i++){ if(columns[i] === undefined || columns[i].disabled) continue; // Turn duplicate columns into an array - if (columns_duplicates_to_array === true && obj[columns[i].name] !== undefined) { + if (group_columns_by_name === true && obj[columns[i].name] !== undefined) { if (Array.isArray(obj[columns[i].name])) { obj[columns[i].name] = obj[columns[i].name].concat(record[i]); } else { diff --git a/packages/csv-parse/lib/index.d.ts b/packages/csv-parse/lib/index.d.ts index 78114f792..f16b9d314 100644 --- a/packages/csv-parse/lib/index.d.ts +++ b/packages/csv-parse/lib/index.d.ts @@ -82,8 +82,8 @@ export interface Options { * Convert values into an array of values when columns are activated and * when multiple columns of the same name are found. */ - columns_duplicates_to_array?: boolean; - columnsDuplicatesToArray?: boolean; + group_columns_by_name?: boolean; + groupColumnsByName?: boolean; /** * Treat all the characters after this one as a comment, default to '' (disabled). */ @@ -241,7 +241,7 @@ export type CsvErrorCode = | 'CSV_INVALID_OPTION_CAST' | 'CSV_INVALID_OPTION_CAST_DATE' | 'CSV_INVALID_OPTION_COLUMNS' - | 'CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY' + | 'CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME' | 'CSV_INVALID_OPTION_COMMENT' | 'CSV_INVALID_OPTION_DELIMITER' | 'CSV_INVALID_OPTION_ON_RECORD' diff --git a/packages/csv-parse/lib/index.js b/packages/csv-parse/lib/index.js index 1057bc49e..c954146d7 100644 --- a/packages/csv-parse/lib/index.js +++ b/packages/csv-parse/lib/index.js @@ -170,18 +170,18 @@ class Parser extends Transform { `got ${JSON.stringify(options.columns)}` ], options); } - // Normalize option `columns_duplicates_to_array` - if(options.columns_duplicates_to_array === undefined || options.columns_duplicates_to_array === null || options.columns_duplicates_to_array === false){ - options.columns_duplicates_to_array = false; - }else if(options.columns_duplicates_to_array !== true){ - throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', [ - 'Invalid option columns_duplicates_to_array:', + // Normalize option `group_columns_by_name` + if(options.group_columns_by_name === undefined || options.group_columns_by_name === null || options.group_columns_by_name === false){ + options.group_columns_by_name = false; + }else if(options.group_columns_by_name !== true){ + throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [ + 'Invalid option group_columns_by_name:', 'expect an boolean,', - `got ${JSON.stringify(options.columns_duplicates_to_array)}` + `got ${JSON.stringify(options.group_columns_by_name)}` ], options); }else if(options.columns === false){ - throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', [ - 'Invalid option columns_duplicates_to_array:', + throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [ + 'Invalid option group_columns_by_name:', 'the `columns` mode must be activated.' ], options); } @@ -860,7 +860,7 @@ class Parser extends Transform { } } __onRecord(){ - const {columns, columns_duplicates_to_array, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_records_with_empty_values} = this.options; + const {columns, group_columns_by_name, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_records_with_empty_values} = this.options; const {enabled, record} = this.state; if(enabled === false){ return this.__resetRecord(); @@ -924,7 +924,7 @@ class Parser extends Transform { for(let i = 0, l = record.length; i < l; i++){ if(columns[i] === undefined || columns[i].disabled) continue; // Turn duplicate columns into an array - if (columns_duplicates_to_array === true && obj[columns[i].name] !== undefined) { + if (group_columns_by_name === true && obj[columns[i].name] !== undefined) { if (Array.isArray(obj[columns[i].name])) { obj[columns[i].name] = obj[columns[i].name].concat(record[i]); } else { diff --git a/packages/csv-parse/samples/option.columns_duplicates_to_array.js b/packages/csv-parse/samples/option.group_columns_by_name.js similarity index 91% rename from packages/csv-parse/samples/option.columns_duplicates_to_array.js rename to packages/csv-parse/samples/option.group_columns_by_name.js index 8dcfb2b99..b1fb2f313 100644 --- a/packages/csv-parse/samples/option.columns_duplicates_to_array.js +++ b/packages/csv-parse/samples/option.group_columns_by_name.js @@ -8,7 +8,7 @@ athos,porthos,aramis porthos,d_artagnan,athos `.trim(), { columns: true, - columns_duplicates_to_array: true + group_columns_by_name: true }, function(err, records){ assert.deepStrictEqual( records, [{ diff --git a/packages/csv-parse/test/api.types.ts b/packages/csv-parse/test/api.types.ts index 3a1a2f229..e52dc0b0b 100644 --- a/packages/csv-parse/test/api.types.ts +++ b/packages/csv-parse/test/api.types.ts @@ -28,8 +28,8 @@ describe('API Types', () => { const options: Options = parser.options const keys: string[] = Object.keys(options) keys.sort().should.eql([ - 'bom', 'cast', 'cast_date', 'columns', 'columns_duplicates_to_array', - 'comment', 'delimiter', 'encoding', 'escape', 'from', 'from_line', + 'bom', 'cast', 'cast_date', 'columns', 'comment', 'delimiter', + 'encoding', 'escape', 'from', 'from_line', 'group_columns_by_name', 'ignore_last_delimiters', 'info', 'ltrim', 'max_record_size', 'objname', 'on_record', 'quote', 'raw', 'record_delimiter', 'relax_column_count', 'relax_column_count_less', @@ -162,10 +162,10 @@ describe('API Types', () => { } }) - it('columns_duplicates_to_array', () => { + it('group_columns_by_name', () => { const options: Options = {} - options.columns_duplicates_to_array = true - options.columnsDuplicatesToArray = true + options.group_columns_by_name = true + options.groupColumnsByName = true }) it('comment', () => { diff --git a/packages/csv-parse/test/option.cast.coffee b/packages/csv-parse/test/option.cast.coffee index 7c051658c..4d35c8c0c 100644 --- a/packages/csv-parse/test/option.cast.coffee +++ b/packages/csv-parse/test/option.cast.coffee @@ -255,9 +255,9 @@ describe 'Option `cast`', -> code: 'CSV_INVALID_COLUMN_DEFINITION' next() - describe 'columns_duplicates_to_array', -> + describe 'group_columns_by_name', -> - it 'leading zeros are maintained when columns_duplicates_to_array is true', (next) -> + it 'leading zeros are maintained when group_columns_by_name is true', (next) -> parse """ FIELD_1,FIELD_1,FIELD_1 0,2,3 @@ -265,7 +265,7 @@ describe 'Option `cast`', -> """, cast: true columns: true - columns_duplicates_to_array: true + group_columns_by_name: true , (err, records) -> records.should.eql [ 'FIELD_1': [0, 2, 3] diff --git a/packages/csv-parse/test/option.columns_duplicates_to_array.coffee b/packages/csv-parse/test/option.columns_duplicates_to_array.coffee index 87e304bc4..cf9f0c582 100644 --- a/packages/csv-parse/test/option.columns_duplicates_to_array.coffee +++ b/packages/csv-parse/test/option.columns_duplicates_to_array.coffee @@ -2,25 +2,25 @@ import { parse } from '../lib/index.js' import { assert_error } from './api.assert_error.coffee' -describe 'Option `columns_duplicates_to_array`', -> +describe 'Option `group_columns_by_name`', -> it 'validate', -> (-> - parse "", columns_duplicates_to_array: 'invalid' + parse "", group_columns_by_name: 'invalid' ).should.throw - code: 'CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY' + code: 'CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME' message: [ - 'Invalid option columns_duplicates_to_array:' + 'Invalid option group_columns_by_name:' 'expect an boolean, got "invalid"' ].join ' ' it 'require columns to be active', -> (-> - parse "", columns_duplicates_to_array: true + parse "", group_columns_by_name: true ).should.throw - code: 'CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY' + code: 'CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME' message: [ - 'Invalid option columns_duplicates_to_array:' + 'Invalid option group_columns_by_name:' 'the `columns` mode must be activated.' ].join ' ' @@ -29,7 +29,7 @@ describe 'Option `columns_duplicates_to_array`', -> FIELD_1,FIELD_1 ABC,DEF GHI,JKL - """, columns: true, columns_duplicates_to_array: false, (err, records) -> + """, columns: true, group_columns_by_name: false, (err, records) -> records.should.eql [ 'FIELD_1': 'DEF' , @@ -42,7 +42,7 @@ describe 'Option `columns_duplicates_to_array`', -> FIELD_1,FIELD_1 ABC,DEF GHI,JKL - """, columns: true, columns_duplicates_to_array: true, (err, records) -> + """, columns: true, group_columns_by_name: true, (err, records) -> records.should.eql [ 'FIELD_1': ['ABC', 'DEF'] ,