Options
All
  • Public
  • Public/Protected
  • All
Menu

CSV data abstraction

Hierarchy

Index

Constructors

constructor

  • new CSVObject(string?: string, headers?: boolean, delimiter?: string, enclosure?: string): CSVObject
  • CSVObject stores all the information for a CSV document and provides easy access to all the columns and values and allows us to operate with it's data easily.

    Parameters

    • Default value string: string = ""

      A string containing valid csv data

    • Default value headers: boolean = false

      Specifies if the first row of the provided csv data contains the column names or not. It is important to correctly set this value to avoid invalid data

    • Default value delimiter: string = ","

      The character that is used as the csv delimiter. ',' is set by default

    • Default value enclosure: string = """

      The character that is used to escape fields when special characters are found

    Returns CSVObject

    The constructed CSVObject

Properties

Protected _cells

Stores all the table cells data. The values are stored as key / value where key is the row and column index (r-c) and value the stored item

Protected _columnNames

_columnNames: HashMapObject

Stores a list with all the column names on the table. The values are stored as key / value where key is the column index and value the column label

Protected _columnsCount

_columnsCount: number = 0

Stores the number of columns on the current table instance

Private _hasHeaders

_hasHeaders: boolean = false

True if the CSV data was loaded with headers enabled or false if not

Protected _rowsCount

_rowsCount: number = 0

Stores the number of rows on the current table instance

Methods

Private _defineHeaders

  • _defineHeaders(): void
  • Auxiliary method to load the first csv row as the column names and avoid duplicate column names

    Returns void

    void

Private _escapeField

  • _escapeField(field: string, delimiter: string, enclosure: string): string
  • Auxiliary method to correctly format a csv field so it can be stored as a string

    Parameters

    • field: string

      The field that has to be formatted

    • delimiter: string

      The character that is used as the csv delimiter. ',' is set by default

    • enclosure: string

      The character that is used to escape fields when special characters are found

    Returns string

    The field correctly scaped and ready to be stored on a string

Private _findNextDelimiterIndex

  • _findNextDelimiterIndex(string: string, currentIndex: number, delimiter: string, stringLen: number): number
  • Auxiliary method that looks for the next delimiter or newline characters on the csv string starting at the specified position.

    Parameters

    • string: string

      The full csv string to search in.

    • currentIndex: number

      The csv string starting point for the search

    • delimiter: string

      The character that is used as the csv delimiter

    • stringLen: number

    Returns number

    The index where the next delimiter or newline character is found

Private _insertField

  • _insertField(currentRow: number, currentColumn: number, fieldValue: string): void
  • Auxiliary method that is used to add a new field to the table at the specified position

    Parameters

    • currentRow: number

      The row where we want to add the field

    • currentColumn: number

      The column where we want to add the field

    • fieldValue: string

      The value we want to add to the field

    Returns void

    void

addColumns

  • addColumns(number: number, names?: string[], at?: number): boolean
  • Add the specified amount of columns to the table.

    Parameters

    • number: number

      The number of columns that will be added to the table

    • Default value names: string[] = []

      Optionally we can list all the labels to define for the new columns that will be added

    • Default value at: number = -1

      Defines the column index where the new columns will be inserted. Old columns that are located at the insertion point will not be deleted, they will be moved to the Right. By default all the new columns will be appended at the end of the table unless a positive value is specified here.

    Returns boolean

    True if the operation was successful

addRows

  • addRows(number: number, at?: number): boolean
  • Add the specified amount of rows to the table.

    Parameters

    • number: number

      The number of rows that will be added to the table

    • Default value at: number = -1

      Defines the row index where the new rows will be inserted. Old rows that are located at the insertion point will not be deleted, they will be moved down. By default all the new rows will be appended at the bottom of the table unless a positive value is specified here.

    Returns boolean

    True if the operation was successful

countCells

  • countCells(): number
  • Get the total number of cells that are currently available on this table

    Returns number

    The total number of cells on the table

countColumns

  • countColumns(): number
  • Get the total number of columns that are currently available on this table

    Returns number

    The total number of columns on the table

countRows

  • countRows(): number
  • Get the total number of rows that are currently available on this table

    Returns number

    The total number of rows on the table

getCell

  • getCell(row: number, column: number | string): string
  • Get the value contained at the specified csv cell

    Parameters

    • row: number

      An integer containing the index for the row that we want to retrieve

    • column: number | string

      An integer or a string containing the index or label for the column that we want to retrieve

    Returns string

    The value for the cell that is located at the specified row and column

getColumn

  • getColumn(column: string | number): any[]
  • Get all the elements that are located at the specified column index or label.

    Parameters

    • column: string | number

      An integer or a string containing the index or label for the column that we want to retrieve

    Returns any[]

    All the table elements that belong to the required column

getColumnIndex

  • getColumnIndex(name: string): number
  • Get the numeric column index from it's label

    Parameters

    • name: string

      The label for an existing column

    Returns number

    The numeric index that is related to the given column label

getColumnName

  • getColumnName(columnIndex: number): any
  • Get the defined column name for a given column index

    Parameters

    • columnIndex: number

      a numeric column index

    Returns any

    The column label for the specified numeric index

getColumnNames

  • getColumnNames(): string[]
  • Get a list with all the currently defined column names in the same order as they are assigned to the table. If the table contains columns but no names are defined, a list with empty strings will be returned

    Returns string[]

    A list of strings with the column names

getRow

  • getRow(row: number): any[]
  • Get all the elements that are located at the specified row index

    Parameters

    • row: number

      An integer containing the index for the row that we want to retrieve

    Returns any[]

    All the table elements that belong to the required row

isEqualTo

  • isEqualTo(csv: any): boolean
  • Check if two provided CSV structures represent the same data

    Parameters

    • csv: any

      A valid string or CSVObject to compare with the current one

    Returns boolean

    true if the two CSV elements are considered equal, false if not

removeColumn

  • removeColumn(column: number | string): void
  • Delete a whole column and all its related data from the table

    Parameters

    • column: number | string

      An integer or a string containing the index or label for the column that we want to delete

    Returns void

    void

removeRow

  • removeRow(row: number): void
  • Delete a whole row and all its related data from the table

    Parameters

    • row: number

      An integer containing the index for the row that we want to delete

    Returns void

    void

setCell

  • setCell(row: number, column: number | string, value: any): any
  • Set the value for a csv cell

    see

    TableObject.setCell

    Parameters

    • row: number

      An integer containing the index for the row that we want to set

    • column: number | string

      An integer or a string containing the index or label for the column that we want to set

    • value: any

      The value we want to set to the specified cell. Only string values are allowed

    Returns any

    The assigned value after beign stored into the csv cell

setColumn

  • setColumn(column: number | string, data: any[]): void
  • Fill the data on all the rows for the given column index or label

    Parameters

    • column: number | string

      An integer or a string containing the index or label for the column that we want to fill

    • data: any[]

      An array with all the values that will be assigned to the table rows on the specified column. Array length must match rows number

    Returns void

    void

setColumnName

  • setColumnName(column: number | string, name: string): boolean
  • Set the label to an existing table column.

    Parameters

    • column: number | string

      An integer or a string containing the index or label for the column to which we want to assign a label

    • name: string

      The new label that will be assigned to the specified column

    Returns boolean

    True if the column name was correctly assigned

setColumnNames

  • setColumnNames(names: string[]): string[]
  • Define the names for the current table columns (Already defined column names will be overriden).

    Parameters

    • names: string[]

      List of names that will be applied to the table columns. It must have the same number of items and in the same order as the table columns.

    Returns string[]

    The list of column names after beign assigned

setRow

  • setRow(row: number, data: any[]): void
  • Fill all the data for the specified row

    Parameters

    • row: number

      An integer containing the index for the row that we want to set

    • data: any[]

      An array with all the values that will be assigned to the table row. Array length must match columns number

    Returns void

    void

toString

  • toString(delimiter?: string, enclosure?: string): string
  • Generate the textual representation for the csv data stored on this object. The output of this method is ready to be stored on a physical .csv file.

    Parameters

    • Default value delimiter: string = ","

      The character that is used as the csv delimiter. ',' is set by default

    • Default value enclosure: string = """

      The character that is used to escape fields when special characters are found

    Returns string

    A valid csv string ready to be stored on a .csv file

Static isCSV

  • isCSV(value: any): boolean
  • Check if the provided value contains valid CSV information.

    Parameters

    • value: any

      Object to test for valid CSV data. Accepted values are: Strings containing CSV data or CSVObject elements

    Returns boolean

    True if the received object represent valid CSV data. False otherwise.

Generated using TypeDoc