Skip to content

Commit

Permalink
Implement feedback on defaultTransform
Browse files Browse the repository at this point in the history
It’s goooooood! Nice
  • Loading branch information
Loque- committed Aug 7, 2018
1 parent 9a45366 commit 749101b
Showing 1 changed file with 4 additions and 23 deletions.
27 changes: 4 additions & 23 deletions packages/utils/src/tableUtils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 749101b

Please sign in to comment.