<commit_before>import * as _ from 'lodash';
import {IDataLayer} from '../../src/layer';
import {IHttpRequest} from '../../src/http.request';
import {IHttpResponse, HttpResponse} from '../../src/http.response';
import {IMap} from '@elium/mighty-js';

export class MockLayer<T> implements IDataLayer {
  rows: Array<T>;

  constructor(rows: Array<T>) {
    this.rows = rows;
  }

  create(request: IHttpRequest): Promise<IHttpResponse> {
    return new Promise((resolve) => {
      const id = this._getMaxId(this.rows);
      const data = _.merge({}, request.data, {id: id + 1});
      this.rows.push(data);
      resolve(new HttpResponse({data: data}));
    });
  }


  findOne(request: IHttpRequest): Promise<IHttpResponse> {
    return new Promise((resolve) => {
      const row = _.find(this.rows, request.criteria);
      resolve(new HttpResponse({data: _.cloneDeep(row)}));
    });
  }


  find(request: IHttpRequest): Promise<IHttpResponse> {
    return new Promise((resolve) => {
      const rows = _.filter(this.rows, request.criteria);
      resolve(new HttpResponse({data: _.cloneDeep(rows)}));
    });
  }


  save(request: IHttpRequest): Promise<IHttpResponse> {
    return new Promise((resolve, reject) => {
      const index = _.findIndex(this.rows, request.criteria);
      if (index < 0) {
        reject(new HttpResponse({error: new Error("There is no match for this row criteria")}));
      } else {
        this.rows.splice(index, 1, request.data);
        resolve(new HttpResponse({data: _.cloneDeep(request.data)}));
      }
    });
  }


  destroy(request: IHttpRequest): Promise<IHttpResponse> {
    return new Promise((resolve, reject) => {
      const index = _.findIndex(this.rows, request.criteria);
      if (index < 0) {
        reject(new HttpResponse({error: new Error("There is no match for this row criteria")}));
      } else {
        const row = _.first(this.rows.splice(index, 1));
        resolve(new HttpResponse({data: row}));
      }
    });
  }


  private _getMaxId(rows: Array<IMap<any>>): number {
    const ids: Array<number> = _.map(rows, (row) => row["id"]);
    return _.max(ids);
  }<commit_msg>fix: status code<commit_after>import * as _ from 'lodash';
import {IDataLayer} from '../../src/layer';
import {IHttpRequest} from '../../src/http.request';
import {IHttpResponse, HttpResponse} from '../../src/http.response';
import {IMap} from '@elium/mighty-js';

export class MockLayer<T> implements IDataLayer {
  rows: Array<T>;

  constructor(rows: Array<T>) {
    this.rows = rows;
  }

  create(request: IHttpRequest): Promise<IHttpResponse> {
    return new Promise((resolve) => {
      const id = this._getMaxId(this.rows);
      const data = _.merge({}, request.data, {id: id + 1});
      this.rows.push(data);
      resolve(new HttpResponse({data: data, status: 200}));
    });
  }


  findOne(request: IHttpRequest): Promise<IHttpResponse> {
    return new Promise((resolve) => {
      const row = _.find(this.rows, request.criteria);
      resolve(new HttpResponse({data: _.cloneDeep(row), status: 200}));
    });
  }


  find(request: IHttpRequest): Promise<IHttpResponse> {
    return new Promise((resolve) => {
      const rows = _.filter(this.rows, request.criteria);
      resolve(new HttpResponse({data: _.cloneDeep(rows), status: 200}));
    });
  }


  save(request: IHttpRequest): Promise<IHttpResponse> {
    return new Promise((resolve, reject) => {
      const index = _.findIndex(this.rows, request.criteria);
      if (index < 0) {
        reject(new HttpResponse({error: new Error("There is no match for this row criteria"), status: 400}));
      } else {
        this.rows.splice(index, 1, request.data);
        resolve(new HttpResponse({data: _.cloneDeep(request.data), status: 200}));
      }
    });
  }


  destroy(request: IHttpRequest): Promise<IHttpResponse> {
    return new Promise((resolve, reject) => {
      const index = _.findIndex(this.rows, request.criteria);
      if (index < 0) {
        reject(new HttpResponse({error: new Error("There is no match for this row criteria"), status: 400}));
      } else {
        const row = _.first(this.rows.splice(index, 1));
        resolve(new HttpResponse({data: row, status: 200}));
      }
    });
  }


  private _getMaxId(rows: Array<IMap<any>>): number {
    const ids: Array<number> = _.map(rows, (row) => row["id"]);
    return _.max(ids);
  }
{'fraction_non_alphanumeric': 0.16408032861706984, 'fraction_numerical': 0.007074395253308992, 'mean_word_length': 3.2928501469147893, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 37, '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': '3914684', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1352, 'n_words': 326}