Skip to content

Commit

Permalink
fix: Rename hasCSVColumnTitle to noHeader (#216)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Rename hasCSVColumnTitle to noHeader to keep in line with the CLI
  • Loading branch information
juanjoDiaz authored and knownasilya committed Jan 18, 2018
1 parent 3359e8d commit f053c8b
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ try {
- Supports optional custom delimiters
- Supports optional custom eol value
- Supports optional custom quotation marks
- Not create CSV column title by passing hasCSVColumnTitle: false, into params.
- Not create CSV column title by passing noHeader: false, into params.
- If field is not exist in object then the field value in CSV will be empty.
- Preserve new lines in values. Should be used with \r\n line endings for full compatibility with Excel.
- Add a BOM character at the beginning of the csv to make Excel displaying special characters correctly.
Expand All @@ -64,7 +64,7 @@ try {
- `defaultValue` - String, default value to use when missing data. Defaults to `<empty>` if not specified. (Overridden by `fields[].default`)
- `quotes` - String, quotes around cell values and column names. Defaults to `"` if not specified.
- `doubleQuotes` - String, the value to replace double quotes in strings. Defaults to 2x`quotes` (for example `""`) if not specified.
- `hasCSVColumnTitle` - Boolean, determines whether or not CSV file will contain a title column. Defaults to `true` if not specified.
- `noHeader` - Boolean, determines whether or not CSV file will contain a title column. Defaults to `true` if not specified.
- `eol` - String, it gets added to each row of data. Defaults to `` if not specified.
- `newLine` - String, overrides the default OS line ending (i.e. `\n` on Unix and `\r\n` on Windows).
- `flatten` - Boolean, flattens nested JSON using [flat]. Defaults to `false`.
Expand Down
2 changes: 1 addition & 1 deletion bin/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ getFields(function (err, fields) {
var opts = {
data: input,
fields: fields,
hasCSVColumnTitle: program.header,
noHeader: program.header,
quotes: program.quote,
defaultValue: program.defaultValue,
flatten: program.flatten,
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ declare namespace json2csv {
defaultValue?: string;
quotes?: string;
doubleQuotes?: string;
hasCSVColumnTitle?: boolean;
noHeader?: boolean;
eol?: string;
newLine?: string;
flatten?: boolean;
Expand Down
12 changes: 6 additions & 6 deletions lib/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var flatten = require('flat');
* @property {String} [defaultValue="<empty>"] - default value to use when missing data
* @property {String} [quotes='"'] - quotes around cell values and column names
* @property {String} [doubleQuotes='""'] - the value to replace double quotes in strings
* @property {Boolean} [hasCSVColumnTitle=true] - determines whether or not CSV file will contain a title column
* @property {Boolean} [noHeader=true] - determines whether or not CSV file will contain a title column
* @property {String} [eol=''] - it gets added to each row of data
* @property {String} [newLine] - overrides the default OS line ending (\n on Unix \r\n on Windows)
* @property {Boolean} [flatten=false] - flattens nested JSON using flat (https://www.npmjs.com/package/flat)
Expand All @@ -34,7 +34,7 @@ var flatten = require('flat');
* Main function that converts json to csv.
*
* @param {Json2CsvParams} params Function parameters containing data, fields,
* delimiter (default is ','), hasCSVColumnTitle (default is true)
* delimiter (default is ','), noHeader (default is true)
* and default value (default is '')
* @param {Function} [callback] Callback function
* if error, returning error in call back.
Expand Down Expand Up @@ -75,7 +75,7 @@ module.exports = function (params, callback) {
* Note that this modifies params.
*
* @param {Json2CsvParams} params Function parameters containing data, fields,
* delimiter, default value, mark quotes and hasCSVColumnTitle
* delimiter, default value, mark quotes and noHeader
*/
function checkParams(params) {
params.data = params.data || [];
Expand Down Expand Up @@ -133,8 +133,8 @@ function checkParams(params) {
//#check default value
params.defaultValue = params.defaultValue;

//#check hasCSVColumnTitle, if it is not explicitly set to false then true.
params.hasCSVColumnTitle = params.hasCSVColumnTitle !== false;
//#check noHeader, if it is not explicitly set to false then true.
params.noHeader = params.noHeader !== false;

//#check include empty rows, defaults to false
params.includeEmptyRows = params.includeEmptyRows || false;
Expand All @@ -161,7 +161,7 @@ function createColumnTitles(params) {
var str = '';

//if CSV has column title, then create it
if (params.hasCSVColumnTitle) {
if (params.noHeader) {
params.fieldNames.forEach(function (element) {
if (str !== '') {
str += params.del;
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
json2csv({
data: jsonDefault,
fields: ['carModel', 'price', 'color'],
hasCSVColumnTitle: false
noHeader: false
}, function (error, csv) {
t.error(error);
t.equal(csv, csvFixtures.withoutTitle);
Expand Down

0 comments on commit f053c8b

Please sign in to comment.