From 5bf5779cfaf13e2141d72056832da63d6c7285bd Mon Sep 17 00:00:00 2001 From: Aleksei Androsov Date: Sun, 17 May 2020 19:35:06 +0300 Subject: [PATCH] Generate function without external dependencies --- lib/formatter.js | 39 ++++++++++++++++++++------------------- package.json | 1 - 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/formatter.js b/lib/formatter.js index c36a86dc..982d9383 100644 --- a/lib/formatter.js +++ b/lib/formatter.js @@ -1,7 +1,5 @@ 'use strict'; -const generateFunction = require('generate-function'); - function getFormatter(metric, defaultLabels) { const defaultLabelNames = Object.keys(defaultLabels); const name = metric.name; @@ -9,23 +7,26 @@ function getFormatter(metric, defaultLabels) { const type = metric.type; const labelsCode = getLabelsCode(metric, defaultLabelNames, defaultLabels); - const gen = generateFunction(); - gen(` - function format(item, escapeLabelValue, getValueAsString) { - const info = '# HELP ${name} ${help}\\n# TYPE ${name} ${type}\\n'; - let values = ''; - for (const val of item.values || []) { - val.labels = val.labels || {}; - let metricName = val.metricName || '${name}'; - const labels = \`${labelsCode}\`; - const hasLabels = labels.length > 0; - metricName += \`\${hasLabels ? '{' : ''}\${labels}\${hasLabels ? '}' : ''}\`; - let line = \`\${metricName} \${getValueAsString(val.value)}\`; - values += \`\${line}\\n\`; - } - return info + values; - }`); - return gen.toFunction(); + // eslint-disable-next-line no-new-func + return new Function( + 'item', + 'escapeLabelValue', + 'getValueAsString', + ` + const info = '# HELP ${name} ${help}\\n# TYPE ${name} ${type}\\n'; + let values = ''; + for (const val of item.values || []) { + val.labels = val.labels || {}; + let metricName = val.metricName || '${name}'; + const labels = \`${labelsCode}\`; + const hasLabels = labels.length > 0; + metricName += \`\${hasLabels ? '{' : ''}\${labels}\${hasLabels ? '}' : ''}\`; + let line = \`\${metricName} \${getValueAsString(val.value)}\`; + values += \`\${line}\\n\`; + } + return info + values; + `, + ); } function getLabelsCode(metric, defaultLabelNames, defaultLabels) { diff --git a/package.json b/package.json index 8a7fbcff..ed814896 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,6 @@ "typescript": "^3.0.3" }, "dependencies": { - "generate-function": "^2.3.1", "tdigest": "^0.1.1" }, "types": "./index.d.ts",