<commit_before>import { SerializedType, JsonObject } from "./serialized-type";
import { STObject } from "./st-object";
import { BinaryParser } from "../serdes/binary-parser";
import { Buffer } from "buffer/";

const ARRAY_END_MARKER = Buffer.from([0xf1]);
const ARRAY_END_MARKER_NAME = "ArrayEndMarker";

const OBJECT_END_MARKER = Buffer.from([0xe1]);

/**
 * TypeGuard for Array<JsonObject>
 */
function isObjects(args): args is Array<JsonObject> {
  return (
    Array.isArray(args) && (args.length === 0 || typeof args[0] === "object")
  );
}

/**
 * Class for serializing and deserializing Arrays of Objects
 */
class STArray extends SerializedType {
  /**
   * Construct an STArray from a BinaryParser
   *
   * @param parser BinaryParser to parse an STArray from
   * @returns An STArray Object
   */
  static fromParser(parser: BinaryParser): STArray {
    const bytes: Array<Buffer> = [];

    while (!parser.end()) {
      const field = parser.readField();
      if (field.name === ARRAY_END_MARKER_NAME) {
        break;
      }

      bytes.push(
        field.header,
        parser.readFieldValue(field).toBytes(),
        OBJECT_END_MARKER
      );
    }

    bytes.push(ARRAY_END_MARKER);
    return new STArray(Buffer.concat(bytes));
  }

  /**
   * Construct an STArray from an Array of JSON Objects
   *
   * @param value STArray or Array of Objects to parse into an STArray
   * @returns An STArray object
   */
  static from<T extends STArray | Array<JsonObject>>(value: T): STArray {
    if (value instanceof STArray) {
      return value;
    }

    if (isObjects(value)) {
      const bytes: Array<Buffer> = [];
      value.forEach((obj) => {
        bytes.push(STObject.from(obj).toBytes());
      });

      bytes.push(ARRAY_END_MARKER);
      return new STArray(Buffer.concat(bytes));
    }

    throw new Error("Cannot construct Currency from value given");
  }

  /**
   * Return the JSON representation of this.bytes
   *
   * @returns An Array of JSON objects
   */
  toJSON(): Array<JsonObject> {
    const result: Array<JsonObject> = [];

    const arrayParser = new BinaryParser(this.toString());

    while (!arrayParser.end()) {
      const field = arrayParser.readField();
      if (field.name === ARRAY_END_MARKER_NAME) {
        break;
      }

      const outer = {};
      outer[field.name] = STObject.fromParser(arrayParser).toJSON();
      result.push(outer);
    }

    return result;
  }
}

export { STArray };
<commit_msg>fix typo in the text of exception (#114)<commit_after>import { SerializedType, JsonObject } from "./serialized-type";
import { STObject } from "./st-object";
import { BinaryParser } from "../serdes/binary-parser";
import { Buffer } from "buffer/";

const ARRAY_END_MARKER = Buffer.from([0xf1]);
const ARRAY_END_MARKER_NAME = "ArrayEndMarker";

const OBJECT_END_MARKER = Buffer.from([0xe1]);

/**
 * TypeGuard for Array<JsonObject>
 */
function isObjects(args): args is Array<JsonObject> {
  return (
    Array.isArray(args) && (args.length === 0 || typeof args[0] === "object")
  );
}

/**
 * Class for serializing and deserializing Arrays of Objects
 */
class STArray extends SerializedType {
  /**
   * Construct an STArray from a BinaryParser
   *
   * @param parser BinaryParser to parse an STArray from
   * @returns An STArray Object
   */
  static fromParser(parser: BinaryParser): STArray {
    const bytes: Array<Buffer> = [];

    while (!parser.end()) {
      const field = parser.readField();
      if (field.name === ARRAY_END_MARKER_NAME) {
        break;
      }

      bytes.push(
        field.header,
        parser.readFieldValue(field).toBytes(),
        OBJECT_END_MARKER
      );
    }

    bytes.push(ARRAY_END_MARKER);
    return new STArray(Buffer.concat(bytes));
  }

  /**
   * Construct an STArray from an Array of JSON Objects
   *
   * @param value STArray or Array of Objects to parse into an STArray
   * @returns An STArray object
   */
  static from<T extends STArray | Array<JsonObject>>(value: T): STArray {
    if (value instanceof STArray) {
      return value;
    }

    if (isObjects(value)) {
      const bytes: Array<Buffer> = [];
      value.forEach((obj) => {
        bytes.push(STObject.from(obj).toBytes());
      });

      bytes.push(ARRAY_END_MARKER);
      return new STArray(Buffer.concat(bytes));
    }

    throw new Error("Cannot construct STArray from value given");
  }

  /**
   * Return the JSON representation of this.bytes
   *
   * @returns An Array of JSON objects
   */
  toJSON(): Array<JsonObject> {
    const result: Array<JsonObject> = [];

    const arrayParser = new BinaryParser(this.toString());

    while (!arrayParser.end()) {
      const field = arrayParser.readField();
      if (field.name === ARRAY_END_MARKER_NAME) {
        break;
      }

      const outer = {};
      outer[field.name] = STObject.fromParser(arrayParser).toJSON();
      result.push(outer);
    }

    return result;
  }
}

export { STArray };

{'fraction_non_alphanumeric': 0.12963709677419355, 'fraction_numerical': 0.0030241935483870967, 'mean_word_length': 2.956140350877193, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3586780', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1501, 'n_words': 389}