import camelCase from 'lodash.camelcase'; import snakeCase from 'lodash.snakecase'; export function modifyObjectKeys(object, modify) { if ( object === undefined || object === null || (typeof object !== 'object' && !Array.isArray(object)) ) { return object; } if (Array.isArray(object)) { return object.map(value => modifyObjectKeys(value, modify)); } const result = {}; Object.entries(object).forEach(([key, value]) => { result[modify(key)] = modifyObjectKeys(value, modify); }); return result; } export function camelCaseObject(object) { return modifyObjectKeys(object, camelCase); } export function snakeCaseObject(object) { return modifyObjectKeys(object, snakeCase); } export function convertKeyNames(object, nameMap) { const transformer = key => (nameMap[key] === undefined ? key : nameMap[key]); return modifyObjectKeys(object, transformer); } /** * Helper class to save time when writing out action types for asynchronous methods. Also helps * ensure that actions are namespaced. * * TODO: Put somewhere common to it can be used by other MFEs. */ export class AsyncActionType { constructor(topic, name) { this.topic = topic; this.name = name; } get BASE() { return `${this.topic}__${this.name}`; } get BEGIN() { return `${this.topic}__${this.name}__BEGIN`; } get SUCCESS() { return `${this.topic}__${this.name}__SUCCESS`; } get FAILURE() { return `${this.topic}__${this.name}__FAILURE`; } get RESET() { return `${this.topic}__${this.name}__RESET`; } }