-
Notifications
You must be signed in to change notification settings - Fork 380
/
Copy pathutil.js
130 lines (110 loc) · 2.6 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
exports.getValueAsString = function getValueString(value) {
if (Number.isNaN(value)) {
return 'Nan';
} else if (!Number.isFinite(value)) {
if (value < 0) {
return '-Inf';
} else {
return '+Inf';
}
} else {
return `${value}`;
}
};
exports.removeLabels = function removeLabels(
hashMap,
labels,
sortedLabelNames,
) {
const hash = hashObject(labels, sortedLabelNames);
delete hashMap[hash];
};
exports.setValue = function setValue(hashMap, value, labels) {
const hash = hashObject(labels);
hashMap[hash] = {
value: typeof value === 'number' ? value : 0,
labels: labels || {},
};
return hashMap;
};
exports.setValueDelta = function setValueDelta(
hashMap,
deltaValue,
labels,
hash = '',
) {
const value = typeof deltaValue === 'number' ? deltaValue : 0;
if (hashMap[hash]) {
hashMap[hash].value += value;
} else {
hashMap[hash] = { value, labels };
}
return hashMap;
};
exports.getLabels = function (labelNames, args) {
if (typeof args[0] === 'object') {
return args[0];
}
if (labelNames.length !== args.length) {
throw new Error(
`Invalid number of arguments (${args.length}): "${args.join(
', ',
)}" for label names (${labelNames.length}): "${labelNames.join(', ')}".`,
);
}
const acc = {};
for (let i = 0; i < labelNames.length; i++) {
acc[labelNames[i]] = args[i];
}
return acc;
};
function fastHashObject(keys, labels) {
if (keys.length === 0) {
return '';
}
let hash = '';
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const value = labels[key];
if (value === undefined) continue;
hash += `${key}:${value},`;
}
return hash;
}
function hashObject(labels, labelNames) {
// We don't actually need a hash here. We just need a string that
// is unique for each possible labels object and consistent across
// calls with equivalent labels objects.
if (labelNames) {
return fastHashObject(labelNames, labels);
}
const keys = Object.keys(labels);
if (keys.length > 1) {
keys.sort(); // need consistency across calls
}
return fastHashObject(keys, labels);
}
exports.hashObject = hashObject;
exports.isObject = function isObject(obj) {
return obj !== null && typeof obj === 'object';
};
exports.nowTimestamp = function nowTimestamp() {
return Date.now() / 1000;
};
class Grouper extends Map {
/**
* Adds the `value` to the `key`'s array of values.
* @param {*} key Key to set.
* @param {*} value Value to add to `key`'s array.
* @returns {undefined} undefined.
*/
add(key, value) {
if (this.has(key)) {
this.get(key).push(value);
} else {
this.set(key, [value]);
}
}
}
exports.Grouper = Grouper;