<commit_before>import * as culori from 'culori'
import _ from 'lodash'

function isValidColor(color) {
  return culori.parse(color) !== undefined
}

export function withAlphaValue(color, alphaValue, defaultValue) {
  if (_.isFunction(color)) {
    return color({ opacityValue: alphaValue })
  }

  if (isValidColor(color)) {
    // Parse color
    const parsed = culori.parse(color)

    // Apply alpha value
    parsed.alpha = alphaValue

    // Return formatted string
    if (parsed.mode === 'hsl') {
      return culori.formatHsl(parsed)
    } else {
      return culori.formatRgb(parsed)
    }
  }

  return defaultValue
}

export default function withAlphaVariable({ color, property, variable }) {
  if (_.isFunction(color)) {
    return {
      [variable]: '1',
      [property]: color({ opacityVariable: variable, opacityValue: `var(${variable})` }),
    }
  }

  if (isValidColor(color)) {
    const parsed = culori.parse(color)

    if ('alpha' in parsed) {
      // Has an alpha value, return color as-is
      return {
        [property]: color,
      }
    }

    const formatFn = parsed.mode === 'hsl' ? 'formatHsl' : 'formatRgb'
    const value = culori[formatFn]({
      ...parsed,
      alpha: 0.3,
    }).replace('0.3)', `var(${variable}))`)

    return {
      [variable]: '1',
      [property]: value,
    }
  }

  return {
    [property]: color,
  }
}<commit_msg>Fix handling of CSS variable declarations<commit_after>import * as culori from 'culori'
import _ from 'lodash'

function isValidColor(color) {
  return culori.parse(color) !== undefined
}

export function withAlphaValue(color, alphaValue, defaultValue) {
  if (_.isFunction(color)) {
    return color({ opacityValue: alphaValue })
  }

  if (isValidColor(color)) {
    // Parse color
    const parsed = culori.parse(color)

    // Apply alpha value
    parsed.alpha = alphaValue

    // Format string
    let value
    if (parsed.mode === 'hsl') {
      value = culori.formatHsl(parsed)
    } else {
      value = culori.formatRgb(parsed)
    }

    // Correctly apply CSS variable alpha value
    if (typeof alphaValue === 'string' && alphaValue.startsWith('var(') && value.endsWith('NaN)')) {
      value = value.replace('NaN)', `${alphaValue})`)
    }

    // Color could not be formatted correctly
    if (!value.includes('NaN')) {
      return value
    }
  }

  return defaultValue
}

export default function withAlphaVariable({ color, property, variable }) {
  if (_.isFunction(color)) {
    return {
      [variable]: '1',
      [property]: color({ opacityVariable: variable, opacityValue: `var(${variable})` }),
    }
  }

  if (isValidColor(color)) {
    const parsed = culori.parse(color)

    if ('alpha' in parsed) {
      // Has an alpha value, return color as-is
      return {
        [property]: color,
      }
    }

    const formatFn = parsed.mode === 'hsl' ? 'formatHsl' : 'formatRgb'
    const value = culori[formatFn]({
      ...parsed,
      alpha: NaN, // intentionally set to `NaN` for replacing
    }).replace('NaN)', `var(${variable}))`)

    return {
      [variable]: '1',
      [property]: value,
    }
  }

  return {
    [property]: color,
  }
}
{'fraction_non_alphanumeric': 0.14452877925363694, 'fraction_numerical': 0.002530044275774826, 'mean_word_length': 2.769964243146603, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5154617', 'n_tokens_mistral': 995, 'n_tokens_neox': 982, 'n_words': 253}