<commit_before>var addSpaces = function(line, requiredSpaces) {

	if (line.length == 1) {
		return line;
	}

	var words = line.slice(0, line.length -1);
	var wordsObject = [];
	words.forEach(function(word) {
		wordsObject.push({spaces: []});
	});
	
	while (requiredSpaces > 0) {
		var lineLength = line.length - 1;
	
		for (var i = 0; i < words.length && requiredSpaces > 0; i++) {
			//line.splice(line.indexOf(words[i]) + 1, 0, ' ');	
			wordsObject[i].spaces.push(' ');
			requiredSpaces--;
		}
	}

	var spaces = 0;
	wordsObject.forEach(function(o, index) {
		for (var i = 0; i < o.spaces.length; i++) {
			words.splice(index + 1 + spaces + i, 0, ' ');
		}

		spaces += o.spaces.length;
	});

	if (line.length > 1) {
		return words.concat(line[line.length - 1]);
	} 

	return words;
}

var justify = function(str, len) {
	var words = str.split(' ');
	var lines = [];

	var numberOfWords = words.length;
	for (var i = 0; i < numberOfWords; i++) {
		var line = [];
		var totalLineLength = 0;
		var currWordLength = 0;
		var wordCount = 0;

		for (var j = i; j < numberOfWords; j++) {
			if (words[j].length + totalLineLength < len) {
				var currWord = words[j];
				line.push(currWord);
				currWordLength += currWord.length;

				// Add space to each word
				totalLineLength += currWord.length + 1;
				wordCount++;
			} else {

				// Skip all added words
				i += wordCount - 1;
				break;
			}
		}
		if (i + 1 == numberOfWords) {
			lines.push(line.join(''));
		} else {
			line = addSpaces(line, len - currWordLength);
			lines.push(line.join(''));
		}

	}

	return lines.join('\n');
};
//var str = 'Benny wne go for away die new baby new majit';
var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum sagittis dolor mauris, at elementum ligula tempor eget. In quis rhoncus nunc, at aliquet orci. Fusce at dolor sit amet felis suscipit tristique. Nam a imperdiet tellus. Nulla eu vestibulum urna. Vivamus tincidunt suscipit enim, nec ultrices nisi volutpat ac. Maecenas sit amet lacinia arcu, non dictum justo. Donec sed quam vel risus faucibus euismod. Suspendisse rhoncus rhoncus felis at fermentum. Donec lorem magna, ultricies a nunc sit amet, blandit fringilla nunc. In vestibulum velit ac felis rhoncus pellentesque. Mauris at tellus enim. Aliquam eleifend tempus dapibus. Pellentesque commodo, nisi sit amet hendrerit fringilla, ante odio porta lacus, ut elementum justo nulla et dolor.';

console.log(justify(str, 30));<commit_msg>do not justify last line<commit_after>var addSpaces = function(line, requiredSpaces) {

	if (line.length == 1) {
		return line;
	}

	var words = line.slice(0, line.length -1);
	var spacePositions = word.length - 1;

}

var justify = function(str, len) {
	var words = str.split(' ');
	var lines = [];

	var numberOfWords = words.length;
	for (var i = 0; i < numberOfWords; i++) {
		var line = [];
		var totalLineLength = 0;
		var currWordLength = 0;
		var wordCount = 0;

		for (var j = i; j < numberOfWords; j++) {
			if (words[j].length + totalLineLength < len) {
				var currWord = words[j];
				line.push(currWord);
				currWordLength += currWord.length;

				// Add space to each word
				totalLineLength += currWord.length + 1;
				wordCount++;
			} else {

				// Skip all added words
				i += wordCount - 1;
				break;
			}
		}
		if (i + 1 == numberOfWords) {
			lines.push(line.join(''));
		} else {
			line = addSpaces(line, len - currWordLength);
			lines.push(line.join(''));
		}

	}

	return lines.join('\n');
};
//var str = 'Benny wne go for away die new baby new majit';
var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum sagittis dolor mauris, at elementum ligula tempor eget. In quis rhoncus nunc, at aliquet orci. Fusce at dolor sit amet felis suscipit tristique. Nam a imperdiet tellus. Nulla eu vestibulum urna. Vivamus tincidunt suscipit enim, nec ultrices nisi volutpat ac. Maecenas sit amet lacinia arcu, non dictum justo. Donec sed quam vel risus faucibus euismod. Suspendisse rhoncus rhoncus felis at fermentum. Donec lorem magna, ultricies a nunc sit amet, blandit fringilla nunc. In vestibulum velit ac felis rhoncus pellentesque. Mauris at tellus enim. Aliquam eleifend tempus dapibus. Pellentesque commodo, nisi sit amet hendrerit fringilla, ante odio porta lacus, ut elementum justo nulla et dolor.';

console.log(justify(str, 30));
{'fraction_non_alphanumeric': 0.12064220183486239, 'fraction_numerical': 0.008486238532110092, 'mean_word_length': 3.422920892494929, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '621891', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1567, 'n_words': 501}