Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Collection

This class represents a collection of data entries. An instance can be created from different kinds of input using the static methods starting with from (that are available on the subclasses) and converted to other kinds of output using the instance methods starting with to.

This class is designed to handle large files or directories by using iterators and reading files one line at a time.

Hierarchy

Index

Properties

Protected _entries

_entries: function = [].values

The internal entries iterator. In some cases iterating over lines is simple. For example, if we create an instance from string or array we already have all the lines. However, if the input is a file, the entries iterator will be a function that reads and parses one line at a time.

Type declaration

    • (): IterableIterator<IAnyObject>
    • Returns IterableIterator<IAnyObject>

Protected _lines

_lines: function = [].values

The internal lines iterator. In some cases iterating over lines is simple. For example, if we create an instance from string or array we already have all the lines. However, if the input is a file, the lines iterator will be a function that reads one line at a time.

Type declaration

    • (): IterableIterator<string>
    • Returns IterableIterator<string>

Methods

entries

  • entries(): IterableIterator<IAnyObject>
  • The entries iterator of the instance. Yields lines as objects.

    Returns IterableIterator<IAnyObject>

lines

  • lines(): IterableIterator<string>
  • The lines iterator of the instance. Yields lines as strings.

    Returns IterableIterator<string>

setEntries

  • setEntries(entriesIterator: function): Collection
  • Sets the entries iterator of the instance. Useful while composing an instance from different sources. The "entries" might be JSON objects representing NDJSON lines or JS arrays of strings representing a line in CSV or TSV file.

    Parameters

    • entriesIterator: function
        • (): IterableIterator<IAnyObject>
        • Returns IterableIterator<IAnyObject>

    Returns Collection

    The instance to allow chaining

setLines

  • Sets the entries iterator of the instance. Useful while composing an instance from different sources

    Parameters

    • linesIterator: function

      The iterator to use

        • (): IterableIterator<string>
        • Returns IterableIterator<string>

    Returns Collection

    The instance to allow chaining

Abstract toArray

  • toArray(): IAnyObject[]
  • Converts the contents of the collection to array of "values". The subclasses must implement this depending on the output format they represent.

    Returns IAnyObject[]

Abstract toFile

  • Writes the collection to a file. The subclasses must implement this depending on the output format they represent.

    Parameters

    • path: string
    • Optional options: any

    Returns Collection

toJSON

  • toJSON(): IAnyObject[]
  • Converts the contents of the collection to array of "values". The values are json objects representing each line. The result does not include the header in case of delimited formats.

    NOTE: Don't use this for big objects/files because the output array is built into memory and then returned. For big files iterate over the entries instead, which will yield the same objects:

    for (const entry of collection.entries()) {
        // entry is an object representing a row
    }
    alias

    toArray This is just a "magic method" that will make it possible to call JSON.stringify() on an instance and get a valid JSON result.

    Returns IAnyObject[]

Abstract toString

  • toString(): string
  • Serializes the contents of the collection to a string. The subclasses must implement this depending on the output format they represent.

    Returns string

Abstract toStringArray

  • toStringArray(): string[]
  • Converts the contents of the collection to an array of strings. The subclasses must implement this depending on the output format they represent.

    Returns string[]

Generated using TypeDoc