-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
79 lines (72 loc) · 2.09 KB
/
utils.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
/**
* @file UTils functions for redacting routine
* @author Mukul<[email protected]>
*/
function getProp(model, path, def) {
path = path || '';
model = model || {};
def = typeof def === 'undefined' ? '' : def;
const parts = path.split('.');
if (parts.length > 1 && typeof model[parts[0]] === 'object') {
return getProp(model[parts[0]], parts.splice(1).join('.'), def);
}
return model[parts[0]] || def;
}
function setProp(data, path, value) {
const pList = path.split('.');
const len = pList.length;
for (let i = 0; i < len - 1; i++) {
const elem = pList[i];
if (!data[elem]) data[elem] = {};
data = data[elem];
}
data[pList[len - 1]] = value;
}
/**
* redactString - Add * at every odd index of String
*
* @param {String} str String to redact
* @return {String} Redacted String
*/
function redactString(str) {
Array.prototype.map.call(str, (x, j) => {
if (j % 2 === 0) str = `${str.substring(0, j - 1)}*${str.substring(j, str.length)}`;
});
return str;
}
/**
* flatten - Flattends the json
*
* Eg. { id: 1, crm: { fname: 'Abc' }, ml: { something: '33'} } will get converted to
* { id: 1, crm.fname: 1, ml.something: 33 }
*/
function flatten(obj, path = []) {
if (!obj) {
return null;
}
obj = JSON.parse(JSON.stringify(obj));
return Object.keys(obj).reduce((result, prop) => {
if (typeof obj[prop] !== 'object') {
result[path.concat(prop).join('.')] = obj[prop];
return result;
}
return Object.assign(result, flatten(obj[prop], path.concat(prop), result));
}, {});
}
const utils = {};
/**
* redactJSON - Redacts JSON doc
*
* @param {Object} mapping mapping for redacting specific entity
* @param {Object} data array of JSON
* @return {Object} description
*/
utils.redactJSON = function (mapping, data) {
mapping.map((elm) => {
const key = elm.path;
const redactedValue = redactString(getProp(data, key, ''));
setProp(data, key, redactedValue);
});
return data;
}
module.exports = utils;