Skip to content

Commit

Permalink
Refactor flattenColorPalette util function (#2677)
Browse files Browse the repository at this point in the history
  • Loading branch information
20lives authored Oct 27, 2020
1 parent 8613259 commit 232035e
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions src/util/flattenColorPalette.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import _ from 'lodash'
const flattenColorPalette = (colors) =>
Object.assign(
...Object.entries(colors).flatMap(([color, values]) =>
typeof values == 'object'
? Object.entries(flattenColorPalette(values)).map(([number, hex]) => ({
[color + (number === 'DEFAULT' ? '' : `-${number}`)]: hex,
}))
: [{ [`${color}`]: values }]
)
)

export default function flattenColorPalette(colors) {
const result = _(colors)
.flatMap((color, name) => {
if (_.isFunction(color) || !_.isObject(color)) {
return [[name, color]]
}

return _.map(flattenColorPalette(color), (value, key) => {
const suffix = key === 'DEFAULT' ? '' : `-${key}`
return [`${name}${suffix}`, value]
})
})
.fromPairs()
.value()

return result
}
export default flattenColorPalette

0 comments on commit 232035e

Please sign in to comment.