Skip to content

Commit

Permalink
src: resolve a TODO and use a loop intead of arr.reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
zbjornson committed Sep 19, 2021
1 parent f177b1f commit c9bf1d8
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 20 deletions.
4 changes: 2 additions & 2 deletions lib/counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ class Counter extends Metric {
};
}

remove() {
const labels = getLabels(this.labelNames, arguments) || {};
remove(...args) {
const labels = getLabels(this.labelNames, args) || {};
validateLabel(this.labelNames, labels);
return removeLabels.call(this, this.hashMap, labels);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/gauge.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ class Gauge extends Metric {
return this.hashMap[hash] ? this.hashMap[hash].value : 0;
}

labels() {
const labels = getLabels(this.labelNames, arguments);
labels(...args) {
const labels = getLabels(this.labelNames, args);
validateLabel(this.labelNames, labels);
return {
inc: this.inc.bind(this, labels),
Expand All @@ -129,8 +129,8 @@ class Gauge extends Metric {
};
}

remove() {
const labels = getLabels(this.labelNames, arguments);
remove(...args) {
const labels = getLabels(this.labelNames, args);
validateLabel(this.labelNames, labels);
removeLabels.call(this, this.hashMap, labels);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,17 @@ class Histogram extends Metric {
return startTimer.call(this, labels)();
}

labels() {
const labels = getLabels(this.labelNames, arguments);
labels(...args) {
const labels = getLabels(this.labelNames, args);
validateLabel(this.labelNames, labels);
return {
observe: observe.call(this, labels),
startTimer: startTimer.call(this, labels),
};
}

remove() {
const labels = getLabels(this.labelNames, arguments);
remove(...args) {
const labels = getLabels(this.labelNames, args);
validateLabel(this.labelNames, labels);
removeLabels.call(this, this.hashMap, labels);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,17 @@ class Summary extends Metric {
return startTimer.call(this, labels)();
}

labels() {
const labels = getLabels(this.labelNames, arguments);
labels(...args) {
const labels = getLabels(this.labelNames, args);
validateLabel(this.labelNames, labels);
return {
observe: observe.call(this, labels),
startTimer: startTimer.call(this, labels),
};
}

remove() {
const labels = getLabels(this.labelNames, arguments);
remove(...args) {
const labels = getLabels(this.labelNames, args);
validateLabel(this.labelNames, labels);
removeLabels.call(this, this.hashMap, labels);
}
Expand Down
11 changes: 5 additions & 6 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ exports.setValue = function setValue(hashMap, value, labels) {
return hashMap;
};

// TODO: For node 6, use rest params
exports.getLabels = function (labelNames, args) {
if (typeof args[0] === 'object') {
return args[0];
Expand All @@ -38,11 +37,11 @@ exports.getLabels = function (labelNames, args) {
throw new Error('Invalid number of arguments');
}

const argsAsArray = Array.prototype.slice.call(args);
return labelNames.reduce((acc, label, index) => {
acc[label] = argsAsArray[index];
return acc;
}, {});
const acc = {};
for (let i = 0; i < labelNames.length; i++) {
acc[labelNames[i]] = args[i];
}
return acc;
};

function hashObject(labels) {
Expand Down

0 comments on commit c9bf1d8

Please sign in to comment.