<commit_before>'use strict';

/** @interface */
const IQueryResponse = require('../interfaces/i-query-response');

/**
 * Abstract QueryResponse class
 * @implements {IQueryResponse}
 * @extends {EventEmitter}
 */
class AbstractQueryResponse extends IQueryResponse {
	constructor() {
		super();

		/**
		 * @type {Array.<ITorrent>}
		 * @protected
		 */
		this._torrents = [];
	}

	/**
	 * @inheritDoc
	 */
	sortByField(field) {
		return new Promise((resolve, reject) => {
			try {
				this._torrents.sort((torrentA, torrentB) => {
					if (torrentA['_' + field] > torrentB['_' + field]) {
						return 1;
					} else if (torrentA['_' + field] < torrentB['_' + field]) {
						return -1
					} else {
						return 0;
					}
				});
			} catch(err) {
				reject(err);
			}
			resolve();
		});
	}

	/**
	 * @inheritDoc
	 */
	filter(queryParams) {
		return new Promise((resolve, reject) => {
			try {
				var torrents = this._torrents.filter(torrent => {
					let pass = true;
					Object.keys(queryParams).forEach(field => {
						let torrentField = 'get' + field[0].toUpperCase() + field.substr(1);
						if (queryParams[field] !== torrent[torrentField]()) {
							pass = false;
						}
					});
					return pass;
				});
			} catch(err) {
				reject(err);
			}
			resolve(torrents);
		});
	}

	/**
	 * @inheritDoc
	 */
	getItems() {
		return this._torrents;
	}
}

module.exports = AbstractQueryResponse;
<commit_msg>Created new abstract method which should parse abstract-response data<commit_after>'use strict';

/** @interface */
const IQueryResponse = require('../interfaces/i-query-response');

/**
 * Abstract QueryResponse class
 * @param {*} responseData
 * @implements {IQueryResponse}
 * @extends {EventEmitter}
 */
class AbstractQueryResponse extends IQueryResponse {
	constructor(responseData) {
		super();

		/**
		 * @type {Array.<ITorrent>}
		 * @protected
		 */
		this._torrents = [];

		this._parseResponseData(responseData)
			.then(torrents => {
				this._torrents = torrents;
				this.emit('init:complete', torrents);
			})
			.catch(err => {
				this.emit('init:error', err);
			});
	}

	/**
	 * @inheritDoc
	 */
	sortByField(field) {
		field = 'get' + field[0].toUpperCase() + field.substr(1);
		return new Promise((resolve, reject) => {
			try {
				this._torrents.sort((torrentA, torrentB) => {
					let fieldA = torrentA[field];
					let fieldB = torrentB[field];
					if (fieldA > fieldB) {
						return 1;
					} else if (fieldA < fieldB) {
						return -1
					} else {
						return 0;
					}
				});
			} catch(err) {
				reject(err);
			}
			resolve();
		});
	}

	/**
	 * @inheritDoc
	 */
	filter(queryParams) {
		return new Promise((resolve, reject) => {
			try {
				var torrents = this._torrents.filter(torrent => {
					let fields = Object.keys(queryParams);
					return fields.every((field) => {
						let torrentField = 'get' + field[0].toUpperCase() + field.substr(1);
						return queryParams[field] === torrent[torrentField]();						return true;
					});
				});
			} catch(err) {
				reject(err);
			}
			resolve(torrents);
		});
	}

	/**
	 * @inheritDoc
	 */
	getItems() {
		return this._torrents;
	}

	/**
	 * This abstract method should overrides in all adapters
	 * @param {*} data
	 * @return {Promise.<error|Array.<ITorrent>>} torrents
	 * @abstract
	 * @protected
	 */
	_parseResponseData(data) {
		throw Error('Protected method _parseResponseData is not implemented');
	}
}


module.exports = AbstractQueryResponse;

{'fraction_non_alphanumeric': 0.16541570438799078, 'fraction_numerical': 0.003464203233256351, 'mean_word_length': 2.8585746102449887, 'pattern_counts': {'":': 0, '<': 9, '<?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': '4109592', 'n_tokens_mistral': 1442, 'n_tokens_neox': 1145, 'n_words': 223}