{"size": 1995, "ext": "js", "max_stars_repo_path": "lib/placeholders.js", "max_stars_repo_name": "flesler/parallel", "max_stars_repo_stars_event_min_datetime": "2015-12-10T22:42:52.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-21T11:43:47.000Z", "max_issues_repo_path": "lib/placeholders.js", "max_issues_repo_name": "flesler/parallel", "max_issues_repo_issues_event_min_datetime": "2017-05-08T23:18:54.000Z", "max_issues_repo_issues_event_max_datetime": "2017-05-12T20:13:21.000Z", "max_forks_repo_path": "lib/placeholders.js", "max_forks_repo_name": "flesler/parallel", "max_forks_repo_forks_event_min_datetime": "2017-03-09T16:10:21.000Z", "max_forks_repo_forks_event_max_datetime": "2021-03-08T07:09:42.000Z"}
{"max_stars_count": 30, "max_issues_count": 1, "max_forks_count": 3, "avg_line_length": 28.0985915493, "max_line_length": 91, "alphanum_fraction": 0.5959899749}
var opts = require('./opts');

// See https://www.gnu.org/software/parallel/man.html#OPTIONS
const OPS = {
	// {} input line
	'': function(val) { return val; },
	// {.} input line without extension
	'.': function(val) { return val.replace(/\.[^.]*$/,''); },
	// {/} basename of the input line
	'/': function(val) { return val.split(/[\/\\]/).pop(); },
	// {//} dirname of the input line
	'//': function(val) { return val.split(/([\/\\])/).slice(0,-2).join(''); },
	// {/.} basename of the input line without extension
	'/.': function(val) { return OPS['.'](OPS['/'](val)); },
	// {#} sequence number of the job to run, [1, ]
	'#': function(_, jobId) { return jobId; },
	// {%} job slot number [1, --jobs]
	'%': function(_, jobId) { return opts.maxJobs ? ((jobId-1) % opts.maxJobs) + 1 : jobId; }
};

// Matches any operator above, optionally preceeded by a number, enclosed in curly brackets
const REGEX = new RegExp('\\{(-?\\d+)?('+Object.keys(OPS).map(escape).join('|')+')}', 'g');

exports.parse = function(jobId, lines) {
	var any = false;
	var out = [];
	opts._.forEach(function(arg) {
		// No placeholder in this one
		if (!REGEX.test(arg)) {
			return out.push(arg);
		}
		any = true;
		lines.forEach(function(line) {
			out.push(replace(arg, line, jobId));
		});
	});
	// No placeholder, append input to end
	if (!any) out.push.apply(out, lines);
	return out;
};

function replace(arg, line, jobId) {
	return arg.replace(REGEX, function(_, num, op) {
		var val = extract(line, num);
		return OPS[op](val, jobId);
	});
}

function escape(op) {
	return op && '\\' + op.split('').join('\\');
}

function extract(line, num) {
	num = parseInt(num, 10);
	if (!num) return line;
	var cols = split(line);
	if (num < 0) {
		num = cols.length + num;
	} else {
		// They are 1-based
		num--;
	}
	return cols[num] || '';
}

var sep;
function split(line) {
	sep = sep || new RegExp(opts.colSep);
	// TODO: Support quoted columns and that kind of thing
	return line.replace(/^"|"$/g, '').split(sep);
}