rofrischmann/fela

View on GitHub
packages/fela-plugin-friendly-pseudo-class/src/index.js

Summary

Maintainability
A
1 hr
Test Coverage
import isPlainObject from 'isobject'

const FRIENDLY_PSEUDO_REGEX = /^on([A-Z])/
const UPPERCASE_REGEX = /([A-Z])/g
const ON_REGEX = /^on-(.*)/g

const pseudoElements = [
  'after',
  'before',
  'first-letter',
  'first-line',
  'selection',
  'backdrop',
  'placeholder',
  'marker',
  'spelling-error',
  'grammar-error',
]

function friendlyPseudoClassPlugin(style) {
  for (const property in style) {
    const value = style[property]

    if (isPlainObject(value)) {
      const resolvedValue = friendlyPseudoClassPlugin(value)

      if (property.match(FRIENDLY_PSEUDO_REGEX) !== null) {
        const pseudo = property
          .replace(UPPERCASE_REGEX, (match) => '-' + match.toLowerCase())
          .replace(
            ON_REGEX,
            (match, p1) => `${pseudoElements.includes(p1) ? '::' : ':'}${p1}`
          )

        style[pseudo] = resolvedValue
        delete style[property]
      } else {
        style[property] = resolvedValue
      }
    }
  }

  return style
}

export default function friendlyPseudoClass() {
  return friendlyPseudoClassPlugin
}