Skip to content

Commit

Permalink
cache buildSelectorVariant
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait authored and adamwathan committed Aug 18, 2020
1 parent 0631851 commit fab4d7b
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/util/buildSelectorVariant.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import parser from 'postcss-selector-parser'
import tap from 'lodash/tap'
import { useMemo } from './useMemo'

export default function buildSelectorVariant(selector, variantName, separator, onError = () => {}) {
return parser(selectors => {
tap(selectors.first.filter(({ type }) => type === 'class').pop(), classSelector => {
if (classSelector === undefined) {
onError('Variant cannot be generated because selector contains no classes.')
return
}
const buildSelectorVariant = useMemo(
(selector, variantName, separator, onError = () => {}) => {
return parser(selectors => {
tap(selectors.first.filter(({ type }) => type === 'class').pop(), classSelector => {
if (classSelector === undefined) {
onError('Variant cannot be generated because selector contains no classes.')
return
}

classSelector.value = `${variantName}${separator}${classSelector.value}`
})
}).processSync(selector)
}
classSelector.value = `${variantName}${separator}${classSelector.value}`
})
}).processSync(selector)
},
(selector, variantName, separator) => [selector, variantName, separator].join('||')
)

export default buildSelectorVariant

0 comments on commit fab4d7b

Please sign in to comment.