forked from solvvy/redact-pii
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (38 loc) · 1.18 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
39
40
41
42
43
44
45
46
var omit = require('lodash/omit');
var forEach = require('lodash/forEach');
var mapValues = require('lodash/mapValues');
var snakeCase = require('lodash/snakeCase');
var patterns = require('./patterns.js');
function defaultReplace(name) {
return name === 'name' ? '$1NAME' : snakeCase(name).toUpperCase();
}
function determineReplace(options) {
if (typeof options.replace === 'function') {
return name => options.replace(name, defaultReplace(name));
} else if (typeof options.replace === 'string') {
return () => options.replace;
} else {
return defaultReplace;
}
}
function Redactor(userOpts) {
var patternsToUse = Object.assign({}, patterns, omit(userOpts, ['replace']));
var replace = determineReplace(userOpts || {});
var replacements = mapValues(patternsToUse, (v, name) => replace(name));
return {
redact(text) {
if (typeof text !== 'string') {
return text;
}
forEach(patternsToUse, function (pattern, name) {
if (pattern === false) {
return;
}
text = text.replace(pattern, replacements[name]);
});
return text;
}
};
}
Object.assign(Redactor, {patterns});
module.exports = Redactor;