Skip to content

Instantly share code, notes, and snippets.

@lizzymendivil
Created September 18, 2018 00:28
Show Gist options
  • Save lizzymendivil/d4ce517e7b71d3db9349f2d7a1a38de7 to your computer and use it in GitHub Desktop.
Save lizzymendivil/d4ce517e7b71d3db9349f2d7a1a38de7 to your computer and use it in GitHub Desktop.
export function flattenMessages(nestedMessages, prefix = '') {
return Object.keys(nestedMessages).reduce((messages, key) => {
const value = nestedMessages[key];
const prefixedKey = prefix ? `${prefix}.${key}` : key;
if (typeof value === 'string') {
messages[prefixedKey] = value;
} else {
Object.assign(messages, flattenMessages(value, prefixedKey));
}
return messages;
}, {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment