'use strict';

var _ = require('lodash');

Given a url extracts any named template parameters beginning with a colon.

E.g. /foo/:bar/baz/:quux —-> [ ‘bar’, ‘quux’ ]

function templateParams(url) {
	return url.match(/:([^/]+)/g);
}

Given a url and some parameters replaces all named parameters with those supplied.

function template(url, params) {
	var templated = url;
	var keys = _.keys(params);
	var loweredKeys = _.map(keys, function (key) {
		return key.toLowerCase();
	});
	var keyLookup = _.zipObject(loweredKeys, keys);

	_.each(templateParams(url), function replaceParam(param) {
		var normalisedParam = param.toLowerCase().substr(1);

		if (!keyLookup[normalisedParam]) {
			throw new Error('Missing ' + normalisedParam);
		}

		templated = templated.replace(param,
			params[keyLookup[normalisedParam]]);
		delete params[keyLookup[normalisedParam]];
	});

	return templated;
}

module.exports = exports = {
	templateParams: templateParams,
	template: template
};
h