-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (32 loc) · 1.33 KB
/
index.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
const mapValues = (obj, fn, ...extras) => {
const newObj = {};
for (let [key, val] of Object.entries(obj)) {
newObj[key] = fn(val, key, ...extras);
}
return newObj;
}
const getMaskedStringValue = (matched, start, middle, end) => `${start}${Array(middle.length).join('*')}${end}`;
const maskString = value => {
if (value.length <= 3) return `${value[0]}${Array(value.length - 1).join('*')}`;
if (value.length >= 256) return `${value.slice(0, 25)}…******…******`;
const atPos = value.indexOf('@');
if (atPos !== -1) {
const maskedTo = maskString(value.substring(0, atPos));
return `${maskedTo}${value.substring(atPos)}`;
}
const toKeep = Math.max(Math.floor(value.length / 3 / 2), 1);
const re = new RegExp(`(^.{${toKeep}})(.+)(.{${toKeep}}$)`);
return value.replace(re, getMaskedStringValue);
};
const maskObject = (secretObject, keysToOmit) => mapValues(secretObject, maskValue, keysToOmit);
const maskValue = (value, key, keysToOmit) => {
if (keysToOmit && keysToOmit.indexOf(key) !== -1) return value;
if (typeof value === 'string') return maskString(value);
if (typeof value === 'object') return maskObject(value, keysToOmit);
return `masked*${typeof value}`;
};
const masky = (valuesToMask, keysToOmit) => mapValues(valuesToMask, maskValue, keysToOmit);
module.exports = {
mask: masky,
maskString,
};