Before the refactor, there were some functions taking too much responsibility, that is why the code was split into small functions which can be easier to test, debug and read.
They are a few if and else statements that can be refactored applying functional programming as pointed in the comments or with ES6 features like the default parameters, this way the quality of the code is considerably improved.
I personally like to define constant variables like defaultBodyFont or allowedFonts in a separate file as types and name them with capitals, exp:
export const FONT_TYPES = {
BODY_FONT: "lato",
ALLOWED_FONTS : ["lato", "arial", "helvetica", "courier"],
}
For the brevity of the test, they were left the way it was.
Using a library like Jest, I will test the "small" functions first, before testing the main one ( getBranding(user) ), this way, if there is a problem, I would have a better idea where it is and how to tackle it, exp:
test(convert colours into rgb', () => {
expect(newColours([{ name: "primary", value: "#333" }])).toBe({ primary: "rgb(51, 51, 51)" });
})
Yes, as it is showed in the optimized code.
- The newColours and validateBodyFont could have been done in one reduce method, however, I decided to left them separate for better understanding and readability.