json-2-csv
Advanced tools
Comparing version 3.18.1 to 3.19.0
@@ -1,2 +0,2 @@ | ||
export interface ISharedOptions { | ||
interface SharedConverterOptions { | ||
/** | ||
@@ -59,4 +59,14 @@ * Specifies the different types of delimiters | ||
export interface IFullOptions extends ISharedOptions { | ||
export interface Csv2JsonOptions extends SharedConverterOptions { | ||
/** | ||
* Specify the header fields in the event that the CSV does not container a header line | ||
* | ||
* If you want to generate a nested object (ie. {info : {name: 'Mike'}}), then use `.` characters in the string to denote a nested field, like ['info.name'] | ||
* If your CSV has a header line included, then don't specify the option to utilize the default values that will be parsed from the CSV. | ||
*/ | ||
headerFields?: string[]; | ||
} | ||
export interface Json2CsvOptions extends SharedConverterOptions { | ||
/** | ||
* Should all documents have the same schema? | ||
@@ -125,9 +135,9 @@ * @default false | ||
export function json2csv(data: object[], | ||
callback: (err?: Error, csv?: string) => void, options?: IFullOptions): void; | ||
callback: (err?: Error, csv?: string) => void, options?: Json2CsvOptions): void; | ||
export function json2csvAsync(data: object[], options?: IFullOptions): Promise<string>; | ||
export function json2csvAsync(data: object[], options?: Json2CsvOptions): Promise<string>; | ||
export function csv2json(csv: string, | ||
callback: (err?: Error, data?: any[]) => void, options?: ISharedOptions): void; | ||
callback: (err?: Error, data?: any[]) => void, options?: Csv2JsonOptions): void; | ||
export function csv2jsonAsync(csv: string, options?: ISharedOptions): Promise<any[]>; | ||
export function csv2jsonAsync(csv: string, options?: Csv2JsonOptions): Promise<any[]>; |
@@ -33,13 +33,21 @@ 'use strict'; | ||
function retrieveHeading(lines) { | ||
let params = {lines}, | ||
let params = {lines}; | ||
if (options.headerFields) { | ||
params.headerFields = options.headerFields.map((headerField, index) => ({ | ||
value: processHeaderKey(headerField), | ||
index | ||
})); | ||
} else { | ||
// Generate and return the heading keys | ||
headerRow = params.lines[0]; | ||
params.headerFields = headerRow.map((headerKey, index) => ({ | ||
value: processHeaderKey(headerKey), | ||
index: index | ||
})); | ||
let headerRow = params.lines[0]; | ||
params.headerFields = headerRow.map((headerKey, index) => ({ | ||
value: processHeaderKey(headerKey), | ||
index | ||
})); | ||
// If the user provided keys, filter the generated keys to just the user provided keys so we also have the key index | ||
if (options.keys) { | ||
params.headerFields = params.headerFields.filter((headerKey) => options.keys.includes(headerKey.value)); | ||
// If the user provided keys, filter the generated keys to just the user provided keys so we also have the key index | ||
if (options.keys) { | ||
params.headerFields = params.headerFields.filter((headerKey) => options.keys.includes(headerKey.value)); | ||
} | ||
} | ||
@@ -231,3 +239,7 @@ | ||
function retrieveRecordLines(params) { | ||
params.recordLines = params.lines.splice(1); // All lines except for the header line | ||
if (options.headerFields) { // This option is passed for instances where the CSV has no header line | ||
params.recordLines = params.lines; | ||
} else { // All lines except for the header line | ||
params.recordLines = params.lines.splice(1); | ||
} | ||
@@ -234,0 +246,0 @@ return params; |
@@ -8,3 +8,3 @@ { | ||
"description": "A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.", | ||
"version": "3.18.1", | ||
"version": "3.19.0", | ||
"homepage": "https://mrodrig.github.io/json-2-csv", | ||
@@ -11,0 +11,0 @@ "repository": { |
@@ -167,2 +167,6 @@ # json-2-csv | ||
* Default: `false` | ||
* `headerFields` - Array - Specify the field names (as strings) in place of a header line in the CSV itself. | ||
* Default: Parses the header fields directly from the CSV string | ||
* If you want to generate a nested object (ie. `{info : {name: 'Mike'}}`), then use `.` characters in the string to denote a nested field, like ['info.name'] | ||
* If your CSV has a header line included, then don't specify the option to utilize the default values that will be parsed from the CSV. | ||
* `keys` - Array - Specify the keys (as strings) that should be converted. | ||
@@ -169,0 +173,0 @@ * Default: `null` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
70418
1275
277