Home / Function/ tryValueReplacements() — tailwindcss Function Reference

tryValueReplacements() — tailwindcss Function Reference

Architecture documentation for the tryValueReplacements() function in canonicalize-candidates.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  42b4defe_f5ba_2195_5bc6_48ede8ab230e["tryValueReplacements()"]
  7d350d81_5de1_f9f3_5b2c_19ec8fd3c37e["canonicalize-candidates.ts"]
  42b4defe_f5ba_2195_5bc6_48ede8ab230e -->|defined in| 7d350d81_5de1_f9f3_5b2c_19ec8fd3c37e
  7119037a_09b3_c392_43c1_53860e443d24["arbitraryValueToBareValueUtility()"]
  7119037a_09b3_c392_43c1_53860e443d24 -->|calls| 42b4defe_f5ba_2195_5bc6_48ede8ab230e
  06ed9408_12cf_7ddd_a435_8cdd942de1d4["add()"]
  42b4defe_f5ba_2195_5bc6_48ede8ab230e -->|calls| 06ed9408_12cf_7ddd_a435_8cdd942de1d4
  a15b3c4a_76ff_0090_fc86_bac24f0a4135["isValidSpacingMultiplier()"]
  42b4defe_f5ba_2195_5bc6_48ede8ab230e -->|calls| a15b3c4a_76ff_0090_fc86_bac24f0a4135
  4e39f1e7_ebcb_7f62_4a59_b645e71d50fd["isPositiveInteger()"]
  42b4defe_f5ba_2195_5bc6_48ede8ab230e -->|calls| 4e39f1e7_ebcb_7f62_4a59_b645e71d50fd
  style 42b4defe_f5ba_2195_5bc6_48ede8ab230e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/canonicalize-candidates.ts lines 1470–1527

function* tryValueReplacements(
  candidate: Extract<Candidate, { kind: 'functional' }>,
  value: string = candidate.value?.value ?? '',
  seen: Set<string> = new Set(),
): Generator<NamedUtilityValue> {
  if (seen.has(value)) return
  seen.add(value)

  // 0. Just try to drop the square brackets and see if it works
  // 1. A number (with increments of .25)
  yield {
    kind: 'named',
    value,
    fraction: null,
  }

  // 2. A percentage (with increments of .25 followed by a `%`)
  //    Try to drop the `%` and see if it works
  if (value.endsWith('%') && isValidSpacingMultiplier(value.slice(0, -1))) {
    yield {
      kind: 'named',
      value: value.slice(0, -1),
      fraction: null,
    }
  }

  // 3. A ratio with whole numbers
  if (value.includes('/')) {
    let [numerator, denominator] = value.split('/')
    if (isPositiveInteger(numerator) && isPositiveInteger(denominator)) {
      yield {
        kind: 'named',
        value: numerator,
        fraction: `${numerator}/${denominator}`,
      }
    }
  }

  // It could also be that we have `20px`, we can try just `20` and see if it
  // results in the same signature.
  let allNumbersAndFractions = new Set<string>()

  // Figure out all numbers and fractions in the value
  for (let match of value.matchAll(/(\d+\/\d+)|(\d+\.?\d+)/g)) {
    allNumbersAndFractions.add(match[0].trim())
  }

  // Sort the numbers and fractions where the smallest length comes first. This
  // will result in the smallest replacement.
  let options = Array.from(allNumbersAndFractions).sort((a, z) => {
    return a.length - z.length
  })

  // Try all the options
  for (let option of options) {
    yield* tryValueReplacements(candidate, option, seen)
  }
}

Subdomains

Frequently Asked Questions

What does tryValueReplacements() do?
tryValueReplacements() is a function in the tailwindcss codebase, defined in packages/tailwindcss/src/canonicalize-candidates.ts.
Where is tryValueReplacements() defined?
tryValueReplacements() is defined in packages/tailwindcss/src/canonicalize-candidates.ts at line 1470.
What does tryValueReplacements() call?
tryValueReplacements() calls 3 function(s): add, isPositiveInteger, isValidSpacingMultiplier.
What calls tryValueReplacements()?
tryValueReplacements() is called by 1 function(s): arbitraryValueToBareValueUtility.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free