Comparing version 1.1.0 to 1.2.0
{ | ||
"name": "safe-env", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Returns all environment variables with sensitive values hidden, great for logs", | ||
@@ -5,0 +5,0 @@ "author": "Gleb Bahmutov <gleb.bahmutov@gmail.com>", |
@@ -47,2 +47,20 @@ # safe-env | ||
## Custom predicate function | ||
You can pass a custom predicate function that returns true based on the | ||
property name to filter custom values. For example to hide all properties | ||
with string 'token' in them | ||
```js | ||
const o = { | ||
foo: 42, | ||
myToken: 'secret', | ||
'another-token': 'very secret' | ||
} | ||
const tokenName = (key) => key.toLowerCase().indexOf('token') !== -1 | ||
const out = safeEnv(tokenName, o) | ||
// out has both 'myToken' and 'another-token' values replaced | ||
// with '<hidden>' | ||
``` | ||
### Small print | ||
@@ -52,3 +70,2 @@ | ||
* [@bahmutov](https://twitter.com/bahmutov) | ||
@@ -58,3 +75,2 @@ * [glebbahmutov.com](http://glebbahmutov.com) | ||
License: MIT - do anything with the code, but don't blame me if it does not work. | ||
@@ -61,0 +77,0 @@ |
@@ -6,2 +6,3 @@ 'use strict' | ||
const privateKeys = require('./private-keys') | ||
const replacement = '<hidden>' | ||
@@ -14,13 +15,32 @@ function upperCaseEnvVariables (object) { | ||
function hideSomeVariables (o, names) { | ||
const hideSpecifiedKeys = (val, key) => { | ||
return R.contains(key, names) ? '<hidden>' : val | ||
function valueKeyPredicate (keyPredicate) { | ||
return function (val, key) { | ||
return keyPredicate(key) ? replacement : val | ||
} | ||
return R.mapObjIndexed(hideSpecifiedKeys, o) | ||
} | ||
function hideSomeVariables (o, names) { | ||
const hideSpecifiedKeys = valueKeyPredicate((key) => R.contains(key, names)) | ||
return filterPredicate(hideSpecifiedKeys, o) | ||
} | ||
function filterStringMatches (names, object) { | ||
const ups = upperCaseEnvVariables(object) | ||
return hideSomeVariables(ups, names) | ||
} | ||
function filterPredicate (predicate, object) { | ||
return R.mapObjIndexed(predicate, object) | ||
} | ||
function safeEnv (names, object) { | ||
names = names || privateKeys | ||
const ups = upperCaseEnvVariables(object || process.env) | ||
return hideSomeVariables(ups, names) | ||
object = object || process.env | ||
if (Array.isArray(names)) { | ||
return filterStringMatches(names, object) | ||
} | ||
if (typeof names === 'function') { | ||
return filterPredicate(valueKeyPredicate(names), object) | ||
} | ||
throw new Error('Not sure what to do with these arguments') | ||
} | ||
@@ -27,0 +47,0 @@ |
7337
54
111