PsyTask | API Docs
    Preparing search index...

    Class Collector<T>

    Data collector. Collect, serialize and save data.

    Type Parameters

    • T extends LooseObject

    Hierarchy (View Summary)

    • generic<{ add: T; chunk: string }>
      • Collector

    Constructors

    • Built-in supports for CSV and JSON formats. You can extend this by Collector.serializers or provide serializer parameter.

      Type Parameters

      • T extends LooseObject

      Parameters

      • filename: string = ...

        data-${Date.now()}.csv

      • Optionalserializer: Serializer<T>

        An Serializer. If not provided, a default serializer based on the file extension will be used.

      Returns Collector<T>

    Properties

    filename: string = ...

    data-${Date.now()}.csv

    rows: T[] = []
    serializers: {
        csv: {
            body: (row: LooseObject) => string;
            footer: () => string;
            header: (row: LooseObject) => string;
        };
        json: {
            body: (row: LooseObject, rows: LooseObject[]) => string;
            footer: () => string;
            header: () => string;
        };
    } & Record<string, Serializer<LooseObject>> = serializers

    Map of serializers by file extension

    You can add your own Serializer to this map.

    Type Declaration

    • csv: {
          body: (row: LooseObject) => string;
          footer: () => string;
          header: (row: LooseObject) => string;
      }
    • json: {
          body: (row: LooseObject, rows: LooseObject[]) => string;
          footer: () => string;
          header: () => string;
      }

      JSON

    Add Markdown serializer

    Collector.serializers['md'] = {
    head: (row) => '', // generate header from the first row
    body: (row) => '', // generate body from each row
    tail: () => '', // generate footer
    };
    using dc = new Collector('data.md'); // now you can save to Markdown file

    Methods

    • Add a data row. For the default serializer, object fields will be serialized using JSON.stringify.

      Parameters

      • row: T

      Returns string

      The total serialized data up to now.

    • Download final serialized data

      Parameters

      • suffix: string = ''

      Returns void

    • Get the final serialized data.

      Returns string

      Call multiple times

      using dc = new Collector('test.csv');

      dc.add({ a: 1, b: 'hello' });
      dc.final() === 'a,b\n1,hello'; // true

      dc.add({ a: 2, b: 'world' });
      dc.final() === 'a,b\n1,hello\n2,world'; //true