@apartmentlist/js-trace-logger
Version:
Logger outputs messages with Trace ID
43 lines (42 loc) • 1.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatUTCDateRuby = exports.extractParams = exports.compileTemplate = void 0;
const compileTemplate = function (str) {
const definedParams = (0, exports.extractParams)(str);
const func = new Function(...definedParams, ['return `', str, '`;'].join(''));
return function (passed) {
const passedParams = [];
definedParams.forEach((definedKey) => {
const passedValue = passed[definedKey];
if (passedValue === undefined) {
throw new Error(`You need to pass a value with "${definedKey}"`);
}
passedParams.push(passedValue);
});
return func.apply(func, passedParams);
};
};
exports.compileTemplate = compileTemplate;
const extractParams = function (str) {
// The following regex is not comprehensive
// but I think it's enough for now?
const reg = /\$\{(\w+)\}/g;
const params = [];
let matches;
while ((matches = reg.exec(str))) {
params.push(matches[1]);
}
return params;
};
exports.extractParams = extractParams;
const formatUTCDateRuby = function (d) {
// '2020-05-13 18:01:16 +0000'
const year = d.getUTCFullYear();
const month = `0${d.getUTCMonth() + 1}`.slice(-2);
const day = `0${d.getUTCDate()}`.slice(-2);
const hour = `0${d.getUTCHours()}`.slice(-2);
const minutes = `0${d.getUTCMinutes()}`.slice(-2);
const seconds = `0${d.getUTCSeconds()}`.slice(-2);
return `${year}-${month}-${day} ${hour}:${minutes}:${seconds} +0000`;
};
exports.formatUTCDateRuby = formatUTCDateRuby;