csv-parse

  • Version 6.1.0
  • Published
  • 1.44 MB
  • No dependencies
  • MIT license

Install

npm i csv-parse
yarn add csv-parse
pnpm add csv-parse

Overview

CSV parsing implementing the Node.js `stream.Transform` API

Index

Functions

function normalize_options

normalize_options: (opts: Options) => OptionsNormalized;

    function parse

    parse: {
    <T = unknown>(
    input: string | Buffer | Uint8Array,
    options: OptionsWithColumns<T>,
    callback?: Callback<T>
    ): Parser;
    (input: any, options: Options<string[]>, callback?: Callback<string[]>): Parser;
    <T = unknown>(options: OptionsWithColumns<T>, callback?: Callback<T>): Parser;
    (options: Options<string[]>, callback?: Callback<string[]>): Parser;
    (input: any, callback?: Callback<string[]>): Parser;
    (callback?: Callback<string[]>): Parser;
    };

      Classes

      class CsvError

      class CsvError extends Error {}

        constructor

        constructor(
        code: CsvErrorCode,
        message: string | string[],
        options?: OptionsNormalized<string[]>,
        ...contexts: unknown[]
        );

          property code

          readonly code: CsvErrorCode;

            class Parser

            class Parser extends stream.Transform {}

              constructor

              constructor(options: Options<string[]>);

                property info

                readonly info: Info;

                  property options

                  readonly options: OptionsNormalized<string[]>;

                    Interfaces

                    interface CastingContext

                    interface CastingContext {}

                      property column

                      readonly column: number | string;

                        property empty_lines

                        readonly empty_lines: number;

                          property error

                          readonly error: CsvError;

                            property header

                            readonly header: boolean;

                              property index

                              readonly index: number;

                                property invalid_field_length

                                readonly invalid_field_length: number;

                                  property lines

                                  readonly lines: number;

                                    property quoting

                                    readonly quoting: boolean;

                                      property raw

                                      readonly raw: string | undefined;

                                        property records

                                        readonly records: number;

                                          interface Info

                                          interface Info {}

                                            property bytes

                                            readonly bytes: number;
                                            • Count of the number of processed bytes.

                                            property columns

                                            readonly columns: boolean | { name: string }[] | { disabled: true }[];
                                            • Normalized verion of options.columns when options.columns is true, boolean otherwise.

                                            property comment_lines

                                            readonly comment_lines: number;
                                            • Count the number of lines being fully commented.

                                            property empty_lines

                                            readonly empty_lines: number;
                                            • Count the number of processed empty lines.

                                            property invalid_field_length

                                            readonly invalid_field_length: number;
                                            • Number of non uniform records when relax_column_count is true.

                                            property lines

                                            readonly lines: number;
                                            • The number of lines encountered in the source dataset, start at 1 for the first line.

                                            property records

                                            readonly records: number;
                                            • Count the number of processed records.

                                            interface Options

                                            interface Options<T = string[]> {}

                                              property auto_parse

                                              auto_parse?: boolean | CastingFunction;
                                              • If true, the parser will attempt to convert read data types to native types.

                                                Deprecated

                                                Use cast

                                              property auto_parse_date

                                              auto_parse_date?: boolean | CastingDateFunction;
                                              • If true, the parser will attempt to convert read data types to dates. It requires the "auto_parse" option.

                                                Deprecated

                                                Use cast_date

                                              property autoParse

                                              autoParse?: boolean | CastingFunction;

                                                property autoParseDate

                                                autoParseDate?: boolean | CastingDateFunction;

                                                  property bom

                                                  bom?: boolean;
                                                  • If true, detect and exclude the byte order mark (BOM) from the CSV input if present.

                                                  property cast

                                                  cast?: boolean | CastingFunction;
                                                  • If true, the parser will attempt to convert input string to native types. If a function, receive the value as first argument, a context as second argument and return a new value. More information about the context properties is available below.

                                                  property cast_date

                                                  cast_date?: boolean | CastingDateFunction;
                                                  • If true, the parser will attempt to convert input string to dates. If a function, receive the value as argument and return a new value. It requires the "auto_parse" option. Be careful, it relies on Date.parse.

                                                  property castDate

                                                  castDate?: boolean | CastingDateFunction;

                                                    property columns

                                                    columns?:
                                                    | boolean
                                                    | ColumnOption<
                                                    T extends string[] ? string : T extends unknown ? string : keyof T
                                                    >[]
                                                    | ((
                                                    record: T
                                                    ) => ColumnOption<
                                                    T extends string[] ? string : T extends unknown ? string : keyof T
                                                    >[]);
                                                    • List of fields as an array, a user defined callback accepting the first line and returning the column names or true if autodiscovered in the first CSV line, default to null, affect the result data set in the sense that records will be objects instead of arrays.

                                                    property comment

                                                    comment?: string | boolean | null;
                                                    • Treat all the characters after this one as a comment, default to '' (disabled).

                                                    property comment_no_infix

                                                    comment_no_infix?: boolean | null;
                                                    • Restrict the definition of comments to a full line. Comment characters defined in the middle of the line are not interpreted as such. The option require the activation of comments.

                                                    property delimiter

                                                    delimiter?: string | string[] | Buffer;
                                                    • Set the field delimiter. One character only, defaults to comma.

                                                    property encoding

                                                    encoding?: BufferEncoding | boolean | null | undefined;
                                                    • Set the source and destination encoding, a value of null returns buffer instead of strings.

                                                    property escape

                                                    escape?: string | null | boolean | Buffer;
                                                    • Set the escape character, one character only, defaults to double quotes.

                                                    property from

                                                    from?: number | string;
                                                    • Start handling records from the requested number of records.

                                                    property from_line

                                                    from_line?: null | number | string;
                                                    • Start handling records from the requested line number.

                                                    property fromLine

                                                    fromLine?: null | number | string;

                                                      property group_columns_by_name

                                                      group_columns_by_name?: boolean;
                                                      • Convert values into an array of values when columns are activated and when multiple columns of the same name are found.

                                                      property groupColumnsByName

                                                      groupColumnsByName?: boolean;

                                                        property ignore_last_delimiters

                                                        ignore_last_delimiters?: boolean | number;
                                                        • Don't interpret delimiters as such in the last field according to the number of fields calculated from the number of columns, the option require the presence of the column option when true.

                                                        property info

                                                        info?: boolean;
                                                        • Generate two properties info and record where info is a snapshot of the info object at the time the record was created and record is the parsed array or object.

                                                        property ltrim

                                                        ltrim?: boolean | null;
                                                        • If true, ignore whitespace immediately following the delimiter (i.e. left-trim all fields), defaults to false. Does not remove whitespace in a quoted field.

                                                        property max_record_size

                                                        max_record_size?: number | null | string;
                                                        • Maximum numer of characters to be contained in the field and line buffers before an exception is raised, used to guard against a wrong delimiter or record_delimiter, default to 128000 characters.

                                                        property maxRecordSize

                                                        maxRecordSize?: number;

                                                          property objname

                                                          objname?: Buffer | null | number | string;
                                                          • Name of header-record title to name objects by.

                                                          property on_record

                                                          on_record?: (record: T, context: CastingContext) => T | null | undefined;
                                                          • Alter and filter records by executing a user defined function.

                                                          property on_skip

                                                          on_skip?: (err: CsvError | undefined, raw: string | undefined) => undefined;
                                                          • Function called when an error occured if the skip_records_with_error option is activated.

                                                          property onRecord

                                                          onRecord?: (record: T, context: CastingContext) => T | null | undefined;

                                                            property onSkip

                                                            onSkip?: (err: CsvError | undefined, raw: string | undefined) => undefined;

                                                              property quote

                                                              quote?: string | boolean | Buffer | null;
                                                              • Optional character surrounding a field, one character only, defaults to double quotes.

                                                              property raw

                                                              raw?: boolean | null;
                                                              • Generate two properties raw and row where raw is the original CSV row content and row is the parsed array or object.

                                                              property record_delimiter

                                                              record_delimiter?: string | Buffer | null | (string | Buffer | null)[];
                                                              • One or multiple characters used to delimit record rows; defaults to auto discovery if not provided. Supported auto discovery method are Linux ("\n"), Apple ("\r") and Windows ("\r\n") row delimiters.

                                                              property recordDelimiter

                                                              recordDelimiter?: string | Buffer | null | (string | Buffer | null)[];

                                                                property relax_column_count

                                                                relax_column_count?: boolean | null;
                                                                • Discard inconsistent columns count, default to false.

                                                                property relax_column_count_less

                                                                relax_column_count_less?: boolean | null;
                                                                • Discard inconsistent columns count when the record contains less fields than expected, default to false.

                                                                property relax_column_count_more

                                                                relax_column_count_more?: boolean | null;
                                                                • Discard inconsistent columns count when the record contains more fields than expected, default to false.

                                                                property relax_quotes

                                                                relax_quotes?: boolean | null;
                                                                • Preserve quotes inside unquoted field.

                                                                property relaxColumnCount

                                                                relaxColumnCount?: boolean | null;

                                                                  property relaxColumnCountLess

                                                                  relaxColumnCountLess?: boolean | null;

                                                                    property relaxColumnCountMore

                                                                    relaxColumnCountMore?: boolean | null;

                                                                      property relaxQuotes

                                                                      relaxQuotes?: boolean | null;

                                                                        property rtrim

                                                                        rtrim?: boolean | null;
                                                                        • If true, ignore whitespace immediately preceding the delimiter (i.e. right-trim all fields), defaults to false. Does not remove whitespace in a quoted field.

                                                                        property skip_empty_lines

                                                                        skip_empty_lines?: boolean | null;
                                                                        • Dont generate empty values for empty lines. Defaults to false

                                                                        property skip_records_with_empty_values

                                                                        skip_records_with_empty_values?: boolean | null;
                                                                        • Don't generate records for lines containing empty column values (column matching /\s*/), defaults to false.

                                                                        property skip_records_with_error

                                                                        skip_records_with_error?: boolean | null;
                                                                        • Skip a line with error found inside and directly go process the next line.

                                                                        property skipEmptyLines

                                                                        skipEmptyLines?: boolean | null;

                                                                          property skipRecordsWithEmptyValues

                                                                          skipRecordsWithEmptyValues?: boolean | null;

                                                                            property skipRecordsWithError

                                                                            skipRecordsWithError?: boolean | null;

                                                                              property to

                                                                              to?: null | number | string;
                                                                              • Stop handling records after the requested number of records.

                                                                              property to_line

                                                                              to_line?: null | number | string;
                                                                              • Stop handling records after the requested line number.

                                                                              property toLine

                                                                              toLine?: null | number | string;

                                                                                property trim

                                                                                trim?: boolean | null;
                                                                                • If true, ignore whitespace immediately around the delimiter, defaults to false. Does not remove whitespace in a quoted field.

                                                                                interface OptionsNormalized

                                                                                interface OptionsNormalized<T = string[]> {}

                                                                                  property auto_parse

                                                                                  auto_parse?: boolean | CastingFunction;

                                                                                    property auto_parse_date

                                                                                    auto_parse_date?: boolean | CastingDateFunction;

                                                                                      property bom

                                                                                      bom?: boolean;
                                                                                      • If true, detect and exclude the byte order mark (BOM) from the CSV input if present.

                                                                                      property cast

                                                                                      cast?: boolean | CastingFunction;
                                                                                      • If true, the parser will attempt to convert input string to native types. If a function, receive the value as first argument, a context as second argument and return a new value. More information about the context properties is available below.

                                                                                      property cast_date

                                                                                      cast_date?: boolean | CastingDateFunction;
                                                                                      • If true, the parser will attempt to convert input string to dates. If a function, receive the value as argument and return a new value. It requires the "auto_parse" option. Be careful, it relies on Date.parse.

                                                                                      property cast_first_line_to_header

                                                                                      cast_first_line_to_header?: (
                                                                                      record: T
                                                                                      ) => ColumnOption<
                                                                                      T extends string[] ? string : T extends unknown ? string : keyof T
                                                                                      >[];
                                                                                      • Internal property string the function to

                                                                                      property columns

                                                                                      columns:
                                                                                      | boolean
                                                                                      | ColumnOption<
                                                                                      T extends string[] ? string : T extends unknown ? string : keyof T
                                                                                      >[];
                                                                                      • List of fields as an array, a user defined callback accepting the first line and returning the column names or true if autodiscovered in the first CSV line, default to null, affect the result data set in the sense that records will be objects instead of arrays.

                                                                                      property comment

                                                                                      comment: string | null;
                                                                                      • Treat all the characters after this one as a comment, default to '' (disabled).

                                                                                      property comment_no_infix

                                                                                      comment_no_infix: boolean;
                                                                                      • Restrict the definition of comments to a full line. Comment characters defined in the middle of the line are not interpreted as such. The option require the activation of comments.

                                                                                      property delimiter

                                                                                      delimiter: Buffer[];
                                                                                      • Set the field delimiter. One character only, defaults to comma.

                                                                                      property encoding

                                                                                      encoding: BufferEncoding | null;
                                                                                      • Set the source and destination encoding, a value of null returns buffer instead of strings.

                                                                                      property escape

                                                                                      escape: null | Buffer;
                                                                                      • Set the escape character, one character only, defaults to double quotes.

                                                                                      property from

                                                                                      from: number;
                                                                                      • Start handling records from the requested number of records.

                                                                                      property from_line

                                                                                      from_line: number;
                                                                                      • Start handling records from the requested line number.

                                                                                      property group_columns_by_name

                                                                                      group_columns_by_name: boolean;
                                                                                      • Convert values into an array of values when columns are activated and when multiple columns of the same name are found.

                                                                                      property ignore_last_delimiters

                                                                                      ignore_last_delimiters: boolean | number;
                                                                                      • Don't interpret delimiters as such in the last field according to the number of fields calculated from the number of columns, the option require the presence of the column option when true.

                                                                                      property info

                                                                                      info: boolean;
                                                                                      • Generate two properties info and record where info is a snapshot of the info object at the time the record was created and record is the parsed array or object.

                                                                                      property ltrim

                                                                                      ltrim: boolean;
                                                                                      • If true, ignore whitespace immediately following the delimiter (i.e. left-trim all fields), defaults to false. Does not remove whitespace in a quoted field.

                                                                                      property max_record_size

                                                                                      max_record_size: number;
                                                                                      • Maximum numer of characters to be contained in the field and line buffers before an exception is raised, used to guard against a wrong delimiter or record_delimiter, default to 128000 characters.

                                                                                      property objname

                                                                                      objname: number | string | undefined;
                                                                                      • Name of header-record title to name objects by.

                                                                                      property on_record

                                                                                      on_record?: (record: T, context: CastingContext) => T | undefined;
                                                                                      • Alter and filter records by executing a user defined function.

                                                                                      property quote

                                                                                      quote?: Buffer | null;
                                                                                      • Optional character surrounding a field, one character only, defaults to double quotes.

                                                                                      property raw

                                                                                      raw: boolean;
                                                                                      • Generate two properties raw and row where raw is the original CSV row content and row is the parsed array or object.

                                                                                      property record_delimiter

                                                                                      record_delimiter: Buffer[];
                                                                                      • One or multiple characters used to delimit record rows; defaults to auto discovery if not provided. Supported auto discovery method are Linux ("\n"), Apple ("\r") and Windows ("\r\n") row delimiters.

                                                                                      property relax_column_count

                                                                                      relax_column_count: boolean;
                                                                                      • Discard inconsistent columns count, default to false.

                                                                                      property relax_column_count_less

                                                                                      relax_column_count_less: boolean;
                                                                                      • Discard inconsistent columns count when the record contains less fields than expected, default to false.

                                                                                      property relax_column_count_more

                                                                                      relax_column_count_more: boolean;
                                                                                      • Discard inconsistent columns count when the record contains more fields than expected, default to false.

                                                                                      property relax_quotes

                                                                                      relax_quotes: boolean;
                                                                                      • Preserve quotes inside unquoted field.

                                                                                      property rtrim

                                                                                      rtrim: boolean;
                                                                                      • If true, ignore whitespace immediately preceding the delimiter (i.e. right-trim all fields), defaults to false. Does not remove whitespace in a quoted field.

                                                                                      property skip_empty_lines

                                                                                      skip_empty_lines: boolean;
                                                                                      • Dont generate empty values for empty lines. Defaults to false

                                                                                      property skip_records_with_empty_values

                                                                                      skip_records_with_empty_values: boolean;
                                                                                      • Don't generate records for lines containing empty column values (column matching /\s*/), defaults to false.

                                                                                      property skip_records_with_error

                                                                                      skip_records_with_error: boolean;
                                                                                      • Skip a line with error found inside and directly go process the next line.

                                                                                      property to

                                                                                      to: number;
                                                                                      • Stop handling records after the requested number of records.

                                                                                      property to_line

                                                                                      to_line: number;
                                                                                      • Stop handling records after the requested line number.

                                                                                      property trim

                                                                                      trim: boolean;
                                                                                      • If true, ignore whitespace immediately around the delimiter, defaults to false. Does not remove whitespace in a quoted field.

                                                                                      Type Aliases

                                                                                      type Callback

                                                                                      type Callback<T = string[]> = (
                                                                                      err: CsvError | undefined,
                                                                                      records: T[],
                                                                                      info?: Info
                                                                                      ) => void;

                                                                                        type CastingDateFunction

                                                                                        type CastingDateFunction = (value: string, context: CastingContext) => Date;

                                                                                          type CastingFunction

                                                                                          type CastingFunction = (value: string, context: CastingContext) => unknown;

                                                                                            type ColumnOption

                                                                                            type ColumnOption<K = string> = K | undefined | null | false | { name: K };

                                                                                              type CsvErrorCode

                                                                                              type CsvErrorCode =
                                                                                              | 'CSV_INVALID_ARGUMENT'
                                                                                              | 'CSV_INVALID_CLOSING_QUOTE'
                                                                                              | 'CSV_INVALID_COLUMN_DEFINITION'
                                                                                              | 'CSV_INVALID_COLUMN_MAPPING'
                                                                                              | 'CSV_INVALID_OPTION_BOM'
                                                                                              | 'CSV_INVALID_OPTION_CAST'
                                                                                              | 'CSV_INVALID_OPTION_CAST_DATE'
                                                                                              | 'CSV_INVALID_OPTION_COLUMNS'
                                                                                              | 'CSV_INVALID_OPTION_COMMENT'
                                                                                              | 'CSV_INVALID_OPTION_DELIMITER'
                                                                                              | 'CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME'
                                                                                              | 'CSV_INVALID_OPTION_ON_RECORD'
                                                                                              | 'CSV_MAX_RECORD_SIZE'
                                                                                              | 'CSV_NON_TRIMABLE_CHAR_AFTER_CLOSING_QUOTE'
                                                                                              | 'CSV_OPTION_COLUMNS_MISSING_NAME'
                                                                                              | 'CSV_QUOTE_NOT_CLOSED'
                                                                                              | 'CSV_RECORD_INCONSISTENT_FIELDS_LENGTH'
                                                                                              | 'CSV_RECORD_INCONSISTENT_COLUMNS'
                                                                                              | 'CSV_UNKNOWN_ERROR'
                                                                                              | 'INVALID_OPENING_QUOTE';

                                                                                                Package Files (1)

                                                                                                Dependencies (0)

                                                                                                No dependencies.

                                                                                                Dev Dependencies (17)

                                                                                                Peer Dependencies (0)

                                                                                                No peer dependencies.

                                                                                                Badge

                                                                                                To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                                                                You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/csv-parse.

                                                                                                • Markdown
                                                                                                  [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/csv-parse)
                                                                                                • HTML
                                                                                                  <a href="https://www.jsdocs.io/package/csv-parse"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>