Created
October 24, 2018 01:44
-
-
Save moraispgsi/ca652c48cbe4e0635b36bbcfe8676a2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Default means that it will take the full namespace of the object | |
const defaultValue = ''; | |
const keys = { | |
title: defaultValue, | |
severity: { | |
title: defaultValue, | |
domain: { | |
information: defaultValue, | |
warning: defaultValue, | |
error: defaultValue | |
} | |
}, | |
myArray: [ | |
defaultValue, | |
defaultValue, | |
] | |
}; | |
function traverse (object, property_accumulator, namespace) { | |
for (let property in object) { | |
if (object.hasOwnProperty(property)) { | |
const dashCaseProperty = property.replace(/([A-Z])/g, function($1){return "_"+$1.toLowerCase();}); | |
if (object[property] && typeof object[property] === 'object') { | |
traverse(object[property], `${property_accumulator}_${dashCaseProperty}`, namespace); | |
} else if(object[property] === defaultValue) { | |
object[property] = `${namespace}${property_accumulator}_${dashCaseProperty}`; | |
} | |
} | |
} | |
} | |
traverse(keys, '', 'vends'); | |
console.log(keys) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment