Browse Source

Merge pull request #884 from gre/fix-recursion-anonymizer

Fix recursion anonymizer
master
Gaëtan Renaudeau 7 years ago
committed by GitHub
parent
commit
3935795f6a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      src/helpers/anonymizer.js

11
src/helpers/anonymizer.js

@ -16,25 +16,28 @@ function filepathReplace(path: string) {
return path.replace(cwd, '.').replace(configDir, '$USER_DATA')
}
function filepathRecursiveReplacer(obj: mixed) {
function filepathRecursiveReplacer(obj: mixed, seen: Array<*>) {
if (obj && typeof obj === 'object') {
seen.push(obj)
if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) {
const item = obj[i]
if (seen.indexOf(item) !== -1) return
if (typeof item === 'string') {
obj[i] = filepathReplace(item)
} else {
filepathRecursiveReplacer(item)
filepathRecursiveReplacer(item, seen)
}
}
} else {
for (const k in obj) {
if (typeof obj.hasOwnProperty === 'function' && obj.hasOwnProperty(k)) {
const value = obj[k]
if (seen.indexOf(value) !== -1) return
if (typeof value === 'string') {
obj[k] = filepathReplace(value)
} else {
filepathRecursiveReplacer(obj[k])
filepathRecursiveReplacer(obj[k], seen)
}
}
}
@ -52,5 +55,5 @@ export default {
filepath: filepathReplace,
filepathRecursiveReplacer,
filepathRecursiveReplacer: (obj: mixed) => filepathRecursiveReplacer(obj, []),
}

Loading…
Cancel
Save