Files
Mario Romano 07807e7bc3 # add dist
2016-04-21 11:56:31 +01:00

35 lines
1.2 KiB
JavaScript

'use strict';
var value = require('es5-ext/object/valid-value')
, normalize = require('es5-ext/object/normalize-options')
, map = Array.prototype.map, keys = Object.keys
, stringify = JSON.stringify;
module.exports = function (data, context/*, options*/) {
var names, argNames, argValues, options = Object(arguments[2]);
(value(data) && value(data.literals) && value(data.substitutions));
context = normalize(context);
names = keys(context);
argNames = names.join(', ');
argValues = names.map(function (name) { return context[name]; });
return [data.literals].concat(map.call(data.substitutions, function (expr) {
var resolver;
if (!expr) return undefined;
try {
resolver = new Function(argNames, 'return (' + expr + ')');
} catch (e) {
throw new TypeError("Unable to compile expression:\n\targs: " + stringify(argNames) +
"\n\tbody: " + stringify(expr) + "\n\terror: " + e.stack);
}
try {
return resolver.apply(null, argValues);
} catch (e) {
if (options.partial) return '${' + expr + '}';
throw new TypeError("Unable to resolve expression:\n\targs: " + stringify(argNames) +
"\n\tbody: " + stringify(expr) + "\n\terror: " + e.stack);
}
}));
};