<commit_before>var Promise = require('bluebird');
var path = require('path');
var fs = require('fs');

/**
 * A refeference to a file.
 * @constructor
 * @argument {String} filepath - The path of the file.
 */
function File(filepath) {

	/**
	 * The path of the file.
	 * @type {String}
	 */
	this.path = path.resolve(filepath);

	/**
	 * The file cache for storing read data
	 * @type {null|String|Buffer}
	 */
	this.cache = null;

	/**
	 * Default values when reading or writing the file.
	 * @type {Object}
	 */
	this.defaults = {
		cache: false,
		expires: 0
	}
}

/**
 * Reads the file.<br/>
 * Promise resolves to the data read by fs.readFile, and rejects to the err from the function.
 * @argument {Object|Boolean|Number} [options=false] - An object containing any options for the function, if given a bool it will be passed along to options.cache and numbers to options.expires.
 * @argument {Boolean} [options.cache=true] - Whether or not the data read should be saved in File#cache.
 * @argument {Number} [options.expires=0] - The time in milliseconds untill the cache is cleared, if x > 1 the cache will not be cleared.
 * @return {Promise} - resolve => data : reject => error
 */
File.prototype.read = function (options) {
	var $ = this;

	if (typeof options === 'undefined') options = $.defaults;
	else if (typeof options === 'boolean') options = { cache: $.defaults.cache, cache: options };
	else if (typeof options === 'number') options = { expires: options , expires: $.defaults.expires};

	return new Promise( function (resolve, reject) {
		fs.readFile($.path, function (err, data) {
			if (err) return reject(err);
			if (options.cache) $.cache = data;
			if (options.expires > 0 && $.cache) setTimeout(function () {
				$.cache = null;
			}, options.expires);
			resolve(data);
		});
	} );
};

/**
 * Writes data to the file.<br />
 * Promise resolves to the file object itself, and rejects to the err from the fs.writeFile function.
 * @argument {Buffer|string} data - The data that is to written to the file.
 * @argument {Boolean} [cache=false] - Whether or not the data written should be save to the File#cache variable.
 * @return {Promise} - resolve => this : reject => error
 */
File.prototype.write = function (data, cache) {
	if (typeof data !== 'string' && !(data instanceof Buffer))
	throw new TypeError('Expected first argument in File#write to be of type string or Buffer' +
		' received: ' + typeof data);
	if (typeof cache === 'undefined') cache = false;
	var $ = this;
	return new Promise( function (resolve, reject ) {
		fs.writeFile($.path, data, function (err) {
			if (err) return reject(err);
			if (cache) $.cache = data;
			resolve($);
		});
	} );
};

/**
 * Copies the file to a specified target destination.<br />
 * Promise resolves to the file representing the origin of the data, and rejects to any error thrown by the write/read streams.
 * @argument {string|File} target - The target path of the function.
 * @return {Promise} - resolve => this : reject => error
 */
File.prototype.copyTo = function (target) {
	var $, self = this;
<commit_msg>Modified File#write to use File#defaults.<commit_after>var Promise = require('bluebird');
var path = require('path');
var fs = require('fs');

/**
 * A refeference to a file.
 * @constructor
 * @argument {String} filepath - The path of the file.
 */
function File(filepath) {

	/**
	 * The path of the file.
	 * @type {String}
	 */
	this.path = path.resolve(filepath);

	/**
	 * The file cache for storing read data
	 * @type {null|String|Buffer}
	 */
	this.cache = null;

	/**
	 * Default values when reading or writing the file.
	 * @type {Object}
	 */
	this.defaults = {
		cache: false,
		expires: 0
	}
}

/**
 * Reads the file.<br/>
 * Promise resolves to the data read by fs.readFile, and rejects to the err from the function.
 * @argument {Object|Boolean|Number} [options=false] - An object containing any options for the operation, if given a bool it will be passed along to options.cache and numbers to options.expires.
 * @argument {Boolean} [options.cache=true] - Whether or not the data read should be saved in File#cache.
 * @argument {Number} [options.expires=0] - The time in milliseconds untill the cache is cleared, if x > 1 the cache will not be cleared.
 * @return {Promise} - resolve => data : reject => error
 */
File.prototype.read = function (options) {
	var $ = this;

	if (typeof options === 'undefined') options = $.defaults;
	else if (typeof options === 'boolean') options = { cache: $.defaults.cache, cache: options };
	else if (typeof options === 'number') options = { expires: options , expires: $.defaults.expires};

	return new Promise( function (resolve, reject) {
		fs.readFile($.path, function (err, data) {
			if (err) return reject(err);
			if (options.cache) {
				$.cache = data;
				if (options.expires > 0) setTimeout(function () {
					$.cache = null;
				}, options.expires);
			}
			resolve(data);
		});
	} );
};

/**
 * Writes data to the file.<br />
 * Promise resolves to the file object itself, and rejects to the err from the fs.writeFile function.
 * @argument {Buffer|string} data - The data that is to written to the file.
 * @argument {Object|Boolean|Number} [options=false] - An object containing any options for the operation, if given a bool it will be passed along to options.cache and numbers to options.expires.
 * @argument {Boolean} [options.cache=true] - Whether or not the data read should be saved in File#cache.
 * @argument {Number} [options.expires=0] - The time in milliseconds untill the cache is cleared, if x > 1 the cache will not be cleared.
 * @return {Promise} - resolve => this : reject => error
 */
File.prototype.write = function (data, options) {
	var $ = this;
	if (typeof data !== 'string' && !(data instanceof Buffer))
		throw new TypeError('Expected first argument in File#write to be of type string or Buffer' +
			' received: ' + typeof data);
	if (typeof options === 'undefined') options = options.defaults;
	else if (typeof options === 'boolean') options = { cache: $.defaults.cache, cache: options };
	else if (typeof options === 'number') options = { expires: options , expires: $.defaults.expires};
	
	return new Promise( function (resolve, reject ) {
		fs.writeFile($.path, data, function (err) {
			if (err) return reject(err);
			if (options.cache) {
				$.cache = data;
				if (options.expires > 0) setTimeout(function () {
					$.cache = null;
				}, options.expires);
			}
			resolve($);
		});
	} );
};

/**
 * Copies the file to a specified target destination.<br />
 * Promise resolves to the file representing the origin of the data, and rejects to any error thrown by the write/read streams.
 * @argument {string|File} target - The target path of the function.
 * @return {Promise} - resolve => this : reject => error
 */
File.prototype.copyTo = function (target) {
	var $, self = this;

{'fraction_non_alphanumeric': 0.13533943887192906, 'fraction_numerical': 0.0015990696322139846, 'mean_word_length': 3.5989304812834226, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, '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': '3764885', 'n_tokens_mistral': 2121, 'n_tokens_neox': 1991, 'n_words': 803}