Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
// @flow
import hyphenate from 'fbjs/lib/hyphenateStyleName'
// import isPlainObject from 'is-plain-object'
import { isPlainObject, isFunction } from 'exotic'
//
import { Interpolation } from '../types'

export const objToCss = (obj: Object, prevKey?: string): string => {
  const css = Object.keys(obj)
    .filter(key => {
      const chunk = obj[key]
      return (
        chunk !== undefined && chunk !== null && chunk !== false && chunk !== ''
      )
    })
    .map(key => {
      if (isPlainObject(obj[key])) {
        return objToCss(obj[key], key)
      }
      return `${hyphenate(key)}: ${obj[key]};`
    })
    .join(' ')
  return prevKey
    ? `${prevKey} {
  ${css}
}`
    : css
}

const flatten = (
  chunks: Array<Interpolation>,
  executionContext?: Object
): Array<Interpolation> =>
  chunks.reduce((ruleSet: Array<Interpolation>, chunk?: Interpolation) => {
    /* Remove falsey values */
    if (
      chunk === undefined ||
      chunk === null ||
      chunk === false ||
      chunk === ''
    ) {
      return ruleSet
    }
    /* Flatten ruleSet */
    if (Array.isArray(chunk)) {
      return [...ruleSet, ...flatten(chunk, executionContext)]
    }

    /* Handle other components */
    if (chunk.hasOwnProperty('styledComponentId')) {
      // $FlowFixMe not sure how to make this pass
      return [...ruleSet, `.${chunk.styledComponentId}`]
    }

    /* Either execute or defer the function */
    // if (isFunction(chunk)) {
    if (typeof chunk === 'function') {
      return executionContext
        ? ruleSet.concat(
          ...flatten([chunk(executionContext)], executionContext)
        )
        : ruleSet.concat(chunk)
    }

    /* Handle objects */
    return ruleSet.concat(
      // $FlowFixMe have to add %checks somehow to isPlainObject
      isPlainObject(chunk) ? objToCss(chunk) : chunk.toString()
    )
  }, [])

export default flatten