<commit_before>var EventEmitter 	= require("events").EventEmitter;

function TimedBuffer ( timeMS, options ){
	if(!timeMS || typeof(timeMS) !== 'number') 
		throw 'TimedBuffer > ERROR: TimedBuffer requires > (int) timeMS';

	if(typeof(options) !== 'object') options = {};

	var self 		= this;

	this.ms 		= options.timePadding+timeMS || timeMS;

	this.buffer 	= [];
	this.length		= options.defaultLength || 2;

	this.sampleRate 				= 0;
	this._sampleRateBuffer			= [];
	this._sampleRateBufferLength	= options.sampleRateRetention || 50;
	this._lastTime					= new Date().getTime();

	this.push 	= function ( item ){
		//Step Time
		this._time();

		//Adjust array size to calculated length
		if(this.buffer.length >= this.length)
			this.buffer = this.buffer.slice(0, this.length-1);

		//Add item to beginning of array
		this.buffer.unshift(item);

		//Send Push Event
		this.emit('add', item);
		this.emit('change', this.buffer);
	}

	this.getByTime = function ( timeMS, skipMS ) {
		if(!timeMS || typeof(timeMS) !== 'number') throw 'TimedBuffer > ERROR: TimedBuffer.getSample requires > int (timeMS) || int (fromMS), int (toMS)';

		if(skipMS) {
			var requiredFromBuffer 	= Math.ceil(timeMS/this.sampleRate),
				skip 				= Math.ceil(skipMS/this.sampleRate);

			return this.buffer.slice(skip, (skip+requiredFromBuffer));
		} else {
			var requiredFromBuffer = Math.ceil(timeMS/this.sampleRate);
			return this.buffer.slice(0, requiredFromBuffer);
		}
	}

	this.getLast = function (){
		return this.buffer[0];
	}

	this._setLength = function ( length ) {
		this.length = length;
	}

	this._calcLength = function () {
		if (!this.sampleRate) return options.defaultLength || 2;
		
		return Math.ceil(this.ms/this.sampleRate);
	}

	this._setSampleRate = function ( sampleRateMS ) {
		this.sampleRate = Math.floor(sampleRateMS)-1;
	}

	this._calcSampleRate = function () {
		return this._sampleRateBuffer
			.reduce(function ( prev, cur, index, array ){
				if(index === array.length-1) 
					return (prev+cur)/array.length;
				else
					return (prev+cur);
			});
	}

	this._addToTimeBuffer = function ( ms ) {
		if(this._sampleRateBuffer.length >= this._sampleRateBufferLength)
			this._sampleRateBuffer.pop();

		this._sampleRateBuffer.unshift(ms);
	}

	this._time 	= function () {
		var time = new Date().getTime();

		//Step Time
		this._addToTimeBuffer(time-this._lastTime);
		this._lastTime 	= time;

		//Step Sample Rate
		var sampleRate 	= this._calcSampleRate();
		this._setSampleRate(sampleRate);

		//Step Length
		var length 		= this._calcLength();
		this._setLength(length);
	}

	return this;
}

TimedBuffer.prototype = Object.create(EventEmitter.prototype);

module.exports = TimedBuffer;<commit_msg>Updated timed-buffer<commit_after>var EventEmitter 	= require("events").EventEmitter;

function TimedBuffer ( timeMS, options ){
	if(!timeMS || typeof(timeMS) !== 'number') 
		throw 'TimedBuffer > ERROR: TimedBuffer requires > (int) timeMS';

	if(typeof(options) !== 'object') options = {};

	var self 		= this;

	this.ms 		= options.timePadding+timeMS || timeMS;

	this.buffer 	= [];
	this.length		= options.defaultLength || 2;

	this.sampleRate 				= 0;
	this._sampleRateBuffer			= [];
	this._sampleRateBufferLength	= options.sampleRateRetention || 50;
	this._lastTime					= new Date().getTime();

	return this;
}

TimedBuffer.prototype = Object.create(EventEmitter.prototype);

TimedBuffer.prototype._time = function () {
	var time = new Date().getTime();

	//Step Time
	this._addToTimeBuffer(time-this._lastTime);
	this._lastTime 	= time;

	//Step Sample Rate
	var sampleRate 	= this._calcSampleRate();
	this._setSampleRate(sampleRate);

	//Step Length
	var length 		= this._calcLength();
	this._setLength(length);
}

TimedBuffer.prototype._addToTimeBuffer = function ( ms ) {
	if(this._sampleRateBuffer.length >= this._sampleRateBufferLength)
		this._sampleRateBuffer.pop();

	this._sampleRateBuffer.unshift(ms);
}

TimedBuffer.prototype._calcSampleRate = function () {
	return this._sampleRateBuffer
		.reduce(function ( prev, cur, index, array ){
			if(index === array.length-1) 
				return (prev+cur)/array.length;
			else
				return (prev+cur);
		});
}

TimedBuffer.prototype._setSampleRate = function ( sampleRateMS ) {
	this.sampleRate = Math.floor(sampleRateMS)-1;
}

TimedBuffer.prototype._calcLength = function () {
	if (!this.sampleRate) return options.defaultLength || 2;
	
	return Math.ceil(this.ms/this.sampleRate);
}

TimedBuffer.prototype._setLength = function ( length ) {
	this.length = length;
}

TimedBuffer.prototype.getLast = function (){
	return this.buffer[0];
}

TimedBuffer.prototype.getByTime = function ( timeMS, skipMS ) {
	if(!timeMS || typeof(timeMS) !== 'number') throw 'TimedBuffer > ERROR: TimedBuffer.getSample requires > int (timeMS) || int (fromMS), int (toMS)';

	if(skipMS) {
		var requiredFromBuffer 	= Math.ceil(timeMS/this.sampleRate),
			skip 				= Math.ceil(skipMS/this.sampleRate);

		return this.buffer.slice(skip, (skip+requiredFromBuffer));
	} else {
		var requiredFromBuffer = Math.ceil(timeMS/this.sampleRate);
		return this.buffer.slice(0, requiredFromBuffer);
	}
}

TimedBuffer.prototype.push 	= function ( item ){
	//Step Time
	this._time();

	//Adjust array size to calculated length
	if(this.buffer.length >= this.length)
		this.buffer = this.buffer.slice(0, this.length-1);

	//Add item to beginning of array
	this.buffer.unshift(item);

	//Send Push Event
	this.emit('add', item);
	this.emit('change', this.buffer);
}

module.exports = TimedBuffer;
{'fraction_non_alphanumeric': 0.14306410720753351, 'fraction_numerical': 0.00398406374501992, 'mean_word_length': 4.085635359116022, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '6388364', 'n_tokens_mistral': 1990, 'n_tokens_neox': 1827, 'n_words': 360}