Skip to content

Instantly share code, notes, and snippets.

@reggi
Created April 19, 2018 19:25
Show Gist options
  • Select an option

  • Save reggi/8c907d576cf35dbd30a8ec6ab36c4e9d to your computer and use it in GitHub Desktop.

Select an option

Save reggi/8c907d576cf35dbd30a8ec6ab36c4e9d to your computer and use it in GitHub Desktop.
const {isPlainObject, isString, mapValues, includes} = require('lodash')
const piiProps = []
const blackoutBulk = (str) => str[0] + Array(str.length - 1).join('▮') + str[str.length - 1]
const blackoutAll = (str) => Array(str.length + 1).join('▮')
const blackout = (str) => (str.length < 5) ? blackoutAll(str) : blackoutBulk(str)
const checkPropPii = (val, key) => includes(piiProps, key) ? blackout(val) : val
const jsonStringify = (val) => {try { return JSON.parse(val) } catch (e) { return val }}
const jsonStringifyObj = (val) => isPlainObject(jsonStringify(val))
const handlePlainObj = (val) => isPlainObject(val) ? hidePii(val) : false
const handleStringObjPlain = (valParsed) => isPlainObject(valParsed) ? hidePii(valParsed) : false
const handleStringObj = (val) => isString(val) && handleStringObjPlain(jsonStringify(val))
const hidePiiCb = (val, key) => handlePlainObj(val, key) || handleStringObj(val, key) || checkPropPii(val, key)
const hidePii = (data) => mapValues(data, hidePiiCb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment