Skip to content

Instantly share code, notes, and snippets.

@moraispgsi
Created October 24, 2018 01:44
Show Gist options
  • Save moraispgsi/ca652c48cbe4e0635b36bbcfe8676a2e to your computer and use it in GitHub Desktop.
Save moraispgsi/ca652c48cbe4e0635b36bbcfe8676a2e to your computer and use it in GitHub Desktop.
//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