Skip to content

Commit b7bf467

Browse files
committed
applyStyles: Uses a single ".reduce()" over multiple maps and filters
1 parent a7fc795 commit b7bf467

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

src/utils/styles/applyStyles/applyStyles.ts

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,32 @@ const createStyleString = (
3333
* Takes only known prop aliases, ignores all the other props.
3434
*/
3535
export default function applyStyles(pristineProps: Props): string {
36-
return (
37-
Object.keys(pristineProps)
38-
// Parse each prop to include "breakpoint" and "behavior"
39-
.map(parsePropName)
40-
// Filter out props that are not included in prop aliases
41-
.filter(({ purePropName }) => propAliases.hasOwnProperty(purePropName))
42-
// Filter out props with "undefined" or "null" as value
43-
.filter(({ originPropName }) => isset(pristineProps[originPropName]))
44-
// Map each prop to a CSS string
45-
.map(({ purePropName, originPropName, breakpoint, behavior }) => {
46-
const { props, transformValue } = propAliases[purePropName]
47-
const propValue = pristineProps[originPropName]
48-
const transformedPropValue = transformValue
49-
? transformValue(propValue)
50-
: propValue
51-
52-
return createStyleString(
53-
props,
54-
transformedPropValue,
55-
breakpoint,
56-
behavior,
57-
)
58-
})
59-
.join(' ')
60-
)
36+
return Object.entries(pristineProps)
37+
.reduce<string[]>((css, [pristinePropName, pristinePropValue]) => {
38+
const { purePropName, breakpoint, behavior } = parsePropName(
39+
pristinePropName,
40+
)
41+
const propAlias = propAliases[purePropName]
42+
43+
// Filter out props with "undefined" or "null" as a value.
44+
// Filter out props that are not in the known prop aliases.
45+
if (!isset(pristineProps[pristinePropName]) || !propAlias) {
46+
return css
47+
}
48+
49+
const { props, transformValue } = propAlias
50+
const propValue = transformValue
51+
? transformValue(pristinePropValue)
52+
: pristinePropValue
53+
54+
const styleString = createStyleString(
55+
props,
56+
propValue,
57+
breakpoint,
58+
behavior,
59+
)
60+
61+
return css.concat(styleString)
62+
}, [])
63+
.join(' ')
6164
}

0 commit comments

Comments
 (0)