<commit_before>
var tz = require('timezone');
var basalprofile = require('../profile/basal.js');
var _ = require('lodash');
var moment = require('moment');

function splitTimespanWithOneSplitter(event,splitter) {
	
	var resultArray = [event];
	
	if (splitter.type === 'recurring') {
		
		var startMinutes = event.started_at.getHours() * 60 + event.started_at.getMinutes();
		var endMinutes = startMinutes + event.duration;

		// 1440 magic for checking if the event overlaps midnight
		
		if ((startMinutes < splitter.minutes && endMinutes > splitter.minutes) || (endMinutes > 1440 && splitter.minutes < (endMinutes - 1440))) {
			
			var event2 = _.cloneDeep(event);
		
			var splitPoint = splitter.minutes;
			if (endMinutes > 1440) { splitPoint = endMinutes - 1440 + splitter.minutes; }

			var event1EndDate = moment(event.started_at).add(splitPoint,'minutes');
			var event1Duration = event1EndDate.diff(event.started_at,'minutes');
			event2.duration  = event.duration - event1Duration;
			event.duration = event1Duration;			
			event2.timestamp = event1EndDate.format();
			event2.started_at = new Date(event2.timestamp);
			event2.date = event2.started_at.getTime();

			resultArray = [event,event2];
		}
	}
	
	return resultArray;
}

function splitTimespan(event, splitterMoments) {
		
	var results = [event];
	var splits = 1;
	
	while(splits > 0) {
	
		var resultArray = [];
		_.forEach(results,function split(o) {
			var splitFound = false;
			_.forEach(splitterMoments,function split(p) {
				var splitResult = splitTimespanWithOneSplitter(o,p);
				if (splitResult.length > 1) { 
					resultArray = resultArray.concat(splitResult);
					splitFound = true;
				}
				return false;
			});
			if (!splitFound) { resultArray = resultArray.concat([o]); }
		});
				
		splits = resultArray.length - results.length;
		results = resultArray;
	}
	
	return results;
}

function calcTempTreatments (inputs) {
  var pumpHistory = inputs.history;
  var profile_data = inputs.profile;
    var tempHistory = [];
    var tempBoluses = [];<commit_msg>rewrite of the splitter based on having better test data, thanks @thebookins !<commit_after>
var tz = require('timezone');
var basalprofile = require('../profile/basal.js');
var _ = require('lodash');
var moment = require('moment');

function splitTimespanWithOneSplitter(event,splitter) {
	
	var resultArray = [event];
	
	if (splitter.type === 'recurring') {
		
		var startMinutes = event.started_at.getHours() * 60 + event.started_at.getMinutes();
		var endMinutes = startMinutes + event.duration;

		// 1440 magic for checking if the event overlaps midnight

		if ((startMinutes < splitter.minutes && endMinutes > splitter.minutes) || (endMinutes > 1440 && splitter.minutes < (endMinutes - 1440))) {

//			console.log('SPLITTING',startMinutes, endMinutes,splitter.minutes);
			
			var event2 = _.cloneDeep(event);
		
			var splitPoint = splitter.minutes;
			if (endMinutes > 1440) { splitPoint = 1440; }
			
			var event1Duration = splitPoint - startMinutes;
			var event1EndDate = moment(event.started_at).add(event1Duration,'minutes');
			event2.duration  = event.duration - event1Duration;
			event.duration = event1Duration;			
			event2.timestamp = event1EndDate.format();
			event2.started_at = new Date(event2.timestamp);
			event2.date = event2.started_at.getTime();

			var startMinutes_new = event.started_at.getHours() * 60 + event.started_at.getMinutes();
			var endMinutes_new = startMinutes_new + event.duration;

			var startMinutes_new2 = event2.started_at.getHours() * 60 + event2.started_at.getMinutes();
			var endMinutes_new2 = startMinutes_new2 + event2.duration;

//			console.log('new event 1',startMinutes_new, endMinutes_new, event.duration);
//			console.log('new event 2',startMinutes_new2, endMinutes_new2, event2.duration);

			resultArray = [event,event2];
		}
	}
	
	return resultArray;
}

function splitTimespan(event, splitterMoments) {
		
	var results = [event];

	var splitFound = true;

	while(splitFound) {

		var resultArray = [];
		splitFound = false;
		
		_.forEach(results,function split(o) {
			_.forEach(splitterMoments,function split(p) {
				var splitResult = splitTimespanWithOneSplitter(o,p);
				if (splitResult.length > 1) {
					resultArray = resultArray.concat(splitResult);
					splitFound = true;
					return false;
				}
			});
			
			if (!splitFound) resultArray = resultArray.concat([o]);;
			
		});

		results = resultArray;
	}
	
	return results;
}

function calcTempTreatments (inputs) {
  var pumpHistory = inputs.history;
  var profile_data = inputs.profile;
    var tempHistory = [];
    var tempBoluses = [];
{'fraction_non_alphanumeric': 0.124512776093547, 'fraction_numerical': 0.019705500216543957, 'mean_word_length': 4.020652173913043, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, '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': '5351437', 'n_tokens_mistral': 1754, 'n_tokens_neox': 1462, 'n_words': 314}