2020-03-26 14:37:35 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
groupRedact,
|
|
|
|
groupRestore,
|
|
|
|
nestedRedact,
|
|
|
|
nestedRestore
|
|
|
|
}
|
|
|
|
|
|
|
|
function groupRestore ({ keys, values, target }) {
|
|
|
|
if (target == null) return
|
|
|
|
const length = keys.length
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
const k = keys[i]
|
|
|
|
target[k] = values[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 15:37:43 +00:00
|
|
|
function groupRedact (o, path, censor, isCensorFct, censorFctTakesPath) {
|
2020-03-26 14:37:35 +00:00
|
|
|
const target = get(o, path)
|
|
|
|
if (target == null) return { keys: null, values: null, target: null, flat: true }
|
|
|
|
const keys = Object.keys(target)
|
2020-11-12 15:37:43 +00:00
|
|
|
const keysLength = keys.length
|
|
|
|
const pathLength = path.length
|
|
|
|
const pathWithKey = censorFctTakesPath ? [...path] : undefined
|
|
|
|
const values = new Array(keysLength)
|
|
|
|
|
|
|
|
for (var i = 0; i < keysLength; i++) {
|
|
|
|
const key = keys[i]
|
|
|
|
values[i] = target[key]
|
|
|
|
|
|
|
|
if (censorFctTakesPath) {
|
|
|
|
pathWithKey[pathLength] = key
|
|
|
|
target[key] = censor(target[key], pathWithKey)
|
|
|
|
} else if (isCensorFct) {
|
|
|
|
target[key] = censor(target[key])
|
|
|
|
} else {
|
|
|
|
target[key] = censor
|
|
|
|
}
|
2020-03-26 14:37:35 +00:00
|
|
|
}
|
|
|
|
return { keys, values, target, flat: true }
|
|
|
|
}
|
|
|
|
|
|
|
|
function nestedRestore (arr) {
|
|
|
|
const length = arr.length
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
const { key, target, value } = arr[i]
|
|
|
|
target[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 15:37:43 +00:00
|
|
|
function nestedRedact (store, o, path, ns, censor, isCensorFct, censorFctTakesPath) {
|
2020-03-26 14:37:35 +00:00
|
|
|
const target = get(o, path)
|
|
|
|
if (target == null) return
|
|
|
|
const keys = Object.keys(target)
|
2020-11-12 15:37:43 +00:00
|
|
|
const keysLength = keys.length
|
|
|
|
for (var i = 0; i < keysLength; i++) {
|
2020-03-26 14:37:35 +00:00
|
|
|
const key = keys[i]
|
2020-11-12 15:37:43 +00:00
|
|
|
const { value, parent, exists } =
|
|
|
|
specialSet(target, key, path, ns, censor, isCensorFct, censorFctTakesPath)
|
2020-03-26 14:37:35 +00:00
|
|
|
|
|
|
|
if (exists === true && parent !== null) {
|
|
|
|
store.push({ key: ns[ns.length - 1], target: parent, value })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
|
|
|
function has (obj, prop) {
|
|
|
|
return Object.prototype.hasOwnProperty.call(obj, prop)
|
|
|
|
}
|
|
|
|
|
2020-11-12 15:37:43 +00:00
|
|
|
function specialSet (o, k, path, afterPath, censor, isCensorFct, censorFctTakesPath) {
|
|
|
|
const afterPathLen = afterPath.length
|
|
|
|
const lastPathIndex = afterPathLen - 1
|
|
|
|
const originalKey = k
|
2020-03-26 14:37:35 +00:00
|
|
|
var i = -1
|
|
|
|
var n
|
|
|
|
var nv
|
|
|
|
var ov
|
|
|
|
var oov = null
|
|
|
|
var exists = true
|
|
|
|
ov = n = o[k]
|
|
|
|
if (typeof n !== 'object') return { value: null, parent: null, exists }
|
2020-11-12 15:37:43 +00:00
|
|
|
while (n != null && ++i < afterPathLen) {
|
|
|
|
k = afterPath[i]
|
2020-03-26 14:37:35 +00:00
|
|
|
oov = ov
|
|
|
|
if (!(k in n)) {
|
|
|
|
exists = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ov = n[k]
|
2020-11-12 15:37:43 +00:00
|
|
|
nv = (i !== lastPathIndex)
|
|
|
|
? ov
|
|
|
|
: (isCensorFct
|
|
|
|
? (censorFctTakesPath ? censor(ov, [...path, originalKey, ...afterPath]) : censor(ov))
|
|
|
|
: censor)
|
|
|
|
n[k] = (has(n, k) && nv === ov) || (nv === undefined && censor !== undefined) ? n[k] : nv
|
2020-03-26 14:37:35 +00:00
|
|
|
n = n[k]
|
|
|
|
if (typeof n !== 'object') break
|
|
|
|
}
|
|
|
|
return { value: ov, parent: oov, exists }
|
|
|
|
}
|
2020-11-12 15:37:43 +00:00
|
|
|
|
2020-03-26 14:37:35 +00:00
|
|
|
function get (o, p) {
|
|
|
|
var i = -1
|
|
|
|
var l = p.length
|
|
|
|
var n = o
|
|
|
|
while (n != null && ++i < l) {
|
|
|
|
n = n[p[i]]
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|