<commit_before>'use strict';

var async = require('async');
var parser = require('./parser');
var helpers = require('./helper').helpers;

var runtime = module.exports = {};


runtime.template = function(template, params, callback) {
	var parsed = Array.isArray(template) ? template : parser.parse(template);
	var series;
	var substituted = [];

	async.series(
		series = parsed.map(function(section, i) {
			return function(done) {
			    if(Object(section) === section){
			    	runtime.tasks(section, params, function(err, value) {
			    	    if(err){
			    	    	return done(err);
			    	    }

			    	    substituted[i] = value;
			    	    done();
			    	});

			    }else{
			    	substituted[i] = section;
			    	done();
			    }
			}
		}),

		function(err) {
		    if(err){
		    	return callback && callback(err)
		    }

		    series.length = 0;

		    callback && callback(null, substituted.join(''));
		}
	);

	// if the task is synchronous, return value
	if(
		substituted.length === parsed.length && 
		substituted.every(function(s) {
		    return s;
		})
	){
		return substituted.join('');
	}
};


// run a single task
// @param {Object} task returnValue of parser.single

// { helper: [], param: 'abc' }<commit_msg>fix an error that occurs if no `params` passed<commit_after>'use strict';

var async = require('async');
var parser = require('./parser');
var helpers = require('./helper').helpers;

var runtime = module.exports = {};


// @public
runtime.template = function(template, params, callback) {
	params = params || {};

	var parsed = Array.isArray(template) ? template : parser.parse(template);
	var series;
	var substituted = [];

	async.series(
		series = parsed.map(function(section, i) {
			return function(done) {
			    if(Object(section) === section){
			    	runtime.tasks(section, params, function(err, value) {
			    	    if(err){
			    	    	return done(err);
			    	    }

			    	    substituted[i] = value;
			    	    done();
			    	});

			    }else{
			    	substituted[i] = section;
			    	done();
			    }
			}
		}),

		function(err) {
		    if(err){
		    	return callback && callback(err)
		    }

		    series.length = 0;

		    callback && callback(null, substituted.join(''));
		}
	);

	// if the task is synchronous, return value
	if(
		substituted.length === parsed.length && 
		substituted.every(function(s) {
		    return s;
		})
	){
		return substituted.join('');
	}
};


// no fault tolarance and argument overloading
// run a single task
// @param {Object} task returnValue of parser.single

// { helper: [], param: 'abc' }
{'fraction_non_alphanumeric': 0.15405405405405406, 'fraction_numerical': 0.0007722007722007722, 'mean_word_length': 2.496626180836707, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7007336', 'n_tokens_mistral': 944, 'n_tokens_neox': 782, 'n_words': 191}