diff --git a/packages/utils/src/tableUtils/index.js b/packages/utils/src/tableUtils/index.js index 9d58c1d..62609bd 100644 --- a/packages/utils/src/tableUtils/index.js +++ b/packages/utils/src/tableUtils/index.js @@ -29,32 +29,13 @@ export const rowsFromArray = (array, fields, skipEmptyRows) => { // TODO: ALL THE DOCS // TODO: SOME TESTS // Empty values and keys are treated the same -export const rowsFromObject = (object, fields, skipEmptyValues, defaultTransform) => { +export const rowsFromObject = (object, fields, skipEmptyValues, defaultTransform = value => value) => { return fields.reduce( - (table, { key, heading, names, transform }) => { - let result; - + (table, { key, heading, names, transform = defaultTransform}) => { // If there is a name attribute in the fields object use it, otherwise fallback to the key const nameAttribute = names ? names : key; - - // Do we have a specific transform to run? - if (transform) { - result = transform(object[key], object); - // if not, do we have a default transform to run? - } else if (defaultTransform) { - result = defaultTransform(object[key], object); - } else { - result = object[key]; - } - - // Is the value undefined? - // This can happen if there; - // is no property in the object for provided key AND - // there is no value for a found property in the object - if (result === undefined) { - // If it is, normalise it to an empty string so we can decide if we want skip rendering or not - result = ''; - } + // Run any passed transforms and normalise undefined values to an empty string + const result = transform(object[key], object) || ''; // Empty values are empty strings (normalised above) // We never render null